Skip to content

Commit

Permalink
feat: add ban/timeout event
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Aug 24, 2023
1 parent 63f253f commit 18a8adb
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 192 deletions.
101 changes: 87 additions & 14 deletions apps/events/internal/grpc_impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package grpc_impl

import (
"context"
"fmt"
"math"
"strings"
"time"

"github.com/samber/lo"
"github.com/satont/twir/apps/events/internal"
Expand All @@ -23,7 +26,10 @@ func NewEvents(services *internal.Services) *EventsGrpcImplementation {
}
}

func (c *EventsGrpcImplementation) Follow(_ context.Context, msg *events.FollowMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) Follow(
_ context.Context,
msg *events.FollowMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand All @@ -37,7 +43,10 @@ func (c *EventsGrpcImplementation) Follow(_ context.Context, msg *events.FollowM
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) Subscribe(_ context.Context, msg *events.SubscribeMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) Subscribe(
_ context.Context,
msg *events.SubscribeMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand Down Expand Up @@ -127,7 +136,10 @@ func (c *EventsGrpcImplementation) FirstUserMessage(
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) Raided(_ context.Context, msg *events.RaidedMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) Raided(
_ context.Context,
msg *events.RaidedMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand Down Expand Up @@ -159,7 +171,10 @@ func (c *EventsGrpcImplementation) TitleOrCategoryChanged(
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) StreamOnline(_ context.Context, msg *events.StreamOnlineMessage) (
func (c *EventsGrpcImplementation) StreamOnline(
_ context.Context,
msg *events.StreamOnlineMessage,
) (
*emptypb.Empty, error,
) {
go c.processEvent(
Expand All @@ -174,7 +189,10 @@ func (c *EventsGrpcImplementation) StreamOnline(_ context.Context, msg *events.S
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) StreamOffline(_ context.Context, msg *events.StreamOfflineMessage) (
func (c *EventsGrpcImplementation) StreamOffline(
_ context.Context,
msg *events.StreamOfflineMessage,
) (
*emptypb.Empty, error,
) {
go c.processEvent(
Expand All @@ -186,7 +204,10 @@ func (c *EventsGrpcImplementation) StreamOffline(_ context.Context, msg *events.
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) SubGift(_ context.Context, msg *events.SubGiftMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) SubGift(
_ context.Context,
msg *events.SubGiftMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand All @@ -201,7 +222,10 @@ func (c *EventsGrpcImplementation) SubGift(_ context.Context, msg *events.SubGif
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) ChatClear(_ context.Context, msg *events.ChatClearMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) ChatClear(
_ context.Context,
msg *events.ChatClearMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{},
Expand All @@ -211,7 +235,10 @@ func (c *EventsGrpcImplementation) ChatClear(_ context.Context, msg *events.Chat
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) Donate(_ context.Context, msg *events.DonateMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) Donate(
_ context.Context,
msg *events.DonateMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand Down Expand Up @@ -246,7 +273,10 @@ func (c *EventsGrpcImplementation) KeywordMatched(
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) GreetingSended(_ context.Context, msg *events.GreetingSendedMessage) (
func (c *EventsGrpcImplementation) GreetingSended(
_ context.Context,
msg *events.GreetingSendedMessage,
) (
*emptypb.Empty, error,
) {
go c.processEvent(
Expand All @@ -263,7 +293,10 @@ func (c *EventsGrpcImplementation) GreetingSended(_ context.Context, msg *events
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) PollBegin(_ context.Context, msg *events.PollBeginMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) PollBegin(
_ context.Context,
msg *events.PollBeginMessage,
) (*emptypb.Empty, error) {
go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
Expand All @@ -284,7 +317,10 @@ func (c *EventsGrpcImplementation) PollBegin(_ context.Context, msg *events.Poll
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) PollProgress(_ context.Context, msg *events.PollProgressMessage) (
func (c *EventsGrpcImplementation) PollProgress(
_ context.Context,
msg *events.PollProgressMessage,
) (
*emptypb.Empty, error,
) {
totalVotes := lo.Reduce(
Expand Down Expand Up @@ -314,7 +350,10 @@ func (c *EventsGrpcImplementation) PollProgress(_ context.Context, msg *events.P
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) PollEnd(_ context.Context, msg *events.PollEndMessage) (*emptypb.Empty, error) {
func (c *EventsGrpcImplementation) PollEnd(
_ context.Context,
msg *events.PollEndMessage,
) (*emptypb.Empty, error) {
totalVotes := lo.Reduce(
msg.Info.Choices, func(acc int, item *events.PollInfo_Choice, _ int) int {
return acc + int(item.Votes)
Expand Down Expand Up @@ -406,7 +445,10 @@ func (c *EventsGrpcImplementation) PredictionProgress(
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) PredictionLock(_ context.Context, msg *events.PredictionLockMessage) (
func (c *EventsGrpcImplementation) PredictionLock(
_ context.Context,
msg *events.PredictionLockMessage,
) (
*emptypb.Empty, error,
) {
totalPoints := lo.Reduce(
Expand Down Expand Up @@ -436,7 +478,10 @@ func (c *EventsGrpcImplementation) PredictionLock(_ context.Context, msg *events
return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) PredictionEnd(_ context.Context, msg *events.PredictionEndMessage) (
func (c *EventsGrpcImplementation) PredictionEnd(
_ context.Context,
msg *events.PredictionEndMessage,
) (
*emptypb.Empty, error,
) {
totalPoints := lo.Reduce(
Expand Down Expand Up @@ -491,3 +536,31 @@ func (c *EventsGrpcImplementation) StreamFirstUserJoin(

return &emptypb.Empty{}, nil
}

func (c *EventsGrpcImplementation) ChannelBan(
_ context.Context,
msg *events.ChannelBanMessage,
) (*emptypb.Empty, error) {
t, _ := time.Parse(time.RFC3339, msg.EndsAt)
banEndsIn := t.Sub(time.Now().UTC())

go c.processEvent(
msg.BaseInfo.ChannelId,
internal.Data{
UserName: msg.UserLogin,
UserDisplayName: msg.UserName,
ModeratorDisplayName: msg.ModeratorUserName,
ModeratorName: msg.ModeratorUserLogin,
BanReason: msg.Reason,
BanEndsInMinutes: lo.If(msg.IsPermanent, "permanent").Else(
fmt.Sprintf(
"%v",
math.Round(banEndsIn.Minutes()),
),
),
},
model.EventChannelBan,
)

return &emptypb.Empty{}, nil
}
3 changes: 1 addition & 2 deletions apps/events/internal/grpc_impl/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package grpc_impl

import (
"context"
"fmt"
"github.com/satont/twir/libs/grpc/generated/websockets"
"sort"
"strconv"
Expand Down Expand Up @@ -115,7 +114,7 @@ func (c *EventsGrpcImplementation) processOperations(
c.services.Logger.Sugar().Error(err)
return
}
fmt.Println("on", event.OnlineOnly)

// won't process stream if event setted to online only streams and stream is offline
if event.OnlineOnly {
stream := &model.ChannelsStreams{}
Expand Down
7 changes: 7 additions & 0 deletions apps/events/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ type Data struct {
PredictionTotalChannelPoints int `json:"predictionTotalChannelPoints"`
PredictionWinner PredictionOutCome `json:"predictionWinner"`
//

ModeratorName string `json:"moderatorName"`
ModeratorDisplayName string `json:"moderatorDisplayName"`

// ban
BanReason string `json:"banReason"`
BanEndsInMinutes string `json:"banEndsInMinutes"`
}

type PredictionOutCome struct {
Expand Down
10 changes: 9 additions & 1 deletion apps/eventsub/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func NewClient(
}

var channels []model.Channels
err := services.Gorm.Where(`"isEnabled" = ? and "isBanned" = ?`, true, false).Find(&channels).Error
err := services.Gorm.Where(
`"isEnabled" = ? and "isBanned" = ?`,
true,
false,
).Find(&channels).Error
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,6 +137,10 @@ func (c *SubClient) SubscribeToNeededEvents(ctx context.Context, userId string)
Version: "1",
Condition: channelCondition,
},
"channel.ban": {
Version: "1",
Condition: channelCondition,
},
}

twitchClient, err := twitch.NewAppClient(*c.services.Config, c.services.Grpc.Tokens)
Expand Down
29 changes: 29 additions & 0 deletions apps/eventsub/internal/handler/ban.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package handler

import (
"context"
"github.com/dnsge/twitch-eventsub-bindings"
"github.com/satont/twir/libs/grpc/generated/events"
)

func (c *Handler) handleBan(
_ *eventsub_bindings.ResponseHeaders,
event *eventsub_bindings.EventChannelBan,
) {
c.services.Grpc.Events.ChannelBan(
context.TODO(), &events.ChannelBanMessage{
BaseInfo: &events.BaseInfo{
ChannelId: event.BroadcasterUserID,
},
UserName: event.UserName,
UserLogin: event.UserLogin,
BroadcasterUserName: event.BroadcasterUserName,
BroadcasterUserLogin: event.BroadcasterUserLogin,
ModeratorUserName: event.ModeratorUserName,
ModeratorUserLogin: event.ModeratorUserLogin,
Reason: event.Reason,
EndsAt: event.EndsAt,
IsPermanent: event.IsPermanent,
},
)
}
1 change: 1 addition & 0 deletions apps/eventsub/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewHandler(services *types.Services) *Handler {
manager.HandleChannelPredictionProgress = myHandler.handleChannelPredictionProgress
manager.HandleChannelPredictionLock = myHandler.handleChannelPredictionLock
manager.HandleChannelPredictionEnd = myHandler.handleChannelPredictionEnd
manager.HandleChannelBan = myHandler.handleBan

return myHandler
}
7 changes: 7 additions & 0 deletions frontend/dashboard/src/components/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
IconStar,
IconPick,
IconGift,
IconUserCancel,
} from '@tabler/icons-vue';
import { FunctionalComponent } from 'vue';

Expand Down Expand Up @@ -215,4 +216,10 @@ export const EVENTS: Record<string, TwirEvent> = {
},
},
},

CHANNEL_BAN: {
name: 'User banned/timeouted',
icon: IconUserCancel,
variables: ['moderatorName', 'moderatorDisplayName', 'banReason', 'banEndsInMinutes'],
},
};
6 changes: 5 additions & 1 deletion frontend/dashboard/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@
"totalUsers": "Total users who voted for winner option",
"totalPoints": "Total used channel points for winner option",
"topUsers": "Top 10 users who voted for winner option"
}
},
"moderatorName": "Moderator login, who triggered action",
"moderatorDisplayName": "Moderator display name, who triggered action",
"banReason": "Reason of ban",
"banEndsInMinutes": "Ban in minutes, or \"permanent\" if permanent ban"
}
},
"overlays": {
Expand Down
1 change: 1 addition & 0 deletions libs/gomodels/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
EventTypePredictionEnd = "PREDICTION_END"
EventTypePredictionLock = "PREDICTION_LOCK"
EventStreamFirstUserJoin = "STREAM_FIRST_USER_JOIN"
EventChannelBan = "CHANNEL_BAN"
)

type Event struct {
Expand Down
Loading

0 comments on commit 18a8adb

Please sign in to comment.