Skip to content

Commit

Permalink
feat(bots): use greetings repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 19, 2024
1 parent a04ca86 commit 69da021
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 71 deletions.
18 changes: 13 additions & 5 deletions apps/api-gql/internal/delivery/gql/resolvers/greetings.resolver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions apps/api-gql/internal/services/greetings/greetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func (c *Service) GetManyByChannelID(ctx context.Context, channelID string) (
[]entity.Greeting,
error,
) {
dbGreetings, err := c.greetingsRepository.GetManyByChannelID(ctx, channelID)
dbGreetings, err := c.greetingsRepository.GetManyByChannelID(
ctx,
channelID,
greetings.GetManyInput{},
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,11 +122,10 @@ type UpdateInput struct {
ActorID string
ChannelID string

UserID *string
Enabled *bool
Text *string
IsReply *bool
Processed *bool
UserID *string
Enabled *bool
Text *string
IsReply *bool
}

func (c *Service) Update(ctx context.Context, id uuid.UUID, input UpdateInput) (
Expand All @@ -148,7 +151,7 @@ func (c *Service) Update(ctx context.Context, id uuid.UUID, input UpdateInput) (
Enabled: input.Enabled,
Text: input.Text,
IsReply: input.IsReply,
Processed: input.Processed,
Processed: lo.ToPtr(false),
},
)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions apps/bots/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import (
"github.com/twirapp/twir/libs/grpc/parser"
"github.com/twirapp/twir/libs/grpc/tokens"
"github.com/twirapp/twir/libs/grpc/websockets"
keywordsrepository "github.com/twirapp/twir/libs/repositories/keywords"
keywordsrepositorypgx "github.com/twirapp/twir/libs/repositories/keywords/pgx"
"github.com/twirapp/twir/libs/uptrace"
"go.uber.org/fx"

keywordsrepository "github.com/twirapp/twir/libs/repositories/keywords"
keywordsrepositorypgx "github.com/twirapp/twir/libs/repositories/keywords/pgx"

greetingsrepository "github.com/twirapp/twir/libs/repositories/greetings"
greetingsrepositorypgx "github.com/twirapp/twir/libs/repositories/greetings/pgx"
)

var App = fx.Module(
Expand All @@ -35,6 +39,10 @@ var App = fx.Module(
keywordsrepositorypgx.NewFx,
fx.As(new(keywordsrepository.Repository)),
),
fx.Annotate(
greetingsrepositorypgx.NewFx,
fx.As(new(greetingsrepository.Repository)),
),
func(config cfg.Config) tokens.TokensClient {
return clients.NewTokens(config.AppEnv)
},
Expand Down
42 changes: 20 additions & 22 deletions apps/bots/internal/messagehandler/handle_greetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,24 @@ import (
"github.com/twirapp/twir/libs/bus-core/parser"
"github.com/twirapp/twir/libs/grpc/events"
"github.com/twirapp/twir/libs/grpc/websockets"
"gorm.io/gorm"
"github.com/twirapp/twir/libs/repositories/greetings"
)

func (c *MessageHandler) handleGreetings(ctx context.Context, msg handleMessage) error {
if msg.DbStream == nil {
return nil
}

entity := model.ChannelsGreetings{}
err := c.gorm.
WithContext(ctx).
Where(
`"channelId" = ? AND "userId" = ? AND "processed" = ? AND "enabled" = ?`,
msg.BroadcasterUserId,
msg.ChatterUserId,
false,
true,
).
First(&entity).
Error
greeting, err := c.greetingsRepository.GetOneByChannelAndUserID(
ctx, greetings.GetOneInput{
ChannelID: msg.BroadcasterUserId,
UserID: msg.ChatterUserId,
Enabled: lo.ToPtr(true),
Processed: lo.ToPtr(false),
},
)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if errors.Is(err, greetings.ErrNotFound) {
return nil
}
return err
Expand All @@ -46,7 +42,7 @@ func (c *MessageHandler) handleGreetings(ctx context.Context, msg handleMessage)
Where(
"channel_id = ? AND greetings_ids && ?",
msg.BroadcasterUserId,
pq.StringArray{entity.ID},
pq.StringArray{greeting.ID.String()},
).Find(&alert).Error; err != nil {
c.logger.Error("cannot find channel alert", slog.Any("err", err))
return
Expand All @@ -65,19 +61,21 @@ func (c *MessageHandler) handleGreetings(ctx context.Context, msg handleMessage)
)
}()

if err = c.gorm.WithContext(ctx).Model(&entity).Where("id = ?", entity.ID).Select("*").Updates(
map[string]any{
"processed": true,
if _, err := c.greetingsRepository.Update(
ctx,
greeting.ID,
greetings.UpdateInput{
Processed: lo.ToPtr(true),
},
).Error; err != nil {
); err != nil {
return err
}

res, err := c.bus.Parser.ParseVariablesInText.Request(
ctx, parser.ParseVariablesInTextRequest{
ChannelID: msg.BroadcasterUserId,
ChannelName: msg.BroadcasterUserLogin,
Text: entity.Text,
Text: greeting.Text,
UserID: msg.ChatterUserId,
UserLogin: msg.ChatterUserLogin,
UserName: msg.ChatterUserName,
Expand All @@ -92,7 +90,7 @@ func (c *MessageHandler) handleGreetings(ctx context.Context, msg handleMessage)
BroadcasterID: msg.BroadcasterUserId,
SenderID: msg.DbChannel.BotID,
Message: res.Data.Text,
ReplyParentMessageID: lo.If(entity.IsReply, msg.MessageId).Else(""),
ReplyParentMessageID: lo.If(greeting.IsReply, msg.MessageId).Else(""),
},
)

Expand All @@ -103,7 +101,7 @@ func (c *MessageHandler) handleGreetings(ctx context.Context, msg handleMessage)
UserId: msg.ChatterUserId,
UserName: msg.ChatterUserLogin,
UserDisplayName: msg.ChatterUserName,
GreetingText: entity.Text,
GreetingText: greeting.Text,
},
)
if err != nil {
Expand Down
56 changes: 30 additions & 26 deletions apps/bots/internal/messagehandler/messagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/twirapp/twir/libs/grpc/events"
"github.com/twirapp/twir/libs/grpc/parser"
"github.com/twirapp/twir/libs/grpc/websockets"
"github.com/twirapp/twir/libs/repositories/greetings"
"github.com/twirapp/twir/libs/repositories/keywords"
"github.com/twirapp/twir/libs/repositories/keywords/model"
"go.uber.org/fx"
Expand All @@ -33,18 +34,19 @@ type Opts struct {
fx.In
LC fx.Lifecycle

Logger logger.Logger
Gorm *gorm.DB
Redis *redis.Client
TwitchActions *twitchactions.TwitchActions
ParserGrpc parser.ParserClient
WebsocketsGrpc websockets.WebsocketClient
EventsGrpc events.EventsClient
ModerationHelpers *moderationhelpers.ModerationHelpers
Config cfg.Config
Bus *buscore.Bus
KeywordsCacher *generic_cacher.GenericCacher[[]model.Keyword]
KeywordsRepository keywords.Repository
Logger logger.Logger
Gorm *gorm.DB
Redis *redis.Client
TwitchActions *twitchactions.TwitchActions
ParserGrpc parser.ParserClient
WebsocketsGrpc websockets.WebsocketClient
EventsGrpc events.EventsClient
ModerationHelpers *moderationhelpers.ModerationHelpers
Config cfg.Config
Bus *buscore.Bus
KeywordsCacher *generic_cacher.GenericCacher[[]model.Keyword]
KeywordsRepository keywords.Repository
GreetingsRepository greetings.Repository
}

type MessageHandler struct {
Expand All @@ -61,26 +63,28 @@ type MessageHandler struct {
keywordsCacher *generic_cacher.GenericCacher[[]model.Keyword]
votebanMutex *redsync.Mutex

keywordsRepository keywords.Repository
keywordsRepository keywords.Repository
greetingsRepository greetings.Repository
}

func New(opts Opts) *MessageHandler {
votebanLock := redsync.New(goredis.NewPool(opts.Redis))

handler := &MessageHandler{
logger: opts.Logger,
gorm: opts.Gorm,
redis: opts.Redis,
twitchActions: opts.TwitchActions,
parserGrpc: opts.ParserGrpc,
websocketsGrpc: opts.WebsocketsGrpc,
eventsGrpc: opts.EventsGrpc,
moderationHelpers: opts.ModerationHelpers,
config: opts.Config,
bus: opts.Bus,
keywordsCacher: opts.KeywordsCacher,
votebanMutex: votebanLock.NewMutex("bots:voteban_handle_message"),
keywordsRepository: opts.KeywordsRepository,
logger: opts.Logger,
gorm: opts.Gorm,
redis: opts.Redis,
twitchActions: opts.TwitchActions,
parserGrpc: opts.ParserGrpc,
websocketsGrpc: opts.WebsocketsGrpc,
eventsGrpc: opts.EventsGrpc,
moderationHelpers: opts.ModerationHelpers,
config: opts.Config,
bus: opts.Bus,
keywordsCacher: opts.KeywordsCacher,
votebanMutex: votebanLock.NewMutex("bots:voteban_handle_message"),
keywordsRepository: opts.KeywordsRepository,
greetingsRepository: opts.GreetingsRepository,
}

return handler
Expand Down
2 changes: 1 addition & 1 deletion frontend/dashboard/src/api/greetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const useGreetingsApi = createGlobalState(() => {

const useMutationUpdateGreetings = () => useMutation(graphql(`
mutation UpdateGreetings($id: UUID!, $opts: GreetingsUpdateInput!) {
greetingsUpdate(id: UUID, opts: $opts) {
greetingsUpdate(id: $id, opts: $opts) {
id
}
}
Expand Down
7 changes: 7 additions & 0 deletions libs/repositories/greetings/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package greetings

import (
"errors"
)

var ErrNotFound = errors.New("greeting not found")
19 changes: 18 additions & 1 deletion libs/repositories/greetings/greetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import (
)

type Repository interface {
GetManyByChannelID(ctx context.Context, channelID string) ([]model.Greeting, error)
GetManyByChannelID(ctx context.Context, channelID string, filters GetManyInput) (
[]model.Greeting,
error,
)
GetByID(ctx context.Context, id uuid.UUID) (model.Greeting, error)
Create(ctx context.Context, input CreateInput) (model.Greeting, error)
Update(ctx context.Context, id uuid.UUID, input UpdateInput) (model.Greeting, error)
Delete(ctx context.Context, id uuid.UUID) error
GetOneByChannelAndUserID(ctx context.Context, input GetOneInput) (model.Greeting, error)
}

type GetManyInput struct {
Enabled *bool
Processed *bool
}

type CreateInput struct {
Expand All @@ -31,3 +40,11 @@ type UpdateInput struct {
IsReply *bool
Processed *bool
}

type GetOneInput struct {
ChannelID string
UserID string

Enabled *bool
Processed *bool
}
Loading

0 comments on commit 69da021

Please sign in to comment.