github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/channel_sidebar.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 ) 10 11 type SidebarCategoryType string 12 type SidebarCategorySorting string 13 14 const ( 15 // Each sidebar category has a 'type'. System categories are Channels, Favorites and DMs 16 // All user-created categories will have type Custom 17 SidebarCategoryChannels SidebarCategoryType = "channels" 18 SidebarCategoryDirectMessages SidebarCategoryType = "direct_messages" 19 SidebarCategoryFavorites SidebarCategoryType = "favorites" 20 SidebarCategoryCustom SidebarCategoryType = "custom" 21 // Increment to use when adding/reordering things in the sidebar 22 MinimalSidebarSortDistance = 10 23 // Default Sort Orders for categories 24 DefaultSidebarSortOrderFavorites = 0 25 DefaultSidebarSortOrderChannels = DefaultSidebarSortOrderFavorites + MinimalSidebarSortDistance 26 DefaultSidebarSortOrderDMs = DefaultSidebarSortOrderChannels + MinimalSidebarSortDistance 27 // Sorting modes 28 // default for all categories except DMs (behaves like manual) 29 SidebarCategorySortDefault SidebarCategorySorting = "" 30 // sort manually 31 SidebarCategorySortManual SidebarCategorySorting = "manual" 32 // sort by recency (default for DMs) 33 SidebarCategorySortRecent SidebarCategorySorting = "recent" 34 // sort by display name alphabetically 35 SidebarCategorySortAlphabetical SidebarCategorySorting = "alpha" 36 ) 37 38 // SidebarCategory represents the corresponding DB table 39 // SortOrder is never returned to the user and only used for queries 40 type SidebarCategory struct { 41 Id string `json:"id"` 42 UserId string `json:"user_id"` 43 TeamId string `json:"team_id"` 44 SortOrder int64 `json:"-"` 45 Sorting SidebarCategorySorting `json:"sorting"` 46 Type SidebarCategoryType `json:"type"` 47 DisplayName string `json:"display_name"` 48 } 49 50 // SidebarCategoryWithChannels combines data from SidebarCategory table with the Channel IDs that belong to that category 51 type SidebarCategoryWithChannels struct { 52 SidebarCategory 53 Channels []string `json:"channel_ids"` 54 } 55 56 type SidebarCategoryOrder []string 57 58 // OrderedSidebarCategories combines categories, their channel IDs and an array of Category IDs, sorted 59 type OrderedSidebarCategories struct { 60 Categories SidebarCategoriesWithChannels `json:"categories"` 61 Order SidebarCategoryOrder `json:"order"` 62 } 63 64 type SidebarChannel struct { 65 ChannelId string `json:"channel_id"` 66 UserId string `json:"user_id"` 67 CategoryId string `json:"category_id"` 68 SortOrder int64 `json:"-"` 69 } 70 71 type SidebarChannels []*SidebarChannel 72 type SidebarCategoriesWithChannels []*SidebarCategoryWithChannels 73 74 func SidebarCategoryFromJson(data io.Reader) (*SidebarCategoryWithChannels, error) { 75 var o *SidebarCategoryWithChannels 76 err := json.NewDecoder(data).Decode(&o) 77 return o, err 78 } 79 80 func SidebarCategoriesFromJson(data io.Reader) ([]*SidebarCategoryWithChannels, error) { 81 var o []*SidebarCategoryWithChannels 82 err := json.NewDecoder(data).Decode(&o) 83 return o, err 84 } 85 86 func OrderedSidebarCategoriesFromJson(data io.Reader) (*OrderedSidebarCategories, error) { 87 var o *OrderedSidebarCategories 88 err := json.NewDecoder(data).Decode(&o) 89 return o, err 90 } 91 92 func (o SidebarCategoryWithChannels) ToJson() []byte { 93 b, _ := json.Marshal(o) 94 return b 95 } 96 97 func SidebarCategoriesWithChannelsToJson(o []*SidebarCategoryWithChannels) []byte { 98 if b, err := json.Marshal(o); err != nil { 99 return []byte("[]") 100 } else { 101 return b 102 } 103 } 104 105 func (o OrderedSidebarCategories) ToJson() []byte { 106 if b, err := json.Marshal(o); err != nil { 107 return []byte("[]") 108 } else { 109 return b 110 } 111 }