Skip to content

Commit

Permalink
Remove filter funcs from discord. readd some discord messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 14, 2024
1 parent 2be0484 commit 34ce505
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 153 deletions.
10 changes: 5 additions & 5 deletions internal/cmd/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ func refreshFiltersCmd() *cobra.Command {
stateUsecase := state.NewStateUsecase(eventBroadcaster,
state.NewStateRepository(state.NewCollector(serversUsecase)), configUsecase, serversUsecase)

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase))
discordRepository, _ := discord.NewDiscordRepository(conf)

discordUsecase := discord.NewDiscordUsecase(discordRepository, configUsecase)

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase), discordUsecase)
if errImport := wordFilterUsecase.Import(ctx); errImport != nil {
slog.Error("Failed to load filters")
}

discordRepository, _ := discord.NewDiscordRepository(conf)

discordUsecase := discord.NewDiscordUsecase(discordRepository, wordFilterUsecase)

personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, dbUsecase), configUsecase)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(dbUsecase), discordUsecase, configUsecase, personUsecase, nil)
// banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(dbUsecase), personUsecase)
Expand Down
91 changes: 44 additions & 47 deletions internal/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"syscall"
"time"

"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"github.com/leighmacdonald/gbans/internal/app"
"github.com/leighmacdonald/gbans/internal/appeal"
Expand Down Expand Up @@ -69,20 +68,20 @@ func serveCmd() *cobra.Command { //nolint:maintidx
panic(fmt.Sprintf("Failed to read static config: %v", errStatic))
}

dbUsecase := database.New(staticConfig.DatabaseDSN, staticConfig.DatabaseAutoMigrate, staticConfig.DatabaseLogQueries)
if errConnect := dbUsecase.Connect(ctx); errConnect != nil {
db := database.New(staticConfig.DatabaseDSN, staticConfig.DatabaseAutoMigrate, staticConfig.DatabaseLogQueries)
if errConnect := db.Connect(ctx); errConnect != nil {
slog.Error("Cannot initialize database", log.ErrAttr(errConnect))

return
}

defer func() {
if errClose := dbUsecase.Close(); errClose != nil {
if errClose := db.Close(); errClose != nil {
slog.Error("Failed to close database cleanly")
}
}()

configUsecase := config.NewConfigUsecase(staticConfig, config.NewConfigRepository(dbUsecase))
configUsecase := config.NewConfigUsecase(staticConfig, config.NewConfigRepository(db))
if err := configUsecase.Init(ctx); err != nil {
panic(fmt.Sprintf("Failed to init config: %v", err))
}
Expand All @@ -93,20 +92,18 @@ func serveCmd() *cobra.Command { //nolint:maintidx

conf := configUsecase.Config()

var sentryClient *sentry.Client
var errSentry error

sentryClient, errSentry = log.NewSentryClient(conf.Sentry.SentryDSN, conf.Sentry.SentryTrace, conf.Sentry.SentrySampleRate, app.BuildVersion)
if conf.Sentry.SentryDSN != "" {
sentryClient, errSentry := log.NewSentryClient(conf.Sentry.SentryDSN, conf.Sentry.SentryTrace, conf.Sentry.SentrySampleRate, app.BuildVersion)
if errSentry != nil {
slog.Error("Failed to setup sentry client")
} else {
defer sentryClient.Flush(2 * time.Second)
}
}

logCloser := log.MustCreateLogger(conf.Log.File, conf.Log.Level)
defer logCloser()

if errSentry != nil {
slog.Error("Failed to setup sentry client")
} else {
defer sentryClient.Flush(2 * time.Second)
}

eventBroadcaster := fp.NewBroadcaster[logparse.EventType, logparse.ServerEvent]()
weaponsMap := fp.NewMutexMap[logparse.Weapon, int]()

Expand All @@ -117,48 +114,48 @@ func serveCmd() *cobra.Command { //nolint:maintidx
return
}

wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(dbUsecase))
if err := wordFilterUsecase.Import(ctx); err != nil {
slog.Error("Failed to load word filters", log.ErrAttr(err))
discordUsecase := discord.NewDiscordUsecase(discordRepository, configUsecase)

if err := discordUsecase.Start(); err != nil {
slog.Error("Failed to start discord", log.ErrAttr(err))

return
}

discordUsecase := discord.NewDiscordUsecase(discordRepository, wordFilterUsecase)

if err := discordUsecase.Start(); err != nil {
slog.Error("Failed to start discord", log.ErrAttr(err))
wordFilterUsecase := wordfilter.NewWordFilterUsecase(wordfilter.NewWordFilterRepository(db), discordUsecase)
if err := wordFilterUsecase.Import(ctx); err != nil {
slog.Error("Failed to load word filters", log.ErrAttr(err))

return
}

defer discordUsecase.Shutdown(conf.Discord.GuildID)

personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, dbUsecase), configUsecase)
personUsecase := person.NewPersonUsecase(person.NewPersonRepository(conf, db), configUsecase)

networkUsecase := network.NewNetworkUsecase(eventBroadcaster, network.NewNetworkRepository(dbUsecase), personUsecase, configUsecase)
networkUsecase := network.NewNetworkUsecase(eventBroadcaster, network.NewNetworkRepository(db), personUsecase, configUsecase)
go networkUsecase.Start(ctx)

assetRepository := asset.NewLocalRepository(dbUsecase, configUsecase)
assetRepository := asset.NewLocalRepository(db, configUsecase)
if errInitAssets := assetRepository.Init(ctx); errInitAssets != nil {
slog.Error("Failed to init local asset repo", log.ErrAttr(errInitAssets))

return
}

assetUsecase := asset.NewAssetUsecase(assetRepository)
serversUsecase := servers.NewServersUsecase(servers.NewServersRepository(dbUsecase))
demoUsecase := demo.NewDemoUsecase(domain.BucketDemo, demo.NewDemoRepository(dbUsecase), assetUsecase, configUsecase, serversUsecase)
serversUsecase := servers.NewServersUsecase(servers.NewServersRepository(db))
demoUsecase := demo.NewDemoUsecase(domain.BucketDemo, demo.NewDemoRepository(db), assetUsecase, configUsecase, serversUsecase)
go demoUsecase.Start(ctx)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(dbUsecase), discordUsecase, configUsecase, personUsecase, demoUsecase)
reportUsecase := report.NewReportUsecase(report.NewReportRepository(db), discordUsecase, configUsecase, personUsecase, demoUsecase)

stateUsecase := state.NewStateUsecase(eventBroadcaster,
state.NewStateRepository(state.NewCollector(serversUsecase)), configUsecase, serversUsecase)
banUsecase := ban.NewBanSteamUsecase(ban.NewBanSteamRepository(dbUsecase, personUsecase, networkUsecase), personUsecase, configUsecase, discordUsecase, reportUsecase, stateUsecase)
banUsecase := ban.NewBanSteamUsecase(ban.NewBanSteamRepository(db, personUsecase, networkUsecase), personUsecase, configUsecase, discordUsecase, reportUsecase, stateUsecase)

banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(dbUsecase), personUsecase, discordUsecase, configUsecase)
banGroupUsecase := steamgroup.NewBanGroupUsecase(steamgroup.NewSteamGroupRepository(db), personUsecase, discordUsecase, configUsecase)

blocklistUsecase := blocklist.NewBlocklistUsecase(blocklist.NewBlocklistRepository(dbUsecase), banUsecase, banGroupUsecase)
blocklistUsecase := blocklist.NewBlocklistUsecase(blocklist.NewBlocklistRepository(db), banUsecase, banGroupUsecase)
go blocklistUsecase.Start(ctx)

go func() {
Expand All @@ -167,48 +164,48 @@ func serveCmd() *cobra.Command { //nolint:maintidx
}
}()

banASNUsecase := ban.NewBanASNUsecase(ban.NewBanASNRepository(dbUsecase), discordUsecase, networkUsecase, configUsecase, personUsecase)
banNetUsecase := ban.NewBanNetUsecase(ban.NewBanNetRepository(dbUsecase), personUsecase, configUsecase, discordUsecase, stateUsecase)
banASNUsecase := ban.NewBanASNUsecase(ban.NewBanASNRepository(db), discordUsecase, networkUsecase, configUsecase, personUsecase)
banNetUsecase := ban.NewBanNetUsecase(ban.NewBanNetRepository(db), personUsecase, configUsecase, discordUsecase, stateUsecase)

discordOAuthUsecase := discord.NewDiscordOAuthUsecase(discord.NewDiscordOAuthRepository(dbUsecase), configUsecase)
discordOAuthUsecase := discord.NewDiscordOAuthUsecase(discord.NewDiscordOAuthRepository(db), configUsecase)
go discordOAuthUsecase.Start(ctx)

apu := appeal.NewAppealUsecase(appeal.NewAppealRepository(dbUsecase), banUsecase, personUsecase, discordUsecase, configUsecase)
apu := appeal.NewAppealUsecase(appeal.NewAppealRepository(db), banUsecase, personUsecase, discordUsecase, configUsecase)

matchRepo := match.NewMatchRepository(eventBroadcaster, dbUsecase, personUsecase, serversUsecase, discordUsecase, stateUsecase, weaponsMap)
matchRepo := match.NewMatchRepository(eventBroadcaster, db, personUsecase, serversUsecase, discordUsecase, stateUsecase, weaponsMap)
go matchRepo.Start(ctx)

matchUsecase := match.NewMatchUsecase(matchRepo, stateUsecase, serversUsecase, discordUsecase)

chatRepository := chat.NewChatRepository(dbUsecase, personUsecase, wordFilterUsecase, matchUsecase, eventBroadcaster)
chatRepository := chat.NewChatRepository(db, personUsecase, wordFilterUsecase, matchUsecase, eventBroadcaster)
go chatRepository.Start(ctx)

chatUsecase := chat.NewChatUsecase(configUsecase, chatRepository, wordFilterUsecase, stateUsecase, banUsecase, personUsecase, discordUsecase)
go chatUsecase.Start(ctx)

forumUsecase := forum.NewForumUsecase(forum.NewForumRepository(dbUsecase), discordUsecase)
forumUsecase := forum.NewForumUsecase(forum.NewForumRepository(db), discordUsecase)

metricsUsecase := metrics.NewMetricsUsecase(eventBroadcaster)
go metricsUsecase.Start(ctx)

go forumUsecase.Start(ctx)

newsUsecase := news.NewNewsUsecase(news.NewNewsRepository(dbUsecase))
notificationUsecase := notification.NewNotificationUsecase(notification.NewNotificationRepository(dbUsecase), personUsecase)
patreonUsecase := patreon.NewPatreonUsecase(patreon.NewPatreonRepository(dbUsecase), configUsecase)
newsUsecase := news.NewNewsUsecase(news.NewNewsRepository(db))
notificationUsecase := notification.NewNotificationUsecase(notification.NewNotificationRepository(db), personUsecase)
patreonUsecase := patreon.NewPatreonUsecase(patreon.NewPatreonRepository(db), configUsecase)
go patreonUsecase.Start(ctx)

srcdsUsecase := srcds.NewSrcdsUsecase(srcds.NewRepository(dbUsecase), configUsecase, serversUsecase, personUsecase, reportUsecase, discordUsecase, banUsecase)
srcdsUsecase := srcds.NewSrcdsUsecase(srcds.NewRepository(db), configUsecase, serversUsecase, personUsecase, reportUsecase, discordUsecase, banUsecase)

wikiUsecase := wiki.NewWikiUsecase(wiki.NewWikiRepository(dbUsecase))
wikiUsecase := wiki.NewWikiUsecase(wiki.NewWikiRepository(db))

authUsecase := auth.NewAuthUsecase(auth.NewAuthRepository(dbUsecase), configUsecase, personUsecase, banUsecase, serversUsecase)
authUsecase := auth.NewAuthUsecase(auth.NewAuthRepository(db), configUsecase, personUsecase, banUsecase, serversUsecase)
go authUsecase.Start(ctx)

voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(dbUsecase), personUsecase, matchUsecase, discordUsecase, eventBroadcaster)
voteUsecase := votes.NewVoteUsecase(votes.NewVoteRepository(db), personUsecase, matchUsecase, discordUsecase, eventBroadcaster)
go voteUsecase.Start(ctx)

contestUsecase := contest.NewContestUsecase(contest.NewContestRepository(dbUsecase))
contestUsecase := contest.NewContestUsecase(contest.NewContestRepository(db))

// start workers
if conf.General.Mode == domain.ReleaseMode {
Expand Down Expand Up @@ -265,7 +262,7 @@ func serveCmd() *cobra.Command { //nolint:maintidx
}

if conf.SSH.Enabled {
demoFetcher := demo.NewFetcher(dbUsecase, configUsecase, serversUsecase, assetUsecase, demoUsecase)
demoFetcher := demo.NewFetcher(db, configUsecase, serversUsecase, assetUsecase, demoUsecase)
go demoFetcher.Start(ctx)
}

Expand Down
26 changes: 2 additions & 24 deletions internal/discord/discord_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (bot *nullDiscordRepository) Start() error {
return nil
}

func (bot *nullDiscordRepository) SendPayload(_ domain.DiscordChannel, _ *discordgo.MessageEmbed) {
func (bot *nullDiscordRepository) SendPayload(_ string, _ *discordgo.MessageEmbed) {
}

type discordRepository struct {
Expand Down Expand Up @@ -232,33 +232,11 @@ func (bot *discordRepository) sendInteractionResponse(session *discordgo.Session
return nil
}

func (bot *discordRepository) SendPayload(channel domain.DiscordChannel, payload *discordgo.MessageEmbed) {
func (bot *discordRepository) SendPayload(channelID string, payload *discordgo.MessageEmbed) {
if !bot.isReady.Load() {
return
}

var channelID string

switch channel {
case domain.ChannelMod:
channelID = bot.conf.Discord.LogChannelID
case domain.ChannelModLog:
channelID = bot.conf.Discord.LogChannelID
case domain.ChannelPublicLog:
channelID = bot.conf.Discord.PublicLogChannelID
case domain.ChannelPublicMatchLog:
channelID = bot.conf.Discord.PublicMatchLogChannelID
case domain.ChannelModAppealLog:
case domain.ChannelModVoteLog:
case domain.ChannelBanLog:
case domain.ChannelForumLog:
case domain.ChannelWordFilterLog:
}

if channelID == "" {
channelID = bot.conf.Discord.LogChannelID
}

if _, errSend := bot.session.ChannelMessageSendEmbed(channelID, payload); errSend != nil {
slog.Error("Failed to send discord payload", log.ErrAttr(errSend))
}
Expand Down
38 changes: 0 additions & 38 deletions internal/discord/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ func (h discordService) makeOnUnban() func(_ context.Context, _ *discordgo.Sessi
func (h discordService) makeOnFilter() func(_ context.Context, _ *discordgo.Session, _ *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) { //nolint:maintidx
return func(ctx context.Context, session *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
switch interaction.ApplicationCommandData().Options[0].Name {
case "add":
return h.onFilterAdd(ctx, session, interaction)
case "del":
return h.onFilterDel(ctx, session, interaction)
case "check":
return h.onFilterCheck(ctx, session, interaction)
default:
Expand Down Expand Up @@ -501,40 +497,6 @@ func (h discordService) makeOnPlayers() func(context.Context, *discordgo.Session
}
}

func (h discordService) onFilterAdd(ctx context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate,
) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
pattern := opts[domain.OptPattern].StringValue()
isRegex := opts[domain.OptIsRegex].BoolValue()

author, errAuthor := h.getDiscordAuthor(ctx, interaction)
if errAuthor != nil {
return nil, errAuthor
}

return h.discord.FilterAdd(ctx, author, pattern, isRegex)
}

func (h discordService) onFilterDel(ctx context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
wordID := opts["filter"].IntValue()

if wordID <= 0 {
return nil, domain.ErrInvalidFilterID
}

filter, errGetFilter := h.wordFilters.GetFilterByID(ctx, wordID)
if errGetFilter != nil {
return nil, domain.ErrCommandFailed
}

if errDropFilter := h.wordFilters.DropFilter(ctx, filter); errDropFilter != nil {
return nil, domain.ErrCommandFailed
}

return FilterDelMessage(filter), nil
}

func (h discordService) onFilterCheck(_ context.Context, _ *discordgo.Session, interaction *discordgo.InteractionCreate) (*discordgo.MessageEmbed, error) {
opts := domain.OptionMap(interaction.ApplicationCommandData().Options[0].Options)
message := opts[domain.OptMessage].StringValue()
Expand Down
Loading

0 comments on commit 34ce505

Please sign in to comment.