From 49e0473753c2e4e2e02e30c17a7793657b71363f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Thu, 27 Sep 2018 16:15:41 +0200 Subject: MM-11567: Autocomplete search in: for DMs and GMs (#9430) * MM-11567: Autocomplete search in: for DMs and GMs * Adding unit tests * Allowing to search Direct Messages in the autocompletion * Fix it in TE --- app/post.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'app/post.go') diff --git a/app/post.go b/app/post.go index fc941a68f..da6f7ac2b 100644 --- a/app/post.go +++ b/app/post.go @@ -616,6 +616,43 @@ func (a *App) DeletePostFiles(post *model.Post) { } } +func (a *App) parseAndFetchChannelIdByNameFromInFilter(channelName, userId, teamId string, includeDeleted bool) (*model.Channel, error) { + if strings.HasPrefix(channelName, "@") && strings.Contains(channelName, ",") { + var userIds []string + users, err := a.GetUsersByUsernames(strings.Split(channelName[1:], ","), false) + if err != nil { + return nil, err + } + for _, user := range users { + userIds = append(userIds, user.Id) + } + + channel, err := a.GetGroupChannel(userIds) + if err != nil { + return nil, err + } + return channel, nil + } + + if strings.HasPrefix(channelName, "@") && !strings.Contains(channelName, ",") { + user, err := a.GetUserByUsername(channelName[1:]) + if err != nil { + return nil, err + } + channel, err := a.GetDirectChannel(userId, user.Id) + if err != nil { + return nil, err + } + return channel, nil + } + + channel, err := a.GetChannelByName(channelName, teamId, includeDeleted) + if err != nil { + return nil, err + } + return channel, nil +} + func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bool, includeDeletedChannels bool, timeZoneOffset int, page, perPage int) (*model.PostSearchResults, *model.AppError) { paramsList := model.ParseSearchParams(terms, timeZoneOffset) includeDeleted := includeDeletedChannels && *a.Config().TeamSettings.ExperimentalViewArchivedChannels @@ -630,11 +667,12 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr if params.Terms != "*" { // Convert channel names to channel IDs for idx, channelName := range params.InChannels { - if channel, err := a.GetChannelByName(channelName, teamId, includeDeleted); err != nil { + channel, err := a.parseAndFetchChannelIdByNameFromInFilter(channelName, userId, teamId, includeDeletedChannels) + if err != nil { mlog.Error(fmt.Sprint(err)) - } else { - params.InChannels[idx] = channel.Id + continue } + params.InChannels[idx] = channel.Id } // Convert usernames to user IDs @@ -695,6 +733,16 @@ func (a *App) SearchPostsInTeam(terms string, userId string, teamId string, isOr params.OrTerms = isOrSearch // don't allow users to search for everything if params.Terms != "*" { + for idx, channelName := range params.InChannels { + if strings.HasPrefix(channelName, "@") { + channel, err := a.parseAndFetchChannelIdByNameFromInFilter(channelName, userId, teamId, includeDeletedChannels) + if err != nil { + mlog.Error(fmt.Sprint(err)) + continue + } + params.InChannels[idx] = channel.Name + } + } channels = append(channels, a.Srv.Store.Post().Search(teamId, userId, params)) } } -- cgit v1.2.3-1-g7c22