github.com/jfrerich/mattermost-server@v5.8.0-rc2+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 } 54 55 func (o *ChannelMembers) ToJson() string { 56 if b, err := json.Marshal(o); err != nil { 57 return "[]" 58 } else { 59 return string(b) 60 } 61 } 62 63 func (o *ChannelUnread) ToJson() string { 64 b, _ := json.Marshal(o) 65 return string(b) 66 } 67 68 func ChannelMembersFromJson(data io.Reader) *ChannelMembers { 69 var o *ChannelMembers 70 json.NewDecoder(data).Decode(&o) 71 return o 72 } 73 74 func ChannelUnreadFromJson(data io.Reader) *ChannelUnread { 75 var o *ChannelUnread 76 json.NewDecoder(data).Decode(&o) 77 return o 78 } 79 80 func (o *ChannelMember) ToJson() string { 81 b, _ := json.Marshal(o) 82 return string(b) 83 } 84 85 func ChannelMemberFromJson(data io.Reader) *ChannelMember { 86 var o *ChannelMember 87 json.NewDecoder(data).Decode(&o) 88 return o 89 } 90 91 func (o *ChannelMember) IsValid() *AppError { 92 93 if len(o.ChannelId) != 26 { 94 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) 95 } 96 97 if len(o.UserId) != 26 { 98 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) 99 } 100 101 notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP] 102 if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { 103 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", nil, "notify_level="+notifyLevel, http.StatusBadRequest) 104 } 105 106 markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] 107 if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { 108 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", nil, "mark_unread_level="+markUnreadLevel, http.StatusBadRequest) 109 } 110 111 if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok { 112 if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) { 113 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error", nil, "push_notification_level="+pushLevel, http.StatusBadRequest) 114 } 115 } 116 117 if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok { 118 if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) { 119 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error", nil, "push_notification_level="+sendEmail, http.StatusBadRequest) 120 } 121 } 122 123 if ignoreChannelMentions, ok := o.NotifyProps[IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP]; ok { 124 if len(ignoreChannelMentions) > 40 || !IsIgnoreChannelMentionsValid(ignoreChannelMentions) { 125 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.ignore_channel_mentions_value.app_error", nil, "ignore_channel_mentions="+ignoreChannelMentions, http.StatusBadRequest) 126 } 127 } 128 129 return nil 130 } 131 132 func (o *ChannelMember) PreSave() { 133 o.LastUpdateAt = GetMillis() 134 } 135 136 func (o *ChannelMember) PreUpdate() { 137 o.LastUpdateAt = GetMillis() 138 } 139 140 func (o *ChannelMember) GetRoles() []string { 141 return strings.Fields(o.Roles) 142 } 143 144 func IsChannelNotifyLevelValid(notifyLevel string) bool { 145 return notifyLevel == CHANNEL_NOTIFY_DEFAULT || 146 notifyLevel == CHANNEL_NOTIFY_ALL || 147 notifyLevel == CHANNEL_NOTIFY_MENTION || 148 notifyLevel == CHANNEL_NOTIFY_NONE 149 } 150 151 func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { 152 return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION 153 } 154 155 func IsSendEmailValid(sendEmail string) bool { 156 return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false" 157 } 158 159 func IsIgnoreChannelMentionsValid(ignoreChannelMentions string) bool { 160 return ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_ON || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_OFF || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_DEFAULT 161 } 162 163 func GetDefaultChannelNotifyProps() StringMap { 164 return StringMap{ 165 DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 166 MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL, 167 PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 168 EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 169 IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP: IGNORE_CHANNEL_MENTIONS_DEFAULT, 170 } 171 }