feat: Like and unlike posts
This commit is contained in:
parent
0bca082286
commit
9ad0c9b3fd
6 changed files with 94 additions and 2 deletions
|
|
@ -41,8 +41,8 @@
|
||||||
- [x] Show the actor avatar and display name
|
- [x] Show the actor avatar and display name
|
||||||
- [x] Receiving posts w/ images
|
- [x] Receiving posts w/ images
|
||||||
- [ ] Receiving posts w/ videos
|
- [ ] Receiving posts w/ videos
|
||||||
- [ ] Liking posts
|
- [x] Liking posts
|
||||||
- [ ] Unliking posts
|
- [x] Unliking posts
|
||||||
- [x] Displaying CW posts behind CW
|
- [x] Displaying CW posts behind CW
|
||||||
- [ ] Displaying polls
|
- [ ] Displaying polls
|
||||||
- [ ] Voting in polls
|
- [ ] Voting in polls
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ defmodule Postland.Activities do
|
||||||
alias Postland.Activity
|
alias Postland.Activity
|
||||||
alias Postland.Repo
|
alias Postland.Repo
|
||||||
alias Postland.Follows
|
alias Postland.Follows
|
||||||
|
alias Postland.Object
|
||||||
|
|
||||||
def record_markdown_post(markdown, cw, attachments) do
|
def record_markdown_post(markdown, cw, attachments) do
|
||||||
id = Ecto.UUID.autogenerate()
|
id = Ecto.UUID.autogenerate()
|
||||||
|
|
@ -82,6 +83,42 @@ defmodule Postland.Activities do
|
||||||
|> broadcast_to_followers(activity)
|
|> broadcast_to_followers(activity)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def like_post(%{id: post_id} = post) do
|
||||||
|
like_id = url(~p"/activities/#{Ecto.UUID.autogenerate()}")
|
||||||
|
|
||||||
|
activity =
|
||||||
|
%{
|
||||||
|
"id" => like_id,
|
||||||
|
"@context" => "https://www.w3.org/ns/activitystreams",
|
||||||
|
"type" => "Like",
|
||||||
|
"actor" => Postland.my_actor_id(),
|
||||||
|
"object" => post_id
|
||||||
|
}
|
||||||
|
|
||||||
|
Multi.new()
|
||||||
|
|> Multi.insert(:activity, Activity.changeset(activity))
|
||||||
|
|> Multi.update(:update_meta_i_liked, Object.like_changeset(post, like_id))
|
||||||
|
|> Repo.transaction()
|
||||||
|
|> broadcast_to_followers(activity)
|
||||||
|
end
|
||||||
|
|
||||||
|
def unlike_post(%{my_like_id: like_id} = post) do
|
||||||
|
activity =
|
||||||
|
%{
|
||||||
|
"id" => url(~p"/activities/#{Ecto.UUID.autogenerate()}"),
|
||||||
|
"@context" => "https://www.w3.org/ns/activitystreams",
|
||||||
|
"type" => "Undo",
|
||||||
|
"actor" => Postland.my_actor_id(),
|
||||||
|
"object" => like_id
|
||||||
|
}
|
||||||
|
|
||||||
|
Multi.new()
|
||||||
|
|> Multi.insert(:activity, Activity.changeset(activity))
|
||||||
|
|> Multi.update(:object, Object.unlike_changeset(post))
|
||||||
|
|> Repo.transaction()
|
||||||
|
|> broadcast_to_followers(activity)
|
||||||
|
end
|
||||||
|
|
||||||
def broadcast_to_followers({:ok, _} = result, activity) do
|
def broadcast_to_followers({:ok, _} = result, activity) do
|
||||||
my_actor_id = Postland.my_actor_id()
|
my_actor_id = Postland.my_actor_id()
|
||||||
private_key = Postland.Accounts.solo_user().private_key
|
private_key = Postland.Accounts.solo_user().private_key
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ defmodule Postland.Object do
|
||||||
schema "objects" do
|
schema "objects" do
|
||||||
field :type, :string
|
field :type, :string
|
||||||
field :data, :map
|
field :data, :map
|
||||||
|
field :my_like_id, :string
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
@ -24,4 +25,14 @@ defmodule Postland.Object do
|
||||||
|> validate_required(:data)
|
|> validate_required(:data)
|
||||||
|> validate_required(:type)
|
|> validate_required(:type)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def like_changeset(post, like_id) do
|
||||||
|
post
|
||||||
|
|> cast(%{"my_like_id" => like_id}, [:my_like_id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def unlike_changeset(post) do
|
||||||
|
post
|
||||||
|
|> cast(%{"my_like_id" => nil}, [:my_like_id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -253,6 +253,15 @@ defmodule PostlandWeb.CoreComponents do
|
||||||
>
|
>
|
||||||
<.icon name="hero-trash" />
|
<.icon name="hero-trash" />
|
||||||
</a>
|
</a>
|
||||||
|
<a
|
||||||
|
href="#"
|
||||||
|
phx-click={if @post.my_like_id, do: "unlike_post", else: "like_post"}
|
||||||
|
phx-value-post-dom-id={@post_dom_id}
|
||||||
|
phx-value-post-id={@post.id}
|
||||||
|
class={if @post.my_like_id, do: "text-yellow-500", else: "text-gray-500"}
|
||||||
|
>
|
||||||
|
<.icon name="hero-star" />
|
||||||
|
</a>
|
||||||
<!-- Content goes here -->
|
<!-- Content goes here -->
|
||||||
<!-- We use less vertical padding on card footers at all sizes than on headers or body sections -->
|
<!-- We use less vertical padding on card footers at all sizes than on headers or body sections -->
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,32 @@ defmodule PostlandWeb.TimelineLive do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_event("like_post", %{"post-id" => id, "post-dom-id" => dom_id}, socket) do
|
||||||
|
post = Objects.get_by_id(id)
|
||||||
|
|
||||||
|
case Activities.like_post(post) do
|
||||||
|
{:ok, _} ->
|
||||||
|
post = Objects.get_by_id(id)
|
||||||
|
{:noreply, socket |> stream_insert(:posts, post)}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:noreply, socket |> put_flash(:error, "An unexpected error has occurred.")}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("unlike_post", %{"post-id" => id, "post-dom-id" => dom_id}, socket) do
|
||||||
|
post = Objects.get_by_id(id)
|
||||||
|
|
||||||
|
case Activities.unlike_post(post) do
|
||||||
|
{:ok, _} ->
|
||||||
|
post = Objects.get_by_id(id)
|
||||||
|
{:noreply, socket |> stream_insert(:posts, post)}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:noreply, socket |> put_flash(:error, "An unexpected error has occurred.")}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def handle_event("change_post", %{"post" => post} = params, socket) do
|
def handle_event("change_post", %{"post" => post} = params, socket) do
|
||||||
cw = Map.get(params, "cw")
|
cw = Map.get(params, "cw")
|
||||||
{:noreply, socket |> assign(post: post, cw: cw)}
|
{:noreply, socket |> assign(post: post, cw: cw)}
|
||||||
|
|
|
||||||
9
priv/repo/migrations/20241118235445_add_my_like_id.exs
Normal file
9
priv/repo/migrations/20241118235445_add_my_like_id.exs
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
defmodule Postland.Repo.Migrations.AddMyLikeId do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table("objects") do
|
||||||
|
add :my_like_id, :string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue