- Notifications
You must be signed in to change notification settings - Fork1k
feat: support custom notifications#19751
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
2ab1c4d
327ec0d
0d1144a
203668d
d3ffd1a
938d010
9a39693
6b8636d
80c6b22
657a230
681a29f
14e155e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -376,6 +376,10 @@ func (u User) RBACObject() rbac.Object { | ||
return rbac.ResourceUserObject(u.ID) | ||
} | ||
func (u User) IsSystemUser() bool { | ||
return u.IsSystem | ||
} | ||
ssncferreira marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func (u GetUsersRow) RBACObject() rbac.Object { | ||
return rbac.ResourceUserObject(u.ID) | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -333,6 +333,7 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R | ||
// @Param request body codersdk.CustomNotification true "Provide a non-empty title or message" | ||
// @Success 204 "No Content" | ||
// @Failure 400 {object} codersdk.Response "Invalid request body" | ||
// @Failure 403 {object} codersdk.Response "System users cannot send custom notifications" | ||
// @Failure 500 {object} codersdk.Response "Failed to send custom notification" | ||
// @Router /notifications/custom [post] | ||
func (api *API) postCustomNotification(rw http.ResponseWriter, r *http.Request) { | ||
@@ -357,17 +358,36 @@ func (api *API) postCustomNotification(rw http.ResponseWriter, r *http.Request) | ||
return | ||
} | ||
// Block system users from sending custom notifications | ||
user, err := api.Database.GetUserByID(ctx, apiKey.UserID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "send custom notification", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to send custom notification", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
if user.IsSystemUser() { | ||
api.Logger.Error(ctx, "send custom notification: system user is not allowed", | ||
slog.F("id", user.ID.String()), slog.F("name", user.Name)) | ||
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ | ||
Message: "Forbidden", | ||
Detail: "System users cannot send custom notifications.", | ||
}) | ||
return | ||
} | ||
if _, err := api.NotificationsEnqueuer.Enqueue( | ||
| ||
//nolint:gocritic // We need to be notifier to send the notification. | ||
dbauthz.AsNotifier(ctx), | ||
user.ID, | ||
notifications.TemplateCustomNotification, | ||
map[string]string{ | ||
"custom_title": req.Title, | ||
"custom_message": req.Message, | ||
}, | ||
user.ID.String(), | ||
); err != nil { | ||
api.Logger.Error(ctx, "send custom notification", slog.Error(err)) | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.