github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/channel_member.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "net/http" 10 "strings" 11 ) 12 13 const ( 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 26 type 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 34 type ChannelMember struct { 35 ChannelId string `json:"channel_id"` 36 UserId string `json:"user_id"` 37 Roles string `json:"roles"` 38 LastViewedAt int64 `json:"last_viewed_at"` 39 MsgCount int64 `json:"msg_count"` 40 MentionCount int64 `json:"mention_count"` 41 NotifyProps StringMap `json:"notify_props"` 42 LastUpdateAt int64 `json:"last_update_at"` 43 SchemeUser bool `json:"scheme_user"` 44 SchemeAdmin bool `json:"scheme_admin"` 45 ExplicitRoles string `json:"explicit_roles"` 46 } 47 48 type ChannelMembers []ChannelMember 49 50 type ChannelMemberForExport struct { 51 ChannelMember 52 ChannelName string 53 Username string 54 } 55 56 func (o *ChannelMembers) ToJson() string { 57 if b, err := json.Marshal(o); err != nil { 58 return "[]" 59 } else { 60 return string(b) 61 } 62 } 63 64 func (o *ChannelUnread) ToJson() string { 65 b, _ := json.Marshal(o) 66 return string(b) 67 } 68 69 func ChannelMembersFromJson(data io.Reader) *ChannelMembers { 70 var o *ChannelMembers 71 json.NewDecoder(data).Decode(&o) 72 return o 73 } 74 75 func ChannelUnreadFromJson(data io.Reader) *ChannelUnread { 76 var o *ChannelUnread 77 json.NewDecoder(data).Decode(&o) 78 return o 79 } 80 81 func (o *ChannelMember) ToJson() string { 82 b, _ := json.Marshal(o) 83 return string(b) 84 } 85 86 func ChannelMemberFromJson(data io.Reader) *ChannelMember { 87 var o *ChannelMember 88 json.NewDecoder(data).Decode(&o) 89 return o 90 } 91 92 func (o *ChannelMember) IsValid() *AppError { 93 94 if len(o.ChannelId) != 26 { 95 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) 96 } 97 98 if len(o.UserId) != 26 { 99 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) 100 } 101 102 notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP] 103 if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { 104 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", nil, "notify_level="+notifyLevel, http.StatusBadRequest) 105 } 106 107 markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] 108 if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { 109 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", nil, "mark_unread_level="+markUnreadLevel, http.StatusBadRequest) 110 } 111 112 if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok { 113 if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) { 114 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error", nil, "push_notification_level="+pushLevel, http.StatusBadRequest) 115 } 116 } 117 118 if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok { 119 if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) { 120 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error", nil, "push_notification_level="+sendEmail, http.StatusBadRequest) 121 } 122 } 123 124 if ignoreChannelMentions, ok := o.NotifyProps[IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP]; ok { 125 if len(ignoreChannelMentions) > 40 || !IsIgnoreChannelMentionsValid(ignoreChannelMentions) { 126 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.ignore_channel_mentions_value.app_error", nil, "ignore_channel_mentions="+ignoreChannelMentions, http.StatusBadRequest) 127 } 128 } 129 130 return nil 131 } 132 133 func (o *ChannelMember) PreSave() { 134 o.LastUpdateAt = GetMillis() 135 } 136 137 func (o *ChannelMember) PreUpdate() { 138 o.LastUpdateAt = GetMillis() 139 } 140 141 func (o *ChannelMember) GetRoles() []string { 142 return strings.Fields(o.Roles) 143 } 144 145 func IsChannelNotifyLevelValid(notifyLevel string) bool { 146 return notifyLevel == CHANNEL_NOTIFY_DEFAULT || 147 notifyLevel == CHANNEL_NOTIFY_ALL || 148 notifyLevel == CHANNEL_NOTIFY_MENTION || 149 notifyLevel == CHANNEL_NOTIFY_NONE 150 } 151 152 func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { 153 return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION 154 } 155 156 func IsSendEmailValid(sendEmail string) bool { 157 return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false" 158 } 159 160 func IsIgnoreChannelMentionsValid(ignoreChannelMentions string) bool { 161 return ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_ON || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_OFF || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_DEFAULT 162 } 163 164 func GetDefaultChannelNotifyProps() StringMap { 165 return StringMap{ 166 DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 167 MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL, 168 PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 169 EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 170 IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP: IGNORE_CHANNEL_MENTIONS_DEFAULT, 171 } 172 }