postland/lib/postland_web/live/timeline_live.ex

132 lines
3.4 KiB
Elixir
Raw Normal View History

2024-10-16 15:30:23 +00:00
defmodule PostlandWeb.TimelineLive do
use PostlandWeb, :live_view
2024-11-02 00:07:40 +00:00
alias Postland.Activities
alias Postland.Objects
2024-10-16 15:30:23 +00:00
def render(assigns) do
~H"""
2024-11-06 02:53:20 +00:00
<div id="timeline">
2024-11-06 04:19:26 +00:00
<.post_form post_content={@post} attachments={@attachments} upload={@uploads.files} cw={@cw} />
2024-11-06 02:53:20 +00:00
<div id="timeline-posts" phx-update="stream">
<div :for={{id, post} <- @streams.posts} id={id} class="mt-10">
<.post_card post={post} post_dom_id={id} />
</div>
2024-10-16 15:30:23 +00:00
</div>
</div>
"""
end
def mount(_params, _session, socket) do
2024-11-06 02:14:49 +00:00
socket =
socket
|> assign(:post, "")
|> assign(:attachments, [])
2024-11-06 04:19:26 +00:00
|> assign(:cw, nil)
2024-11-06 02:14:49 +00:00
|> stream(:posts, Postland.Timeline.timeline())
|> allow_upload(:files,
accept: ["image/*"],
auto_upload: true,
progress: &handle_progress/3
)
{:ok, socket}
2024-10-16 15:30:23 +00:00
end
def handle_event("create_post", %{"post" => post}, socket) do
2024-11-06 04:19:26 +00:00
{:ok, results} =
Postland.Activities.record_markdown_post(
post,
socket.assigns.cw,
socket.assigns.attachments
)
2024-10-16 15:30:23 +00:00
2024-10-22 15:50:26 +00:00
new_posts =
results
|> Enum.filter(fn {_key, value} ->
is_struct(value, Postland.Object)
end)
socket =
socket
|> assign(:post, "")
2024-11-06 02:14:49 +00:00
|> assign(:attachments, [])
2024-11-06 04:19:26 +00:00
|> assign(:cw, nil)
2024-10-22 15:50:26 +00:00
|> then(fn socket ->
Enum.reduce(new_posts, socket, fn {_id, post}, socket ->
stream_insert(socket, :posts, post, at: 0)
end)
end)
{:noreply, socket}
2024-10-16 15:30:23 +00:00
end
2024-11-02 00:07:40 +00:00
def handle_event("delete_post", %{"post-id" => id, "post-dom-id" => dom_id}, socket) do
post = Objects.get_by_id(id)
case Activities.delete_post(post) do
{:ok, _} ->
{:noreply, socket |> stream_delete_by_dom_id(:posts, dom_id)}
_ ->
{:noreply, socket |> put_flash(:error, "An unexpected error has occurred.")}
end
end
2024-11-06 04:19:26 +00:00
def handle_event("change_post", %{"post" => post} = params, socket) do
cw = Map.get(params, "cw")
{:noreply, socket |> assign(post: post, cw: cw)}
2024-10-16 15:30:23 +00:00
end
2024-11-06 02:14:49 +00:00
def handle_event("remove_attachment", %{"url" => url}, socket) do
attachments =
socket.assigns.attachments
|> Enum.reject(fn %{"url" => attachment_url} -> attachment_url == url end)
{:noreply, assign(socket, attachments: attachments)}
end
def handle_event("change_alt_text", %{"alt" => value, "url" => url}, socket) do
2024-11-06 02:53:20 +00:00
attachments =
socket.assigns.attachments
|> Enum.map(fn
%{"url" => ^url} = attachment ->
Map.put(attachment, "name", value)
other ->
other
end)
{:noreply, assign(socket, attachments: attachments)}
2024-11-06 02:14:49 +00:00
end
2024-11-06 04:19:26 +00:00
def handle_event("add_cw", _, socket) do
{:noreply, assign(socket, cw: "")}
end
def handle_event("remove_cw", _, socket) do
{:noreply, assign(socket, cw: nil)}
end
2024-11-06 02:14:49 +00:00
def handle_progress(:files, entry, socket) do
if entry.done? do
uploaded_file =
consume_uploaded_entry(socket, entry, fn meta ->
Postland.Uploads.handle_upload(entry, meta)
end)
attachment = %{
"type" => "Document",
"mediaType" => entry.client_type,
"url" => unverified_url(PostlandWeb.Endpoint, uploaded_file),
"name" => nil
}
attachments = socket.assigns.attachments ++ [attachment]
{:noreply, assign(socket, attachments: attachments)}
else
{:noreply, socket}
end
end
2024-10-16 15:30:23 +00:00
end