this repo has no description
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: refactor previews into cleaner items

+63 -26
+12 -26
internal/handler/preview.go
··· 68 68 json.NewEncoder(w).Encode(meta) 69 69 return 70 70 } 71 - // If failed, fallthrough or return error? 72 - // Fallthrough to generic scrape might not work if Scrape inside GetRedditPreview failed. 73 - // But let's let fallthrough happen just in case. 71 + } else if strings.Contains(urlParam, "twitter.com") || strings.Contains(urlParam, "x.com") { 72 + meta, err := h.GetTwitterPreview(urlParam) 73 + if err == nil { 74 + json.NewEncoder(w).Encode(meta) 75 + return 76 + } 77 + } else if strings.Contains(urlParam, "youtube.com") || strings.Contains(urlParam, "youtu.be") { 78 + meta, err := h.GetYouTubePreview(urlParam) 79 + if err == nil { 80 + json.NewEncoder(w).Encode(meta) 81 + return 82 + } 74 83 } 75 84 76 85 // 1. Try OEmbed 77 86 if meta, err := h.tryOEmbed(urlParam); err == nil { 78 - // For Twitter/X, if successful, we might still want to return empty object to avoid duplicating 79 - // the widget embedded by server-side logic? 80 - // User request implies utilizing OEmbed. If server-side generates a widget, and we generate a card, 81 - // we have duplication. 82 - // However, returning OEmbed data allows the frontend to optionally replace or augment. 83 - // Current existing logic for Twitter was: 84 - // "If valid (200), return empty success so frontend keeps the existing embed" 85 - // If we stick to that for Twitter/X ONLY: 86 - if strings.Contains(urlParam, "twitter.com") || strings.Contains(urlParam, "x.com") { 87 - json.NewEncoder(w).Encode(map[string]string{}) 88 - return 89 - } 90 - 91 87 json.NewEncoder(w).Encode(meta) 92 88 return 93 89 } ··· 341 337 if u != nil { 342 338 metadata["icon"] = fmt.Sprintf("https://www.google.com/s2/favicons?domain=%s://%s&sz=32", u.Scheme, u.Host) 343 339 } 344 - } 345 - 346 - // Check for YouTube "soft 404" 347 - if strings.Contains(targetURL, "youtube.com") || strings.Contains(targetURL, "youtu.be") { 348 - title, hasTitle := metadata["title"] 349 - if !hasTitle || title == " - YouTube" || title == "YouTube" { 350 - // Special handling for caller to know it's a 404 351 - return nil, fmt.Errorf("status 404") 352 - } 353 - metadata["type"] = "video" 354 340 } 355 341 356 342 return metadata, nil
+23
internal/handler/preview_twitter.go
··· 1 + package handler 2 + 3 + import ( 4 + "strings" 5 + ) 6 + 7 + // GetTwitterPreview handles preview logic for Twitter/X. 8 + // Currently, it leverages the default OEmbed behavior but suppresses the output 9 + // to avoid duplicating the client-side widget if OEmbed is successful. 10 + func (h *Handler) GetTwitterPreview(targetURL string) (map[string]string, error) { 11 + // Try OEmbed first 12 + meta, err := h.tryOEmbed(targetURL) 13 + if err == nil { 14 + // Existing logic: 15 + // "If valid (200), return empty success so frontend keeps the existing embed" 16 + // If we stick to that for Twitter/X ONLY: 17 + if strings.Contains(targetURL, "twitter.com") || strings.Contains(targetURL, "x.com") { 18 + return map[string]string{}, nil 19 + } 20 + return meta, nil 21 + } 22 + return nil, err 23 + }
+28
internal/handler/preview_youtube.go
··· 1 + package handler 2 + 3 + import ( 4 + "fmt" 5 + ) 6 + 7 + // GetYouTubePreview handles preview logic for YouTube. 8 + // It checks generic OG metadata but includes logic to detect "soft 404s" 9 + func (h *Handler) GetYouTubePreview(targetURL string) (map[string]string, error) { 10 + // Default UA 11 + ua := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" 12 + 13 + meta, err := h.scrapeOpenGraph(targetURL, ua) 14 + if err != nil { 15 + return nil, err 16 + } 17 + 18 + // Check for YouTube "soft 404" 19 + // Extracted from original scrapeOpenGraph 20 + title, hasTitle := meta["title"] 21 + if !hasTitle || title == " - YouTube" || title == "YouTube" { 22 + // Special handling for caller to know it's a 404 23 + return nil, fmt.Errorf("status 404") 24 + } 25 + meta["type"] = "video" 26 + 27 + return meta, nil 28 + }