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