github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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 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  
    44  type 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  
    59  type ChannelMembers []ChannelMember
    60  
    61  type ChannelMemberForExport struct {
    62  	ChannelMember
    63  	ChannelName string
    64  	Username    string
    65  }
    66  
    67  func (o *ChannelMembers) ToJson() string {
    68  	if b, err := json.Marshal(o); err != nil {
    69  		return "[]"
    70  	} else {
    71  		return string(b)
    72  	}
    73  }
    74  
    75  func (o *ChannelUnread) ToJson() string {
    76  	b, _ := json.Marshal(o)
    77  	return string(b)
    78  }
    79  
    80  func (o *ChannelUnreadAt) ToJson() string {
    81  	b, _ := json.Marshal(o)
    82  	return string(b)
    83  }
    84  
    85  func ChannelMembersFromJson(data io.Reader) *ChannelMembers {
    86  	var o *ChannelMembers
    87  	json.NewDecoder(data).Decode(&o)
    88  	return o
    89  }
    90  
    91  func ChannelUnreadFromJson(data io.Reader) *ChannelUnread {
    92  	var o *ChannelUnread
    93  	json.NewDecoder(data).Decode(&o)
    94  	return o
    95  }
    96  
    97  func ChannelUnreadAtFromJson(data io.Reader) *ChannelUnreadAt {
    98  	var o *ChannelUnreadAt
    99  	json.NewDecoder(data).Decode(&o)
   100  	return o
   101  }
   102  
   103  func (o *ChannelMember) ToJson() string {
   104  	b, _ := json.Marshal(o)
   105  	return string(b)
   106  }
   107  
   108  func ChannelMemberFromJson(data io.Reader) *ChannelMember {
   109  	var o *ChannelMember
   110  	json.NewDecoder(data).Decode(&o)
   111  	return o
   112  }
   113  
   114  func (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  
   155  func (o *ChannelMember) PreSave() {
   156  	o.LastUpdateAt = GetMillis()
   157  }
   158  
   159  func (o *ChannelMember) PreUpdate() {
   160  	o.LastUpdateAt = GetMillis()
   161  }
   162  
   163  func (o *ChannelMember) GetRoles() []string {
   164  	return strings.Fields(o.Roles)
   165  }
   166  
   167  func IsChannelNotifyLevelValid(notifyLevel string) bool {
   168  	return notifyLevel == CHANNEL_NOTIFY_DEFAULT ||
   169  		notifyLevel == CHANNEL_NOTIFY_ALL ||
   170  		notifyLevel == CHANNEL_NOTIFY_MENTION ||
   171  		notifyLevel == CHANNEL_NOTIFY_NONE
   172  }
   173  
   174  func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool {
   175  	return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION
   176  }
   177  
   178  func IsSendEmailValid(sendEmail string) bool {
   179  	return sendEmail == CHANNEL_NOTIFY_DEFAULT || sendEmail == "true" || sendEmail == "false"
   180  }
   181  
   182  func IsIgnoreChannelMentionsValid(ignoreChannelMentions string) bool {
   183  	return ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_ON || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_OFF || ignoreChannelMentions == IGNORE_CHANNEL_MENTIONS_DEFAULT
   184  }
   185  
   186  func GetDefaultChannelNotifyProps() StringMap {
   187  	return StringMap{
   188  		DESKTOP_NOTIFY_PROP:                 CHANNEL_NOTIFY_DEFAULT,
   189  		MARK_UNREAD_NOTIFY_PROP:             CHANNEL_MARK_UNREAD_ALL,
   190  		PUSH_NOTIFY_PROP:                    CHANNEL_NOTIFY_DEFAULT,
   191  		EMAIL_NOTIFY_PROP:                   CHANNEL_NOTIFY_DEFAULT,
   192  		IGNORE_CHANNEL_MENTIONS_NOTIFY_PROP: IGNORE_CHANNEL_MENTIONS_DEFAULT,
   193  	}
   194  }