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