From cd2d8a2fbb4b9058376208f2b893a7e6611947b6 Mon Sep 17 00:00:00 2001 From: Zafer Cesur Date: Sun, 9 Jun 2024 13:30:44 +0300 Subject: [PATCH] add PlayerComponent (#52) --- assets/js/app.ts | 45 +++-------- lib/algora/events.ex | 17 +++++ .../components/layouts/live.html.heex | 1 - .../components/layouts/live_chat.html.heex | 1 - lib/algora_web/live/embed_live.ex | 41 ++++------ lib/algora_web/live/player_component.ex | 56 ++++++++++++++ lib/algora_web/live/player_live.ex | 26 ------- lib/algora_web/live/subtitle_live/index.ex | 17 ----- lib/algora_web/live/subtitle_live/show.ex | 17 ----- lib/algora_web/live/video_live.ex | 75 +++++-------------- 10 files changed, 116 insertions(+), 180 deletions(-) create mode 100644 lib/algora_web/live/player_component.ex delete mode 100644 lib/algora_web/live/player_live.ex diff --git a/assets/js/app.ts b/assets/js/app.ts index 4d866ca..93e62ab 100644 --- a/assets/js/app.ts +++ b/assets/js/app.ts @@ -150,8 +150,10 @@ const Hooks = { mounted() { const backdrop = document.querySelector("#video-backdrop"); - this.player = videojs("video-player", { - autoplay: true, + this.playerId = this.el.id; + + this.player = videojs(this.el, { + autoplay: "any", liveui: true, html5: { vhs: { @@ -161,6 +163,7 @@ const Hooks = { }); const playVideo = (opts: { + player_id: string; id: string; url: string; title: string; @@ -168,6 +171,10 @@ const Hooks = { current_time?: number; channel_name: string; }) => { + if (this.playerId !== opts.player_id) { + return; + } + const setMediaSession = () => { if (!("mediaSession" in navigator)) { return; @@ -193,22 +200,21 @@ const Hooks = { : {}), }); this.player.src({ src: opts.url, type: opts.player_type }); - this.player.play(); setMediaSession(); if (opts.current_time && opts.player_type !== "video/youtube") { this.player.currentTime(opts.current_time); } - this.player.el().parentElement.classList.remove("hidden"); - this.player.el().parentElement.classList.add("flex"); if (backdrop) { backdrop.classList.remove("opacity-10"); backdrop.classList.add("opacity-20"); } - this.pushEventTo("#clipper", "video_loaded", { id: opts.id }); + if (this.playerId === "video-player") { + this.pushEventTo("#clipper", "video_loaded", { id: opts.id }); + } }; this.handleEvent("play_video", playVideo); @@ -350,33 +356,6 @@ let liveSocket = new LiveSocket("/live", Socket, { let routeUpdated = () => { // TODO: uncomment // Focus.focusMain(); - - const player = document.querySelector("#video-player")?.parentElement; - if (!player) { - return; - } - - const { pathname } = new URL(window.location.href); - if (pathname.endsWith("/embed")) { - return; - } - - const pipClasses = [ - "fixed", - "bottom-0", - "right-0", - "z-[1000]", - "w-[100vw]", - "sm:w-[30vw]", - ]; - - if (/^\/[^\/]+\/\d+$/.test(pathname)) { - player.classList.add("lg:pr-[24rem]"); - player.classList.remove(...pipClasses); - } else { - player.classList.remove("lg:pr-[24rem]"); - player.classList.add(...pipClasses); - } }; // Show progress bar on live navigation and form submits diff --git a/lib/algora/events.ex b/lib/algora/events.ex index 04b752b..aae7d90 100644 --- a/lib/algora/events.ex +++ b/lib/algora/events.ex @@ -199,4 +199,21 @@ defmodule Algora.Events do ) |> Repo.all() end + + def log_watched(user, video) do + actor_id = if user, do: "user_#{user.id}", else: "guest_#{hash_actor_id()}" + + %Event{ + actor_id: actor_id, + user_id: user && user.id, + video_id: video.id, + channel_id: video.user_id, + name: :watched + } + |> Event.changeset(%{}) + |> Repo.insert() + end + + # TODO: + defp hash_actor_id, do: "" end diff --git a/lib/algora_web/components/layouts/live.html.heex b/lib/algora_web/components/layouts/live.html.heex index f368499..ba7f2b4 100644 --- a/lib/algora_web/components/layouts/live.html.heex +++ b/lib/algora_web/components/layouts/live.html.heex @@ -314,7 +314,6 @@ <.live_component module={AlgoraWeb.LayoutComponent} id="layout" />
- <%= live_render(@socket, AlgoraWeb.PlayerLive, id: "player", session: %{}, sticky: true) %> <%= live_render(@socket, AlgoraWeb.ClipperLive, id: "clipper", session: %{}, sticky: true) %> <%= @inner_content %>
diff --git a/lib/algora_web/components/layouts/live_chat.html.heex b/lib/algora_web/components/layouts/live_chat.html.heex index 6e2828c..50b2930 100644 --- a/lib/algora_web/components/layouts/live_chat.html.heex +++ b/lib/algora_web/components/layouts/live_chat.html.heex @@ -10,7 +10,6 @@ <.live_component module={AlgoraWeb.LayoutComponent} id="layout" />
- <%= live_render(@socket, AlgoraWeb.PlayerLive, id: "player", session: %{}, sticky: true) %> <%= @inner_content %>
diff --git a/lib/algora_web/live/embed_live.ex b/lib/algora_web/live/embed_live.ex index ff42dad..047d682 100644 --- a/lib/algora_web/live/embed_live.ex +++ b/lib/algora_web/live/embed_live.ex @@ -3,17 +3,12 @@ defmodule AlgoraWeb.EmbedLive do require Logger alias Algora.{Accounts, Library, Storage, Chat} - alias AlgoraWeb.{LayoutComponent, Presence} + alias AlgoraWeb.{LayoutComponent, Presence, PlayerComponent} def render(assigns) do ~H""" -
-