Live video on the AT Protocol
1package webhook
2
3import (
4 "context"
5 "fmt"
6 "strings"
7
8 "github.com/bluesky-social/indigo/api/bsky"
9 "stream.place/streamplace/pkg/integrations/discord"
10 "stream.place/streamplace/pkg/integrations/discord/discordtypes"
11 "stream.place/streamplace/pkg/streamplace"
12)
13
14// SendChatWebhook sends chat message to a specific webhook
15func SendChatWebhook(ctx context.Context, webhook *streamplace.ServerDefs_Webhook, authorDID string, scm *streamplace.ChatDefs_MessageView) error {
16 // Check if message should be muted
17 if msg, ok := scm.Record.Val.(*streamplace.ChatMessage); ok {
18 if len(webhook.MuteWords) > 0 {
19 messageText := strings.ToLower(msg.Text)
20 for _, muteWord := range webhook.MuteWords {
21 if strings.Contains(messageText, strings.ToLower(muteWord)) {
22 // Message contains a mute word, skip forwarding
23 return nil
24 }
25 }
26 }
27 }
28
29 discordWebhook, err := webhookToDiscordWebhook(webhook)
30 if err != nil {
31 return fmt.Errorf("failed to convert webhook: %w", err)
32 }
33
34 return discord.SendChat(ctx, discordWebhook, authorDID, scm)
35}
36
37// SendLivestreamWebhook sends livestream notification to a specific webhook
38func SendLivestreamWebhook(ctx context.Context, webhook *streamplace.ServerDefs_Webhook, pdsURL string, lsv *streamplace.Livestream_LivestreamView, postView *bsky.FeedDefs_PostView, spcp *streamplace.ChatProfile) error {
39 discordWebhook, err := webhookToDiscordWebhook(webhook)
40 if err != nil {
41 return fmt.Errorf("failed to convert webhook: %w", err)
42 }
43
44 return discord.SendLivestream(ctx, discordWebhook, pdsURL, lsv, postView, spcp)
45}
46
47// webhookToDiscordWebhook converts streamplace.ServerDefs_Webhook to discordtypes.Webhook
48func webhookToDiscordWebhook(webhook *streamplace.ServerDefs_Webhook) (*discordtypes.Webhook, error) {
49 var rewriteRules []*discordtypes.WebhookRewrite
50 for _, rule := range webhook.Rewrite {
51 rewriteRules = append(rewriteRules, &discordtypes.WebhookRewrite{
52 From: rule.From,
53 To: rule.To,
54 })
55 }
56
57 var prefix, suffix string
58 if webhook.Prefix != nil {
59 prefix = *webhook.Prefix
60 }
61 if webhook.Suffix != nil {
62 suffix = *webhook.Suffix
63 }
64
65 return &discordtypes.Webhook{
66 URL: webhook.Url,
67 Prefix: prefix,
68 Suffix: suffix,
69 Rewrite: rewriteRules,
70 }, nil
71}