-
Notifications
You must be signed in to change notification settings - Fork 894
feat(coderd/notifications): improve notification format consistency #14967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
70f3486
feat(notifications): Improve notification format consistency
SasSwart 7e31a34
chore(coderd/notifications): regenerate notification testdata from th…
SasSwart cf3afd4
Merge remote-tracking branch 'origin/main' into jjs/consistent-notifi…
SasSwart 4b85f2b
chore(coderd/database): renumber migration
SasSwart e8ad3ac
chore(coderd/notifications): regenerate testdata
SasSwart 2a4d740
fix(coderd/notifications): remove duplicate function signature
SasSwart e741c43
chore: remove redundant escaping in migration
SasSwart adffe60
chore(coderd/notifications): improve failed test feedback
SasSwart 41ed54a
feat(coderd/database): add new information to the account activated n…
SasSwart 5541331
Merge remote-tracking branch 'origin/main' into jjs/additional-notifi…
SasSwart fe94f0d
chore(coderd/database): rework migration for legibility
SasSwart 98e7501
feat(coderd): send newly required information to notification templates
SasSwart d8e00c2
feat(coderd/notifications): provide additional context to workspace n…
SasSwart d6a339f
fix(coderd/notifications): add a missing call to fmt.Sprintf
SasSwart 920ad31
fix(coderd/notifications): fix oversights in template migration
SasSwart 9e938e5
chore(coderd/provisionerdserver): set the displayname in TestNotifica…
SasSwart 59e57ac
chore(coderd): add more robust testing assertions to TestNotifyDelete…
SasSwart c907238
chore(coderd/notifications): fix migration indentation
SasSwart 2493556
chore(coderd/notifications): regenerate golden files
SasSwart 19dccc8
Merge remote-tracking branch 'origin/main' into jjs/consistent-notifi…
SasSwart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(coderd): send newly required information to notification templates
- Loading branch information
commit 98e7501840a51913eb6b56d55329f14eac5865a1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,7 +194,8 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) { | |
Password: createUser.Password, | ||
OrganizationIDs: []uuid.UUID{defaultOrg.ID}, | ||
}, | ||
LoginType: database.LoginTypePassword, | ||
LoginType: database.LoginTypePassword, | ||
accountCreatorName: "coder", | ||
}) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
|
@@ -479,10 +480,22 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
apiKey := httpmw.APIKey(r) | ||
|
||
accountCreator, err := api.Database.GetUserByID(ctx, apiKey.UserID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Unable to determine the details of the actor creating the account.", | ||
}) | ||
return | ||
} | ||
|
||
user, err := api.CreateUser(ctx, api.Database, CreateUserRequest{ | ||
CreateUserRequestWithOrgs: req, | ||
LoginType: loginType, | ||
accountCreatorName: accountCreator.Name, | ||
}) | ||
|
||
if dbauthz.IsNotAuthorizedError(err) { | ||
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ | ||
Message: "You are not authorized to create users.", | ||
|
@@ -576,11 +589,24 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
apiKey := httpmw.APIKey(r) | ||
|
||
accountDeleter, err := api.Database.GetUserByID(ctx, apiKey.UserID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a follow-up PR, this feels like something that we should inject into the context. |
||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Unable to determine the details of the actor deleting the account.", | ||
}) | ||
return | ||
} | ||
|
||
for _, u := range userAdmins { | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountDeleted, | ||
map[string]string{ | ||
"deleted_account_name": user.Username, | ||
}, "api-users-delete", | ||
"deleted_account_name": user.Username, | ||
"deleted_account_user_name": user.Name, | ||
"account_deleter_user_name": accountDeleter.Name, | ||
}, | ||
"api-users-delete", | ||
user.ID, | ||
); err != nil { | ||
api.Logger.Warn(ctx, "unable to notify about deleted user", slog.F("deleted_user", user.Username), slog.Error(err)) | ||
|
@@ -844,6 +870,14 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW | |
} | ||
} | ||
|
||
actingUser, err := api.Database.GetUserByID(ctx, apiKey.UserID) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Unable to determine the details of the actor creating the account.", | ||
}) | ||
return | ||
} | ||
|
||
targetUser, err := api.Database.UpdateUserStatus(ctx, database.UpdateUserStatusParams{ | ||
ID: user.ID, | ||
Status: status, | ||
|
@@ -858,7 +892,7 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW | |
} | ||
aReq.New = targetUser | ||
|
||
err = api.notifyUserStatusChanged(ctx, user, status) | ||
err = api.notifyUserStatusChanged(ctx, actingUser.Name, user, status) | ||
if err != nil { | ||
api.Logger.Warn(ctx, "unable to notify about changed user's status", slog.F("affected_user", user.Username), slog.Error(err)) | ||
} | ||
|
@@ -871,24 +905,33 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW | |
}) | ||
return | ||
} | ||
|
||
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(targetUser, organizations)) | ||
} | ||
} | ||
|
||
func (api *API) notifyUserStatusChanged(ctx context.Context, user database.User, status database.UserStatus) error { | ||
var key string | ||
func (api *API) notifyUserStatusChanged(ctx context.Context, actingUserName string, targetUser database.User, status database.UserStatus) error { | ||
var labels map[string]string | ||
var adminTemplateID, personalTemplateID uuid.UUID | ||
switch status { | ||
case database.UserStatusSuspended: | ||
key = "suspended_account_name" | ||
labels = map[string]string{ | ||
"suspended_account_name": targetUser.Username, | ||
"suspended_account_user_name": targetUser.Name, | ||
"account_suspender_user_name": actingUserName, | ||
} | ||
adminTemplateID = notifications.TemplateUserAccountSuspended | ||
personalTemplateID = notifications.TemplateYourAccountSuspended | ||
case database.UserStatusActive: | ||
key = "activated_account_name" | ||
labels = map[string]string{ | ||
"activated_account_name": targetUser.Username, | ||
"activated_account_user_name": targetUser.Name, | ||
"account_activator_user_name": actingUserName, | ||
} | ||
adminTemplateID = notifications.TemplateUserAccountActivated | ||
personalTemplateID = notifications.TemplateYourAccountActivated | ||
default: | ||
api.Logger.Error(ctx, "user status is not supported", slog.F("username", user.Username), slog.F("user_status", string(status))) | ||
api.Logger.Error(ctx, "user status is not supported", slog.F("username", targetUser.Username), slog.F("user_status", string(status))) | ||
return xerrors.Errorf("unable to notify admins as the user's status is unsupported") | ||
} | ||
|
||
|
@@ -900,21 +943,17 @@ func (api *API) notifyUserStatusChanged(ctx context.Context, user database.User, | |
// Send notifications to user admins and affected user | ||
for _, u := range userAdmins { | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, adminTemplateID, | ||
map[string]string{ | ||
key: user.Username, | ||
}, "api-put-user-status", | ||
user.ID, | ||
labels, "api-put-user-status", | ||
targetUser.ID, | ||
); err != nil { | ||
api.Logger.Warn(ctx, "unable to notify about changed user's status", slog.F("affected_user", user.Username), slog.Error(err)) | ||
api.Logger.Warn(ctx, "unable to notify about changed user's status", slog.F("affected_user", targetUser.Username), slog.Error(err)) | ||
} | ||
} | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, user.ID, personalTemplateID, | ||
map[string]string{ | ||
key: user.Username, | ||
}, "api-put-user-status", | ||
user.ID, | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, targetUser.ID, personalTemplateID, | ||
labels, "api-put-user-status", | ||
targetUser.ID, | ||
); err != nil { | ||
api.Logger.Warn(ctx, "unable to notify user about status change of their account", slog.F("affected_user", user.Username), slog.Error(err)) | ||
api.Logger.Warn(ctx, "unable to notify user about status change of their account", slog.F("affected_user", targetUser.Username), slog.Error(err)) | ||
} | ||
return nil | ||
} | ||
|
@@ -1280,8 +1319,9 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques | |
|
||
type CreateUserRequest struct { | ||
codersdk.CreateUserRequestWithOrgs | ||
LoginType database.LoginType | ||
SkipNotifications bool | ||
LoginType database.LoginType | ||
SkipNotifications bool | ||
accountCreatorName string | ||
} | ||
|
||
func (api *API) CreateUser(ctx context.Context, store database.Store, req CreateUserRequest) (database.User, error) { | ||
|
@@ -1365,13 +1405,16 @@ func (api *API) CreateUser(ctx context.Context, store database.Store, req Create | |
for _, u := range userAdmins { | ||
if _, err := api.NotificationsEnqueuer.Enqueue(ctx, u.ID, notifications.TemplateUserAccountCreated, | ||
map[string]string{ | ||
"created_account_name": user.Username, | ||
"created_account_name": user.Username, | ||
"created_account_user_name": user.Name, | ||
"account_creator": req.accountCreatorName, | ||
}, "api-users-create", | ||
user.ID, | ||
); err != nil { | ||
api.Logger.Warn(ctx, "unable to notify about created user", slog.F("created_user", user.Username), slog.Error(err)) | ||
} | ||
} | ||
|
||
return user, err | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.