66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package discord
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
|
|
"git.handmade.network/hmn/hmn/src/db"
|
|
"git.handmade.network/hmn/hmn/src/models"
|
|
"git.handmade.network/hmn/hmn/src/oops"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// NOTE(ben): This is maybe redundant with the regexes we use for markdown. But
|
|
// do we actually want to reuse those, or should we keep them separate?
|
|
var RESnippetableUrl = regexp.MustCompile(`^https?://(youtu\.be|(www\.)?youtube\.com/watch)`)
|
|
|
|
// Given a Discord message for which we have saved content, this function will
|
|
// return either an asset ID or a URL with which to make a snippet.
|
|
//
|
|
// Be aware that this function may return absolutely nothing.
|
|
func getSnippetAssetOrUrl(ctx context.Context, tx db.ConnOrTx, msgID string) (*uuid.UUID, string, error) {
|
|
// Check attachments
|
|
attachments, err := db.Query(ctx, tx, models.DiscordMessageAttachment{},
|
|
`
|
|
SELECT $columns
|
|
FROM handmade_discordmessageattachment
|
|
WHERE message_id = $1
|
|
`,
|
|
msgID,
|
|
)
|
|
if err != nil {
|
|
return nil, "", oops.New(err, "failed to fetch message attachments")
|
|
}
|
|
for _, iattachment := range attachments {
|
|
attachment := iattachment.(*models.DiscordMessageAttachment)
|
|
return &attachment.AssetID, "", nil
|
|
}
|
|
|
|
// Check embeds
|
|
embeds, err := db.Query(ctx, tx, models.DiscordMessageEmbed{},
|
|
`
|
|
SELECT $columns
|
|
FROM handmade_discordmessageembed
|
|
WHERE message_id = $1
|
|
`,
|
|
msgID,
|
|
)
|
|
if err != nil {
|
|
return nil, "", oops.New(err, "failed to fetch discord embeds")
|
|
}
|
|
for _, iembed := range embeds {
|
|
embed := iembed.(*models.DiscordMessageEmbed)
|
|
if embed.VideoID != nil {
|
|
return embed.VideoID, "", nil
|
|
} else if embed.ImageID != nil {
|
|
return embed.ImageID, "", nil
|
|
} else if embed.URL != nil {
|
|
if RESnippetableUrl.MatchString(*embed.URL) {
|
|
return nil, *embed.URL, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, "", nil
|
|
}
|