Skip to content

Commit 4c8da03

Browse files
committed
feat(eventsub): add vip add and remove handlers
1 parent 8c12b73 commit 4c8da03

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package mappers
22

33
import (
4+
model "github.com/satont/twir/libs/gomodels"
45
"github.com/twirapp/twir/apps/api-gql/internal/delivery/gql/gqlmodel"
5-
"github.com/twirapp/twir/apps/api-gql/internal/entity"
66
)
77

8-
var eventSubConditionTypeGqlToEntity = map[gqlmodel.EventsubSubscribeConditionInput]entity.EventsubSubscribeCondition{
9-
gqlmodel.EventsubSubscribeConditionInputChannel: entity.EventsubSubscribeConditionChannel,
10-
gqlmodel.EventsubSubscribeConditionInputUser: entity.EventsubSubscribeConditionUser,
11-
gqlmodel.EventsubSubscribeConditionInputChannelWithModeratorID: entity.EventsubSubscribeConditionChannelWithModeratorID,
12-
gqlmodel.EventsubSubscribeConditionInputChannelWithBotID: entity.EventsubSubscribeConditionChannelWithBotID,
8+
var eventSubConditionTypeGqlToEntity = map[gqlmodel.EventsubSubscribeConditionInput]model.EventsubConditionType{
9+
gqlmodel.EventsubSubscribeConditionInputChannel: model.EventsubConditionTypeBroadcasterUserID,
10+
gqlmodel.EventsubSubscribeConditionInputUser: model.EventsubConditionTypeUserID,
11+
gqlmodel.EventsubSubscribeConditionInputChannelWithModeratorID: model.EventsubConditionTypeBroadcasterWithModeratorID,
12+
gqlmodel.EventsubSubscribeConditionInputChannelWithBotID: model.EventsubConditionTypeBroadcasterWithUserID,
1313
}
1414

15-
func ConditionTypeGqlToEntity(conditionType gqlmodel.EventsubSubscribeConditionInput) entity.EventsubSubscribeCondition {
15+
func ConditionTypeGqlToEntity(conditionType gqlmodel.EventsubSubscribeConditionInput) model.EventsubConditionType {
1616
return eventSubConditionTypeGqlToEntity[conditionType]
1717
}

apps/api-gql/internal/services/admin-actions/admin-actions.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/redis/go-redis/v9"
88
"github.com/samber/lo"
99
model "github.com/satont/twir/libs/gomodels"
10-
"github.com/twirapp/twir/apps/api-gql/internal/entity"
1110
buscore "github.com/twirapp/twir/libs/bus-core"
1211
"github.com/twirapp/twir/libs/bus-core/eventsub"
1312
"github.com/twirapp/twir/libs/bus-core/timers"
@@ -68,9 +67,9 @@ func (c *Service) DropAllAuthSessions(ctx context.Context) error {
6867
}
6968

7069
type EventSubSubscribeInput struct {
71-
Type string `json:"type"`
72-
Version string `json:"version"`
73-
Condition entity.EventsubSubscribeCondition `json:"condition"`
70+
Type string `json:"type"`
71+
Version string `json:"version"`
72+
Condition model.EventsubConditionType `json:"condition"`
7473
}
7574

7675
func (c *Service) EventSubSubscribe(ctx context.Context, input EventSubSubscribeInput) error {

apps/eventsub/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ require (
3232
github.com/twirapp/twir/libs/grpc v0.0.0-20241105005614-f68dbf11ade1
3333
github.com/twirapp/twir/libs/integrations v0.0.0-20241105005614-f68dbf11ade1
3434
github.com/twirapp/twir/libs/uptrace v0.0.0-20241105005614-f68dbf11ade1
35-
github.com/twirapp/twitch-eventsub-framework v1.3.7
35+
github.com/twirapp/twitch-eventsub-framework v1.3.8
3636
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0
3737
go.opentelemetry.io/otel v1.33.0
3838
go.opentelemetry.io/otel/trace v1.33.0

apps/eventsub/go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,22 @@ github.com/twirapp/twir/libs/cache v0.0.0-20241105005614-f68dbf11ade1 h1:LfNuHsD
152152
github.com/twirapp/twir/libs/cache v0.0.0-20241105005614-f68dbf11ade1/go.mod h1:h4TtQax+U9vXDy815YWOnggdD82xK2tEln2/YfRYVbI=
153153
github.com/twirapp/twitch-eventsub-framework v1.3.7 h1:HwpolVPgZRVCJCD+AzlLol2nRZId+lLLR41qXilDYKY=
154154
github.com/twirapp/twitch-eventsub-framework v1.3.7/go.mod h1:G67t1kwDexqC38efhh97md/HcnEZFqPFs+B1HeZkBtc=
155+
github.com/twirapp/twitch-eventsub-framework v1.3.8 h1:ODAFrPYVxLq4R/bFBhNF8laRnRkBoqNRH1VxXlOQFK4=
156+
github.com/twirapp/twitch-eventsub-framework v1.3.8/go.mod h1:G67t1kwDexqC38efhh97md/HcnEZFqPFs+B1HeZkBtc=
155157
github.com/uptrace/uptrace-go v1.31.0 h1:i+AsiXVZ5NHHYWvlmJyTqg5l9sLqYN/KSZeLWr3kAM8=
156158
github.com/uptrace/uptrace-go v1.31.0/go.mod h1:zX5JqvEoLpFbbtGN+G1/vf276rfoHiTFrh7yt1XdVCY=
157159
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
158160
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
159161
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
162+
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
160163
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0 h1:yMkBS9yViCc7U7yeLzJPM2XizlfdVvBRSmsQDWu6qc0=
161164
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.56.0/go.mod h1:n8MR6/liuGB5EmTETUBeU5ZgqMOlqKRxUaqPQBOANZ8=
162165
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 h1:yd02MEjBdJkG3uabWP9apV+OuWRIXGDuJEUJbOHmCFU=
166+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0/go.mod h1:umTcuxiv1n/s/S6/c2AT/g2CQ7u5C59sHDNmfSwgz7Q=
163167
go.opentelemetry.io/contrib/instrumentation/runtime v0.56.0 h1:s7wHG+t8bEoH7ibWk1nk682h7EoWLJ5/8j+TSO3bX/o=
164168
go.opentelemetry.io/contrib/instrumentation/runtime v0.56.0/go.mod h1:Q8Hsv3d9DwryfIl+ebj4mY81IYVRSPy4QfxroVZwqLo=
165169
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
170+
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
166171
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0 h1:mMOmtYie9Fx6TSVzw4W+NTpvoaS1JWWga37oI1a/4qQ=
167172
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.7.0/go.mod h1:yy7nDsMMBUkD+jeekJ36ur5f3jJIrmCwUrY67VFhNpA=
168173
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0 h1:ZsXq73BERAiNuuFXYqP4MR5hBrjXfMGSO+Cx7qoOZiM=
@@ -176,13 +181,15 @@ go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0/go.mod h1:fcwWuDua
176181
go.opentelemetry.io/otel/log v0.7.0 h1:d1abJc0b1QQZADKvfe9JqqrfmPYQCz2tUSO+0XZmuV4=
177182
go.opentelemetry.io/otel/log v0.7.0/go.mod h1:2jf2z7uVfnzDNknKTO9G+ahcOAyWcp1fJmk/wJjULRo=
178183
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
184+
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
179185
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
180186
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
181187
go.opentelemetry.io/otel/sdk/log v0.7.0 h1:dXkeI2S0MLc5g0/AwxTZv6EUEjctiH8aG14Am56NTmQ=
182188
go.opentelemetry.io/otel/sdk/log v0.7.0/go.mod h1:oIRXpW+WD6M8BuGj5rtS0aRu/86cbDV/dAfNaZBIjYM=
183189
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
184190
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
185191
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
192+
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
186193
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
187194
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
188195
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
@@ -204,18 +211,24 @@ golang.ngrok.com/muxado/v2 v2.0.1/go.mod h1:wzxJYX4xiAtmwumzL+QsukVwFRXmPNv86vB8
204211
golang.ngrok.com/ngrok v1.11.0 h1:lvbBcoOvH+Ek15wgrjvxpCB+PBM7vinU6jQPsrCdOLw=
205212
golang.ngrok.com/ngrok v1.11.0/go.mod h1:1/gLOyOJm7ygHJlcEbtldFLQwQnQ42z+rucpLE2YsvA=
206213
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
214+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
207215
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
208216
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
209217
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
210218
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
211219
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
220+
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
212221
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
222+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
213223
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
214224
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
215225
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
216226
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
227+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
217228
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
229+
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
218230
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
231+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
219232
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
220233
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
221234
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=

apps/eventsub/internal/handler/handler.go

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ func New(opts Opts) *Handler {
123123
handler.HandleChannelPointsRewardAdd = myHandler.handleChannelPointsRewardAdd
124124
handler.HandleChannelPointsRewardUpdate = myHandler.handleChannelPointsRewardUpdate
125125
handler.HandleChannelPointsRewardRemove = myHandler.handleChannelPointsRewardRemove
126+
handler.HandleChannelVipAdd = myHandler.handleChannelVipAdd
127+
handler.HandleChannelVipRemove = myHandler.handleChannelVipRemove
126128

127129
httpHandler := otelhttp.NewHandler(handler, "")
128130

apps/eventsub/internal/handler/vip.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package handler
2+
3+
import (
4+
"log/slog"
5+
6+
model "github.com/satont/twir/libs/gomodels"
7+
eventsub_bindings "github.com/twirapp/twitch-eventsub-framework/esb"
8+
)
9+
10+
func (c *Handler) handleChannelVipAdd(
11+
_ *eventsub_bindings.ResponseHeaders,
12+
event *eventsub_bindings.ChannelVipAdd,
13+
) {
14+
c.logger.Info(
15+
"channel vip add",
16+
slog.String("channelId", event.BroadcasterUserId),
17+
slog.String("userId", event.UserId),
18+
slog.String("userName", event.UserLogin),
19+
)
20+
21+
if err := c.gorm.Model(&model.UsersStats{}).
22+
Where(`"userId" = ? and "channelId" = ?`, event.UserId, event.BroadcasterUserId).
23+
Update(`"is_vip"`, true).Error; err != nil {
24+
c.logger.Error(err.Error(), slog.Any("err", err))
25+
}
26+
}
27+
28+
func (c *Handler) handleChannelVipRemove(
29+
_ *eventsub_bindings.ResponseHeaders,
30+
event *eventsub_bindings.ChannelVipRemove,
31+
) {
32+
c.logger.Info(
33+
"channel vip remove",
34+
slog.String("channelId", event.BroadcasterUserId),
35+
slog.String("userId", event.UserId),
36+
slog.String("userName", event.UserLogin),
37+
)
38+
39+
if err := c.gorm.Model(&model.UsersStats{}).
40+
Where(`"userId" = ? and "channelId" = ?`, event.UserId, event.BroadcasterUserId).
41+
Update(`"is_vip"`, false).Error; err != nil {
42+
c.logger.Error(err.Error(), slog.Any("err", err))
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
SELECT 'up SQL query';
4+
INSERT INTO eventsub_topics (topic, version, condition_type)
5+
VALUES ('channel.vip.add', '1', 'BROADCASTER_USER_ID'),
6+
('channel.vip.remove', '1', 'BROADCASTER_USER_ID');
7+
-- +goose StatementEnd
8+
9+
-- +goose Down
10+
-- +goose StatementBegin
11+
SELECT 'down SQL query';
12+
-- +goose StatementEnd

0 commit comments

Comments
 (0)