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