From 21b427f508578323969e0434661aa1e533cf7105 Mon Sep 17 00:00:00 2001 From: Sand Merchant Date: Fri, 11 Aug 2023 21:50:50 +0000 Subject: [PATCH] added rudimentry enbeded video support --- src/lib/Video.svelte | 72 +++++++++++++++++++ src/lib/feeds/posts/PostContent.svelte | 18 ++++- src/lib/feeds/posts/post-utils.ts | 23 +++++- .../posts/previews/CardPostPreview.svelte | 7 ++ .../feeds/posts/previews/CardPostVideo.svelte | 48 +++++++++++++ 5 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 src/lib/Video.svelte create mode 100644 src/lib/feeds/posts/previews/CardPostVideo.svelte diff --git a/src/lib/Video.svelte b/src/lib/Video.svelte new file mode 100644 index 0000000..a420623 --- /dev/null +++ b/src/lib/Video.svelte @@ -0,0 +1,72 @@ + + +{#if error} +
+ +
+{:else if nsfw && $nsfwImageHandling === 'HIDE' && !showAnyway} + +{:else if valid} + + +{/if} + + diff --git a/src/lib/feeds/posts/PostContent.svelte b/src/lib/feeds/posts/PostContent.svelte index 8695d1c..a3d4711 100644 --- a/src/lib/feeds/posts/PostContent.svelte +++ b/src/lib/feeds/posts/PostContent.svelte @@ -7,7 +7,7 @@
- {#if hasBody} + {#if postAssertions.has.body} {/if} {#if probablyImage && postView.post.url} @@ -17,17 +17,29 @@ in the feed height, so images can't be lazy --> {/if} + {#if postAssertions.has.video} + +
diff --git a/src/lib/feeds/posts/post-utils.ts b/src/lib/feeds/posts/post-utils.ts index 6a84322..ea030e5 100644 --- a/src/lib/feeds/posts/post-utils.ts +++ b/src/lib/feeds/posts/post-utils.ts @@ -36,11 +36,26 @@ export function hasImageExtension(url?: string) { } } +export function hasVideoExtension(url?: string) { + if (!isValidUrl(url)) { + return false; + } + + try { + const u = new URL(url); + return /\.(mp4|mpv|mov)$/.test(u.pathname); + } catch (e) { + return false; + } +} + export interface PostAssertions { imageSrc?: string; + videoSrc?: string; // existence of certain types of embedded content has: { image: boolean; + video: boolean; body: boolean; embed: boolean; any: boolean; @@ -53,19 +68,25 @@ export interface PostAssertions { export function makePostAssertions(pv: PostView, myUserId?: number): PostAssertions { let imageSrc = pv.post.thumbnail_url; + let videoSrc = pv.post.embed_video_url; if (!imageSrc && hasImageExtension(pv.post.url)) { imageSrc = pv.post.url; } + if (!videoSrc && hasVideoExtension(pv.post.url)) { + videoSrc = pv.post.url; + } const hasParts = { image: !!imageSrc, + video: !!videoSrc, body: !!pv.post.body?.trim(), - embed: !!pv.post.embed_title + embed: !!(pv.post.embed_title || videoSrc) }; return { imageSrc, + videoSrc, // assertions about embedded contents has: { ...hasParts, diff --git a/src/lib/feeds/posts/previews/CardPostPreview.svelte b/src/lib/feeds/posts/previews/CardPostPreview.svelte index 6e588ae..057a58d 100644 --- a/src/lib/feeds/posts/previews/CardPostPreview.svelte +++ b/src/lib/feeds/posts/previews/CardPostPreview.svelte @@ -50,6 +50,12 @@ {/if} + {#if postAssertions.has.video} +
+ +
+ {/if} + {#if postView.post.embed_description || (postView.post.embed_title && mode === 'list')}
@@ -80,6 +86,7 @@ import { Stack } from 'sheodox-ui'; import PostTitle from '../PostTitle.svelte'; import CardPostImage from './CardPostImage.svelte'; + import CardPostVideo from './CardPostVideo.svelte'; import PostTime from '../PostTime.svelte'; import { createEventDispatcher } from 'svelte'; import type { PostView } from 'lemmy-js-client'; diff --git a/src/lib/feeds/posts/previews/CardPostVideo.svelte b/src/lib/feeds/posts/previews/CardPostVideo.svelte new file mode 100644 index 0000000..4d609c5 --- /dev/null +++ b/src/lib/feeds/posts/previews/CardPostVideo.svelte @@ -0,0 +1,48 @@ + + +
+ {#if postView.post.nsfw && $nsfwImageHandling === 'HIDE' && !showAnyway} +
+ +
+ {:else if postAssertions.has.video} + +
+ +