github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/services/searchengine/bleveengine/common.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package bleveengine
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/mattermost/mattermost-server/v5/model"
    10  	"github.com/mattermost/mattermost-server/v5/services/searchengine"
    11  )
    12  
    13  type BLVChannel struct {
    14  	Id          string
    15  	TeamId      []string
    16  	NameSuggest []string
    17  }
    18  
    19  type BLVUser struct {
    20  	Id                         string
    21  	SuggestionsWithFullname    []string
    22  	SuggestionsWithoutFullname []string
    23  	TeamsIds                   []string
    24  	ChannelsIds                []string
    25  }
    26  
    27  type BLVPost struct {
    28  	Id          string
    29  	TeamId      string
    30  	ChannelId   string
    31  	UserId      string
    32  	CreateAt    int64
    33  	Message     string
    34  	Type        string
    35  	Hashtags    []string
    36  	Attachments string
    37  }
    38  
    39  type BLVFile struct {
    40  	Id        string
    41  	CreatorId string
    42  	ChannelId string
    43  	CreateAt  int64
    44  	Name      string
    45  	Content   string
    46  	Extension string
    47  }
    48  
    49  func BLVChannelFromChannel(channel *model.Channel) *BLVChannel {
    50  	displayNameInputs := searchengine.GetSuggestionInputsSplitBy(channel.DisplayName, " ")
    51  	nameInputs := searchengine.GetSuggestionInputsSplitByMultiple(channel.Name, []string{"-", "_"})
    52  
    53  	return &BLVChannel{
    54  		Id:          channel.Id,
    55  		TeamId:      []string{channel.TeamId},
    56  		NameSuggest: append(displayNameInputs, nameInputs...),
    57  	}
    58  }
    59  
    60  func BLVUserFromUserAndTeams(user *model.User, teamsIds, channelsIds []string) *BLVUser {
    61  	usernameSuggestions := searchengine.GetSuggestionInputsSplitByMultiple(user.Username, []string{".", "-", "_"})
    62  
    63  	fullnameStrings := []string{}
    64  	if user.FirstName != "" {
    65  		fullnameStrings = append(fullnameStrings, user.FirstName)
    66  	}
    67  	if user.LastName != "" {
    68  		fullnameStrings = append(fullnameStrings, user.LastName)
    69  	}
    70  
    71  	fullnameSuggestions := []string{}
    72  	if len(fullnameStrings) > 0 {
    73  		fullname := strings.Join(fullnameStrings, " ")
    74  		fullnameSuggestions = searchengine.GetSuggestionInputsSplitBy(fullname, " ")
    75  	}
    76  
    77  	nicknameSuggesitons := []string{}
    78  	if user.Nickname != "" {
    79  		nicknameSuggesitons = searchengine.GetSuggestionInputsSplitBy(user.Nickname, " ")
    80  	}
    81  
    82  	usernameAndNicknameSuggestions := append(usernameSuggestions, nicknameSuggesitons...)
    83  
    84  	return &BLVUser{
    85  		Id:                         user.Id,
    86  		SuggestionsWithFullname:    append(usernameAndNicknameSuggestions, fullnameSuggestions...),
    87  		SuggestionsWithoutFullname: usernameAndNicknameSuggestions,
    88  		TeamsIds:                   teamsIds,
    89  		ChannelsIds:                channelsIds,
    90  	}
    91  }
    92  
    93  func BLVUserFromUserForIndexing(userForIndexing *model.UserForIndexing) *BLVUser {
    94  	user := &model.User{
    95  		Id:        userForIndexing.Id,
    96  		Username:  userForIndexing.Username,
    97  		Nickname:  userForIndexing.Nickname,
    98  		FirstName: userForIndexing.FirstName,
    99  		LastName:  userForIndexing.LastName,
   100  		CreateAt:  userForIndexing.CreateAt,
   101  		DeleteAt:  userForIndexing.DeleteAt,
   102  	}
   103  
   104  	return BLVUserFromUserAndTeams(user, userForIndexing.TeamsIds, userForIndexing.ChannelsIds)
   105  }
   106  
   107  func BLVPostFromPost(post *model.Post, teamId string) *BLVPost {
   108  	p := &model.PostForIndexing{
   109  		TeamId: teamId,
   110  	}
   111  	post.ShallowCopy(&p.Post)
   112  	return BLVPostFromPostForIndexing(p)
   113  }
   114  
   115  func BLVPostFromPostForIndexing(post *model.PostForIndexing) *BLVPost {
   116  	return &BLVPost{
   117  		Id:        post.Id,
   118  		TeamId:    post.TeamId,
   119  		ChannelId: post.ChannelId,
   120  		UserId:    post.UserId,
   121  		CreateAt:  post.CreateAt,
   122  		Message:   post.Message,
   123  		Type:      post.Type,
   124  		Hashtags:  strings.Fields(post.Hashtags),
   125  	}
   126  }
   127  
   128  func BLVFileFromFileInfo(fileInfo *model.FileInfo, channelId string) *BLVFile {
   129  	return &BLVFile{
   130  		Id:        fileInfo.Id,
   131  		ChannelId: channelId,
   132  		CreatorId: fileInfo.CreatorId,
   133  		CreateAt:  fileInfo.CreateAt,
   134  		Content:   fileInfo.Content,
   135  		Extension: fileInfo.Extension,
   136  		Name:      fileInfo.Name,
   137  	}
   138  }
   139  
   140  func BLVFileFromFileForIndexing(file *model.FileForIndexing) *BLVFile {
   141  	return &BLVFile{
   142  		Id:        file.Id,
   143  		ChannelId: file.ChannelId,
   144  		CreatorId: file.CreatorId,
   145  		CreateAt:  file.CreateAt,
   146  		Content:   file.Content,
   147  		Extension: file.Extension,
   148  		Name:      file.Name,
   149  	}
   150  }