feat: Deleting posts

This commit is contained in:
Ro 2024-11-01 19:07:40 -05:00
parent 1bd8ce9c6b
commit b49925b830
Signed by: ro
GPG key ID: 5B5AD5A568CDABF9
5 changed files with 53 additions and 4 deletions

View file

@ -12,7 +12,7 @@
- [x] My posts
- [x] Posts from accounts you follow
- [x] Show the actor avatar and display name
- [ ] Deleting posts
- [x] Deleting posts
- [x] Profile
- [x] Name field (for display name)
- [x] Bust actor cache when you update your profile
@ -31,6 +31,7 @@
- [ ] DMs
- [ ] Support authenticated fetch of outbox (by allowed domains / servers)
- [ ] Followers-only posts (or maybe this is handled because we only send posts to followers? but we also include public in the TO field?)
- [ ] Check the domain of the public key against the domain of the object being CRUDed
## UX

View file

@ -58,6 +58,21 @@ defmodule Postland.Activities do
|> broadcast_to_followers(activity)
end
def delete_post(post) do
activity =
%{
"@context" => "https://www.w3.org/ns/activitystreams",
"id" => url(~p"/activities/#{Ecto.UUID.autogenerate()}"),
"type" => "Delete",
"actor" => Postland.my_actor_id(),
"object" => post.id
}
post
|> Repo.delete()
|> broadcast_to_followers(activity)
end
def broadcast_to_followers({:ok, _} = result, activity) do
my_actor_id = Postland.my_actor_id()
private_key = Postland.Accounts.solo_user().private_key
@ -65,8 +80,6 @@ defmodule Postland.Activities do
Postland.Follows.all_followers()
|> Enum.map(fn %{follower: follower} ->
inbox = Postland.Actors.inbox(follower)
dbg(follower)
dbg(inbox)
case inbox do
nil ->

11
lib/postland/objects.ex Normal file
View file

@ -0,0 +1,11 @@
defmodule Postland.Objects do
alias Postland.Object
alias Postland.Repo
import Ecto.Query
def get_by_id(id) do
from(o in Object, where: o.id == ^id)
|> Repo.one()
end
end

View file

@ -115,6 +115,15 @@ defmodule PostlandWeb.CoreComponents do
<%= {:safe, Postland.Timeline.html_content(@post)} %>
</div>
<div class="px-4 py-4 sm:px-6">
<a
:if={@author["id"] == Postland.my_actor_id()}
href="#"
phx-click="delete_post"
phx-value-post-dom-id={@post_dom_id}
phx-value-post-id={@post.id}
>
<.icon name="hero-trash" />
</a>
<!-- Content goes here -->
<!-- We use less vertical padding on card footers at all sizes than on headers or body sections -->
</div>

View file

@ -1,12 +1,15 @@
defmodule PostlandWeb.TimelineLive do
use PostlandWeb, :live_view
alias Postland.Activities
alias Postland.Objects
def render(assigns) do
~H"""
<.post_form post_content={@post} />
<div id="timeline-posts" phx-update="stream">
<div :for={{id, post} <- @streams.posts} id={id} class="mt-10">
<.post_card post={post} />
<.post_card post={post} post_dom_id={id} />
</div>
</div>
"""
@ -37,6 +40,18 @@ defmodule PostlandWeb.TimelineLive do
{:noreply, socket}
end
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
def handle_event("change_post", %{"post" => post}, socket) do
{:noreply, socket |> assign(:post, post)}
end