github.com/levb/mattermost-server@v5.3.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 ) 21 22 type ChannelUnread struct { 23 TeamId string `json:"team_id"` 24 ChannelId string `json:"channel_id"` 25 MsgCount int64 `json:"msg_count"` 26 MentionCount int64 `json:"mention_count"` 27 NotifyProps StringMap `json:"-"` 28 } 29 30 type ChannelMember struct { 31 ChannelId string `json:"channel_id"` 32 UserId string `json:"user_id"` 33 Roles string `json:"roles"` 34 LastViewedAt int64 `json:"last_viewed_at"` 35 MsgCount int64 `json:"msg_count"` 36 MentionCount int64 `json:"mention_count"` 37 NotifyProps StringMap `json:"notify_props"` 38 LastUpdateAt int64 `json:"last_update_at"` 39 SchemeUser bool `json:"scheme_user"` 40 SchemeAdmin bool `json:"scheme_admin"` 41 ExplicitRoles string `json:"explicit_roles"` 42 } 43 44 type ChannelMembers []ChannelMember 45 46 func (o *ChannelMembers) ToJson() string { 47 if b, err := json.Marshal(o); err != nil { 48 return "[]" 49 } else { 50 return string(b) 51 } 52 } 53 54 func (o *ChannelUnread) ToJson() string { 55 b, _ := json.Marshal(o) 56 return string(b) 57 } 58 59 func ChannelMembersFromJson(data io.Reader) *ChannelMembers { 60 var o *ChannelMembers 61 json.NewDecoder(data).Decode(&o) 62 return o 63 } 64 65 func ChannelUnreadFromJson(data io.Reader) *ChannelUnread { 66 var o *ChannelUnread 67 json.NewDecoder(data).Decode(&o) 68 return o 69 } 70 71 func (o *ChannelMember) ToJson() string { 72 b, _ := json.Marshal(o) 73 return string(b) 74 } 75 76 func ChannelMemberFromJson(data io.Reader) *ChannelMember { 77 var o *ChannelMember 78 json.NewDecoder(data).Decode(&o) 79 return o 80 } 81 82 func (o *ChannelMember) IsValid() *AppError { 83 84 if len(o.ChannelId) != 26 { 85 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.channel_id.app_error", nil, "", http.StatusBadRequest) 86 } 87 88 if len(o.UserId) != 26 { 89 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.user_id.app_error", nil, "", http.StatusBadRequest) 90 } 91 92 notifyLevel := o.NotifyProps[DESKTOP_NOTIFY_PROP] 93 if len(notifyLevel) > 20 || !IsChannelNotifyLevelValid(notifyLevel) { 94 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.notify_level.app_error", nil, "notify_level="+notifyLevel, http.StatusBadRequest) 95 } 96 97 markUnreadLevel := o.NotifyProps[MARK_UNREAD_NOTIFY_PROP] 98 if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { 99 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.unread_level.app_error", nil, "mark_unread_level="+markUnreadLevel, http.StatusBadRequest) 100 } 101 102 if pushLevel, ok := o.NotifyProps[PUSH_NOTIFY_PROP]; ok { 103 if len(pushLevel) > 20 || !IsChannelNotifyLevelValid(pushLevel) { 104 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.push_level.app_error", nil, "push_notification_level="+pushLevel, http.StatusBadRequest) 105 } 106 } 107 108 if sendEmail, ok := o.NotifyProps[EMAIL_NOTIFY_PROP]; ok { 109 if len(sendEmail) > 20 || !IsSendEmailValid(sendEmail) { 110 return NewAppError("ChannelMember.IsValid", "model.channel_member.is_valid.email_value.app_error", nil, "push_notification_level="+sendEmail, http.StatusBadRequest) 111 } 112 } 113 114 return nil 115 } 116 117 func (o *ChannelMember) PreSave() { 118 o.LastUpdateAt = GetMillis() 119 } 120 121 func (o *ChannelMember) PreUpdate() { 122 o.LastUpdateAt = GetMillis() 123 } 124 125 func (o *ChannelMember) GetRoles() []string { 126 return strings.Fields(o.Roles) 127 } 128 129 func IsChannelNotifyLevelValid(notifyLevel string) bool { 130 return notifyLevel == CHANNEL_NOTIFY_DEFAULT || 131 notifyLevel == CHANNEL_NOTIFY_ALL || 132 notifyLevel == CHANNEL_NOTIFY_MENTION || 133 notifyLevel == CHANNEL_NOTIFY_NONE 134 } 135 136 func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { 137 return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION 138 } 139 140 func IsSendEmailValid(sendEmail string) bool { 141 return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false" 142 } 143 144 func GetDefaultChannelNotifyProps() StringMap { 145 return StringMap{ 146 DESKTOP_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 147 MARK_UNREAD_NOTIFY_PROP: CHANNEL_MARK_UNREAD_ALL, 148 PUSH_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 149 EMAIL_NOTIFY_PROP: CHANNEL_NOTIFY_DEFAULT, 150 } 151 }