[CLD-6630] Cleanup: Refactor IP Filters email notification logic into app layer (#26072)

* Refactor IP Filters email notification logic into app layer

* Forgot to git add
This commit is contained in:
Nick Misasi 2024-02-20 10:52:55 -05:00 committed by GitHub
parent 7d8a56019b
commit 287962a309
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 30 deletions

View File

@ -4,12 +4,10 @@
package api4
import (
"context"
"encoding/json"
"net/http"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/v8/channels/app"
"github.com/mattermost/mattermost/server/v8/channels/audit"
"github.com/mattermost/mattermost/server/v8/einterfaces"
@ -83,34 +81,7 @@ func applyIPFilters(c *Context, w http.ResponseWriter, r *http.Request) {
auditRec.Success()
cloudWorkspaceOwnerEmailAddress := ""
if c.App.License().IsCloud() {
portalUserCustomer, cErr := c.App.Cloud().GetCloudCustomer(c.AppContext.Session().UserId)
if cErr != nil {
c.Logger.Error("Failed to get portal user customer", mlog.Err(cErr))
}
if cErr == nil && portalUserCustomer != nil {
cloudWorkspaceOwnerEmailAddress = portalUserCustomer.Email
}
}
go func() {
initiatingUser, err := c.App.Srv().Store().User().GetProfileByIds(context.Background(), []string{c.AppContext.Session().UserId}, nil, true)
if err != nil {
c.Logger.Error("Failed to get initiating user", mlog.Err(err))
}
users, err := c.App.Srv().Store().User().GetSystemAdminProfiles()
if err != nil {
c.Logger.Error("Failed to get system admins", mlog.Err(err))
}
for _, user := range users {
if err = c.App.Srv().EmailService.SendIPFiltersChangedEmail(user.Email, initiatingUser[0], *c.App.Config().ServiceSettings.SiteURL, *c.App.Config().CloudSettings.CWSURL, user.Locale, cloudWorkspaceOwnerEmailAddress == user.Email); err != nil {
c.Logger.Error("Error while sending IP filters changed email", mlog.Err(err))
}
}
}()
go c.App.SendIPFiltersChangedEmail(c.AppContext, c.AppContext.Session().UserId)
if err := json.NewEncoder(w).Encode(updatedAllowedRanges); err != nil {
c.Err = model.NewAppError("getIPFilters", "api.context.ip_filtering.get_ip_filters.app_error", nil, err.Error(), http.StatusInternalServerError)

View File

@ -1076,6 +1076,7 @@ type AppIface interface {
SendDelinquencyEmail(emailToSend model.DelinquencyEmail) *model.AppError
SendEmailVerification(user *model.User, newEmail, redirect string) *model.AppError
SendEphemeralPost(c request.CTX, userID string, post *model.Post) *model.Post
SendIPFiltersChangedEmail(c request.CTX, userID string) error
SendNotifications(c request.CTX, post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList, setOnline bool) ([]string, error)
SendNotifyAdminPosts(c request.CTX, workspaceName string, currentSKU string, trial bool) *model.AppError
SendPasswordReset(email string, siteURL string) (bool, *model.AppError)

View File

@ -0,0 +1,42 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"context"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
)
func (a *App) SendIPFiltersChangedEmail(c request.CTX, userID string) error {
cloudWorkspaceOwnerEmailAddress := ""
if a.License().IsCloud() {
portalUserCustomer, cErr := a.Cloud().GetCloudCustomer(userID)
if cErr != nil {
c.Logger().Error("Failed to get portal user customer", mlog.Err(cErr))
}
if cErr == nil && portalUserCustomer != nil {
cloudWorkspaceOwnerEmailAddress = portalUserCustomer.Email
}
}
initiatingUser, err := a.Srv().Store().User().GetProfileByIds(context.Background(), []string{userID}, nil, true)
if err != nil {
c.Logger().Error("Failed to get initiating user", mlog.Err(err))
}
users, err := a.Srv().Store().User().GetSystemAdminProfiles()
if err != nil {
c.Logger().Error("Failed to get system admins", mlog.Err(err))
}
for _, user := range users {
if err = a.Srv().EmailService.SendIPFiltersChangedEmail(user.Email, initiatingUser[0], *a.Config().ServiceSettings.SiteURL, *a.Config().CloudSettings.CWSURL, user.Locale, cloudWorkspaceOwnerEmailAddress == user.Email); err != nil {
c.Logger().Error("Error while sending IP filters changed email", mlog.Err(err))
}
}
return nil
}

View File

@ -15850,6 +15850,28 @@ func (a *OpenTracingAppLayer) SendEphemeralPost(c request.CTX, userID string, po
return resultVar0
}
func (a *OpenTracingAppLayer) SendIPFiltersChangedEmail(c request.CTX, userID string) error {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendIPFiltersChangedEmail")
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.SendIPFiltersChangedEmail(c, userID)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) SendNoCardPaymentFailedEmail() *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendNoCardPaymentFailedEmail")