diff --git a/server/services/store/mattermostauthlayer/mattermostauthlayer.go b/server/services/store/mattermostauthlayer/mattermostauthlayer.go index cf3006e8b..b038a0aad 100644 --- a/server/services/store/mattermostauthlayer/mattermostauthlayer.go +++ b/server/services/store/mattermostauthlayer/mattermostauthlayer.go @@ -741,6 +741,51 @@ func (s *MattermostAuthLayer) SearchBoardsForUser(term, userID string, includePu return s.boardsFromRows(rows) } +// searchBoardsForUserInTeam returns all boards that match with the +// term that are either private and which the user is a member of, or +// they're open, regardless of the user membership. +// Search is case-insensitive. +func (s *MattermostAuthLayer) SearchBoardsForUserInTeam(teamID, term, userID string) ([]*model.Board, error) { + query := s.getQueryBuilder(). + Select(boardFields("b.")...). + Distinct(). + From(s.tablePrefix + "boards as b"). + LeftJoin(s.tablePrefix + "board_members as bm on b.id=bm.board_id"). + LeftJoin("ChannelMembers as cm on cm.channelId=b.channel_id"). + Where(sq.Eq{"b.is_template": false}). + Where(sq.Eq{"b.team_id": teamID}). + Where(sq.Or{ + sq.Eq{"b.type": model.BoardTypeOpen}, + sq.Eq{"bm.user_id": userID}, + sq.Eq{"cm.userId": userID}, + }) + + if term != "" { + // break search query into space separated words + // and search for all words. + // This should later be upgraded to industrial-strength + // word tokenizer, that uses much more than space + // to break words. + + conditions := sq.And{} + + for _, word := range strings.Split(strings.TrimSpace(term), " ") { + conditions = append(conditions, sq.Like{"lower(b.title)": "%" + strings.ToLower(word) + "%"}) + } + + query = query.Where(conditions) + } + + rows, err := query.Query() + if err != nil { + s.logger.Error(`searchBoardsForUser ERROR`, mlog.Err(err)) + return nil, err + } + defer s.CloseRows(rows) + + return s.boardsFromRows(rows) +} + func (s *MattermostAuthLayer) boardsFromRows(rows *sql.Rows) ([]*model.Board, error) { boards := []*model.Board{}