Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions internal/service/search_parser/search_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,22 @@ func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags [][]
func (sp *SearchParser) parseUserID(ctx context.Context, query *string, currentUserID string) (userID string) {
var (
exprUsername = `user:(\S+)`
exprMe = "user:me"
q = *query
)

re := regexp.MustCompile(exprUsername)
res := re.FindStringSubmatch(q)
if strings.Contains(q, exprMe) {
userID = currentUserID
q = strings.ReplaceAll(q, exprMe, "")
} else if len(res) > 1 {
if len(res) > 1 {
name := res[1]
user, has, err := sp.userCommon.GetUserBasicInfoByUserName(ctx, name)
if err == nil && has {
userID = user.ID
if name == "me" {
userID = currentUserID
q = re.ReplaceAllString(q, "")
} else if sp.userCommon != nil {
user, has, err := sp.userCommon.GetUserBasicInfoByUserName(ctx, name)
if err == nil && has {
userID = user.ID
q = re.ReplaceAllString(q, "")
}
}
}
*query = strings.TrimSpace(q)
Expand Down
53 changes: 53 additions & 0 deletions internal/service/search_parser/search_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package search_parser

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseUserID_UserMe(t *testing.T) {
sp := &SearchParser{}
ctx := context.Background()

t.Run("parse user:me with logged in user", func(t *testing.T) {
q := "user:me hello world"
userID := sp.parseUserID(ctx, &q, "user-123")
assert.Equal(t, "user-123", userID)
assert.Equal(t, "hello world", q)
})

t.Run("parse user:me when signed out", func(t *testing.T) {
q := "user:me hello world"
userID := sp.parseUserID(ctx, &q, "")
assert.Equal(t, "", userID)
assert.Equal(t, "hello world", q)
})

t.Run("username starting with me should not be parsed as user:me", func(t *testing.T) {
q := "user:melissa service"
// without userCommon returning a user, it shouldn't match user:me
userID := sp.parseUserID(ctx, &q, "user-123")
assert.NotEqual(t, "user-123", userID)
})
}