github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/searchlayer/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  	"sync/atomic"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/mlog"
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/services/searchengine"
    13  	"github.com/mattermost/mattermost-server/v5/store"
    14  )
    15  
    16  type SearchStore struct {
    17  	store.Store
    18  	searchEngine *searchengine.Broker
    19  	user         *SearchUserStore
    20  	team         *SearchTeamStore
    21  	channel      *SearchChannelStore
    22  	post         *SearchPostStore
    23  	fileInfo     *SearchFileInfoStore
    24  	configValue  atomic.Value
    25  }
    26  
    27  func NewSearchLayer(baseStore store.Store, searchEngine *searchengine.Broker, cfg *model.Config) *SearchStore {
    28  	searchStore := &SearchStore{
    29  		Store:        baseStore,
    30  		searchEngine: searchEngine,
    31  	}
    32  	searchStore.configValue.Store(cfg)
    33  	searchStore.channel = &SearchChannelStore{ChannelStore: baseStore.Channel(), rootStore: searchStore}
    34  	searchStore.post = &SearchPostStore{PostStore: baseStore.Post(), rootStore: searchStore}
    35  	searchStore.team = &SearchTeamStore{TeamStore: baseStore.Team(), rootStore: searchStore}
    36  	searchStore.user = &SearchUserStore{UserStore: baseStore.User(), rootStore: searchStore}
    37  	searchStore.fileInfo = &SearchFileInfoStore{FileInfoStore: baseStore.FileInfo(), rootStore: searchStore}
    38  
    39  	return searchStore
    40  }
    41  
    42  func (s *SearchStore) UpdateConfig(cfg *model.Config) {
    43  	s.configValue.Store(cfg)
    44  }
    45  
    46  func (s *SearchStore) getConfig() *model.Config {
    47  	return s.configValue.Load().(*model.Config)
    48  }
    49  
    50  func (s *SearchStore) Channel() store.ChannelStore {
    51  	return s.channel
    52  }
    53  
    54  func (s *SearchStore) Post() store.PostStore {
    55  	return s.post
    56  }
    57  
    58  func (s *SearchStore) FileInfo() store.FileInfoStore {
    59  	return s.fileInfo
    60  }
    61  
    62  func (s *SearchStore) Team() store.TeamStore {
    63  	return s.team
    64  }
    65  
    66  func (s *SearchStore) User() store.UserStore {
    67  	return s.user
    68  }
    69  
    70  func (s *SearchStore) indexUserFromID(userId string) {
    71  	user, err := s.User().Get(context.Background(), userId)
    72  	if err != nil {
    73  		return
    74  	}
    75  	s.indexUser(user)
    76  }
    77  
    78  func (s *SearchStore) indexUser(user *model.User) {
    79  	for _, engine := range s.searchEngine.GetActiveEngines() {
    80  		if engine.IsIndexingEnabled() {
    81  			runIndexFn(engine, func(engineCopy searchengine.SearchEngineInterface) {
    82  				userTeams, nErr := s.Team().GetTeamsByUserId(user.Id)
    83  				if nErr != nil {
    84  					mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.String("search_engine", engineCopy.GetName()), mlog.Err(nErr))
    85  					return
    86  				}
    87  
    88  				userTeamsIds := []string{}
    89  				for _, team := range userTeams {
    90  					userTeamsIds = append(userTeamsIds, team.Id)
    91  				}
    92  
    93  				userChannelMembers, err := s.Channel().GetAllChannelMembersForUser(user.Id, false, true)
    94  				if err != nil {
    95  					mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.String("search_engine", engineCopy.GetName()), mlog.Err(err))
    96  					return
    97  				}
    98  
    99  				userChannelsIds := []string{}
   100  				for channelId := range userChannelMembers {
   101  					userChannelsIds = append(userChannelsIds, channelId)
   102  				}
   103  
   104  				if err := engineCopy.IndexUser(user, userTeamsIds, userChannelsIds); err != nil {
   105  					mlog.Error("Encountered error indexing user", mlog.String("user_id", user.Id), mlog.String("search_engine", engineCopy.GetName()), mlog.Err(err))
   106  					return
   107  				}
   108  				mlog.Debug("Indexed user in search engine", mlog.String("search_engine", engineCopy.GetName()), mlog.String("user_id", user.Id))
   109  			})
   110  		}
   111  	}
   112  }
   113  
   114  // Runs an indexing function synchronously or asynchronously depending on the engine
   115  func runIndexFn(engine searchengine.SearchEngineInterface, indexFn func(searchengine.SearchEngineInterface)) {
   116  	if engine.IsIndexingSync() {
   117  		indexFn(engine)
   118  		if err := engine.RefreshIndexes(); err != nil {
   119  			mlog.Error("Encountered error refresh the indexes", mlog.Err(err))
   120  		}
   121  	} else {
   122  		go (func(engineCopy searchengine.SearchEngineInterface) {
   123  			indexFn(engineCopy)
   124  		})(engine)
   125  	}
   126  }