summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v6/model/channel_sidebar.go
blob: d5d21112da28b2aaa9728674e8074fc3d4f244c0 (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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package model

import (
	"encoding/json"
	"errors"
	"regexp"
)

type SidebarCategoryType string
type SidebarCategorySorting string

const (
	// Each sidebar category has a 'type'. System categories are Channels, Favorites and DMs
	// All user-created categories will have type Custom
	SidebarCategoryChannels       SidebarCategoryType = "channels"
	SidebarCategoryDirectMessages SidebarCategoryType = "direct_messages"
	SidebarCategoryFavorites      SidebarCategoryType = "favorites"
	SidebarCategoryCustom         SidebarCategoryType = "custom"
	// Increment to use when adding/reordering things in the sidebar
	MinimalSidebarSortDistance = 10
	// Default Sort Orders for categories
	DefaultSidebarSortOrderFavorites = 0
	DefaultSidebarSortOrderChannels  = DefaultSidebarSortOrderFavorites + MinimalSidebarSortDistance
	DefaultSidebarSortOrderDMs       = DefaultSidebarSortOrderChannels + MinimalSidebarSortDistance
	// Sorting modes
	// default for all categories except DMs (behaves like manual)
	SidebarCategorySortDefault SidebarCategorySorting = ""
	// sort manually
	SidebarCategorySortManual SidebarCategorySorting = "manual"
	// sort by recency (default for DMs)
	SidebarCategorySortRecent SidebarCategorySorting = "recent"
	// sort by display name alphabetically
	SidebarCategorySortAlphabetical SidebarCategorySorting = "alpha"
)

// SidebarCategory represents the corresponding DB table
type SidebarCategory struct {
	Id          string                 `json:"id"`
	UserId      string                 `json:"user_id"`
	TeamId      string                 `json:"team_id"`
	SortOrder   int64                  `json:"sort_order"`
	Sorting     SidebarCategorySorting `json:"sorting"`
	Type        SidebarCategoryType    `json:"type"`
	DisplayName string                 `json:"display_name"`
	Muted       bool                   `json:"muted"`
	Collapsed   bool                   `json:"collapsed"`
}

// SidebarCategoryWithChannels combines data from SidebarCategory table with the Channel IDs that belong to that category
type SidebarCategoryWithChannels struct {
	SidebarCategory
	Channels []string `json:"channel_ids"`
}

func (sc SidebarCategoryWithChannels) ChannelIds() []string {
	return sc.Channels
}

type SidebarCategoryOrder []string

// OrderedSidebarCategories combines categories, their channel IDs and an array of Category IDs, sorted
type OrderedSidebarCategories struct {
	Categories SidebarCategoriesWithChannels `json:"categories"`
	Order      SidebarCategoryOrder          `json:"order"`
}

type SidebarChannel struct {
	ChannelId  string `json:"channel_id"`
	UserId     string `json:"user_id"`
	CategoryId string `json:"category_id"`
	SortOrder  int64  `json:"-"`
}

type SidebarChannels []*SidebarChannel
type SidebarCategoriesWithChannels []*SidebarCategoryWithChannels

var categoryIdPattern = regexp.MustCompile("(favorites|channels|direct_messages)_[a-z0-9]{26}_[a-z0-9]{26}")

func IsValidCategoryId(s string) bool {
	// Category IDs can either be regular IDs
	if IsValidId(s) {
		return true
	}

	// Or default categories can follow the pattern {type}_{userID}_{teamID}
	return categoryIdPattern.MatchString(s)
}

func (SidebarCategoryType) ImplementsGraphQLType(name string) bool {
	return name == "SidebarCategoryType"
}

func (t SidebarCategoryType) MarshalJSON() ([]byte, error) {
	return json.Marshal(string(t))
}

func (t *SidebarCategoryType) UnmarshalGraphQL(input interface{}) error {
	chType, ok := input.(string)
	if !ok {
		return errors.New("wrong type")
	}

	*t = SidebarCategoryType(chType)
	return nil
}

func (SidebarCategorySorting) ImplementsGraphQLType(name string) bool {
	return name == "SidebarCategorySorting"
}

func (t SidebarCategorySorting) MarshalJSON() ([]byte, error) {
	return json.Marshal(string(t))
}

func (t *SidebarCategorySorting) UnmarshalGraphQL(input interface{}) error {
	chType, ok := input.(string)
	if !ok {
		return errors.New("wrong type")
	}

	*t = SidebarCategorySorting(chType)
	return nil
}