[MM-43649] - Ability for end users to notify admin to upgrade workspace (#20338)

* [MM-43649] - Ability for end users to notify admin to upgrade workspace

* add test case

* move code to app layer

* feedback impl

* feedback impl-1

* feedback impl

* simplify logic

* fix typo and lint

* fix app layers

* Use a single critical section for users notified admin (#20488)

* Use a single critical section for users notifying admin

* change persistence mechanism

* add more tests

* change bot message

* fix translations

* update logic to notify once within cooloff period

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Nathaniel Allred <neallred@protonmail.com>
Этот коммит содержится в:
Allan Guwatudde
2022-06-30 09:24:42 +03:00
коммит произвёл GitHub
родитель dad8eab777
Коммит 1d9fa3c333
9 изменённых файлов: 293 добавлений и 1 удалений

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

@@ -890,6 +890,7 @@ type AppIface interface {
NotificationsLog() *mlog.Logger
NotifyAndSetWarnMetricAck(warnMetricId string, sender *model.User, forceAck bool, isBot bool) *model.AppError
NotifySharedChannelUserUpdate(user *model.User)
NotifySystemAdminsToUpgrade(c *request.Context, currentUserTeamID string) *model.AppError
OpenInteractiveDialog(request model.OpenDialogRequest) *model.AppError
OriginChecker() func(*http.Request) bool
PatchChannel(c *request.Context, channel *model.Channel, patch *model.ChannelPatch, userID string) (*model.Channel, *model.AppError)

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

@@ -4,15 +4,108 @@
package app
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/mattermost/mattermost-server/v6/app/request"
"github.com/mattermost/mattermost-server/v6/einterfaces"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/i18n"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
)
func (a *App) NotifySystemAdminsToUpgrade(c *request.Context, currentUserTeamID string) *model.AppError {
userId := c.Session().Id
fakeId := strings.ReplaceAll(model.CloudNotifyAdminInfo, "_", "") + "123456"
// check if already notified
notificationPref, err := a.Srv().Store.Preference().Get(fakeId, model.PreferenceCloudUserEphemeralInfo, model.CloudNotifyAdminInfo)
if err != nil {
mlog.Warn("Unable to get preference cloud_user_ephemeral_info", mlog.Err(err))
}
if notificationPref != nil {
info := &model.AdminNotificationUserInfo{}
err = json.Unmarshal([]byte(notificationPref.Value), info)
if err != nil {
mlog.Warn("Unable to Unmarshal", mlog.Err(err))
}
if !model.CanNotify(info.LastNotificationTimestamp) {
return model.NewAppError("app.NotifySystemAdminsToUpgrade", "api.cloud.notify_admin_to_upgrade_error.already_notified", nil, "", http.StatusForbidden)
}
}
team, appErr := a.GetTeam(currentUserTeamID)
if appErr != nil {
return appErr
}
sysadmins, appErr := a.GetUsersFromProfiles(&model.UserGetOptions{
Page: 0,
PerPage: 100,
Role: model.SystemAdminRoleId,
Inactive: false,
})
if appErr != nil {
return appErr
}
systemBot, appErr := a.GetSystemBot()
if appErr != nil {
return appErr
}
for _, admin := range sysadmins {
T := i18n.GetUserTranslations(admin.Locale)
channel, appErr := a.GetOrCreateDirectChannel(c, systemBot.UserId, admin.Id)
if appErr != nil {
mlog.Warn("Error getting direct channel", mlog.Err(appErr))
continue
}
post := &model.Post{
Message: T("api.cloud.upgrade_plan_bot_message", map[string]interface{}{"TeamName": team.Name}),
UserId: systemBot.UserId,
ChannelId: channel.Id,
Type: fmt.Sprintf("%sup_notification", model.PostCustomTypePrefix), // webapp will have to create renderer for this custom post type
}
_, appErr = a.CreatePost(c, post, channel, false, true)
if appErr != nil {
mlog.Warn("Error creating post", mlog.Err(appErr))
continue
}
}
// mark as done for current user until end of cool off period
out, err := json.Marshal(&model.AdminNotificationUserInfo{
LastUserIDToNotify: userId,
LastNotificationTimestamp: model.GetMillis(),
})
if err != nil {
mlog.Warn("Unable to Marshal", mlog.Err(err))
}
pref := model.Preference{
UserId: fakeId, // to only have one preference for now and not a preference per user
Category: model.PreferenceCloudUserEphemeralInfo,
Name: model.CloudNotifyAdminInfo,
Value: string(out),
}
if err := a.Srv().Store.Preference().Save(model.Preferences{pref}); err != nil {
mlog.Warn("Encountered error saving cloud_user_ephemeral_info preference", mlog.Err(err))
}
return nil
}
type cloudWrapper struct {
cloud einterfaces.CloudInterface
}

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

@@ -12280,6 +12280,28 @@ func (a *OpenTracingAppLayer) NotifySharedChannelUserUpdate(user *model.User) {
a.app.NotifySharedChannelUserUpdate(user)
}
func (a *OpenTracingAppLayer) NotifySystemAdminsToUpgrade(c *request.Context, currentUserTeamID string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.NotifySystemAdminsToUpgrade")
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.NotifySystemAdminsToUpgrade(c, currentUserTeamID)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) OpenInteractiveDialog(request model.OpenDialogRequest) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.OpenInteractiveDialog")