Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize mention #3479

Merged
merged 2 commits into from
May 15, 2023
Merged
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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.151.3
0.151.4
40 changes: 5 additions & 35 deletions protocol/messenger_mention.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (m *MentionManager) addMentionableUser(mentionableUsers map[string]*Mention
return nil
}

func (m *MentionManager) CheckMentions(chatID, text string) (string, error) {
func (m *MentionManager) ReplaceWithPublicKey(chatID, text string) (string, error) {
chat, _ := m.allChats.Load(chatID)
if chat == nil {
return "", fmt.Errorf("chat not found when check mentions, chatID: %s", chatID)
Expand Down Expand Up @@ -323,7 +323,7 @@ func (m *MentionManager) OnChangeText(chatID, text string) (*ChatMentionContext,
return m.CalculateSuggestions(chatID, text)
}

func (m *MentionManager) RecheckAtIdxs(chatID string, text string, publicKey string) (*ChatMentionContext, error) {
func (m *MentionManager) recheckAtIdxs(chatID string, text string, publicKey string) (*ChatMentionContext, error) {
user, err := m.getMentionableUser(chatID, publicKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -394,7 +394,7 @@ func (m *MentionManager) calculateSuggestions(chatID string, text string, mentio
ctx.MentionSuggestions = suggestions
}

func (m *MentionManager) NewInputTextWithMention(chatID, text, primaryName string) *ChatMentionContext {
func (m *MentionManager) SelectMention(chatID, text, primaryName, publicKey string) (*ChatMentionContext, error) {
ctx := m.getChatMentionContext(chatID)
state := ctx.MentionState

Expand All @@ -413,7 +413,8 @@ func (m *MentionManager) NewInputTextWithMention(chatID, text, primaryName strin
}

ctx.NewText = string(tr[:atSignIdx+1]) + primaryName + space + string(tr[mentionEnd:])
return ctx

return m.recheckAtIdxs(chatID, ctx.NewText, publicKey)
}

func (m *MentionManager) clearSuggestions(chatID string) {
Expand All @@ -429,37 +430,6 @@ func (m *MentionManager) ClearMentions(chatID string) {
m.clearSuggestions(chatID)
}

func (m *MentionManager) HandleSelectionChange(chatID, text string, start int, end int) *ChatMentionContext {
ctx := m.getChatMentionContext(chatID)
m.handleSelectionChange(chatID, text, start, end, ctx.MentionSuggestions)
return ctx
}

func (m *MentionManager) handleSelectionChange(chatID, text string, start int, end int, mentionableUsers map[string]*MentionableUser) {
ctx := m.getChatMentionContext(chatID)
state := ctx.MentionState
if state != nil && len(state.AtIdxs) > 0 {
var atIdx *AtIndexEntry
for _, idx := range state.AtIdxs {
if start >= idx.From && end-1 <= idx.To {
atIdx = idx
break
}
}

if atIdx != nil {
newText := ""
state.Start = end
state.End = end
state.NewText = &newText
m.calculateSuggestions(chatID, text, mentionableUsers)
} else {
m.clearSuggestions(chatID)
}
}
ctx.PreviousText = text
}

func (m *MentionManager) ToInputField(chatID, text string) (*ChatMentionContext, error) {
mentionableUsers, err := m.getMentionableUsers(chatID)
if err != nil {
Expand Down
35 changes: 21 additions & 14 deletions services/ext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,30 +1302,37 @@ func (api *PublicAPI) Messenger() *protocol.Messenger {
return api.service.messenger
}

func (api *PublicAPI) ChatMentionCheckMentions(chatID, text string) (string, error) {
return api.service.messenger.GetMentionsManager().CheckMentions(chatID, text)
}

// ChatMentionReplaceWithPublicKey checks if the text contains mentions and replace mention with user public key.
// e.g. abc @alice -> abc 0x123
func (api *PublicAPI) ChatMentionReplaceWithPublicKey(chatID, text string) (string, error) {
return api.service.messenger.GetMentionsManager().ReplaceWithPublicKey(chatID, text)
}

// ChatMentionOnChangeText
// chatID: chat id
// text: the full text user input in the chat input field
// as performance consideration, we don't need to call this function each time after user input a character,
// say user input "abc", we don't need to call this function 3 times, instead,
// we can call this function 2 times as following:
// 1. user input "a", call this function with text "a"
// 2. user input "c", call this function with text "abc"
// whatever, we should ensure ChatMentionOnChangeText know(invoked) the latest full text.
// ChatMentionOnChangeText will maintain state of fulltext and diff between previous/latest full text internally.
func (api *PublicAPI) ChatMentionOnChangeText(chatID, text string) (*protocol.ChatMentionContext, error) {
return api.service.messenger.GetMentionsManager().OnChangeText(chatID, text)
}

func (api *PublicAPI) ChatMentionRecheckAtIdxs(chatID string, text string, publicKey string) (*protocol.ChatMentionContext, error) {
return api.service.messenger.GetMentionsManager().RecheckAtIdxs(chatID, text, publicKey)
}

func (api *PublicAPI) ChatMentionNewInputTextWithMention(chatID, text, primaryName string) *protocol.ChatMentionContext {
return api.service.messenger.GetMentionsManager().NewInputTextWithMention(chatID, text, primaryName)
// ChatMentionSelectMention select mention from mention suggestion list
func (api *PublicAPI) ChatMentionSelectMention(chatID, text, primaryName, publicKey string) (*protocol.ChatMentionContext, error) {
return api.service.messenger.GetMentionsManager().SelectMention(chatID, text, primaryName, publicKey)
}

func (api *PublicAPI) ChatMentionClearMentions(chatID string) {
api.service.messenger.GetMentionsManager().ClearMentions(chatID)
}

func (api *PublicAPI) ChatMentionHandleSelectionChange(chatID, text string, start int, end int) *protocol.ChatMentionContext {
return api.service.messenger.GetMentionsManager().HandleSelectionChange(chatID, text, start, end)
}

// ChatMentionToInputField checks if the text contains mentions and replace mention with readable username.
// generally, this function is invoked before user editing a sent message.
func (api *PublicAPI) ChatMentionToInputField(chatID, text string) (*protocol.ChatMentionContext, error) {
return api.service.messenger.GetMentionsManager().ToInputField(chatID, text)
}
Expand Down