github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/group_syncable.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  	"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  	SchemeAdmin bool              `json:"scheme_admin"`
    34  	CreateAt    int64             `json:"create_at"`
    35  	DeleteAt    int64             `json:"delete_at"`
    36  	UpdateAt    int64             `json:"update_at"`
    37  	Type        GroupSyncableType `db:"-" json:"-"`
    38  
    39  	// Values joined in from the associated team and/or channel
    40  	ChannelDisplayName string `db:"-" json:"-"`
    41  	TeamDisplayName    string `db:"-" json:"-"`
    42  	TeamType           string `db:"-" json:"-"`
    43  	ChannelType        string `db:"-" json:"-"`
    44  	TeamID             string `db:"-" json:"-"`
    45  }
    46  
    47  func (syncable *GroupSyncable) IsValid() *AppError {
    48  	if !IsValidId(syncable.GroupId) {
    49  		return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.group_id.app_error", nil, "", http.StatusBadRequest)
    50  	}
    51  	if !IsValidId(syncable.SyncableId) {
    52  		return NewAppError("GroupSyncable.SyncableIsValid", "model.group_syncable.syncable_id.app_error", nil, "", http.StatusBadRequest)
    53  	}
    54  	return nil
    55  }
    56  
    57  func (syncable *GroupSyncable) UnmarshalJSON(b []byte) error {
    58  	var kvp map[string]interface{}
    59  	err := json.Unmarshal(b, &kvp)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	for key, value := range kvp {
    64  		switch key {
    65  		case "team_id":
    66  			syncable.SyncableId = value.(string)
    67  			syncable.Type = GroupSyncableTypeTeam
    68  		case "channel_id":
    69  			syncable.SyncableId = value.(string)
    70  			syncable.Type = GroupSyncableTypeChannel
    71  		case "group_id":
    72  			syncable.GroupId = value.(string)
    73  		case "auto_add":
    74  			syncable.AutoAdd = value.(bool)
    75  		default:
    76  		}
    77  	}
    78  	return nil
    79  }
    80  
    81  func (syncable *GroupSyncable) MarshalJSON() ([]byte, error) {
    82  	type Alias GroupSyncable
    83  
    84  	switch syncable.Type {
    85  	case GroupSyncableTypeTeam:
    86  		return json.Marshal(&struct {
    87  			TeamID          string `json:"team_id"`
    88  			TeamDisplayName string `json:"team_display_name,omitempty"`
    89  			TeamType        string `json:"team_type,omitempty"`
    90  			*Alias
    91  		}{
    92  			TeamDisplayName: syncable.TeamDisplayName,
    93  			TeamType:        syncable.TeamType,
    94  			TeamID:          syncable.SyncableId,
    95  			Alias:           (*Alias)(syncable),
    96  		})
    97  	case GroupSyncableTypeChannel:
    98  		return json.Marshal(&struct {
    99  			ChannelID          string `json:"channel_id"`
   100  			ChannelDisplayName string `json:"channel_display_name,omitempty"`
   101  			ChannelType        string `json:"channel_type,omitempty"`
   102  
   103  			TeamID          string `json:"team_id,omitempty"`
   104  			TeamDisplayName string `json:"team_display_name,omitempty"`
   105  			TeamType        string `json:"team_type,omitempty"`
   106  
   107  			*Alias
   108  		}{
   109  			ChannelID:          syncable.SyncableId,
   110  			ChannelDisplayName: syncable.ChannelDisplayName,
   111  			ChannelType:        syncable.ChannelType,
   112  
   113  			TeamID:          syncable.TeamID,
   114  			TeamDisplayName: syncable.TeamDisplayName,
   115  			TeamType:        syncable.TeamType,
   116  
   117  			Alias: (*Alias)(syncable),
   118  		})
   119  	default:
   120  		return nil, &json.MarshalerError{
   121  			Err: fmt.Errorf("unknown syncable type: %s", syncable.Type),
   122  		}
   123  	}
   124  }
   125  
   126  type GroupSyncablePatch struct {
   127  	AutoAdd     *bool `json:"auto_add"`
   128  	SchemeAdmin *bool `json:"scheme_admin"`
   129  }
   130  
   131  func (syncable *GroupSyncable) Patch(patch *GroupSyncablePatch) {
   132  	if patch.AutoAdd != nil {
   133  		syncable.AutoAdd = *patch.AutoAdd
   134  	}
   135  	if patch.SchemeAdmin != nil {
   136  		syncable.SchemeAdmin = *patch.SchemeAdmin
   137  	}
   138  }
   139  
   140  type UserTeamIDPair struct {
   141  	UserID string
   142  	TeamID string
   143  }
   144  
   145  type UserChannelIDPair struct {
   146  	UserID    string
   147  	ChannelID string
   148  }
   149  
   150  func GroupSyncableFromJson(data io.Reader) *GroupSyncable {
   151  	groupSyncable := &GroupSyncable{}
   152  	bodyBytes, _ := ioutil.ReadAll(data)
   153  	json.Unmarshal(bodyBytes, groupSyncable)
   154  	return groupSyncable
   155  }
   156  
   157  func GroupSyncablesFromJson(data io.Reader) []*GroupSyncable {
   158  	groupSyncables := []*GroupSyncable{}
   159  	bodyBytes, _ := ioutil.ReadAll(data)
   160  	json.Unmarshal(bodyBytes, &groupSyncables)
   161  	return groupSyncables
   162  }
   163  
   164  func NewGroupTeam(groupID, teamID string, autoAdd bool) *GroupSyncable {
   165  	return &GroupSyncable{
   166  		GroupId:    groupID,
   167  		SyncableId: teamID,
   168  		Type:       GroupSyncableTypeTeam,
   169  		AutoAdd:    autoAdd,
   170  	}
   171  }
   172  
   173  func NewGroupChannel(groupID, channelID string, autoAdd bool) *GroupSyncable {
   174  	return &GroupSyncable{
   175  		GroupId:    groupID,
   176  		SyncableId: channelID,
   177  		Type:       GroupSyncableTypeChannel,
   178  		AutoAdd:    autoAdd,
   179  	}
   180  }