Add Twitter video support and use sql.Null[T] for nullable columns
- Store Twitter video URLs in embed_video_url column instead of downloading - Play videos directly from Twitter's CDN in <video> element - Fix Twitter API parsing (content_type/url fields) - Strip t.co URLs from tweet text descriptions - Use sql.Null[T] generic type for nullable DB columns - Add Nullable[T] and Ptr[T] helper functions - Add play indicator overlay for video items in grid - Add migration for embed_video_url column
This commit is contained in:
parent
cdcc5b5293
commit
2887d9c430
8 changed files with 226 additions and 120 deletions
|
|
@ -9,45 +9,63 @@ import (
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Nullable creates a sql.Null[T] from a pointer.
|
||||||
|
func Nullable[T any](v *T) sql.Null[T] {
|
||||||
|
if v == nil {
|
||||||
|
return sql.Null[T]{}
|
||||||
|
}
|
||||||
|
return sql.Null[T]{V: *v, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ptr returns a pointer from sql.Null[T], or nil if not valid.
|
||||||
|
func Ptr[T any](n sql.Null[T]) *T {
|
||||||
|
if !n.Valid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &n.V
|
||||||
|
}
|
||||||
|
|
||||||
type Row struct {
|
type Row struct {
|
||||||
ID int64
|
ID int64
|
||||||
PubID string
|
PubID string
|
||||||
Title *string
|
Title sql.Null[string]
|
||||||
Description *string
|
Description sql.Null[string]
|
||||||
LinkURL *string
|
LinkURL sql.Null[string]
|
||||||
ItemType string // 'image', 'video', 'quote', 'embed'
|
ItemType string // 'image', 'video', 'quote', 'embed'
|
||||||
EmbedProvider *string
|
EmbedProvider sql.Null[string]
|
||||||
EmbedVideoID *string
|
EmbedVideoID sql.Null[string]
|
||||||
EmbedHTML *string
|
EmbedHTML sql.Null[string]
|
||||||
|
EmbedVideoURL sql.Null[string]
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
DeletedAt *time.Time
|
DeletedAt sql.Null[time.Time]
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateParams struct {
|
type CreateParams struct {
|
||||||
Title *string
|
Title sql.Null[string]
|
||||||
Description *string
|
Description sql.Null[string]
|
||||||
LinkURL *string
|
LinkURL sql.Null[string]
|
||||||
ItemType string
|
ItemType string
|
||||||
EmbedProvider *string
|
EmbedProvider sql.Null[string]
|
||||||
EmbedVideoID *string
|
EmbedVideoID sql.Null[string]
|
||||||
EmbedHTML *string
|
EmbedHTML sql.Null[string]
|
||||||
|
EmbedVideoURL sql.Null[string]
|
||||||
}
|
}
|
||||||
|
|
||||||
// QCreate creates a new item.
|
// QCreate creates a new item.
|
||||||
func QCreate(ctx context.Context, db *sql.DB, p CreateParams) (Row, error) {
|
func QCreate(ctx context.Context, db *sql.DB, p CreateParams) (Row, error) {
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO item (title, description, link_url, item_type, embed_provider, embed_video_id, embed_html)
|
INSERT INTO item (title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, embed_video_url)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
RETURNING id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, created_at, deleted_at
|
RETURNING id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, embed_video_url, created_at, deleted_at
|
||||||
`
|
`
|
||||||
|
|
||||||
var row Row
|
var row Row
|
||||||
var pubID pgtype.UUID
|
var pubID pgtype.UUID
|
||||||
err := db.QueryRowContext(ctx, query,
|
err := db.QueryRowContext(ctx, query,
|
||||||
p.Title, p.Description, p.LinkURL, p.ItemType, p.EmbedProvider, p.EmbedVideoID, p.EmbedHTML,
|
p.Title, p.Description, p.LinkURL, p.ItemType, p.EmbedProvider, p.EmbedVideoID, p.EmbedHTML, p.EmbedVideoURL,
|
||||||
).Scan(
|
).Scan(
|
||||||
&row.ID, &pubID, &row.Title, &row.Description, &row.LinkURL,
|
&row.ID, &pubID, &row.Title, &row.Description, &row.LinkURL,
|
||||||
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML,
|
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML, &row.EmbedVideoURL,
|
||||||
&row.CreatedAt, &row.DeletedAt,
|
&row.CreatedAt, &row.DeletedAt,
|
||||||
)
|
)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -59,7 +77,7 @@ func QCreate(ctx context.Context, db *sql.DB, p CreateParams) (Row, error) {
|
||||||
// QFindByPubID finds an item by its public ID.
|
// QFindByPubID finds an item by its public ID.
|
||||||
func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, created_at, deleted_at
|
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, embed_video_url, created_at, deleted_at
|
||||||
FROM item
|
FROM item
|
||||||
WHERE pub_id = $1
|
WHERE pub_id = $1
|
||||||
`
|
`
|
||||||
|
|
@ -68,7 +86,7 @@ func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
||||||
var pubUUID pgtype.UUID
|
var pubUUID pgtype.UUID
|
||||||
err := db.QueryRowContext(ctx, query, pubID).Scan(
|
err := db.QueryRowContext(ctx, query, pubID).Scan(
|
||||||
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
||||||
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML,
|
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML, &row.EmbedVideoURL,
|
||||||
&row.CreatedAt, &row.DeletedAt,
|
&row.CreatedAt, &row.DeletedAt,
|
||||||
)
|
)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|
@ -84,7 +102,7 @@ func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
||||||
// QFindByID finds an item by its internal ID.
|
// QFindByID finds an item by its internal ID.
|
||||||
func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, created_at, deleted_at
|
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, embed_video_url, created_at, deleted_at
|
||||||
FROM item
|
FROM item
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
@ -93,7 +111,7 @@ func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
||||||
var pubUUID pgtype.UUID
|
var pubUUID pgtype.UUID
|
||||||
err := db.QueryRowContext(ctx, query, id).Scan(
|
err := db.QueryRowContext(ctx, query, id).Scan(
|
||||||
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
||||||
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML,
|
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML, &row.EmbedVideoURL,
|
||||||
&row.CreatedAt, &row.DeletedAt,
|
&row.CreatedAt, &row.DeletedAt,
|
||||||
)
|
)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
|
@ -109,7 +127,7 @@ func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
||||||
// QList returns all non-deleted items, newest first.
|
// QList returns all non-deleted items, newest first.
|
||||||
func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, created_at, deleted_at
|
SELECT id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, embed_video_url, created_at, deleted_at
|
||||||
FROM item
|
FROM item
|
||||||
WHERE deleted_at IS NULL
|
WHERE deleted_at IS NULL
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
|
|
@ -127,7 +145,7 @@ func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
||||||
var pubUUID pgtype.UUID
|
var pubUUID pgtype.UUID
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
||||||
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML,
|
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML, &row.EmbedVideoURL,
|
||||||
&row.CreatedAt, &row.DeletedAt,
|
&row.CreatedAt, &row.DeletedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -141,7 +159,7 @@ func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
||||||
// QListByTag returns all non-deleted items with a specific tag, newest first.
|
// QListByTag returns all non-deleted items with a specific tag, newest first.
|
||||||
func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error) {
|
func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT i.id, i.pub_id, i.title, i.description, i.link_url, i.item_type, i.embed_provider, i.embed_video_id, i.embed_html, i.created_at, i.deleted_at
|
SELECT i.id, i.pub_id, i.title, i.description, i.link_url, i.item_type, i.embed_provider, i.embed_video_id, i.embed_html, i.embed_video_url, i.created_at, i.deleted_at
|
||||||
FROM item i
|
FROM item i
|
||||||
JOIN item_tag it ON i.id = it.item_id
|
JOIN item_tag it ON i.id = it.item_id
|
||||||
JOIN tag t ON it.tag_id = t.id
|
JOIN tag t ON it.tag_id = t.id
|
||||||
|
|
@ -161,7 +179,7 @@ func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error)
|
||||||
var pubUUID pgtype.UUID
|
var pubUUID pgtype.UUID
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
&row.ID, &pubUUID, &row.Title, &row.Description, &row.LinkURL,
|
||||||
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML,
|
&row.ItemType, &row.EmbedProvider, &row.EmbedVideoID, &row.EmbedHTML, &row.EmbedVideoURL,
|
||||||
&row.CreatedAt, &row.DeletedAt,
|
&row.CreatedAt, &row.DeletedAt,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -173,9 +191,9 @@ func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateParams struct {
|
type UpdateParams struct {
|
||||||
Title *string
|
Title sql.Null[string]
|
||||||
Description *string
|
Description sql.Null[string]
|
||||||
LinkURL *string
|
LinkURL sql.Null[string]
|
||||||
}
|
}
|
||||||
|
|
||||||
// QUpdate updates an item's editable fields.
|
// QUpdate updates an item's editable fields.
|
||||||
|
|
@ -196,6 +214,13 @@ func QUpdateType(ctx context.Context, db *sql.DB, id int64, itemType string) err
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QUpdateVideoURL updates an item's embed video URL.
|
||||||
|
func QUpdateVideoURL(ctx context.Context, db *sql.DB, id int64, videoURL string) error {
|
||||||
|
query := `UPDATE item SET embed_video_url = $2 WHERE id = $1`
|
||||||
|
_, err := db.ExecContext(ctx, query, id, videoURL)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// QSoftDelete soft deletes an item.
|
// QSoftDelete soft deletes an item.
|
||||||
func QSoftDelete(ctx context.Context, db *sql.DB, id int64) error {
|
func QSoftDelete(ctx context.Context, db *sql.DB, id int64) error {
|
||||||
query := `UPDATE item SET deleted_at = NOW() WHERE id = $1`
|
query := `UPDATE item SET deleted_at = NOW() WHERE id = $1`
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -27,12 +28,14 @@ type VideoInfo struct {
|
||||||
Description string
|
Description string
|
||||||
ThumbnailURL string
|
ThumbnailURL string
|
||||||
EmbedHTML string
|
EmbedHTML string
|
||||||
|
VideoURL string // Direct video URL (for Twitter videos)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
youtubeRegex = regexp.MustCompile(`(?:youtube\.com/(?:watch\?v=|embed/|v/|shorts/)|youtu\.be/)([a-zA-Z0-9_-]{11})`)
|
youtubeRegex = regexp.MustCompile(`(?:youtube\.com/(?:watch\?v=|embed/|v/|shorts/)|youtu\.be/)([a-zA-Z0-9_-]{11})`)
|
||||||
vimeoRegex = regexp.MustCompile(`(?:vimeo\.com/(?:video/)?|player\.vimeo\.com/video/)(\d+)`)
|
vimeoRegex = regexp.MustCompile(`(?:vimeo\.com/(?:video/)?|player\.vimeo\.com/video/)(\d+)`)
|
||||||
twitterRegex = regexp.MustCompile(`(?:twitter\.com|x\.com)/([^/]+)/status/(\d+)`)
|
twitterRegex = regexp.MustCompile(`(?:twitter\.com|x\.com)/([^/]+)/status/(\d+)`)
|
||||||
|
tcoRegex = regexp.MustCompile(`\s*https://t\.co/\S+`)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Detect checks if a URL is a YouTube, Vimeo, or Twitter/X post and returns its info.
|
// Detect checks if a URL is a YouTube, Vimeo, or Twitter/X post and returns its info.
|
||||||
|
|
@ -121,6 +124,13 @@ type twitterSyndicationResponse struct {
|
||||||
MediaDetails []struct {
|
MediaDetails []struct {
|
||||||
MediaURLHTTPS string `json:"media_url_https"`
|
MediaURLHTTPS string `json:"media_url_https"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
VideoInfo struct {
|
||||||
|
Variants []struct {
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
Bitrate int `json:"bitrate,omitempty"`
|
||||||
|
} `json:"variants"`
|
||||||
|
} `json:"video_info"`
|
||||||
} `json:"mediaDetails"`
|
} `json:"mediaDetails"`
|
||||||
Video struct {
|
Video struct {
|
||||||
Poster string `json:"poster"`
|
Poster string `json:"poster"`
|
||||||
|
|
@ -152,13 +162,26 @@ func fetchTwitter(ctx context.Context, tweetID string, originalURL string) (*Vid
|
||||||
return nil, fmt.Errorf("twitter syndication decode: %w", err)
|
return nil, fmt.Errorf("twitter syndication decode: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find thumbnail - prefer photos, then video poster
|
// Find thumbnail and video URL from media
|
||||||
var thumbnailURL string
|
var thumbnailURL, videoURL string
|
||||||
if len(tweet.Photos) > 0 {
|
if len(tweet.Photos) > 0 {
|
||||||
thumbnailURL = tweet.Photos[0].URL
|
thumbnailURL = tweet.Photos[0].URL
|
||||||
} else if len(tweet.MediaDetails) > 0 {
|
} else if len(tweet.MediaDetails) > 0 {
|
||||||
thumbnailURL = tweet.MediaDetails[0].MediaURLHTTPS
|
media := tweet.MediaDetails[0]
|
||||||
} else if tweet.Video.Poster != "" {
|
thumbnailURL = media.MediaURLHTTPS
|
||||||
|
|
||||||
|
// Extract video URL - find highest bitrate MP4
|
||||||
|
if media.Type == "video" || media.Type == "animated_gif" {
|
||||||
|
var bestBitrate int
|
||||||
|
for _, v := range media.VideoInfo.Variants {
|
||||||
|
if v.ContentType == "video/mp4" && v.Bitrate >= bestBitrate {
|
||||||
|
bestBitrate = v.Bitrate
|
||||||
|
videoURL = v.URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if thumbnailURL == "" && tweet.Video.Poster != "" {
|
||||||
thumbnailURL = tweet.Video.Poster
|
thumbnailURL = tweet.Video.Poster
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -173,12 +196,17 @@ func fetchTwitter(ctx context.Context, tweetID string, originalURL string) (*Vid
|
||||||
title = fmt.Sprintf("%s (@%s)", tweet.User.Name, tweet.User.ScreenName)
|
title = fmt.Sprintf("%s (@%s)", tweet.User.Name, tweet.User.ScreenName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up tweet text - remove trailing t.co URLs
|
||||||
|
description := tcoRegex.ReplaceAllString(tweet.Text, "")
|
||||||
|
description = strings.TrimSpace(description)
|
||||||
|
|
||||||
return &VideoInfo{
|
return &VideoInfo{
|
||||||
Provider: ProviderTwitter,
|
Provider: ProviderTwitter,
|
||||||
VideoID: tweetID,
|
VideoID: tweetID,
|
||||||
Title: title,
|
Title: title,
|
||||||
Description: tweet.Text,
|
Description: description,
|
||||||
ThumbnailURL: thumbnailURL,
|
ThumbnailURL: thumbnailURL,
|
||||||
|
VideoURL: videoURL,
|
||||||
EmbedHTML: embedHTML,
|
EmbedHTML: embedHTML,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -118,13 +118,13 @@ func HandleCreateItem(rc *RequestContext, w http.ResponseWriter, r *http.Request
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||||
Title: req.Title,
|
Title: item.Nullable(req.Title),
|
||||||
Description: req.Description,
|
Description: item.Nullable(req.Description),
|
||||||
LinkURL: req.LinkURL,
|
LinkURL: item.Nullable(req.LinkURL),
|
||||||
ItemType: req.ItemType,
|
ItemType: req.ItemType,
|
||||||
EmbedProvider: req.EmbedProvider,
|
EmbedProvider: item.Nullable(req.EmbedProvider),
|
||||||
EmbedVideoID: req.EmbedVideoID,
|
EmbedVideoID: item.Nullable(req.EmbedVideoID),
|
||||||
EmbedHTML: req.EmbedHTML,
|
EmbedHTML: item.Nullable(req.EmbedHTML),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -173,9 +173,9 @@ func HandleUpdateItem(rc *RequestContext, w http.ResponseWriter, r *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
|
if err := item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
|
||||||
Title: req.Title,
|
Title: item.Nullable(req.Title),
|
||||||
Description: req.Description,
|
Description: item.Nullable(req.Description),
|
||||||
LinkURL: req.LinkURL,
|
LinkURL: item.Nullable(req.LinkURL),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -241,11 +241,11 @@ func buildItemResponse(ctx context.Context, rc *RequestContext, it item.Row) (it
|
||||||
|
|
||||||
resp := itemResponse{
|
resp := itemResponse{
|
||||||
ID: it.PubID,
|
ID: it.PubID,
|
||||||
Title: it.Title,
|
Title: item.Ptr(it.Title),
|
||||||
Description: it.Description,
|
Description: item.Ptr(it.Description),
|
||||||
LinkURL: it.LinkURL,
|
LinkURL: item.Ptr(it.LinkURL),
|
||||||
ItemType: it.ItemType,
|
ItemType: it.ItemType,
|
||||||
EmbedHTML: it.EmbedHTML,
|
EmbedHTML: item.Ptr(it.EmbedHTML),
|
||||||
Tags: tagNames,
|
Tags: tagNames,
|
||||||
CreatedAt: it.CreatedAt.Format(time.RFC3339),
|
CreatedAt: it.CreatedAt.Format(time.RFC3339),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
@ -17,6 +18,22 @@ import (
|
||||||
"lookbook/internal/video"
|
"lookbook/internal/video"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// strOrNil returns nil if s is empty, otherwise a pointer to s.
|
||||||
|
func strOrNil(s string) *string {
|
||||||
|
if s == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
// nullStr creates a sql.Null[string] from a string (empty string = invalid).
|
||||||
|
func nullStr(s string) sql.Null[string] {
|
||||||
|
if s == "" {
|
||||||
|
return sql.Null[string]{}
|
||||||
|
}
|
||||||
|
return sql.Null[string]{V: s, Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
type urlMetadata struct {
|
type urlMetadata struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
|
|
@ -26,9 +43,9 @@ type urlMetadata struct {
|
||||||
Provider string
|
Provider string
|
||||||
VideoID string
|
VideoID string
|
||||||
EmbedHTML string
|
EmbedHTML string
|
||||||
|
VideoURL string // Direct video URL (for Twitter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchURLMetadata fetches metadata for a URL, trying embed detection first, then OpenGraph
|
|
||||||
func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
|
func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
|
||||||
// Check if it's a YouTube/Vimeo/Twitter embed
|
// Check if it's a YouTube/Vimeo/Twitter embed
|
||||||
videoInfo, err := embed.Detect(ctx, url)
|
videoInfo, err := embed.Detect(ctx, url)
|
||||||
|
|
@ -41,6 +58,7 @@ func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
|
||||||
Provider: string(videoInfo.Provider),
|
Provider: string(videoInfo.Provider),
|
||||||
VideoID: videoInfo.VideoID,
|
VideoID: videoInfo.VideoID,
|
||||||
EmbedHTML: videoInfo.EmbedHTML,
|
EmbedHTML: videoInfo.EmbedHTML,
|
||||||
|
VideoURL: videoInfo.VideoURL,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,33 +169,34 @@ func HandleCreateFromLink(rc *RequestContext, w http.ResponseWriter, r *http.Req
|
||||||
itemType = "link"
|
itemType = "link"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For embeds, fetch thumbnail and video URL
|
||||||
|
var imageURL, videoURL string
|
||||||
|
if req.ImageURL != nil {
|
||||||
|
imageURL = *req.ImageURL
|
||||||
|
}
|
||||||
|
if itemType == "embed" && embedProvider != nil {
|
||||||
|
if videoInfo, err := embed.Detect(ctx, req.URL); err == nil && videoInfo != nil {
|
||||||
|
imageURL = videoInfo.ThumbnailURL
|
||||||
|
videoURL = videoInfo.VideoURL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create the item
|
// Create the item
|
||||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||||
Title: req.Title,
|
Title: item.Nullable(req.Title),
|
||||||
Description: req.Description,
|
Description: item.Nullable(req.Description),
|
||||||
LinkURL: &req.URL,
|
LinkURL: item.Nullable(&req.URL),
|
||||||
ItemType: itemType,
|
ItemType: itemType,
|
||||||
EmbedProvider: embedProvider,
|
EmbedProvider: item.Nullable(embedProvider),
|
||||||
EmbedVideoID: embedVideoID,
|
EmbedVideoID: item.Nullable(embedVideoID),
|
||||||
EmbedHTML: embedHTML,
|
EmbedHTML: item.Nullable(embedHTML),
|
||||||
|
EmbedVideoURL: nullStr(videoURL),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download and store image if available
|
// Download and store thumbnail
|
||||||
var imageURL string
|
|
||||||
if req.ImageURL != nil {
|
|
||||||
imageURL = *req.ImageURL
|
|
||||||
}
|
|
||||||
|
|
||||||
// For embeds, fetch thumbnail
|
|
||||||
if itemType == "embed" && embedProvider != nil {
|
|
||||||
if videoInfo, err := embed.Detect(ctx, req.URL); err == nil && videoInfo != nil {
|
|
||||||
imageURL = videoInfo.ThumbnailURL
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if imageURL != "" {
|
if imageURL != "" {
|
||||||
imgData, contentType, err := opengraph.DownloadImage(ctx, imageURL)
|
imgData, contentType, err := opengraph.DownloadImage(ctx, imageURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -277,17 +296,9 @@ func HandleUpload(rc *RequestContext, w http.ResponseWriter, r *http.Request) er
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create item
|
// Create item
|
||||||
var titlePtr, descPtr *string
|
|
||||||
if title != "" {
|
|
||||||
titlePtr = &title
|
|
||||||
}
|
|
||||||
if description != "" {
|
|
||||||
descPtr = &description
|
|
||||||
}
|
|
||||||
|
|
||||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||||
Title: titlePtr,
|
Title: item.Nullable(strOrNil(title)),
|
||||||
Description: descPtr,
|
Description: item.Nullable(strOrNil(description)),
|
||||||
ItemType: itemType,
|
ItemType: itemType,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -359,9 +370,9 @@ func HandleCreateQuote(rc *RequestContext, w http.ResponseWriter, r *http.Reques
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
|
||||||
Title: req.Source,
|
Title: item.Nullable(req.Source),
|
||||||
Description: &req.Text,
|
Description: nullStr(req.Text),
|
||||||
LinkURL: req.SourceURL,
|
LinkURL: item.Nullable(req.SourceURL),
|
||||||
ItemType: "quote",
|
ItemType: "quote",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -517,32 +528,28 @@ func HandleRefreshMetadata(rc *RequestContext, w http.ResponseWriter, r *http.Re
|
||||||
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
return writeJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
||||||
}
|
}
|
||||||
|
|
||||||
if it.LinkURL == nil || *it.LinkURL == "" {
|
if !it.LinkURL.Valid || it.LinkURL.V == "" {
|
||||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": "item has no link URL"})
|
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": "item has no link URL"})
|
||||||
}
|
}
|
||||||
|
|
||||||
meta, err := fetchURLMetadata(ctx, *it.LinkURL)
|
meta, err := fetchURLMetadata(ctx, it.LinkURL.V)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("failed to fetch: %v", err)})
|
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("failed to fetch: %v", err)})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update title and description
|
// Update title, description, and video URL
|
||||||
var titlePtr, descPtr *string
|
|
||||||
if meta.Title != "" {
|
|
||||||
titlePtr = &meta.Title
|
|
||||||
}
|
|
||||||
if meta.Description != "" {
|
|
||||||
descPtr = &meta.Description
|
|
||||||
}
|
|
||||||
item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
|
item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
|
||||||
Title: titlePtr,
|
Title: nullStr(meta.Title),
|
||||||
Description: descPtr,
|
Description: nullStr(meta.Description),
|
||||||
LinkURL: it.LinkURL,
|
LinkURL: it.LinkURL,
|
||||||
})
|
})
|
||||||
|
if meta.VideoURL != "" {
|
||||||
|
item.QUpdateVideoURL(ctx, rc.DB, it.ID, meta.VideoURL)
|
||||||
|
}
|
||||||
|
|
||||||
// Download and replace thumbnail
|
// Download and replace thumbnail
|
||||||
if meta.ImageURL != "" {
|
if meta.ImageURL != "" {
|
||||||
// Delete existing media for this item
|
// Delete existing thumbnails
|
||||||
media.QDeleteByItemID(ctx, rc.DB, it.ID)
|
media.QDeleteByItemID(ctx, rc.DB, it.ID)
|
||||||
|
|
||||||
imgData, contentType, err := opengraph.DownloadImage(ctx, meta.ImageURL)
|
imgData, contentType, err := opengraph.DownloadImage(ctx, meta.ImageURL)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.soup.land/soup/sxgo/ssr"
|
"git.soup.land/soup/sxgo/ssr"
|
||||||
|
|
@ -29,6 +30,7 @@ type homeItem struct {
|
||||||
Tags []string
|
Tags []string
|
||||||
ThumbnailID *int64
|
ThumbnailID *int64
|
||||||
MediaID *int64
|
MediaID *int64
|
||||||
|
HasVideo bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h homeContent) Render(sw *ssr.Writer) error {
|
func (h homeContent) Render(sw *ssr.Writer) error {
|
||||||
|
|
@ -57,8 +59,10 @@ func (h homeContent) Render(sw *ssr.Writer) error {
|
||||||
</div>
|
</div>
|
||||||
{{else if .ThumbnailID}}
|
{{else if .ThumbnailID}}
|
||||||
<img src="/media/{{.ThumbnailID}}" alt="{{if .Title}}{{.Title}}{{else}}Image{{end}}" loading="lazy">
|
<img src="/media/{{.ThumbnailID}}" alt="{{if .Title}}{{.Title}}{{else}}Image{{end}}" loading="lazy">
|
||||||
|
{{if or .HasVideo (eq .ItemType "video")}}<div class="play-indicator">▶</div>{{end}}
|
||||||
{{else if .MediaID}}
|
{{else if .MediaID}}
|
||||||
<img src="/media/{{.MediaID}}" alt="{{if .Title}}{{.Title}}{{else}}Image{{end}}" loading="lazy">
|
<img src="/media/{{.MediaID}}" alt="{{if .Title}}{{.Title}}{{else}}Image{{end}}" loading="lazy">
|
||||||
|
{{if or .HasVideo (eq .ItemType "video")}}<div class="play-indicator">▶</div>{{end}}
|
||||||
{{else if eq .ItemType "embed"}}
|
{{else if eq .ItemType "embed"}}
|
||||||
<div class="embed-placeholder">
|
<div class="embed-placeholder">
|
||||||
<span>▶</span>
|
<span>▶</span>
|
||||||
|
|
@ -160,11 +164,11 @@ func HandleHome(rc *RequestContext, w http.ResponseWriter, r *http.Request) erro
|
||||||
for _, it := range items {
|
for _, it := range items {
|
||||||
hi := homeItem{
|
hi := homeItem{
|
||||||
ID: it.PubID,
|
ID: it.PubID,
|
||||||
Title: it.Title,
|
Title: item.Ptr(it.Title),
|
||||||
Description: it.Description,
|
Description: item.Ptr(it.Description),
|
||||||
LinkURL: it.LinkURL,
|
LinkURL: item.Ptr(it.LinkURL),
|
||||||
ItemType: it.ItemType,
|
ItemType: it.ItemType,
|
||||||
EmbedHTML: it.EmbedHTML,
|
EmbedHTML: item.Ptr(it.EmbedHTML),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get tags
|
// Get tags
|
||||||
|
|
@ -187,8 +191,16 @@ func HandleHome(rc *RequestContext, w http.ResponseWriter, r *http.Request) erro
|
||||||
hi.ThumbnailID = &m.ID
|
hi.ThumbnailID = &m.ID
|
||||||
} else if m.MediaType == "original" {
|
} else if m.MediaType == "original" {
|
||||||
hi.MediaID = &m.ID
|
hi.MediaID = &m.ID
|
||||||
|
if strings.HasPrefix(m.ContentType, "video/") {
|
||||||
|
hi.HasVideo = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also check for embed video URL
|
||||||
|
if it.EmbedVideoURL.Valid && it.EmbedVideoURL.V != "" {
|
||||||
|
hi.HasVideo = true
|
||||||
|
}
|
||||||
|
|
||||||
homeItems = append(homeItems, hi)
|
homeItems = append(homeItems, hi)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.soup.land/soup/sxgo/ssr"
|
"git.soup.land/soup/sxgo/ssr"
|
||||||
|
|
@ -24,10 +25,12 @@ type itemPageData struct {
|
||||||
LinkURL *string
|
LinkURL *string
|
||||||
ItemType string
|
ItemType string
|
||||||
EmbedHTML *string
|
EmbedHTML *string
|
||||||
|
EmbedVideoURL *string
|
||||||
Tags []string
|
Tags []string
|
||||||
CreatedAt string
|
CreatedAt string
|
||||||
ThumbnailID *int64
|
ThumbnailID *int64
|
||||||
MediaID *int64
|
MediaID *int64
|
||||||
|
MediaIsVideo bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c itemPageContent) Render(sw *ssr.Writer) error {
|
func (c itemPageContent) Render(sw *ssr.Writer) error {
|
||||||
|
|
@ -48,7 +51,13 @@ func (c itemPageContent) Render(sw *ssr.Writer) error {
|
||||||
</video>
|
</video>
|
||||||
</div>
|
</div>
|
||||||
{{else if eq .Item.ItemType "embed"}}
|
{{else if eq .Item.ItemType "embed"}}
|
||||||
{{if .Item.ThumbnailID}}
|
{{if .Item.EmbedVideoURL}}
|
||||||
|
<div class="video-container">
|
||||||
|
<video controls poster="{{if .Item.ThumbnailID}}/media/{{.Item.ThumbnailID}}{{end}}">
|
||||||
|
<source src="{{.Item.EmbedVideoURL}}" type="video/mp4">
|
||||||
|
</video>
|
||||||
|
</div>
|
||||||
|
{{else if .Item.ThumbnailID}}
|
||||||
<div class="image-container">
|
<div class="image-container">
|
||||||
<img src="/media/{{.Item.ThumbnailID}}" alt="{{if .Item.Title}}{{.Item.Title}}{{else}}Embed{{end}}">
|
<img src="/media/{{.Item.ThumbnailID}}" alt="{{if .Item.Title}}{{.Item.Title}}{{else}}Embed{{end}}">
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -162,6 +171,7 @@ func HandleItemPage(rc *RequestContext, w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
// Get media
|
// Get media
|
||||||
var thumbnailID, mediaID *int64
|
var thumbnailID, mediaID *int64
|
||||||
|
var mediaIsVideo bool
|
||||||
mediaList, err := media.QFindByItemID(ctx, rc.DB, it.ID)
|
mediaList, err := media.QFindByItemID(ctx, rc.DB, it.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -171,28 +181,31 @@ func HandleItemPage(rc *RequestContext, w http.ResponseWriter, r *http.Request)
|
||||||
thumbnailID = &m.ID
|
thumbnailID = &m.ID
|
||||||
} else if m.MediaType == "original" {
|
} else if m.MediaType == "original" {
|
||||||
mediaID = &m.ID
|
mediaID = &m.ID
|
||||||
|
mediaIsVideo = strings.HasPrefix(m.ContentType, "video/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data := itemPageData{
|
data := itemPageData{
|
||||||
ID: it.PubID,
|
ID: it.PubID,
|
||||||
Title: it.Title,
|
Title: item.Ptr(it.Title),
|
||||||
Description: it.Description,
|
Description: item.Ptr(it.Description),
|
||||||
LinkURL: it.LinkURL,
|
LinkURL: item.Ptr(it.LinkURL),
|
||||||
ItemType: it.ItemType,
|
ItemType: it.ItemType,
|
||||||
EmbedHTML: it.EmbedHTML,
|
EmbedHTML: item.Ptr(it.EmbedHTML),
|
||||||
|
EmbedVideoURL: item.Ptr(it.EmbedVideoURL),
|
||||||
Tags: tagNames,
|
Tags: tagNames,
|
||||||
CreatedAt: it.CreatedAt.Format("Jan 2, 2006"),
|
CreatedAt: it.CreatedAt.Format("Jan 2, 2006"),
|
||||||
ThumbnailID: thumbnailID,
|
ThumbnailID: thumbnailID,
|
||||||
MediaID: mediaID,
|
MediaID: mediaID,
|
||||||
|
MediaIsVideo: mediaIsVideo,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
sw := ssr.NewWriter(w, rc.TmplCache)
|
sw := ssr.NewWriter(w, rc.TmplCache)
|
||||||
|
|
||||||
var title string
|
var title string
|
||||||
if it.Title != nil {
|
if it.Title.Valid {
|
||||||
title = *it.Title
|
title = it.Title.V
|
||||||
}
|
}
|
||||||
|
|
||||||
page := components.Page{
|
page := components.Page{
|
||||||
|
|
|
||||||
5
internal/migrations/sql/0003_embed_video_url.sql
Normal file
5
internal/migrations/sql/0003_embed_video_url.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
-- +goose Up
|
||||||
|
ALTER TABLE item ADD COLUMN embed_video_url TEXT;
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
ALTER TABLE item DROP COLUMN embed_video_url;
|
||||||
|
|
@ -238,6 +238,22 @@ button, input, textarea, select {
|
||||||
font-size: 2rem;
|
font-size: 2rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Play Indicator */
|
||||||
|
.play-indicator {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 0.5rem;
|
||||||
|
right: 0.5rem;
|
||||||
|
width: 2rem;
|
||||||
|
height: 2rem;
|
||||||
|
background: rgba(0,0,0,0.7);
|
||||||
|
color: #fff;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
/* Item Tags */
|
/* Item Tags */
|
||||||
.item-tags {
|
.item-tags {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue