From c9e9fa27cb10beac9fba62a9db7ee3c9096ebde7 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Fri, 14 Jun 2024 02:27:32 -0600 Subject: [PATCH] Update log channels to new configs. --- internal/ban/background.go | 2 +- internal/ban/ban_asn_usecase.go | 2 +- internal/ban/ban_steam_usecase.go | 6 +++--- internal/cmd/serve.go | 2 +- internal/discord/responses.go | 29 +++++++++++++++++++++++++---- internal/news/news_service.go | 2 +- internal/report/report_service.go | 10 +++++----- internal/votes/vote_usecase.go | 28 +++++++++++++++++++++------- 8 files changed, 58 insertions(+), 23 deletions(-) diff --git a/internal/ban/background.go b/internal/ban/background.go index 289d0e51..9ba04b1d 100644 --- a/internal/ban/background.go +++ b/internal/ban/background.go @@ -55,7 +55,7 @@ func Start(ctx context.Context, banUsecase domain.BanSteamUsecase, banNetUsecase name = person.SteamID.String() } - discordUsecase.SendPayload(domain.ChannelModLog, discord.BanExpiresMessage(ban, person, configUsecase.ExtURL(ban))) + discordUsecase.SendPayload(domain.ChannelBanLog, discord.BanExpiresMessage(ban, person, configUsecase.ExtURL(ban))) slog.Info("Ban expired", slog.String("reason", ban.Reason.String()), diff --git a/internal/ban/ban_asn_usecase.go b/internal/ban/ban_asn_usecase.go index cc183ba1..6878f49c 100644 --- a/internal/ban/ban_asn_usecase.go +++ b/internal/ban/ban_asn_usecase.go @@ -70,7 +70,7 @@ func (s banASN) Unban(ctx context.Context, asnNum string) (bool, error) { return false, errors.Join(errDrop, domain.ErrDropASNBan) } - s.discord.SendPayload(domain.ChannelModLog, discord.UnbanASNMessage(asNum)) + s.discord.SendPayload(domain.ChannelBanLog, discord.UnbanASNMessage(asNum)) return true, nil } diff --git a/internal/ban/ban_steam_usecase.go b/internal/ban/ban_steam_usecase.go index 4407abb7..407bad31 100644 --- a/internal/ban/ban_steam_usecase.go +++ b/internal/ban/ban_steam_usecase.go @@ -147,14 +147,14 @@ func (s banSteamUsecase) Ban(ctx context.Context, curUser domain.PersonInfo, ban slog.Int64("sid64", banSteam.TargetID.Int64())) } - s.discord.SendPayload(domain.ChannelModLog, discord.KickPlayerEmbed(target)) + s.discord.SendPayload(domain.ChannelBanLog, discord.KickPlayerEmbed(target)) } else if banSteam.BanType == domain.NoComm { if errSilence := s.state.Silence(ctx, banSteam.TargetID, banSteam.Reason); errSilence != nil && !errors.Is(errSilence, domain.ErrPlayerNotFound) { slog.Error("Failed to silence player", log.ErrAttr(errSilence), slog.Int64("sid64", banSteam.TargetID.Int64())) } - s.discord.SendPayload(domain.ChannelModLog, discord.SilenceEmbed(target)) + s.discord.SendPayload(domain.ChannelBanLog, discord.SilenceEmbed(target)) } return nil @@ -186,7 +186,7 @@ func (s banSteamUsecase) Unban(ctx context.Context, targetSID steamid.SteamID, r return false, errors.Join(err, domain.ErrFetchPerson) } - s.discord.SendPayload(domain.ChannelModLog, discord.UnbanMessage(s.config, person)) + s.discord.SendPayload(domain.ChannelBanLog, discord.UnbanMessage(s.config, person)) return true, nil } diff --git a/internal/cmd/serve.go b/internal/cmd/serve.go index 754ab129..d339e2c4 100644 --- a/internal/cmd/serve.go +++ b/internal/cmd/serve.go @@ -202,7 +202,7 @@ func serveCmd() *cobra.Command { //nolint:maintidx authUsecase := auth.NewAuthUsecase(auth.NewAuthRepository(db), configUsecase, personUsecase, banUsecase, serversUsecase) go authUsecase.Start(ctx) - voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(db), personUsecase, matchUsecase, discordUsecase, eventBroadcaster) + voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(db), personUsecase, matchUsecase, discordUsecase, configUsecase, eventBroadcaster) go voteUsecase.Start(ctx) contestUsecase := contest.NewContestUsecase(contest.NewContestRepository(db)) diff --git a/internal/discord/responses.go b/internal/discord/responses.go index 16f5942b..3c69b207 100644 --- a/internal/discord/responses.go +++ b/internal/discord/responses.go @@ -1148,14 +1148,35 @@ func NotificationMessage(message string, link string) *discordgo.MessageEmbed { return msgEmbed.Embed().Truncate().MessageEmbed } -func VoteResultMessage(result domain.VoteResult) *discordgo.MessageEmbed { +func VoteResultMessage(conf domain.Config, result domain.VoteResult, source domain.Person, target domain.Person) *discordgo.MessageEmbed { + avatarSource := domain.NewAvatarLinks(source.AvatarHash) + avatarTarget := domain.NewAvatarLinks(target.AvatarHash) + msgEmbed := NewEmbed("Vote Result") + if result.Success { + msgEmbed.emb.Color = ColourSuccess + } else { + msgEmbed.emb.Color = ColourWarn + } + + msgEmbed.emb.Thumbnail = &discordgo.MessageEmbedThumbnail{ + URL: avatarTarget.Full(), + } + + msgEmbed.emb.Author = &discordgo.MessageEmbedAuthor{ + URL: conf.ExtURL(source), + Name: source.PersonaName, + IconURL: avatarSource.Full(), + } + msgEmbed.Embed(). - AddField("Initiator", result.SourceID.String()). - AddField("Target", result.TargetID.String()). + AddField("Caller SID", result.SourceID.String()). + AddField("Target", target.PersonaName). + AddField("Target SID", result.TargetID.String()). AddField("Code", fmt.Sprintf("%d", result.Code)). AddField("Success", strconv.FormatBool(result.Success)). - AddField("Server", strconv.FormatInt(int64(result.ServerID), 10)) + AddField("Server", strconv.FormatInt(int64(result.ServerID), 10)). + InlineAllFields() return msgEmbed.Embed().Truncate().MessageEmbed } diff --git a/internal/news/news_service.go b/internal/news/news_service.go index 444bb249..93c7352b 100644 --- a/internal/news/news_service.go +++ b/internal/news/news_service.go @@ -76,7 +76,7 @@ func (h newsHandler) onAPIPostNewsCreate() gin.HandlerFunc { return } - ctx.JSON(http.StatusCreated, req) + ctx.JSON(http.StatusCreated, entry) go h.discord.SendPayload(domain.ChannelModLog, discord.NewNewsMessage(req.BodyMD, req.Title)) } diff --git a/internal/report/report_service.go b/internal/report/report_service.go index f8e10a4f..11b08ae4 100644 --- a/internal/report/report_service.go +++ b/internal/report/report_service.go @@ -81,7 +81,7 @@ func (h reportHandler) onAPIPostBanState() gin.HandlerFunc { ctx.JSON(http.StatusOK, report) - h.discord.SendPayload(domain.ChannelModLog, discord.EditBanAppealStatusMessage()) + h.discord.SendPayload(domain.ChannelModAppealLog, discord.EditBanAppealStatusMessage()) } } @@ -218,7 +218,7 @@ func (h reportHandler) onAPIPostReportCreate() gin.HandlerFunc { msg := discord.NewInGameReportResponse(report, conf.ExtURL(report), currentUser, conf.ExtURL(currentUser), demoURL) - h.discord.SendPayload(domain.ChannelModLog, msg) + h.discord.SendPayload(domain.ChannelModAppealLog, msg) } } @@ -443,7 +443,7 @@ func (h reportHandler) onAPIPostReportMessage() gin.HandlerFunc { conf := h.config.Config() - h.discord.SendPayload(domain.ChannelModLog, + h.discord.SendPayload(domain.ChannelModAppealLog, discord.NewReportMessageResponse(msg.MessageMD, conf.ExtURL(report), person, conf.ExtURL(person))) } } @@ -516,7 +516,7 @@ func (h reportHandler) onAPIEditReportMessage() gin.HandlerFunc { msg := discord.EditReportMessageResponse(req.BodyMD, existing.MessageMD, conf.ExtURLRaw("/report/%d", existing.ReportID), curUser, conf.ExtURL(curUser)) - h.discord.SendPayload(domain.ChannelModLog, msg) + h.discord.SendPayload(domain.ChannelModAppealLog, msg) } } @@ -561,6 +561,6 @@ func (h reportHandler) onAPIDeleteReportMessage() gin.HandlerFunc { conf := h.config.Config() - h.discord.SendPayload(domain.ChannelModLog, discord.DeleteReportMessage(existing, curUser, conf.ExtURL(curUser))) + h.discord.SendPayload(domain.ChannelModAppealLog, discord.DeleteReportMessage(existing, curUser, conf.ExtURL(curUser))) } } diff --git a/internal/votes/vote_usecase.go b/internal/votes/vote_usecase.go index ce489fb7..c671f61f 100644 --- a/internal/votes/vote_usecase.go +++ b/internal/votes/vote_usecase.go @@ -13,21 +13,24 @@ import ( ) type voteUsecase struct { - repository domain.VoteRepository - persons domain.PersonUsecase - matches domain.MatchUsecase - discord domain.DiscordUsecase + repository domain.VoteRepository + persons domain.PersonUsecase + matches domain.MatchUsecase + discord domain.DiscordUsecase + config domain.ConfigUsecase + broadcaster *fp.Broadcaster[logparse.EventType, logparse.ServerEvent] } func NewVoteUsecase(repository domain.VoteRepository, persons domain.PersonUsecase, matched domain.MatchUsecase, - discord domain.DiscordUsecase, broadcaster *fp.Broadcaster[logparse.EventType, logparse.ServerEvent], + discord domain.DiscordUsecase, config domain.ConfigUsecase, broadcaster *fp.Broadcaster[logparse.EventType, logparse.ServerEvent], ) domain.VoteUsecase { return &voteUsecase{ repository: repository, persons: persons, matches: matched, discord: discord, + config: config, broadcaster: broadcaster, } } @@ -153,11 +156,22 @@ func (u voteUsecase) Start(ctx context.Context) { slog.Error("Failed to add vote result", log.ErrAttr(err)) } - u.discord.SendPayload(domain.ChannelModVoteLog, discord.VoteResultMessage(result)) - recent = append(recent, result) delete(active, evt.ServerID) + + source, errSource := u.persons.GetOrCreatePersonBySteamID(ctx, result.SourceID) + if errSource != nil { + slog.Error("Failed to load vote source", log.ErrAttr(errSource), slog.String("steam_id", result.SourceID.String())) + } + + target, errTarget := u.persons.GetOrCreatePersonBySteamID(ctx, result.SourceID) + if errTarget != nil { + slog.Error("Failed to load vote target", log.ErrAttr(errSource), slog.String("steam_id", result.TargetID.String())) + } + + u.discord.SendPayload(domain.ChannelModVoteLog, discord.VoteResultMessage(u.config.Config(), result, source, target)) + } } }