From bbbf3ad38f649008e58e2eb11f0bee96722243c7 Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Sat, 2 Nov 2024 14:51:32 -0400 Subject: Remove Discord Activity code from this branch --- lib/mediasync/application.ex | 7 --- lib/mediasync/constants.ex | 4 -- lib/mediasync/discord_api.ex | 15 ----- lib/mediasync/room.ex | 40 +----------- lib/mediasync/router.ex | 144 ++++--------------------------------------- lib/mediasync/templates.ex | 15 +---- lib/mix/tasks/vendor.ex | 18 ------ 7 files changed, 18 insertions(+), 225 deletions(-) delete mode 100644 lib/mediasync/constants.ex delete mode 100644 lib/mediasync/discord_api.ex (limited to 'lib') diff --git a/lib/mediasync/application.ex b/lib/mediasync/application.ex index 5cef37a..1e8f6d4 100644 --- a/lib/mediasync/application.ex +++ b/lib/mediasync/application.ex @@ -25,13 +25,6 @@ defmodule Mediasync.Application do {Registry, keys: :duplicate, name: Mediasync.RoomSubscriptionRegistry} ] - children = - if Application.fetch_env!(:mediasync, :enable_discord_activity?) do - [{Registry, keys: :duplicate, name: Mediasync.DiscordActivityInstanceRegistry} | children] - else - children - end - System.no_halt(true) # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/mediasync/constants.ex b/lib/mediasync/constants.ex deleted file mode 100644 index 9aabc45..0000000 --- a/lib/mediasync/constants.ex +++ /dev/null @@ -1,4 +0,0 @@ -defmodule Mediasync.Constants do - def query_param_discord_activity_inner, do: "discord_activity_inner" - def query_param_instance_id, do: "instance_id" -end diff --git a/lib/mediasync/discord_api.ex b/lib/mediasync/discord_api.ex deleted file mode 100644 index da1d39c..0000000 --- a/lib/mediasync/discord_api.ex +++ /dev/null @@ -1,15 +0,0 @@ -defmodule Mediasync.DiscordAPI do - @base_url "https://discord.com/api/v10/" - - @spec get_user!(String.t()) :: map() - def get_user!(access_token) do - response = - Req.get!(@base_url <> "users/@me", headers: [authorization: "Bearer #{access_token}"]) - - if response.status == 200 do - response.body - else - raise "Discord API responded with status code #{response.status}" - end - end -end diff --git a/lib/mediasync/room.ex b/lib/mediasync/room.ex index f518220..3a8c3cc 100644 --- a/lib/mediasync/room.ex +++ b/lib/mediasync/room.ex @@ -18,9 +18,7 @@ defmodule Mediasync.Room.State do :video_info, :host_user_token_hash, :room_id, - :host_username, :viewer_usernames, - :discord_instance_id, host_disconnected_tries: @host_disconnected_tries_max ] @@ -32,9 +30,7 @@ defmodule Mediasync.Room.State do video_info: Mediasync.Room.VideoInfo.t(), host_user_token_hash: Mediasync.UserToken.hash(), room_id: Mediasync.RoomID.t() | nil, - host_username: String.t() | nil, viewer_usernames: [String.t()] | nil, - discord_instance_id: String.t() | nil, host_disconnected_tries: integer() } end @@ -93,45 +89,15 @@ defmodule Mediasync.Room do end @inactive_check_wait_milliseconds 10 * 1000 - @discord_activity_instance_rooms_max 20 @impl true @spec init(Mediasync.Room.State.t()) :: {:ok, Mediasync.Room.State.t()} def init(state = %Mediasync.Room.State{}) do - discord_activity_ok? = - if state.discord_instance_id do - instance_room_count = - Registry.count_match( - Mediasync.DiscordActivityInstanceRegistry, - state.discord_instance_id, - :_ - ) - - if instance_room_count < @discord_activity_instance_rooms_max do - Registry.register( - Mediasync.DiscordActivityInstanceRegistry, - state.discord_instance_id, - %{ - host_username: state.host_username, - room_id: state.room_id - } - ) - - true - else - false - end - end - - if discord_activity_ok? != false do - Process.send_after(self(), :check_if_active, @inactive_check_wait_milliseconds) + Process.send_after(self(), :check_if_active, @inactive_check_wait_milliseconds) - Logger.info("Created room #{state.room_id}") + Logger.info("Created room #{state.room_id}") - {:ok, state} - else - {:stop, :discord_activity_instance_max_rooms_reached} - end + {:ok, state} end @impl true diff --git a/lib/mediasync/router.ex b/lib/mediasync/router.ex index 3219300..dd73cb6 100644 --- a/lib/mediasync/router.ex +++ b/lib/mediasync/router.ex @@ -1,5 +1,4 @@ defmodule Mediasync.Router do - import Mediasync.Constants import Mediasync.Utils import Mediasync.UserToken use Plug.Router @@ -12,7 +11,13 @@ defmodule Mediasync.Router do plug(:put_secret_key_base) - plug(:session_wrapper) + plug(Plug.Session, + store: :cookie, + key: "_mediasync_session", + encryption_salt: {Mediasync.Utils, :get_session_encryption_salt, []}, + signing_salt: {Mediasync.Utils, :get_session_signing_salt, []} + ) + plug(:fetch_session) plug(:ensure_user_token) @@ -28,25 +33,9 @@ defmodule Mediasync.Router do plug(:dispatch) get "/" do - enable_discord_activity? = Application.fetch_env!(:mediasync, :enable_discord_activity?) - - conn = - conn - |> put_html_content_type() - - cond do - enable_discord_activity? and Map.has_key?(conn.query_params, query_param_instance_id()) -> - conn - |> put_session("discord_instance_id", conn.query_params["instance_id"]) - |> send_resp(200, Mediasync.Templates.discord_activity()) - - enable_discord_activity? and - Map.has_key?(conn.query_params, query_param_discord_activity_inner()) -> - send_resp(conn, 200, Mediasync.Templates.home(true)) - - true -> - send_resp(conn, 200, Mediasync.Templates.home()) - end + conn + |> put_html_content_type() + |> send_resp(200, Mediasync.Templates.home()) end post "/host_room" do @@ -71,38 +60,16 @@ defmodule Mediasync.Router do Mediasync.HTTPErrors.send_invalid_video_url(conn) true -> - in_discord_activity? = - Application.fetch_env!(:mediasync, :enable_discord_activity?) and - Map.has_key?(conn.query_params, query_param_discord_activity_inner()) - - {suffix, host_username, instance_id} = - if in_discord_activity? do - {"?#{query_param_discord_activity_inner()}", - Mediasync.DiscordAPI.get_user!(get_session(conn, "discord_access_token"))[ - "username" - ], get_session(conn, "discord_instance_id")} - else - {"", nil, nil} - end - case DynamicSupervisor.start_child( Mediasync.RoomSupervisor, {Mediasync.Room, %Mediasync.Room.State{ video_info: video_info, - host_user_token_hash: get_user_token_hash!(conn), - host_username: host_username, - discord_instance_id: instance_id + host_user_token_hash: get_user_token_hash!(conn) }} ) do {:ok, _pid, room_id} -> - redirect(conn, status: 303, location: "/room/#{room_id}#{suffix}") - - {:error, :discord_activity_instance_max_rooms_reached} -> - Mediasync.HTTPErrors.send_bad_request( - conn, - message: "Cannot host more rooms in this activity instance." - ) + redirect(conn, status: 303, location: "/room/#{room_id}") end end end @@ -112,18 +79,13 @@ defmodule Mediasync.Router do case Registry.lookup(Mediasync.RoomRegistry, room_id) do [{pid, _value}] -> - in_discord_activity? = - Application.fetch_env!(:mediasync, :enable_discord_activity?) and - Map.has_key?(conn.query_params, query_param_discord_activity_inner()) - conn |> put_html_content_type() |> send_resp( 200, Mediasync.Templates.room( Mediasync.Room.get_video_info(pid), - room_id, - in_discord_activity? + room_id ) ) @@ -194,60 +156,6 @@ defmodule Mediasync.Router do end end - post "/discord_activity/access_token" do - if Application.fetch_env!(:mediasync, :enable_discord_activity?) do - if Map.has_key?(conn.query_params, query_param_discord_activity_inner()) do - response = - Req.post!("https://discord.com/api/oauth2/token", - headers: [ - content_type: "application/x-www-form-urlencoded" - ], - form: [ - client_id: Application.fetch_env!(:mediasync, :discord_client_id), - client_secret: Application.fetch_env!(:mediasync, :discord_client_secret), - grant_type: "authorization_code", - code: conn.body_params["code"] - ] - ) - - access_token = response.body["access_token"] - - conn - |> put_session("discord_access_token", access_token) - |> put_plain_text_content_type() - |> send_resp(200, access_token) - else - # If the query param isn't present, we won't be able to modify the session (see session_wrapper/2). - Mediasync.HTTPErrors.send_bad_request(conn, - message: - "This route must always be called with the query param ?#{query_param_discord_activity_inner()}" - ) - end - else - Mediasync.HTTPErrors.send_not_found(conn) - end - end - - get "/discord_activity/rooms_for_instance" do - if Application.fetch_env!(:mediasync, :enable_discord_activity?) do - values = - for {pid, value} <- - Registry.match( - Mediasync.DiscordActivityInstanceRegistry, - conn.query_params["instance_id"], - :_ - ) do - Map.put(value, "filename", Path.basename(URI.new!(Mediasync.Room.get_video_info(pid).url).path)) - end - - conn - |> put_json_content_type() - |> send_resp(200, Jason.encode!(values)) - else - Mediasync.HTTPErrors.send_not_found(conn) - end - end - match _ do Mediasync.HTTPErrors.send_not_found(conn) end @@ -262,30 +170,4 @@ defmodule Mediasync.Router do Mediasync.HTTPErrors.send_unknown(conn) end end - - defp session_wrapper(conn, _opts) do - query_params = fetch_query_params(conn).query_params - - in_discord_activity? = - Application.fetch_env!(:mediasync, :enable_discord_activity?) and - (Map.has_key?(query_params, query_param_discord_activity_inner()) or - (conn.request_path == "/" and - Map.has_key?(query_params, query_param_instance_id()))) - - Plug.Session.call( - conn, - Plug.Session.init( - store: :cookie, - key: "_mediasync_session", - encryption_salt: {Mediasync.Utils, :get_session_encryption_salt, []}, - signing_salt: {Mediasync.Utils, :get_session_signing_salt, []}, - extra: - if in_discord_activity? do - "Domain=#{Application.fetch_env!(:mediasync, :discord_client_id)}.discordsays.com; SameSite=None; Partitioned; Secure;" - else - "" - end - ) - ) - end end diff --git a/lib/mediasync/templates.ex b/lib/mediasync/templates.ex index b9ef937..e82a723 100644 --- a/lib/mediasync/templates.ex +++ b/lib/mediasync/templates.ex @@ -1,21 +1,10 @@ defmodule Mediasync.Templates do require EEx - def home() do - home(false) - end - - def room(video_info, room_id) do - room(video_info, room_id, false) - end - - EEx.function_from_file(:def, :home, "priv/home.html.eex", [:in_discord_activity?]) + EEx.function_from_file(:def, :home, "priv/home.html.eex") EEx.function_from_file(:def, :room, "priv/room.html.eex", [ :video_info, - :room_id, - :in_discord_activity? + :room_id ]) - - EEx.function_from_file(:def, :discord_activity, "priv/discord_activity.html.eex") end diff --git a/lib/mix/tasks/vendor.ex b/lib/mix/tasks/vendor.ex index 06e3aa5..2c20bb4 100644 --- a/lib/mix/tasks/vendor.ex +++ b/lib/mix/tasks/vendor.ex @@ -8,24 +8,6 @@ defmodule Mix.Tasks.Vendor do File.cp_r!("node_modules/video.js/dist", "priv/static/video.js") File.cp_r!("node_modules/video.js/LICENSE", "priv/static/video.js/LICENSE") - {_, 0} = System.cmd("npx", ~w(parcel build --target discord-embedded-app-sdk)) - - discord_readme_path = "priv/static/discord-embedded-app-sdk/README.md" - - File.cp_r!( - "node_modules/@discord/embedded-app-sdk/LICENSE.md", - discord_readme_path - ) - - File.write!( - discord_readme_path, - """ - This directory contains a bundled version of https://github.com/discord/embedded-app-sdk/ - See lib/mix/tasks/vendor.ex for how it was generated. The license for the \ - original library is reproduced below:\n - """ <> File.read!(discord_readme_path) - ) - nil end end -- cgit v1.2.3-57-g22cb