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"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
ID int64
|
||||
PubID string
|
||||
Title *string
|
||||
Description *string
|
||||
LinkURL *string
|
||||
Title sql.Null[string]
|
||||
Description sql.Null[string]
|
||||
LinkURL sql.Null[string]
|
||||
ItemType string // 'image', 'video', 'quote', 'embed'
|
||||
EmbedProvider *string
|
||||
EmbedVideoID *string
|
||||
EmbedHTML *string
|
||||
EmbedProvider sql.Null[string]
|
||||
EmbedVideoID sql.Null[string]
|
||||
EmbedHTML sql.Null[string]
|
||||
EmbedVideoURL sql.Null[string]
|
||||
CreatedAt time.Time
|
||||
DeletedAt *time.Time
|
||||
DeletedAt sql.Null[time.Time]
|
||||
}
|
||||
|
||||
type CreateParams struct {
|
||||
Title *string
|
||||
Description *string
|
||||
LinkURL *string
|
||||
Title sql.Null[string]
|
||||
Description sql.Null[string]
|
||||
LinkURL sql.Null[string]
|
||||
ItemType string
|
||||
EmbedProvider *string
|
||||
EmbedVideoID *string
|
||||
EmbedHTML *string
|
||||
EmbedProvider sql.Null[string]
|
||||
EmbedVideoID sql.Null[string]
|
||||
EmbedHTML sql.Null[string]
|
||||
EmbedVideoURL sql.Null[string]
|
||||
}
|
||||
|
||||
// QCreate creates a new item.
|
||||
func QCreate(ctx context.Context, db *sql.DB, p CreateParams) (Row, error) {
|
||||
query := `
|
||||
INSERT INTO item (title, description, link_url, item_type, embed_provider, embed_video_id, embed_html)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, pub_id, title, description, link_url, item_type, embed_provider, embed_video_id, embed_html, created_at, deleted_at
|
||||
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, $8)
|
||||
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 pubID pgtype.UUID
|
||||
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(
|
||||
&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,
|
||||
)
|
||||
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.
|
||||
func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
||||
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
|
||||
WHERE pub_id = $1
|
||||
`
|
||||
|
|
@ -68,7 +86,7 @@ func QFindByPubID(ctx context.Context, db *sql.DB, pubID string) (*Row, error) {
|
|||
var pubUUID pgtype.UUID
|
||||
err := db.QueryRowContext(ctx, query, pubID).Scan(
|
||||
&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,
|
||||
)
|
||||
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.
|
||||
func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
||||
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
|
||||
WHERE id = $1
|
||||
`
|
||||
|
|
@ -93,7 +111,7 @@ func QFindByID(ctx context.Context, db *sql.DB, id int64) (*Row, error) {
|
|||
var pubUUID pgtype.UUID
|
||||
err := db.QueryRowContext(ctx, query, id).Scan(
|
||||
&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,
|
||||
)
|
||||
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.
|
||||
func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
||||
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
|
||||
WHERE deleted_at IS NULL
|
||||
ORDER BY created_at DESC
|
||||
|
|
@ -127,7 +145,7 @@ func QList(ctx context.Context, db *sql.DB) ([]Row, error) {
|
|||
var pubUUID pgtype.UUID
|
||||
if err := rows.Scan(
|
||||
&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,
|
||||
); err != nil {
|
||||
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.
|
||||
func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error) {
|
||||
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
|
||||
JOIN item_tag it ON i.id = it.item_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
|
||||
if err := rows.Scan(
|
||||
&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,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
|
|
@ -173,9 +191,9 @@ func QListByTag(ctx context.Context, db *sql.DB, tagName string) ([]Row, error)
|
|||
}
|
||||
|
||||
type UpdateParams struct {
|
||||
Title *string
|
||||
Description *string
|
||||
LinkURL *string
|
||||
Title sql.Null[string]
|
||||
Description sql.Null[string]
|
||||
LinkURL sql.Null[string]
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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.
|
||||
func QSoftDelete(ctx context.Context, db *sql.DB, id int64) error {
|
||||
query := `UPDATE item SET deleted_at = NOW() WHERE id = $1`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue