Skip to content

Commit

Permalink
Add user restricted and user deleted alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
codemicro committed Jan 25, 2024
1 parent 4312a69 commit 262d98b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
16 changes: 16 additions & 0 deletions internal/discordWebhookNotify/discordWebhookNotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Run() {
events.TopicUserElectionWithdraw,
events.TopicElectionStarted,
events.TopicElectionEnded,
events.TopicUserRestricted,
events.TopicUserDeleted,
)

for msg := range receiver {
Expand Down Expand Up @@ -56,6 +58,20 @@ func Run() {
me.Colour = ColourGood
me.Title = fmt.Sprintf("Voting for %s has ended!", data.Election.RoleName)
me.Description = fmt.Sprintf("Election ID: %d\n\n```%s```", data.Election.ID, data.Result)

case events.TopicUserRestricted:
data := msg.Data.(*events.UserRestrictedData)

me.Colour = ColourMid
me.Title = fmt.Sprintf("%s has been restricted", data.User.Name)
me.Description = fmt.Sprintf("User ID: %s\n\nRestricted by admin with ID %s", data.User.StudentID, data.ActingUserID)

case events.TopicUserDeleted:
data := msg.Data.(*events.UserDeletedData)

me.Colour = ColourMid
me.Title = fmt.Sprintf("Account with ID %s has been deleted", data.UserID)
me.Description = fmt.Sprintf("Deleted by admin with ID %s", data.UserID, data.ActingUserID)
}

if err := sendEmbed(me); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions internal/events/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (

TopicUserElectionStand Topic = "user-stand"
TopicUserElectionWithdraw Topic = "user-withdraw"
TopicUserRestricted Topic = "user-restrict"
TopicUserDeleted Topic = "user-delete"
)

type UserElectionStandData struct {
Expand All @@ -26,3 +28,13 @@ type ElectionEndedData struct {
Election *database.Election `json:"election"`
Result string `json:"result,omitempty"`
}

type UserRestrictedData struct {
User *database.User `json:"user"`
ActingUserID string `json:"actingUserID"`
}

type UserDeletedData struct {
UserID string `json:"user"`
ActingUserID string `json:"actingUserID"`
}
17 changes: 15 additions & 2 deletions internal/httpcore/endpoints_adminapi_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"fmt"
"github.com/CSSUoB/society-voting/internal/database"
"github.com/CSSUoB/society-voting/internal/events"
"github.com/gofiber/fiber/v2"
"sort"
)

func (endpoints) apiAdminDeleteUser(ctx *fiber.Ctx) error {
if _, status := getSessionAuth(ctx); status&authAdminUser == 0 {
actingUserID, authStatus := getSessionAuth(ctx)
if authStatus&authAdminUser == 0 {
return fiber.ErrUnauthorized
}

Expand Down Expand Up @@ -43,6 +45,11 @@ func (endpoints) apiAdminDeleteUser(ctx *fiber.Ctx) error {
return fmt.Errorf("apiAdminDeleteUser commit tx: %w", err)
}

events.SendEvent(events.TopicUserDeleted, &events.UserDeletedData{
ActingUserID: actingUserID,
UserID: request.UserID,
})

ctx.Status(fiber.StatusNoContent)
return nil
}
Expand All @@ -69,7 +76,8 @@ func (endpoints) apiAdminListUsers(ctx *fiber.Ctx) error {
}

func (endpoints) apiAdminToggleRestrictUser(ctx *fiber.Ctx) error {
if _, status := getSessionAuth(ctx); status&authAdminUser == 0 {
actingUserID, authStatus := getSessionAuth(ctx)
if authStatus&authAdminUser == 0 {
return fiber.ErrUnauthorized
}

Expand Down Expand Up @@ -121,5 +129,10 @@ func (endpoints) apiAdminToggleRestrictUser(ctx *fiber.Ctx) error {
return fmt.Errorf("apiAdminToggleRestrictUser commit tx: %w", err)
}

events.SendEvent(events.TopicUserRestricted, &events.UserRestrictedData{
ActingUserID: actingUserID,
User: user,
})

return ctx.JSON(map[string]bool{"isRestricted": user.IsRestricted})
}

0 comments on commit 262d98b

Please sign in to comment.