mattermost/server/public/model/command_args.go

56 lines
1.7 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"github.com/mattermost/mattermost/server/public/shared/i18n"
)
type CommandArgs struct {
UserId string `json:"user_id"`
ChannelId string `json:"channel_id"`
TeamId string `json:"team_id"`
RootId string `json:"root_id"`
ParentId string `json:"parent_id"`
TriggerId string `json:"trigger_id,omitempty"`
Command string `json:"command"`
SiteURL string `json:"-"`
T i18n.TranslateFunc `json:"-"`
UserMentions UserMentionMap `json:"-"`
ChannelMentions ChannelMentionMap `json:"-"`
}
func (o *CommandArgs) Auditable() map[string]any {
return map[string]any{
Audit logging -- convert audit logs to use the new schema (#20526) * Audit logging - new schema added, old schema removed. * fix linter error by running goimports * Address review comments * Address review comments * Example usage of new audit logging API for the updateUserAuth call * fixed unit test on auditing updating user record * Changed the `TestUpdateConfigDiffInAuditRecord` testcase---it failed, because this PR changes how the `meta` field is serialized into the audit log records. * fix linter error * use string constants for record keys * new audit api calls for api4/bot * `Auditable` interface implementations for model classes * New audit calls for channel api * New audit calls for channel_local * renamed receivers for required style reasons * New audit calls for api4/command * renamed receiver * New audit calls for api4/command_local * renamed receiver * fix unit test to reflect changes in the Auditable implementation of the user class * new audit calls for compliance * new audit calls for configs * remove auditRec.addMeta from updateConfig and patchConfig * new audit calls for config_local * new audit calls * new audit calls for ldap, license apis * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * new audit calls * fix linter error * fixed linter error * fixed "user update" test * Don't include all of config when audit logging config changes. Also fix unit test on TestUpdateConfigDiffInAuditRecord * address review comments * Added Auditable() method for UserPatch * Fix duplicative method declaration from merge * Fix styling and API changes issues introduced with merge * Fix broken test Co-authored-by: Daniel Schalla <daniel@schalla.me>
2022-07-14 07:52:46 -04:00
"user_id": o.UserId,
"channel_id": o.ChannelId,
"team_id": o.TeamId,
"root_id": o.RootId,
"parent_id": o.ParentId,
"trigger_id": o.TriggerId,
"command": o.Command,
"site_url": o.SiteURL,
}
}
MM-21987 Resolve mentions in slash commands (#13762) * 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>
2020-03-11 06:50:12 -04:00
// AddUserMention adds or overrides an entry in UserMentions with name username
// and identifier userId
func (o *CommandArgs) AddUserMention(username, userId string) {
if o.UserMentions == nil {
o.UserMentions = make(UserMentionMap)
}
o.UserMentions[username] = userId
}
// AddChannelMention adds or overrides an entry in ChannelMentions with name
// channelName and identifier channelId
func (o *CommandArgs) AddChannelMention(channelName, channelId string) {
if o.ChannelMentions == nil {
o.ChannelMentions = make(ChannelMentionMap)
}
o.ChannelMentions[channelName] = channelId
}