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