Only track approved users.

This commit is contained in:
Asaf Gartner 2022-03-27 19:13:47 +03:00
parent b0cf3e2f15
commit febec72325
2 changed files with 36 additions and 9 deletions

View File

@ -22,14 +22,28 @@ type TwitchStreamer struct {
var twitchRegex = regexp.MustCompile(`twitch\.tv/(?P<login>[^/]+)$`)
func FetchTwitchStreamers(ctx context.Context, dbConn db.ConnOrTx) ([]TwitchStreamer, error) {
streamers, err := db.Query(ctx, dbConn, models.Link{},
type linkResult struct {
Link models.Link `db:"link"`
}
streamers, err := db.Query(ctx, dbConn, linkResult{},
`
SELECT $columns
FROM
handmade_links AS link
LEFT JOIN auth_user AS link_owner ON link_owner.id = link.user_id
WHERE
url ~* 'twitch\.tv/([^/]+)$'
url ~* 'twitch\.tv/([^/]+)$' AND
((link.user_id IS NOT NULL AND link_owner.status = $1) OR (link.project_id IS NOT NULL AND
(SELECT COUNT(*)
FROM
handmade_user_projects AS hup
JOIN auth_user AS project_owner ON project_owner.id = hup.user_id
WHERE
hup.project_id = link.project_id AND
project_owner.status != $1
) = 0))
`,
models.UserStatusApproved,
)
if err != nil {
return nil, oops.New(err, "failed to fetch twitch links")
@ -37,7 +51,7 @@ func FetchTwitchStreamers(ctx context.Context, dbConn db.ConnOrTx) ([]TwitchStre
result := make([]TwitchStreamer, 0, len(streamers))
for _, s := range streamers {
dbStreamer := s.(*models.Link)
dbStreamer := s.(*linkResult).Link
streamer := TwitchStreamer{
UserID: dbStreamer.UserID,

View File

@ -11,6 +11,7 @@ import (
"git.handmade.network/hmn/hmn/src/discord"
"git.handmade.network/hmn/hmn/src/hmndata"
"git.handmade.network/hmn/hmn/src/logging"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/oops"
"git.handmade.network/hmn/hmn/src/perf"
"github.com/jackc/pgx/v4/pgxpool"
@ -343,13 +344,13 @@ func syncWithTwitch(ctx context.Context, dbConn *pgxpool.Pool, updateAll bool) {
log.Info().Interface("Stats", stats).Msg("Twitch sync done")
}
func notifyDiscordOfLiveStream(ctx context.Context, dbConn db.ConnOrTx, twitchLogin string) error {
func notifyDiscordOfLiveStream(ctx context.Context, dbConn db.ConnOrTx, twitchLogin string, title string) error {
var err error
if config.Config.Discord.StreamsChannelID != "" {
err = discord.SendMessages(ctx, dbConn, discord.MessageToSend{
ChannelID: config.Config.Discord.StreamsChannelID,
Req: discord.CreateMessageRequest{
Content: fmt.Sprintf("%s is live: https://twitch.tv/%s", twitchLogin, twitchLogin),
Content: fmt.Sprintf("%s is live: https://twitch.tv/%s\n%s", twitchLogin, twitchLogin, title),
},
})
}
@ -390,8 +391,8 @@ func processEventSubNotification(ctx context.Context, dbConn db.ConnOrTx, notifi
if err != nil {
log.Error().Err(err).Msg("failed to update twitch stream status")
}
if inserted && notification.Type == notificationTypeOnline {
err = notifyDiscordOfLiveStream(ctx, dbConn, status.TwitchLogin)
if inserted {
err = notifyDiscordOfLiveStream(ctx, dbConn, status.TwitchLogin, status.Title)
if err != nil {
log.Error().Err(err).Msg("failed to notify discord")
}
@ -401,7 +402,20 @@ func processEventSubNotification(ctx context.Context, dbConn db.ConnOrTx, notifi
func updateStreamStatusInDB(ctx context.Context, conn db.ConnOrTx, status *streamStatus) (bool, error) {
inserted := false
if isStatusRelevant(status) {
_, err := conn.Exec(ctx,
_, err := db.QueryOne(ctx, conn, models.TwitchStream{},
`
SELECT $columns
FROM twitch_streams
WHERE twitch_id = $1
`,
status.TwitchID,
)
if err == db.NotFound {
inserted = true
} else if err != nil {
return false, oops.New(err, "failed to query existing stream")
}
_, err = conn.Exec(ctx,
`
INSERT INTO twitch_streams (twitch_id, twitch_login, title, started_at)
VALUES ($1, $2, $3, $4)
@ -417,7 +431,6 @@ func updateStreamStatusInDB(ctx context.Context, conn db.ConnOrTx, status *strea
if err != nil {
return false, oops.New(err, "failed to insert twitch streamer into db")
}
inserted = true
} else {
_, err := conn.Exec(ctx,
`