1// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2// See LICENSE.txt for license information. 3 4package bleveengine 5 6import ( 7 "strings" 8 9 "github.com/mattermost/mattermost-server/v6/model" 10 "github.com/mattermost/mattermost-server/v6/services/searchengine" 11) 12 13type BLVChannel struct { 14 Id string 15 TeamId []string 16 NameSuggest []string 17} 18 19type BLVUser struct { 20 Id string 21 SuggestionsWithFullname []string 22 SuggestionsWithoutFullname []string 23 TeamsIds []string 24 ChannelsIds []string 25} 26 27type 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 39type 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 49func 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 60func 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 93func 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 107func 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 115func 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 128func splitFilenameWords(name string) string { 129 result := name 130 result = strings.ReplaceAll(result, "-", " ") 131 result = strings.ReplaceAll(result, ".", " ") 132 return result 133} 134 135func BLVFileFromFileInfo(fileInfo *model.FileInfo, channelId string) *BLVFile { 136 return &BLVFile{ 137 Id: fileInfo.Id, 138 ChannelId: channelId, 139 CreatorId: fileInfo.CreatorId, 140 CreateAt: fileInfo.CreateAt, 141 Content: fileInfo.Content, 142 Extension: fileInfo.Extension, 143 Name: fileInfo.Name + " " + splitFilenameWords(fileInfo.Name), 144 } 145} 146 147func BLVFileFromFileForIndexing(file *model.FileForIndexing) *BLVFile { 148 return &BLVFile{ 149 Id: file.Id, 150 ChannelId: file.ChannelId, 151 CreatorId: file.CreatorId, 152 CreateAt: file.CreateAt, 153 Content: file.Content, 154 Extension: file.Extension, 155 Name: file.Name + " " + splitFilenameWords(file.Name), 156 } 157} 158