[mention] fix relate issues to input segments (#3766)

This commit is contained in:
frank 2023-07-25 16:47:24 +08:00 committed by GitHub
parent c757f6c6b9
commit 14483dabda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 22 deletions

View File

@ -1 +1 @@
0.162.9
0.162.10

View File

@ -318,24 +318,6 @@ func (m *MentionManager) OnChangeText(chatID, fullText string) (*ChatMentionCont
return m.calculateSuggestions(chatID, fullText)
}
func (m *MentionManager) recheckAtIdxs(chatID string, fullText string, publicKey string) (*ChatMentionContext, error) {
user, err := m.mentionableUserGetter.getMentionableUser(chatID, publicKey)
if err != nil {
return nil, err
}
ctx := m.getChatMentionContext(chatID)
state := ctx.MentionState
mentionableUsers := map[string]*MentionableUser{user.ID: user}
newAtIdxs := checkIdxForMentions(fullText, state.AtIdxs, mentionableUsers)
inputSegments, success := calculateInput(fullText, newAtIdxs)
if !success {
m.logger.Warn("recheckAtIdxs: calculateInput failed", zap.String("chatID", chatID), zap.String("fullText", fullText), zap.Any("state", ctx.MentionState))
}
ctx.InputSegments = inputSegments
state.AtIdxs = newAtIdxs
return ctx, nil
}
func (m *MentionManager) calculateSuggestions(chatID, fullText string) (*ChatMentionContext, error) {
ctx := m.getChatMentionContext(chatID)
@ -419,11 +401,10 @@ func (m *MentionManager) SelectMention(chatID, text, primaryName, publicKey stri
ctx.NewText = string(tr[:atSignIdx+1]) + primaryName + space + string(tr[mentionEnd:])
ctx, err := m.recheckAtIdxs(chatID, ctx.NewText, publicKey)
ctx, err := m.OnChangeText(chatID, ctx.NewText)
if err != nil {
return nil, err
}
ctx.PreviousText = ctx.NewText
m.clearSuggestions(chatID)
return ctx, nil
}
@ -735,6 +716,9 @@ func MatchMention(text string, users map[string]*MentionableUser, mentionKeyIdx
func matchMention(text string, users map[string]*MentionableUser, mentionKeyIdx int, nextWordIdx int, words []string) *MentionableUser {
tr := []rune(text)
if nextWordIdx >= len(tr) {
return nil
}
if word := wordRegex.FindString(string(tr[nextWordIdx:])); word != "" {
newWords := append(words, word)
@ -888,7 +872,7 @@ func calculateAtIndexEntries(state *MentionState) ([]*AtIndexEntry, error) {
entry.From += diff
entry.To += diff
}
if entry.From < state.Start && entry.To >= state.Start { // impacted after user edit so need to be rechecked
if entry.From < state.Start && entry.To+1 >= state.Start { // impacted after user edit so need to be rechecked
entry.Checked = false
}
keptAtIndexEntries = append(keptAtIndexEntries, entry)

View File

@ -979,6 +979,34 @@ func TestInputSegments(t *testing.T) {
require.Equal(t, " ", ctx.InputSegments[1].Value)
require.Equal(t, Text, ctx.InputSegments[2].Type)
require.Equal(t, "@ ", ctx.InputSegments[2].Value)
_, err = mentionManager.OnChangeText(chatID, " @ @User Number Three ")
require.NoError(t, err)
_, err = mentionManager.OnChangeText(chatID, "@U @ @User Number Three ")
require.NoError(t, err)
ctx, err = mentionManager.SelectMention(chatID, "@U @ @User Number Three ", "User Number Three", "0xpk3")
require.NoError(t, err)
require.Equal(t, 2, mentionTypeNum(ctx.InputSegments))
ctx, _ = mentionManager.OnChangeText(chatID, "@User Number Threea")
require.Equal(t, 0, mentionTypeNum(ctx.InputSegments))
ctx, _ = mentionManager.OnChangeText(chatID, "@User Number Threea\n@u2\nabc@u3 asa")
require.Equal(t, 2, mentionTypeNum(ctx.InputSegments))
ctx, _ = mentionManager.OnChangeText(chatID, "@User Number Thre\n@u2\nabc@u3 asa")
require.Equal(t, 2, mentionTypeNum(ctx.InputSegments))
require.Equal(t, "@u2", ctx.InputSegments[1].Value)
require.Equal(t, "@u3", ctx.InputSegments[3].Value)
}
func mentionTypeNum(inputSegments []InputSegment) int {
var num int
for _, s := range inputSegments {
if s.Type == Mention {
num++
}
}
return num
}
func setupMentionSuggestionTest(t *testing.T, mentionableUserMapInput map[string]*MentionableUser) (map[string]*MentionableUser, string, *MentionManager) {