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