summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v5/model/group_syncable.go
blob: eb3bdf0978e391bb6aee5ae1a864ebb5ff5af6cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package model

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)

type GroupSyncableType string

const (
	GroupSyncableTypeTeam    GroupSyncableType = "Team"
	GroupSyncableTypeChannel GroupSyncableType = "Channel"
)

func (gst GroupSyncableType) String() string {
	return string(gst)
}

type GroupSyncable struct {
	GroupId string `json:"group_id"`

	// SyncableId represents the Id of the model that is being synced with the group, for example a ChannelId or
	// TeamId.
	SyncableId string `db:"-" json:"-"`

	AutoAdd     bool              `json:"auto_add"`
	SchemeAdmin bool              `json:"scheme_admin"`
	CreateAt    int64             `json:"create_at"`
	DeleteAt    int64             `json:"delete_at"`
	UpdateAt    int64             `json:"update_at"`
	Type        GroupSyncableType `db:"-" json:"-"`

	// Values joined in from the associated team and/or channel
	ChannelDisplayName string `db:"-" json:"-"`
	TeamDisplayName    string `db:"-" json:"-"`
	TeamType           string `db:"-" json:"-"`
	ChannelType        string `db:"-" json:"-"`
	TeamID             string `db:"-" json:"-"`
}

func (syncable *GroupSyncable) IsValid() *AppError {
	if !IsValidId(syncable.GroupId) {
		return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.group_id.app_error", nil, "", http.StatusBadRequest)
	}
	if !IsValidId(syncable.SyncableId) {
		return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.syncable_id.app_error", nil, "", http.StatusBadRequest)
	}
	return nil
}

func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error {
	var kvp map[string]interface{}
	err := json.Unmarshal(b, &kvp)
	if err != nil {
		return err
	}
	var channelId string
	var teamId string
	for key, value := range kvp {
		switch key {
		case "team_id":
			teamId = value.(string)
		case "channel_id":
			channelId = value.(string)
		case "group_id":
			syncable.GroupId = value.(string)
		case "auto_add":
			syncable.AutoAdd = value.(bool)
		default:
		}
	}
	if channelId != "" {
		syncable.TeamID = teamId
		syncable.SyncableId = channelId
		syncable.Type = GroupSyncableTypeChannel
	} else {
		syncable.SyncableId = teamId
		syncable.Type = GroupSyncableTypeTeam
	}
	return nil
}

func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) {
	type Alias GroupSyncable
	switch syncable.Type {
	case GroupSyncableTypeTeam:
		return json.Marshal(&struct {
			TeamID          string            `json:"team_id"`
			TeamDisplayName string            `json:"team_display_name,omitempty"`
			TeamType        string            `json:"team_type,omitempty"`
			Type            GroupSyncableType `json:"type,omitempty"`
			*Alias
		}{
			TeamDisplayName: syncable.TeamDisplayName,
			TeamType:        syncable.TeamType,
			TeamID:          syncable.SyncableId,
			Type:            syncable.Type,
			Alias:           (*Alias)(syncable),
		})
	case GroupSyncableTypeChannel:
		return json.Marshal(&struct {
			ChannelID          string            `json:"channel_id"`
			ChannelDisplayName string            `json:"channel_display_name,omitempty"`
			ChannelType        string            `json:"channel_type,omitempty"`
			Type               GroupSyncableType `json:"type,omitempty"`

			TeamID          string `json:"team_id,omitempty"`
			TeamDisplayName string `json:"team_display_name,omitempty"`
			TeamType        string `json:"team_type,omitempty"`

			*Alias
		}{
			ChannelID:          syncable.SyncableId,
			ChannelDisplayName: syncable.ChannelDisplayName,
			ChannelType:        syncable.ChannelType,
			Type:               syncable.Type,

			TeamID:          syncable.TeamID,
			TeamDisplayName: syncable.TeamDisplayName,
			TeamType:        syncable.TeamType,

			Alias: (*Alias)(syncable),
		})
	default:
		return nil, &json.MarshalerError{
			Err: fmt.Errorf("unknown syncable type: %s", syncable.Type),
		}
	}
}

type GroupSyncablePatch struct {
	AutoAdd     *bool `json:"auto_add"`
	SchemeAdmin *bool `json:"scheme_admin"`
}

func (syncable *GroupSyncable) Patch(patch *GroupSyncablePatch) {
	if patch.AutoAdd != nil {
		syncable.AutoAdd = *patch.AutoAdd
	}
	if patch.SchemeAdmin != nil {
		syncable.SchemeAdmin = *patch.SchemeAdmin
	}
}

type UserTeamIDPair struct {
	UserID string
	TeamID string
}

type UserChannelIDPair struct {
	UserID    string
	ChannelID string
}

func GroupSyncableFromJson(data io.Reader) *GroupSyncable {
	groupSyncable := &GroupSyncable{}
	bodyBytes, _ := ioutil.ReadAll(data)
	json.Unmarshal(bodyBytes, groupSyncable)
	return groupSyncable
}

func GroupSyncablesFromJson(data io.Reader) []*GroupSyncable {
	groupSyncables := []*GroupSyncable{}
	bodyBytes, _ := ioutil.ReadAll(data)
	json.Unmarshal(bodyBytes, &groupSyncables)
	return groupSyncables
}

func NewGroupTeam(groupID, teamID string, autoAdd bool) *GroupSyncable {
	return &GroupSyncable{
		GroupId:    groupID,
		SyncableId: teamID,
		Type:       GroupSyncableTypeTeam,
		AutoAdd:    autoAdd,
	}
}

func NewGroupChannel(groupID, channelID string, autoAdd bool) *GroupSyncable {
	return &GroupSyncable{
		GroupId:    groupID,
		SyncableId: channelID,
		Type:       GroupSyncableTypeChannel,
		AutoAdd:    autoAdd,
	}
}