From a926038cd096d3340749f071a183c46a987e8c77 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 14 Apr 2017 18:08:47 +0200 Subject: [PATCH] Add unliking to activitypub. --- TODO.txt | 4 ++ lib/pleroma/web/activity_pub/activity_pub.ex | 48 ++++++++++++++++---- test/support/factory.ex | 4 +- test/web/activity_pub/activity_pub_test.exs | 20 ++++++++ 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/TODO.txt b/TODO.txt index 42501a637..dd85c5239 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1 +1,5 @@ - Add cache for user fetching / representing. (mostly in TwitterAPI.activity_to_status) + +Unliking: + +- Add a proper undo activity, find out how to ignore those in twitter api. diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index b9ba72b0d..75de2e43c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -24,7 +24,7 @@ def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do # There's already a like here, so return the original activity. ap_id in (object.data["likes"] || []) -> query = from activity in Activity, - where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id}) + where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"}) activity = Repo.one(query) {:ok, activity, object} @@ -47,17 +47,49 @@ def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do changeset = Ecto.Changeset.change(object, data: new_data) {:ok, object} = Repo.update(changeset) - # Update activities that already had this. Could be done in a seperate process. - relevant_activities = Activity.all_by_object_ap_id(id) - Enum.map(relevant_activities, fn (activity) -> - new_activity_data = activity.data |> Map.put("object", new_data) - changeset = Ecto.Changeset.change(activity, data: new_activity_data) - Repo.update(changeset) - end) + update_object_in_activities(object) + {:ok, activity, object} end end + defp update_object_in_activities(%{data: %{"id" => id}} = object) do + # Update activities that already had this. Could be done in a seperate process. + relevant_activities = Activity.all_by_object_ap_id(id) + Enum.map(relevant_activities, fn (activity) -> + new_activity_data = activity.data |> Map.put("object", object.data) + changeset = Ecto.Changeset.change(activity, data: new_activity_data) + Repo.update(changeset) + end) + end + + def unlike(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do + query = from activity in Activity, + where: fragment("? @> ?", activity.data, ^%{actor: ap_id, object: id, type: "Like"}) + + activity = Repo.one(query) + + if activity do + # just delete for now... + {:ok, _activity} = Repo.delete(activity) + + likes = (object.data["likes"] || []) |> List.delete(ap_id) + + new_data = object.data + |> Map.put("like_count", length(likes)) + |> Map.put("likes", likes) + + changeset = Ecto.Changeset.change(object, data: new_data) + {:ok, object} = Repo.update(changeset) + + update_object_in_activities(object) + + {:ok, object} + else + {:ok, object} + end + end + def generate_activity_id do generate_id("activities") end diff --git a/test/support/factory.ex b/test/support/factory.ex index af8e58bd1..3fc9cf710 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -22,7 +22,9 @@ def note_factory do "id" => Pleroma.Web.ActivityPub.ActivityPub.generate_object_id, "actor" => user.ap_id, "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "published_at" => DateTime.utc_now() |> DateTime.to_iso8601 + "published_at" => DateTime.utc_now() |> DateTime.to_iso8601, + "likes" => [], + "like_count" => 0 } %Pleroma.Object{ diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 570120484..203dcaec2 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -142,6 +142,26 @@ test "adds a like activity to the db" do end end + describe "unliking" do + test "unliking a previously liked object" do + note_activity = insert(:note_activity) + object = Object.get_by_ap_id(note_activity.data["object"]["id"]) + user = insert(:user) + + # Unliking something that hasn't been liked does nothing + {:ok, object} = ActivityPub.unlike(user, object) + assert object.data["like_count"] == 0 + + {:ok, like_activity, object} = ActivityPub.like(user, object) + assert object.data["like_count"] == 1 + + {:ok, object} = ActivityPub.unlike(user, object) + assert object.data["like_count"] == 0 + + assert Repo.get(Activity, like_activity.id) == nil + end + end + describe "uploading files" do test "copies the file to the configured folder" do file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}