optimize mention (#3479)

* optimize mention

* bump version
This commit is contained in:
frank 2023-05-15 15:21:41 +08:00 committed by GitHub
parent c020222f1b
commit 39f486a47a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 48 deletions

View File

@ -1 +1 @@
0.151.3
0.151.4

View File

@ -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)
@ -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
@ -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
@ -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) {
@ -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 {

View File

@ -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)
}