mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Create infrastructure to manage mentions
Two new files have been added (along with their tests); namely:
- model/at_mentions.go: utilities to parse and manage mentions; for the moment,
it just contains a regex and a couple of functions to parse possible mentions
and to post-process them, but it can be extended in the future.
- model/mention_map.go: it contains two new types (UserMentionMap and
ChannelMentionMap) that both have FromURLValues and ToURLValues. These types
can be used when adding the mentions to the payload of the plugin slash
commands.
* Extend custom commands payload with mentions
Two couples of new fields are added to the payload; namely:
- user_mentions and user_mentions_ids: two aligned arrays of the same length
containing all the different @-mentions found in the command: the i-th element
of user_mentions_ids is the user identifier of the i-th element of
user_mentions.
- channel_mentions and channel_mentions_ids: two aligned arrays of the same
length containing all the different ~-mentions found in the command: the i-th
element of channel_mentions_ids is the channel identifier of the i-th element
of channel_mentions.
* Fix shadowing of variables and redundant return
* Fix shadowing of variable
* Address review comments (HT @lieut-data)
- Improvements in mentionsToTeamMembers and mentionsToPublicChannels:
- Scope implementation details inside the functions.
- Improve goroutines synchronization by using a sync.WaitGroup.
- Retry lookup of username only if the returned error is http.StatusCode,
so we can return early if the error is more severe.
- Invert check in PossibleAtMentions to improve readability.
- Make user and channel mention keys private to the module.
- Allow the specification of an empty map of mentions in
(Channel|User)MentionsFromURLValues when both mentions keys are absent.
- Replace custom functions in tests with require.Equal on maps.
* Test functions to parse mentions from messages
* Extend plugin commands payload with mentions
* Add functions to CommandArgs to add mentions
The functions make sure that the maps are initialized before adding any value.
* Address review comments (HT @lieut-data)
- Adds a mlog.Warn to avoid burying the error when the user is not found.
- Improve readability in loop populating the mention map by moving the
initialization of the map closer to the loop and by iterating over the channel
itself, not over its length.
* File was not gofmt-ed with -s
* Close channel when all goroutines are finished
* Again, all code should be checked with gofmt -s
* Refactor code out of a goroutine
This change helps improve the readability of the code and does not affect its
overall performance. Less complexity is always better.
* Close channel and iterate over its range
Adapt mentionsToPublicChannels to have the same structure in the management
of the mentions channel as in mentionsToTeamMembers.
* Adapt mentionsToTeamMembers to new App
Commit 17523fa changed the App structure, making the *Server field
private, which is now accessed through the Srv() function.
Co-authored-by: mattermod <mattermod@users.noreply.github.com>
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
type UserMentionMap map[string]string
|
|
type ChannelMentionMap map[string]string
|
|
|
|
const (
|
|
userMentionsKey = "user_mentions"
|
|
userMentionsIdsKey = "user_mentions_ids"
|
|
channelMentionsKey = "channel_mentions"
|
|
channelMentionsIdsKey = "channel_mentions_ids"
|
|
)
|
|
|
|
func UserMentionMapFromURLValues(values url.Values) (UserMentionMap, error) {
|
|
return mentionsFromURLValues(values, userMentionsKey, userMentionsIdsKey)
|
|
}
|
|
|
|
func (m UserMentionMap) ToURLValues() url.Values {
|
|
return mentionsToURLValues(m, userMentionsKey, userMentionsIdsKey)
|
|
}
|
|
|
|
func ChannelMentionMapFromURLValues(values url.Values) (ChannelMentionMap, error) {
|
|
return mentionsFromURLValues(values, channelMentionsKey, channelMentionsIdsKey)
|
|
}
|
|
|
|
func (m ChannelMentionMap) ToURLValues() url.Values {
|
|
return mentionsToURLValues(m, channelMentionsKey, channelMentionsIdsKey)
|
|
}
|
|
|
|
func mentionsFromURLValues(values url.Values, mentionKey, idKey string) (map[string]string, error) {
|
|
mentions, mentionsOk := values[mentionKey]
|
|
ids, idsOk := values[idKey]
|
|
|
|
if !mentionsOk && !idsOk {
|
|
return map[string]string{}, nil
|
|
}
|
|
|
|
if !mentionsOk {
|
|
return nil, fmt.Errorf("%s key not found", mentionKey)
|
|
}
|
|
|
|
if !idsOk {
|
|
return nil, fmt.Errorf("%s key not found", idKey)
|
|
}
|
|
|
|
if len(mentions) != len(ids) {
|
|
return nil, fmt.Errorf("keys %s and %s have different length", mentionKey, idKey)
|
|
}
|
|
|
|
mentionsMap := make(map[string]string)
|
|
for i, mention := range mentions {
|
|
id := ids[i]
|
|
|
|
if oldId, ok := mentionsMap[mention]; ok && oldId != id {
|
|
return nil, fmt.Errorf("key %s has two different values: %s and %s", mention, oldId, id)
|
|
}
|
|
|
|
mentionsMap[mention] = id
|
|
}
|
|
|
|
return mentionsMap, nil
|
|
}
|
|
|
|
func mentionsToURLValues(mentions map[string]string, mentionKey, idKey string) url.Values {
|
|
values := url.Values{}
|
|
|
|
for mention, id := range mentions {
|
|
values.Add(mentionKey, mention)
|
|
values.Add(idKey, id)
|
|
}
|
|
|
|
return values
|
|
}
|