diff --git a/lib/unfinal_web/live/editor_live.ex b/lib/unfinal_web/live/editor_live.ex index fc7f4fd..f6f6708 100644 --- a/lib/unfinal_web/live/editor_live.ex +++ b/lib/unfinal_web/live/editor_live.ex @@ -25,7 +25,7 @@ defmodule UnfinalWeb.EditorLive do writer? = writer?(segments, session, claimed_namespace) if connected?(socket) and writer?, - do: UnfinalWeb.Presence.track(self(), "editing", storage_path, %{path: storage_path}) + do: UnfinalWeb.Presence.track(self(), "editing", storage_path, %{path: storage_path, joined_at: System.system_time(:second)}) if connected?(socket) and not writer?, do: Phoenix.PubSub.subscribe(Unfinal.PubSub, Documents.topic(storage_path)) diff --git a/lib/unfinal_web/live/live_live.ex b/lib/unfinal_web/live/live_live.ex index a68d3d9..34a5459 100644 --- a/lib/unfinal_web/live/live_live.ex +++ b/lib/unfinal_web/live/live_live.ex @@ -23,6 +23,7 @@ defmodule UnfinalWeb.LiveLive do {:ok, assign(socket, active_paths: active_paths, + sorted_paths: sorted_paths(), excerpts: excerpts(active_paths, %{}), authenticated: authenticated, user: user, @@ -34,6 +35,7 @@ defmodule UnfinalWeb.LiveLive do {:ok, assign(socket, active_paths: MapSet.new(), + sorted_paths: [], excerpts: %{}, authenticated: authenticated, user: user, @@ -59,7 +61,7 @@ defmodule UnfinalWeb.LiveLive do excerpts = excerpts(active_paths, socket.assigns.excerpts) - {:noreply, assign(socket, active_paths: active_paths, excerpts: excerpts)} + {:noreply, assign(socket, active_paths: active_paths, sorted_paths: sorted_paths(), excerpts: excerpts)} end def handle_info({:content_updated, path, %{content: content}}, socket) do @@ -88,6 +90,14 @@ defmodule UnfinalWeb.LiveLive do @topic |> Presence.list() |> Map.keys() |> MapSet.new() end + defp sorted_paths do + @topic + |> Presence.list() + |> Enum.map(fn {_key, %{metas: [meta | _]}} -> {meta.path, meta.joined_at} end) + |> Enum.sort_by(fn {_, ts} -> -ts end) + |> Enum.map(fn {path, _} -> path end) + end + defp excerpts(active_paths, current_excerpts) do active_paths |> Enum.map(fn path -> {path, initial_content(path, current_excerpts)} end) @@ -137,7 +147,7 @@ defmodule UnfinalWeb.LiveLive do