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:
soup 2026-01-17 01:23:32 -05:00
parent cdcc5b5293
commit 2887d9c430
Signed by: soup
SSH key fingerprint: SHA256:GYxje8eQkJ6HZKzVWDdyOUF1TyDiprruGhE0Ym8qYDY
8 changed files with 226 additions and 120 deletions

View file

@ -118,13 +118,13 @@ func HandleCreateItem(rc *RequestContext, w http.ResponseWriter, r *http.Request
defer cancel()
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
Title: req.Title,
Description: req.Description,
LinkURL: req.LinkURL,
Title: item.Nullable(req.Title),
Description: item.Nullable(req.Description),
LinkURL: item.Nullable(req.LinkURL),
ItemType: req.ItemType,
EmbedProvider: req.EmbedProvider,
EmbedVideoID: req.EmbedVideoID,
EmbedHTML: req.EmbedHTML,
EmbedProvider: item.Nullable(req.EmbedProvider),
EmbedVideoID: item.Nullable(req.EmbedVideoID),
EmbedHTML: item.Nullable(req.EmbedHTML),
})
if err != nil {
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{
Title: req.Title,
Description: req.Description,
LinkURL: req.LinkURL,
Title: item.Nullable(req.Title),
Description: item.Nullable(req.Description),
LinkURL: item.Nullable(req.LinkURL),
}); err != nil {
return err
}
@ -241,11 +241,11 @@ func buildItemResponse(ctx context.Context, rc *RequestContext, it item.Row) (it
resp := itemResponse{
ID: it.PubID,
Title: it.Title,
Description: it.Description,
LinkURL: it.LinkURL,
Title: item.Ptr(it.Title),
Description: item.Ptr(it.Description),
LinkURL: item.Ptr(it.LinkURL),
ItemType: it.ItemType,
EmbedHTML: it.EmbedHTML,
EmbedHTML: item.Ptr(it.EmbedHTML),
Tags: tagNames,
CreatedAt: it.CreatedAt.Format(time.RFC3339),
}

View file

@ -2,6 +2,7 @@ package handlers
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
@ -17,6 +18,22 @@ import (
"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 {
Title string
Description string
@ -26,9 +43,9 @@ type urlMetadata struct {
Provider string
VideoID 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) {
// Check if it's a YouTube/Vimeo/Twitter embed
videoInfo, err := embed.Detect(ctx, url)
@ -41,6 +58,7 @@ func fetchURLMetadata(ctx context.Context, url string) (*urlMetadata, error) {
Provider: string(videoInfo.Provider),
VideoID: videoInfo.VideoID,
EmbedHTML: videoInfo.EmbedHTML,
VideoURL: videoInfo.VideoURL,
}, nil
}
@ -151,33 +169,34 @@ func HandleCreateFromLink(rc *RequestContext, w http.ResponseWriter, r *http.Req
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
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
Title: req.Title,
Description: req.Description,
LinkURL: &req.URL,
Title: item.Nullable(req.Title),
Description: item.Nullable(req.Description),
LinkURL: item.Nullable(&req.URL),
ItemType: itemType,
EmbedProvider: embedProvider,
EmbedVideoID: embedVideoID,
EmbedHTML: embedHTML,
EmbedProvider: item.Nullable(embedProvider),
EmbedVideoID: item.Nullable(embedVideoID),
EmbedHTML: item.Nullable(embedHTML),
EmbedVideoURL: nullStr(videoURL),
})
if err != nil {
return err
}
// Download and store image if available
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
}
}
// Download and store thumbnail
if imageURL != "" {
imgData, contentType, err := opengraph.DownloadImage(ctx, imageURL)
if err != nil {
@ -277,17 +296,9 @@ func HandleUpload(rc *RequestContext, w http.ResponseWriter, r *http.Request) er
}
// Create item
var titlePtr, descPtr *string
if title != "" {
titlePtr = &title
}
if description != "" {
descPtr = &description
}
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
Title: titlePtr,
Description: descPtr,
Title: item.Nullable(strOrNil(title)),
Description: item.Nullable(strOrNil(description)),
ItemType: itemType,
})
if err != nil {
@ -359,9 +370,9 @@ func HandleCreateQuote(rc *RequestContext, w http.ResponseWriter, r *http.Reques
defer cancel()
it, err := item.QCreate(ctx, rc.DB, item.CreateParams{
Title: req.Source,
Description: &req.Text,
LinkURL: req.SourceURL,
Title: item.Nullable(req.Source),
Description: nullStr(req.Text),
LinkURL: item.Nullable(req.SourceURL),
ItemType: "quote",
})
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"})
}
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"})
}
meta, err := fetchURLMetadata(ctx, *it.LinkURL)
meta, err := fetchURLMetadata(ctx, it.LinkURL.V)
if err != nil {
return writeJSON(w, http.StatusBadRequest, map[string]string{"error": fmt.Sprintf("failed to fetch: %v", err)})
}
// Update title and description
var titlePtr, descPtr *string
if meta.Title != "" {
titlePtr = &meta.Title
}
if meta.Description != "" {
descPtr = &meta.Description
}
// Update title, description, and video URL
item.QUpdate(ctx, rc.DB, it.ID, item.UpdateParams{
Title: titlePtr,
Description: descPtr,
Title: nullStr(meta.Title),
Description: nullStr(meta.Description),
LinkURL: it.LinkURL,
})
if meta.VideoURL != "" {
item.QUpdateVideoURL(ctx, rc.DB, it.ID, meta.VideoURL)
}
// Download and replace thumbnail
if meta.ImageURL != "" {
// Delete existing media for this item
// Delete existing thumbnails
media.QDeleteByItemID(ctx, rc.DB, it.ID)
imgData, contentType, err := opengraph.DownloadImage(ctx, meta.ImageURL)

View file

@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"strings"
"time"
"git.soup.land/soup/sxgo/ssr"
@ -29,6 +30,7 @@ type homeItem struct {
Tags []string
ThumbnailID *int64
MediaID *int64
HasVideo bool
}
func (h homeContent) Render(sw *ssr.Writer) error {
@ -57,8 +59,10 @@ func (h homeContent) Render(sw *ssr.Writer) error {
</div>
{{else if .ThumbnailID}}
<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}}
<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"}}
<div class="embed-placeholder">
<span></span>
@ -160,11 +164,11 @@ func HandleHome(rc *RequestContext, w http.ResponseWriter, r *http.Request) erro
for _, it := range items {
hi := homeItem{
ID: it.PubID,
Title: it.Title,
Description: it.Description,
LinkURL: it.LinkURL,
Title: item.Ptr(it.Title),
Description: item.Ptr(it.Description),
LinkURL: item.Ptr(it.LinkURL),
ItemType: it.ItemType,
EmbedHTML: it.EmbedHTML,
EmbedHTML: item.Ptr(it.EmbedHTML),
}
// Get tags
@ -187,9 +191,17 @@ func HandleHome(rc *RequestContext, w http.ResponseWriter, r *http.Request) erro
hi.ThumbnailID = &m.ID
} else if m.MediaType == "original" {
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)
}

View file

@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"strings"
"time"
"git.soup.land/soup/sxgo/ssr"
@ -18,16 +19,18 @@ type itemPageContent struct {
}
type itemPageData struct {
ID string
Title *string
Description *string
LinkURL *string
ItemType string
EmbedHTML *string
Tags []string
CreatedAt string
ThumbnailID *int64
MediaID *int64
ID string
Title *string
Description *string
LinkURL *string
ItemType string
EmbedHTML *string
EmbedVideoURL *string
Tags []string
CreatedAt string
ThumbnailID *int64
MediaID *int64
MediaIsVideo bool
}
func (c itemPageContent) Render(sw *ssr.Writer) error {
@ -48,7 +51,13 @@ func (c itemPageContent) Render(sw *ssr.Writer) error {
</video>
</div>
{{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">
<img src="/media/{{.Item.ThumbnailID}}" alt="{{if .Item.Title}}{{.Item.Title}}{{else}}Embed{{end}}">
</div>
@ -162,6 +171,7 @@ func HandleItemPage(rc *RequestContext, w http.ResponseWriter, r *http.Request)
// Get media
var thumbnailID, mediaID *int64
var mediaIsVideo bool
mediaList, err := media.QFindByItemID(ctx, rc.DB, it.ID)
if err != nil {
return err
@ -171,28 +181,31 @@ func HandleItemPage(rc *RequestContext, w http.ResponseWriter, r *http.Request)
thumbnailID = &m.ID
} else if m.MediaType == "original" {
mediaID = &m.ID
mediaIsVideo = strings.HasPrefix(m.ContentType, "video/")
}
}
data := itemPageData{
ID: it.PubID,
Title: it.Title,
Description: it.Description,
LinkURL: it.LinkURL,
ItemType: it.ItemType,
EmbedHTML: it.EmbedHTML,
Tags: tagNames,
CreatedAt: it.CreatedAt.Format("Jan 2, 2006"),
ThumbnailID: thumbnailID,
MediaID: mediaID,
ID: it.PubID,
Title: item.Ptr(it.Title),
Description: item.Ptr(it.Description),
LinkURL: item.Ptr(it.LinkURL),
ItemType: it.ItemType,
EmbedHTML: item.Ptr(it.EmbedHTML),
EmbedVideoURL: item.Ptr(it.EmbedVideoURL),
Tags: tagNames,
CreatedAt: it.CreatedAt.Format("Jan 2, 2006"),
ThumbnailID: thumbnailID,
MediaID: mediaID,
MediaIsVideo: mediaIsVideo,
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
sw := ssr.NewWriter(w, rc.TmplCache)
var title string
if it.Title != nil {
title = *it.Title
if it.Title.Valid {
title = it.Title.V
}
page := components.Page{