From d79b4fc11c02cbd193a6f948bb2d358c29a8fdaf Mon Sep 17 00:00:00 2001 From: granbellllc-png Date: Wed, 11 Nov 2026 14:41:13 +0400 Subject: [PATCH] fix(search): prevent username prefix 'me' from matching user:me (#1665) - In parseUserID, extract username from user:(\S+) before checking for 'me' - Only treat query as current user search if exact username token is 'me' - Add unit tests for parseUserID with user:me and usernames prefixed with me Fixes #1665 --- .../service/search_parser/search_parser.go | 17 +++--- .../search_parser/search_parser_test.go | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 internal/service/search_parser/search_parser_test.go diff --git a/internal/service/search_parser/search_parser.go b/internal/service/search_parser/search_parser.go index 3e6182e15..6ac47ce04 100644 --- a/internal/service/search_parser/search_parser.go +++ b/internal/service/search_parser/search_parser.go @@ -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) diff --git a/internal/service/search_parser/search_parser_test.go b/internal/service/search_parser/search_parser_test.go new file mode 100644 index 000000000..899c7e418 --- /dev/null +++ b/internal/service/search_parser/search_parser_test.go @@ -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) + }) +}