github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/group_syncable.go (about) 1 // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "net/http" 12 ) 13 14 type GroupSyncableType string 15 16 const ( 17 GroupSyncableTypeTeam GroupSyncableType = "Team" 18 GroupSyncableTypeChannel GroupSyncableType = "Channel" 19 ) 20 21 func (gst GroupSyncableType) String() string { 22 return string(gst) 23 } 24 25 type GroupSyncable struct { 26 GroupId string `json:"group_id"` 27 28 // SyncableId represents the Id of the model that is being synced with the group, for example a ChannelId or 29 // TeamId. 30 SyncableId string `db:"-" json:"-"` 31 32 AutoAdd bool `json:"auto_add"` 33 CreateAt int64 `json:"create_at"` 34 DeleteAt int64 `json:"delete_at"` 35 UpdateAt int64 `json:"update_at"` 36 Type GroupSyncableType `db:"-" json:"-"` 37 38 // Values joined in from the associated team and/or channel 39 ChannelDisplayName string `db:"-" json:"-"` 40 TeamDisplayName string `db:"-" json:"-"` 41 TeamType string `db:"-" json:"-"` 42 ChannelType string `db:"-" json:"-"` 43 TeamID string `db:"-" json:"-"` 44 } 45 46 func (syncable *GroupSyncable) IsValid() *AppError { 47 if !IsValidId(syncable.GroupId) { 48 return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.group_id.app_error", nil, "", http.StatusBadRequest) 49 } 50 if !IsValidId(syncable.SyncableId) { 51 return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.syncable_id.app_error", nil, "", http.StatusBadRequest) 52 } 53 return nil 54 } 55 56 func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error { 57 var kvp map[string]interface{} 58 err := json.Unmarshal(b, &kvp) 59 if err != nil { 60 return err 61 } 62 for key, value := range kvp { 63 switch key { 64 case "team_id": 65 syncable.SyncableId = value.(string) 66 syncable.Type = GroupSyncableTypeTeam 67 case "channel_id": 68 syncable.SyncableId = value.(string) 69 syncable.Type = GroupSyncableTypeChannel 70 case "group_id": 71 syncable.GroupId = value.(string) 72 case "auto_add": 73 syncable.AutoAdd = value.(bool) 74 default: 75 } 76 } 77 return nil 78 } 79 80 func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) { 81 type Alias GroupSyncable 82 83 switch syncable.Type { 84 case GroupSyncableTypeTeam: 85 return json.Marshal(&struct { 86 TeamID string `json:"team_id"` 87 TeamDisplayName string `json:"team_display_name,omitempty"` 88 TeamType string `json:"team_type,omitempty"` 89 *Alias 90 }{ 91 TeamDisplayName: syncable.TeamDisplayName, 92 TeamType: syncable.TeamType, 93 TeamID: syncable.SyncableId, 94 Alias: (*Alias)(syncable), 95 }) 96 case GroupSyncableTypeChannel: 97 return json.Marshal(&struct { 98 ChannelID string `json:"channel_id"` 99 ChannelDisplayName string `json:"channel_display_name,omitempty"` 100 ChannelType string `json:"channel_type,omitempty"` 101 102 TeamID string `json:"team_id,omitempty"` 103 TeamDisplayName string `json:"team_display_name,omitempty"` 104 TeamType string `json:"team_type,omitempty"` 105 106 *Alias 107 }{ 108 ChannelID: syncable.SyncableId, 109 ChannelDisplayName: syncable.ChannelDisplayName, 110 ChannelType: syncable.ChannelType, 111 112 TeamID: syncable.TeamID, 113 TeamDisplayName: syncable.TeamDisplayName, 114 TeamType: syncable.TeamType, 115 116 Alias: (*Alias)(syncable), 117 }) 118 default: 119 return nil, &json.MarshalerError{ 120 Err: fmt.Errorf("unknown syncable type: %s", syncable.Type), 121 } 122 } 123 } 124 125 type GroupSyncablePatch struct { 126 AutoAdd *bool `json:"auto_add"` 127 } 128 129 func (syncable *GroupSyncable) Patch(patch *GroupSyncablePatch) { 130 if patch.AutoAdd != nil { 131 syncable.AutoAdd = *patch.AutoAdd 132 } 133 } 134 135 type UserTeamIDPair struct { 136 UserID string 137 TeamID string 138 } 139 140 type UserChannelIDPair struct { 141 UserID string 142 ChannelID string 143 } 144 145 func GroupSyncableFromJson(data io.Reader) *GroupSyncable { 146 groupSyncable := &GroupSyncable{} 147 bodyBytes, _ := ioutil.ReadAll(data) 148 json.Unmarshal(bodyBytes, groupSyncable) 149 return groupSyncable 150 } 151 152 func GroupSyncablesFromJson(data io.Reader) []*GroupSyncable { 153 groupSyncables := []*GroupSyncable{} 154 bodyBytes, _ := ioutil.ReadAll(data) 155 json.Unmarshal(bodyBytes, &groupSyncables) 156 return groupSyncables 157 }