Skip to content

Commit

Permalink
feat(parser): add mutex for voteban and check is target channel bot
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed May 25, 2024
1 parent 61df5b9 commit 6323253
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions apps/parser/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"time"

"github.com/getsentry/sentry-go"
"github.com/go-redsync/redsync/v4"
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
commands_bus "github.com/satont/twir/apps/parser/internal/commands-bus"
Expand Down Expand Up @@ -150,6 +152,8 @@ func main() {

bus := buscore.NewNatsBus(nc)

redSync := redsync.New(goredis.NewPool(redisClient))

s := &services.Services{
Config: config,
Logger: logger,
Expand All @@ -168,6 +172,7 @@ func main() {
CommandsCache: commandscache.New(db, redisClient),
SevenTvCache: seventv.New(redisClient),
SevenTvCacheBySevenTvID: seventv.NewBySeventvID(redisClient),
RedSync: redSync,
}

variablesService := variables.New(
Expand Down
28 changes: 23 additions & 5 deletions apps/parser/internal/commands/games/voteban.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/go-redsync/redsync/v4"
"github.com/guregu/null"
"github.com/lib/pq"
command_arguments "github.com/satont/twir/apps/parser/internal/command-arguments"
Expand Down Expand Up @@ -37,11 +38,26 @@ var Voteban = &types.DefaultCommand{
*types.CommandsHandlerResult,
error,
) {
mu := parseCtx.Services.RedSync.NewMutex(
"parser:voteban:"+parseCtx.Channel.ID,
redsync.WithExpiry(5*time.Second),
)
if err := mu.Lock(); err != nil {
return nil, &types.CommandHandlerError{
Message: "cannot lock voteban",
Err: err,
}
}
defer mu.Unlock()

entity := model.ChannelGamesVoteBan{}
if err := parseCtx.Services.Gorm.WithContext(ctx).Where(
`"channel_id" = ?`,
parseCtx.Channel.ID,
).First(&entity).Error; err != nil {
if err := parseCtx.Services.Gorm.
WithContext(ctx).
Preload("Channel").
Where(
`"channel_id" = ?`,
parseCtx.Channel.ID,
).First(&entity).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -62,7 +78,9 @@ var Voteban = &types.DefaultCommand{

targetUser := parseCtx.Mentions[0]

if targetUser.UserId == parseCtx.Channel.ID {
if entity.Channel == nil ||
targetUser.UserId == parseCtx.Channel.ID ||
targetUser.UserId == entity.Channel.BotID {
return nil, nil
}

Expand Down
3 changes: 3 additions & 0 deletions apps/parser/internal/types/services/services.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"github.com/go-redsync/redsync/v4"
"github.com/jmoiron/sqlx"
"github.com/redis/go-redis/v9"
"github.com/satont/twir/apps/parser/internal/task-queue"
Expand Down Expand Up @@ -38,4 +39,6 @@ type Services struct {
CommandsCache *generic_cacher.GenericCacher[[]model.ChannelsCommands]
SevenTvCache *generic_cacher.GenericCacher[*seventv.ProfileResponse]
SevenTvCacheBySevenTvID *generic_cacher.GenericCacher[*seventv.ProfileResponse]
RedSync *redsync.Redsync
CommandsLock *redsync.Mutex
}
2 changes: 2 additions & 0 deletions libs/gomodels/channel_games_voteban.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type ChannelGamesVoteBan struct {
VotingMode ChannelGamesVoteBanVotingMode `gorm:"column:voting_mode;type:channel_games_voteban_voting_mode"`
ChatVotesWordsPositive pq.StringArray `gorm:"column:chat_votes_words_positive;type:text[]"`
ChatVotesWordsNegative pq.StringArray `gorm:"column:chat_votes_words_negative;type:text[]"`

Channel *Channels `gorm:"foreignKey:ChannelID;references:ID"`
}

func (ChannelGamesVoteBan) TableName() string {
Expand Down

0 comments on commit 6323253

Please sign in to comment.