1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2// See LICENSE.txt for license information. 3 4package model 5 6import ( 7 "encoding/json" 8 "io" 9 "net/http" 10 "strings" 11) 12 13const ( 14 CHANNEL_NOTIFY_DEFAULT = "default" 15 CHANNEL_NOTIFY_ALL = "all" 16 CHANNEL_NOTIFY_MENTION = "mention" 17 CHANNEL_NOTIFY_NONE = "none" 18 CHANNEL_MARK_UNREAD_ALL = "all" 19 CHANNEL_MARK_UNREAD_MENTION = "mention" 20 IGNORE_CHANNEL_MENTIONS_DEFAULT = "default" 21 IGNORE_CHANNEL_MENTIONS_OFF = "off" 22 IGNORE_CHANNEL_MENTIONS_ON = "on" 23 IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP = "ignore_channel_mentions" 24) 25 26type ChannelUnread struct { 27 TeamId string `json:"team_id"` 28 ChannelId string `json:"channel_id"` 29 MsgCount int64 `json:"msg_count"` 30 MentionCount int64 `json:"mention_count"` 31 NotifyProps StringMap `json:"-"` 32} 33 34type ChannelUnreadAt struct { 35 TeamId string `json:"team_id"` 36 UserId string `json:"user_id"` 37 ChannelId string `json:"channel_id"` 38 MsgCount int64 `json:"msg_count"` 39 MentionCount int64 `json:"mention_count"` 40 LastViewedAt int64 `json:"last_viewed_at"` 41 NotifyProps StringMap `json:"-"` 42} 43 44type ChannelMember struct { 45 ChannelId string `json:"channel_id"` 46 UserId string `json:"user_id"` 47 Roles string `json:"roles"` 48 LastViewedAt int64 `json:"last_viewed_at"` 49 MsgCount int64 `json:"msg_count"` 50 MentionCount int64 `json:"mention_count"` 51 NotifyProps StringMap `json:"notify_props"` 52 LastUpdateAt int64 `json:"last_update_at"` 53 SchemeGuest bool `json:"scheme_guest"` 54 SchemeUser bool `json:"scheme_user"` 55 SchemeAdmin bool `json:"scheme_admin"` 56 ExplicitRoles string `json:"explicit_roles"` 57} 58 59type ChannelMembers []ChannelMember 60 61type ChannelMemberForExport struct { 62 ChannelMember 63 ChannelName string 64 Username string 65} 66 67func (o *ChannelMembers) ToJson() string { 68 if b, err := json.Marshal(o); err != nil { 69 return "[]" 70 } else { 71 return string(b) 72 } 73} 74 75func (o *ChannelUnread) ToJson() string { 76 b, _ := json.Marshal(o) 77 return string(b) 78} 79 80func (o *ChannelUnreadAt) ToJson() string { 81 b, _ := json.Marshal(o) 82 return string(b) 83} 84 85func ChannelMembersFromJson(data io.Reader) *ChannelMembers { 86 var o *ChannelMembers 87 json.NewDecoder(data).Decode(&o) 88 return o 89} 90 91func ChannelUnreadFromJson(data io.Reader) *ChannelUnread { 92 var o *ChannelUnread 93 json.NewDecoder(data).Decode(&o) 94 return o 95} 96 97func ChannelUnreadAtFromJson(data io.Reader) *ChannelUnreadAt { 98 var o *ChannelUnreadAt 99 json.NewDecoder(data).Decode(&o) 100 return o 101} 102 103func (o *ChannelMember) ToJson() string { 104 b, _ := json.Marshal(o) 105 return string(b) 106} 107 108func ChannelMemberFromJson(data io.Reader) *ChannelMember { 109 var o *ChannelMember 110 json.NewDecoder(data).Decode(&o) 111 return o 112} 113 114func (o *ChannelMember) IsValid() *AppError { 115 116 if !IsValidId(o.ChannelId) { 117 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) 118 } 119 120 if !IsValidId(o.UserId) { 121 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) 122 } 123 124 notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP] 125 if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { 126 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", nil, "notify_level="+notifyLevel, http.StatusBadRequest) 127 } 128 129 markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] 130 if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { 131 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", nil, "mark_unread_level="+markUnreadLevel, http.StatusBadRequest) 132 } 133 134 if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok { 135 if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) { 136 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error", nil, "push_notification_level="+pushLevel, http.StatusBadRequest) 137 } 138 } 139 140 if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok { 141 if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) { 142 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error", nil, "push_notification_level="+sendEmail, http.StatusBadRequest) 143 } 144 } 145 146 if ignoreChannelMentions, ok := o.NotifyProps[IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP]; ok { 147 if len(ignoreChannelMentions) > 40 || !IsIgnoreChannelMentionsValid(ignoreChannelMentions) { 148 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.ignore_channel_mentions_value.app_error", nil, "ignore_channel_mentions="+ignoreChannelMentions, http.StatusBadRequest) 149 } 150 } 151 152 return nil 153} 154 155func (o *ChannelMember) PreSave() { 156 o.LastUpdateAt = GetMillis() 157} 158 159func (o *ChannelMember) PreUpdate() { 160 o.LastUpdateAt = GetMillis() 161} 162 163func (o *ChannelMember) GetRoles() []string { 164 return strings.Fields(o.Roles) 165} 166 167func (o *ChannelMember) SetChannelMuted(muted bool) { 168 if o.IsChannelMuted() { 169 o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] = CHANNEL_MARK_UNREAD_ALL 170 } else { 171 o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] = CHANNEL_MARK_UNREAD_MENTION 172 } 173} 174 175func (o *ChannelMember) IsChannelMuted() bool { 176 return o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] == CHANNEL_MARK_UNREAD_MENTION 177} 178 179func IsChannelNotifyLevelValid(notifyLevel string) bool { 180 return notifyLevel == CHANNEL_NOTIFY_DEFAULT || 181 notifyLevel == CHANNEL_NOTIFY_ALL || 182 notifyLevel == CHANNEL_NOTIFY_MENTION || 183 notifyLevel == CHANNEL_NOTIFY_NONE 184} 185 186func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { 187 return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION 188} 189 190func IsSendEmailValid(sendEmail string) bool { 191 return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false" 192} 193 194func IsIgnoreChannelMentionsValid(ignoreChannelMentions string) bool { 195 return ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_ON || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_OFF || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_DEFAULT 196} 197 198func GetDefaultChannelNotifyProps() StringMap { 199 return StringMap{ 200 DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 201 MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL, 202 PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 203 EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 204 IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP: IGNORE_CHANNEL_MENTIONS_DEFAULT, 205 } 206} 207