github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/store/searchlayer/channel_layer.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package searchlayer 5 6 import ( 7 "github.com/mattermost/mattermost-server/v5/mlog" 8 "github.com/mattermost/mattermost-server/v5/model" 9 "github.com/mattermost/mattermost-server/v5/services/searchengine" 10 "github.com/mattermost/mattermost-server/v5/store" 11 ) 12 13 type SearchChannelStore struct { 14 store.ChannelStore 15 rootStore *SearchStore 16 } 17 18 func (c *SearchChannelStore) deleteChannelIndex(channel *model.Channel) { 19 if channel.Type == model.CHANNEL_OPEN { 20 for _, engine := range c.rootStore.searchEngine.GetActiveEngines() { 21 if engine.IsIndexingEnabled() { 22 runIndexFn(engine, func(engineCopy searchengine.SearchEngineInterface) { 23 if err := engineCopy.DeleteChannel(channel); err != nil { 24 mlog.Error("Encountered error deleting channel", mlog.String("channel_id", channel.Id), mlog.String("search_engine", engineCopy.GetName()), mlog.Err(err)) 25 } 26 mlog.Debug("Removed channel from index in search engine", mlog.String("search_engine", engineCopy.GetName()), mlog.String("channel_id", channel.Id)) 27 }) 28 } 29 } 30 } 31 } 32 33 func (c *SearchChannelStore) indexChannel(channel *model.Channel) { 34 if channel.Type == model.CHANNEL_OPEN { 35 for _, engine := range c.rootStore.searchEngine.GetActiveEngines() { 36 if engine.IsIndexingEnabled() { 37 runIndexFn(engine, func(engineCopy searchengine.SearchEngineInterface) { 38 if err := engineCopy.IndexChannel(channel); err != nil { 39 mlog.Error("Encountered error indexing channel", mlog.String("channel_id", channel.Id), mlog.String("search_engine", engineCopy.GetName()), mlog.Err(err)) 40 } 41 mlog.Debug("Indexed channel in search engine", mlog.String("search_engine", engineCopy.GetName()), mlog.String("channel_id", channel.Id)) 42 }) 43 } 44 } 45 } 46 } 47 48 func (c *SearchChannelStore) Save(channel *model.Channel, maxChannels int64) (*model.Channel, error) { 49 newChannel, err := c.ChannelStore.Save(channel, maxChannels) 50 if err == nil { 51 c.indexChannel(newChannel) 52 } 53 return newChannel, err 54 } 55 56 func (c *SearchChannelStore) Update(channel *model.Channel) (*model.Channel, error) { 57 updatedChannel, err := c.ChannelStore.Update(channel) 58 if err == nil { 59 c.indexChannel(updatedChannel) 60 } 61 return updatedChannel, err 62 } 63 64 func (c *SearchChannelStore) UpdateMember(cm *model.ChannelMember) (*model.ChannelMember, *model.AppError) { 65 member, err := c.ChannelStore.UpdateMember(cm) 66 if err == nil { 67 c.rootStore.indexUserFromID(cm.UserId) 68 channel, channelErr := c.ChannelStore.Get(member.ChannelId, true) 69 if channelErr != nil { 70 mlog.Error("Encountered error indexing user in channel", mlog.String("channel_id", member.ChannelId), mlog.Err(channelErr)) 71 } else { 72 c.rootStore.indexUserFromID(channel.CreatorId) 73 } 74 } 75 return member, err 76 } 77 78 func (c *SearchChannelStore) SaveMember(cm *model.ChannelMember) (*model.ChannelMember, *model.AppError) { 79 member, err := c.ChannelStore.SaveMember(cm) 80 if err == nil { 81 c.rootStore.indexUserFromID(cm.UserId) 82 channel, channelErr := c.ChannelStore.Get(member.ChannelId, true) 83 if channelErr != nil { 84 mlog.Error("Encountered error indexing user in channel", mlog.String("channel_id", member.ChannelId), mlog.Err(channelErr)) 85 } else { 86 c.rootStore.indexUserFromID(channel.CreatorId) 87 } 88 } 89 return member, err 90 } 91 92 func (c *SearchChannelStore) RemoveMember(channelId, userIdToRemove string) *model.AppError { 93 err := c.ChannelStore.RemoveMember(channelId, userIdToRemove) 94 if err == nil { 95 c.rootStore.indexUserFromID(userIdToRemove) 96 } 97 return err 98 } 99 100 func (c *SearchChannelStore) RemoveMembers(channelId string, userIds []string) *model.AppError { 101 if err := c.ChannelStore.RemoveMembers(channelId, userIds); err != nil { 102 return err 103 } 104 105 for _, uid := range userIds { 106 c.rootStore.indexUserFromID(uid) 107 } 108 return nil 109 } 110 111 func (c *SearchChannelStore) CreateDirectChannel(user *model.User, otherUser *model.User) (*model.Channel, error) { 112 channel, err := c.ChannelStore.CreateDirectChannel(user, otherUser) 113 if err == nil { 114 c.rootStore.indexUserFromID(user.Id) 115 c.rootStore.indexUserFromID(otherUser.Id) 116 } 117 return channel, err 118 } 119 120 func (c *SearchChannelStore) SaveDirectChannel(directchannel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) (*model.Channel, error) { 121 channel, err := c.ChannelStore.SaveDirectChannel(directchannel, member1, member2) 122 if err != nil { 123 c.rootStore.indexUserFromID(member1.UserId) 124 c.rootStore.indexUserFromID(member2.UserId) 125 } 126 return channel, err 127 } 128 129 func (c *SearchChannelStore) AutocompleteInTeam(teamId string, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) { 130 var channelList *model.ChannelList 131 var err *model.AppError 132 133 allFailed := true 134 for _, engine := range c.rootStore.searchEngine.GetActiveEngines() { 135 if engine.IsAutocompletionEnabled() { 136 channelList, err = c.searchAutocompleteChannels(engine, teamId, term, includeDeleted) 137 if err != nil { 138 mlog.Error("Encountered error on AutocompleteChannels through SearchEngine. Falling back to default autocompletion.", mlog.String("search_engine", engine.GetName()), mlog.Err(err)) 139 continue 140 } 141 allFailed = false 142 mlog.Debug("Using the first available search engine", mlog.String("search_engine", engine.GetName())) 143 break 144 } 145 } 146 147 if allFailed { 148 mlog.Debug("Using database search because no other search engine is available") 149 channelList, err = c.ChannelStore.AutocompleteInTeam(teamId, term, includeDeleted) 150 if err != nil { 151 return nil, err 152 } 153 } 154 return channelList, err 155 } 156 157 func (c *SearchChannelStore) searchAutocompleteChannels(engine searchengine.SearchEngineInterface, teamId, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) { 158 channelIds, err := engine.SearchChannels(teamId, term) 159 if err != nil { 160 return nil, err 161 } 162 163 channelList := model.ChannelList{} 164 if len(channelIds) > 0 { 165 channels, err := c.ChannelStore.GetChannelsByIds(channelIds, includeDeleted) 166 if err != nil { 167 return nil, err 168 } 169 for _, ch := range channels { 170 channelList = append(channelList, ch) 171 } 172 } 173 174 return &channelList, nil 175 } 176 177 func (c *SearchChannelStore) PermanentDeleteMembersByUser(userId string) *model.AppError { 178 err := c.ChannelStore.PermanentDeleteMembersByUser(userId) 179 if err == nil { 180 c.rootStore.indexUserFromID(userId) 181 } 182 return err 183 } 184 185 func (c *SearchChannelStore) RemoveAllDeactivatedMembers(channelId string) *model.AppError { 186 profiles, errProfiles := c.rootStore.User().GetAllProfilesInChannel(channelId, true) 187 if errProfiles != nil { 188 mlog.Error("Encountered error indexing users for channel", mlog.String("channel_id", channelId), mlog.Err(errProfiles)) 189 } 190 191 err := c.ChannelStore.RemoveAllDeactivatedMembers(channelId) 192 if err == nil && errProfiles == nil { 193 for _, user := range profiles { 194 if user.DeleteAt != 0 { 195 c.rootStore.indexUser(user) 196 } 197 } 198 } 199 return err 200 } 201 202 func (c *SearchChannelStore) PermanentDeleteMembersByChannel(channelId string) *model.AppError { 203 profiles, errProfiles := c.rootStore.User().GetAllProfilesInChannel(channelId, true) 204 if errProfiles != nil { 205 mlog.Error("Encountered error indexing users for channel", mlog.String("channel_id", channelId), mlog.Err(errProfiles)) 206 } 207 208 err := c.ChannelStore.PermanentDeleteMembersByChannel(channelId) 209 if err == nil && errProfiles == nil { 210 for _, user := range profiles { 211 c.rootStore.indexUser(user) 212 } 213 } 214 return err 215 } 216 217 func (c *SearchChannelStore) PermanentDelete(channelId string) error { 218 channel, channelErr := c.ChannelStore.Get(channelId, true) 219 if channelErr != nil { 220 mlog.Error("Encountered error deleting channel", mlog.String("channel_id", channelId), mlog.Err(channelErr)) 221 } 222 err := c.ChannelStore.PermanentDelete(channelId) 223 if err == nil && channelErr == nil { 224 c.deleteChannelIndex(channel) 225 } 226 return err 227 }