Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/unfinal/sqlite_documents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ defmodule Unfinal.SqliteDocuments do
end
end

@doc "List most recently edited documents across all namespaces."
@spec recent_edits(non_neg_integer()) :: [%{path: String.t(), updated_at: String.t()}]
def recent_edits(limit \\ 20) when is_integer(limit) and limit > 0 do
sql = "SELECT path, updated_at FROM documents WHERE updated_at IS NOT NULL ORDER BY updated_at DESC LIMIT ?1"

case query(sql, [limit]) do
{:ok, %{rows: rows}} ->
Enum.map(rows, fn [path, upd] -> %{path: path, updated_at: upd} end)

{:error, _} ->
[]
end
end

@doc "Count all document rows."
@spec count_documents() :: non_neg_integer()
def count_documents do
Expand Down
24 changes: 22 additions & 2 deletions lib/unfinal_web/live/live_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule UnfinalWeb.LiveLive do

alias Unfinal.Documents
alias Unfinal.NamespaceStore
alias Unfinal.SqliteDocuments
alias UnfinalWeb.Layouts
alias UnfinalWeb.Presence

Expand All @@ -21,12 +22,16 @@ defmodule UnfinalWeb.LiveLive do
active_paths = active_paths()
Enum.each(active_paths, &Phoenix.PubSub.subscribe(Unfinal.PubSub, Documents.topic(&1)))

recent_edits = seed_recent_edits()
recent_paths = Map.keys(recent_edits) -- MapSet.to_list(active_paths)
excerpts = excerpts(MapSet.union(active_paths, MapSet.new(recent_paths)), %{})

{:ok,
assign(socket,
active_paths: active_paths,
sorted_paths: sorted_paths(),
excerpts: excerpts(active_paths, %{}),
recent_edits: %{},
excerpts: excerpts,
recent_edits: recent_edits,
authenticated: authenticated,
user: user,
claimed_namespace: claimed_namespace,
Expand Down Expand Up @@ -103,6 +108,21 @@ defmodule UnfinalWeb.LiveLive do

defp claimed_namespace(_session), do: nil

defp seed_recent_edits do
SqliteDocuments.recent_edits(20)
|> Enum.map(fn %{path: path, updated_at: updated_at} ->
{path, parse_timestamp(updated_at)}
end)
|> Map.new()
end

defp parse_timestamp(iso_string) do
case DateTime.from_iso8601(iso_string) do
{:ok, dt, _} -> DateTime.to_unix(dt)
_ -> 0
end
end

defp active_paths do
@topic |> Presence.list() |> Map.keys() |> MapSet.new()
end
Expand Down
Loading