1
0
mirror of https://github.com/algora-io/tv.git synced 2024-11-16 00:58:59 +02:00

improve handling of external messages

This commit is contained in:
zafer 2024-05-23 22:59:08 +03:00
parent 3f6a3dd313
commit 2675a611bb
3 changed files with 51 additions and 34 deletions

View File

@ -235,9 +235,15 @@ defmodule Algora.Accounts do
end
def create_entity!(attrs) do
%Entity{}
|> Entity.changeset(attrs)
|> Repo.insert!()
case %Entity{}
|> Entity.changeset(attrs)
|> Repo.insert() do
{:ok, entity} ->
entity
_ when attrs.id != attrs.handle ->
create_entity!(%{attrs | handle: attrs.id})
end
end
def get_entity!(id), do: Repo.get!(Entity, id)

View File

@ -10,8 +10,12 @@ defmodule Algora.Restream.Websocket do
def handle_frame({:text, msg}, state) do
case Jason.decode(msg) do
{:ok, action} ->
handle_action(action, state)
{:ok, %{"action" => "event", "payload" => payload}} ->
handle_payload(payload, state)
{:ok, message} ->
Logger.info("Received message: #{inspect(message)}")
{:ok, state}
{:error, _reason} ->
Logger.error("Failed to parse message: #{msg}")
@ -24,33 +28,18 @@ defmodule Algora.Restream.Websocket do
{:reconnect, state}
end
defp handle_action(
%{
"action" => "event",
"payload" => %{
"eventPayload" => %{
"author" =>
%{
"name" => handle,
"displayName" => name,
"avatar" => avatar_url,
"id" => platform_id
} = author,
"bot" => false,
"contentModifiers" => %{"whisper" => false},
"text" => body
}
}
} = action,
state
) do
defp handle_payload(%{"eventPayload" => %{"contentModifiers" => %{"whisper" => true}}}, state) do
{:ok, state}
end
defp handle_payload(%{"eventPayload" => %{"author" => author, "text" => body}} = payload, state) do
entity =
Accounts.get_or_create_entity!(%{
name: name,
handle: handle,
avatar_url: avatar_url,
platform: get_platform(action),
platform_id: platform_id,
name: get_name(author),
handle: get_handle(author),
avatar_url: author["avatar"],
platform: get_platform(payload),
platform_id: author["id"],
platform_meta: author
})
@ -61,18 +50,18 @@ defmodule Algora.Restream.Websocket do
Chat.broadcast_message_sent!(message)
_error ->
Logger.error("Failed to persist message: #{inspect(action)}")
Logger.error("Failed to persist payload: #{inspect(payload)}")
end
{:ok, state}
end
defp handle_action(action, state) do
Logger.info("Received message: #{inspect(action)}")
defp handle_payload(payload, state) do
Logger.info("Received payload: #{inspect(payload)}")
{:ok, state}
end
defp get_platform(%{"payload" => %{"connectionIdentifier" => identifier}}) do
defp get_platform(%{"connectionIdentifier" => identifier}) do
parts = String.split(identifier, "-")
case parts do
@ -86,4 +75,11 @@ defmodule Algora.Restream.Websocket do
end
defp get_platform(_action), do: "unknown"
defp get_handle(%{"username" => username}), do: username
defp get_handle(%{"displayName" => displayName}), do: Slug.slugify(displayName)
defp get_handle(%{"id" => id}), do: id
defp get_name(%{"displayName" => displayName}), do: displayName
defp get_name(author), do: get_handle(author)
end

View File

@ -75,6 +75,21 @@ defmodule AlgoraWeb.RTMPDestinationIconComponent do
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 4l11.733 16h4.267l-11.733 -16z" /><path d="M4 20l6.768 -6.768m2.46 -2.46l6.772 -6.772" />
</svg>
<% :twitter -> %>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class={["text-white", @class]}
>
<path stroke="none" d="M0 0h24v24H0z" fill="none" /><path d="M4 4l11.733 16h4.267l-11.733 -16z" /><path d="M4 20l6.768 -6.768m2.46 -2.46l6.772 -6.772" />
</svg>
<% :linkedin -> %>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 72 72" class={[@class]}>
<g fill="none" fill-rule="evenodd">