* Create the system console setting and send to webapp

* MI-1145: Add custom status APIs

* MI-1145 Add slash commands to set and clear status

* Add validation for custom status API

* Trim custom status message

* Code refactoring

- Run gofmt

- Rename constants

* Remove sendUserUpdated webhook event

* Fix recent custom status length

* Update error conditions

* Disable /status slash command when config setting is off

* MI-1155: Create the feature flag for custom status APIs and slash commands

* Move recent custom statuses to user preferences (#7)

* Move recent custom statuses to user preferences

* Code refactoring and feedback changes

* Update slash command text and emoji regex

* Make the custom status feature flag off by default

* Update SetCustomStatus, handle recents not set better

* Update status codes

* Update slash command handling

* Add telementry settings

* Fix i18n order

* Revert "Fix i18n order"

This reverts commit 499f7eaca8180336f5bcca360cc0133365899e08.

* Update i18n strings
Этот коммит содержится в:
Chetanya Kandhari
2021-02-18 16:38:01 +05:30
коммит произвёл GitHub
родитель 22308ea21c
Коммит 7585e16d84
15 изменённых файлов: 509 добавлений и 0 удалений

Просмотреть файл

@@ -847,12 +847,14 @@ type AppIface interface {
ReloadConfig() error
RemoveAllDeactivatedMembersFromChannel(channel *model.Channel) *model.AppError
RemoveConfigListener(id string)
RemoveCustomStatus(userID string) *model.AppError
RemoveDirectory(path string) *model.AppError
RemoveFile(path string) *model.AppError
RemoveLdapPrivateCertificate() *model.AppError
RemoveLdapPublicCertificate() *model.AppError
RemovePlugin(id string) *model.AppError
RemovePluginFromData(data model.PluginEventData)
RemoveRecentCustomStatus(userID string, status *model.CustomStatus) *model.AppError
RemoveSamlIdpCertificate() *model.AppError
RemoveSamlPrivateCertificate() *model.AppError
RemoveSamlPublicCertificate() *model.AppError
@@ -929,6 +931,7 @@ type AppIface interface {
SetActiveChannel(userID string, channelId string) *model.AppError
SetAutoResponderStatus(user *model.User, oldNotifyProps model.StringMap)
SetContext(c context.Context)
SetCustomStatus(userID string, cs *model.CustomStatus) *model.AppError
SetDefaultProfileImage(user *model.User) *model.AppError
SetIpAddress(s string)
SetPath(s string)

Просмотреть файл

@@ -22,6 +22,10 @@ import (
"github.com/mattermost/mattermost-server/v5/utils"
)
const (
CmdCustomStatusTrigger = "status"
)
type CommandProvider interface {
GetTrigger() string
GetCommand(a *App, T goi18n.TranslateFunc) *model.Command
@@ -80,6 +84,11 @@ func (a *App) ListAutocompleteCommands(teamID string, T goi18n.TranslateFunc) ([
commands := make([]*model.Command, 0, 32)
seen := make(map[string]bool)
// Disable custom status slash command if the feature or the setting is off
if !a.Config().FeatureFlags.CustomUserStatuses || !*a.Config().TeamSettings.EnableCustomUserStatuses {
seen[CmdCustomStatusTrigger] = true
}
for _, cmd := range a.PluginCommandsForTeam(teamID) {
if cmd.AutoComplete && !seen[cmd.Trigger] {
seen[cmd.Trigger] = true

Просмотреть файл

@@ -11880,6 +11880,28 @@ func (a *OpenTracingAppLayer) RemoveConfigListener(id string) {
a.app.RemoveConfigListener(id)
}
func (a *OpenTracingAppLayer) RemoveCustomStatus(userID string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveCustomStatus")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.RemoveCustomStatus(userID)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) RemoveDirectory(path string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveDirectory")
@@ -12005,6 +12027,28 @@ func (a *OpenTracingAppLayer) RemovePluginFromData(data model.PluginEventData) {
a.app.RemovePluginFromData(data)
}
func (a *OpenTracingAppLayer) RemoveRecentCustomStatus(userID string, status *model.CustomStatus) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveRecentCustomStatus")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.RemoveRecentCustomStatus(userID, status)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) RemoveSamlIdpCertificate() *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.RemoveSamlIdpCertificate")
@@ -13746,6 +13790,28 @@ func (a *OpenTracingAppLayer) SetBotIconImageFromMultiPartFile(botUserId string,
return resultVar0
}
func (a *OpenTracingAppLayer) SetCustomStatus(userID string, cs *model.CustomStatus) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetCustomStatus")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.SetCustomStatus(userID, cs)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) SetDefaultProfileImage(user *model.User) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SetDefaultProfileImage")

85
app/slashcommands/command_custom_status.go Обычный файл
Просмотреть файл

@@ -0,0 +1,85 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package slashcommands
import (
"strings"
goi18n "github.com/mattermost/go-i18n/i18n"
"github.com/mattermost/mattermost-server/v5/app"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
)
type CustomStatusProvider struct {
}
const (
CmdCustomStatus = app.CmdCustomStatusTrigger
CmdCustomStatusClear = "clear"
DefaultCustomStatusEmoji = "speech_balloon"
)
func init() {
app.RegisterCommandProvider(&CustomStatusProvider{})
}
func (*CustomStatusProvider) GetTrigger() string {
return CmdCustomStatus
}
func (*CustomStatusProvider) GetCommand(a *app.App, T goi18n.TranslateFunc) *model.Command {
return &model.Command{
Trigger: CmdCustomStatus,
AutoComplete: true,
AutoCompleteDesc: T("api.command_custom_status.desc"),
AutoCompleteHint: T("api.command_custom_status.hint"),
DisplayName: T("api.command_custom_status.name"),
}
}
func (*CustomStatusProvider) DoCommand(a *app.App, args *model.CommandArgs, message string) *model.CommandResponse {
if !a.Config().FeatureFlags.CustomUserStatuses || !*a.Config().TeamSettings.EnableCustomUserStatuses {
return nil
}
if message == CmdCustomStatusClear {
if err := a.RemoveCustomStatus(args.UserId); err != nil {
mlog.Error(err.Error())
return &model.CommandResponse{Text: args.T("api.command_custom_status.clear.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: args.T("api.command_custom_status.clear.success"),
}
}
customStatus := &model.CustomStatus{
Emoji: DefaultCustomStatusEmoji,
Text: message,
}
firstEmojiLocations := model.ALL_EMOJI_PATTERN.FindIndex([]byte(message))
if len(firstEmojiLocations) > 0 && firstEmojiLocations[0] == 0 {
// emoji found at starting index
customStatus.Emoji = message[firstEmojiLocations[0]+1 : firstEmojiLocations[1]-1]
customStatus.Text = strings.TrimSpace(message[firstEmojiLocations[1]:])
}
customStatus.TrimMessage()
if err := a.SetCustomStatus(args.UserId, customStatus); err != nil {
mlog.Error(err.Error())
return &model.CommandResponse{Text: args.T("api.command_custom_status.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
}
return &model.CommandResponse{
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
Text: args.T("api.command_custom_status.success", map[string]interface{}{
"EmojiName": ":" + customStatus.Emoji + ":",
"StatusMessage": customStatus.Text,
}),
}
}

Просмотреть файл

@@ -6,6 +6,7 @@ package app
import (
"errors"
"net/http"
"strings"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
@@ -363,3 +364,86 @@ func (a *App) GetStatus(userID string) (*model.Status, *model.AppError) {
func (a *App) IsUserAway(lastActivityAt int64) bool {
return model.GetMillis()-lastActivityAt >= *a.Config().TeamSettings.UserStatusAwayTimeout*1000
}
func (a *App) SetCustomStatus(userID string, cs *model.CustomStatus) *model.AppError {
user, err := a.GetUser(userID)
if err != nil {
return err
}
user.SetCustomStatus(cs)
_, updateErr := a.UpdateUser(user, true)
if updateErr != nil {
return err
}
if err := a.addRecentCustomStatus(userID, cs); err != nil {
a.Log().Error("Can't add recent custom status for", mlog.String("userID", userID), mlog.Err(err))
}
return nil
}
func (a *App) RemoveCustomStatus(userID string) *model.AppError {
user, err := a.GetUser(userID)
if err != nil {
return err
}
user.ClearCustomStatus()
_, updateErr := a.UpdateUser(user, true)
if updateErr != nil {
return err
}
return nil
}
func (a *App) addRecentCustomStatus(userID string, status *model.CustomStatus) *model.AppError {
var newRCS *model.RecentCustomStatuses
pref, err := a.GetPreferenceByCategoryAndNameForUser(userID, model.PREFERENCE_CATEGORY_CUSTOM_STATUS, model.PREFERENCE_NAME_RECENT_CUSTOM_STATUSES)
if err != nil || pref.Value == "" {
newRCS = &model.RecentCustomStatuses{*status}
} else {
existingRCS := model.RecentCustomStatusesFromJson(strings.NewReader(pref.Value))
newRCS = existingRCS.Add(status)
}
pref = &model.Preference{
UserId: userID,
Category: model.PREFERENCE_CATEGORY_CUSTOM_STATUS,
Name: model.PREFERENCE_NAME_RECENT_CUSTOM_STATUSES,
Value: newRCS.ToJson(),
}
if err := a.UpdatePreferences(userID, model.Preferences{*pref}); err != nil {
return err
}
return nil
}
func (a *App) RemoveRecentCustomStatus(userID string, status *model.CustomStatus) *model.AppError {
pref, err := a.GetPreferenceByCategoryAndNameForUser(userID, model.PREFERENCE_CATEGORY_CUSTOM_STATUS, model.PREFERENCE_NAME_RECENT_CUSTOM_STATUSES)
if err != nil {
return err
}
if pref.Value == "" {
return model.NewAppError("RemoveRecentCustomStatus", "api.custom_status.recent_custom_statuses.delete.app_error", nil, "", http.StatusBadRequest)
}
existingRCS := model.RecentCustomStatusesFromJson(strings.NewReader(pref.Value))
if !existingRCS.Contains(status) {
return model.NewAppError("RemoveRecentCustomStatus", "api.custom_status.recent_custom_statuses.delete.app_error", nil, "", http.StatusBadRequest)
}
newRCS := existingRCS.Remove(status)
pref.Value = newRCS.ToJson()
if err := a.UpdatePreferences(userID, model.Preferences{*pref}); err != nil {
return err
}
return nil
}