From e1b12a778211378534fa176bbc4456c3a100b23f Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 17 Feb 2018 20:13:12 +0100 Subject: [PATCH] ActivityPub: Handle incoming likes. --- .../web/activity_pub/transmogrifier.ex | 15 ++++++++++ test/fixtures/mastodon-like.json | 29 +++++++++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 17 +++++++++++ 3 files changed, 61 insertions(+) create mode 100644 test/fixtures/mastodon-like.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 6cf7cfe35..82fcf0898 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -3,6 +3,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do A module to handle coding from internal to wire ActivityPub and back. """ alias Pleroma.User + alias Pleroma.Object alias Pleroma.Web.ActivityPub.ActivityPub @doc """ @@ -62,6 +63,20 @@ def handle_incoming(%{"type" => "Follow", "object" => followed, "actor" => follo end end + def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do + with %User{} = actor <- User.get_or_fetch_by_ap_id(actor), + %Object{} = object <- Object.get_by_ap_id(object_id), + {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do + {:ok, activity} + else + _e -> :error + end + end + + # TODO + # Accept + # Undo + def handle_incoming(_), do: :error @doc diff --git a/test/fixtures/mastodon-like.json b/test/fixtures/mastodon-like.json new file mode 100644 index 000000000..39fb44c4a --- /dev/null +++ b/test/fixtures/mastodon-like.json @@ -0,0 +1,29 @@ +{ + "type": "Like", + "signature": { + "type": "RsaSignature2017", + "signatureValue": "fdxMfQSMwbC6wP6sh6neS/vM5879K67yQkHTbiT5Npr5wAac0y6+o3Ij+41tN3rL6wfuGTosSBTHOtta6R4GCOOhCaCSLMZKypnp1VltCzLDoyrZELnYQIC8gpUXVmIycZbREk22qWUe/w7DAFaKK4UscBlHDzeDVcA0K3Se5Sluqi9/Zh+ldAnEzj/rSEPDjrtvf5wGNf3fHxbKSRKFt90JvKK6hS+vxKUhlRFDf6/SMETw+EhwJSNW4d10yMUakqUWsFv4Acq5LW7l+HpYMvlYY1FZhNde1+uonnCyuQDyvzkff8zwtEJmAXC4RivO/VVLa17SmqheJZfI8oluVg==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T18:57:49Z" + }, + "object": "http://localtesting.pleroma.lol/objects/eb92579d-3417-42a8-8652-2492c2d4f454", + "nickname": "lain", + "id": "http://mastodon.example.org/users/admin#likes/2", + "actor": "http://mastodon.example.org/users/admin", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "toot": "http://joinmastodon.org/ns#", + "sensitive": "as:sensitive", + "ostatus": "http://ostatus.org#", + "movedTo": "as:movedTo", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "atomUri": "ostatus:atomUri", + "Hashtag": "as:Hashtag", + "Emoji": "toot:Emoji" + } + ] +} \ No newline at end of file diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 57598d020..2e7586227 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -3,6 +3,9 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Activity alias Pleroma.User + alias Pleroma.Repo + import Ecto.Query + import Pleroma.Factory alias Pleroma.Web.CommonAPI @@ -43,7 +46,21 @@ test "it works for incoming follow requests" do assert data["type"] == "Follow" assert data["id"] == "http://mastodon.example.org/users/admin#follows/2" assert User.following?(User.get_by_ap_id(data["actor"]), user) + end + test "it works for incoming likes" do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"}) + + data = File.read!("test/fixtures/mastodon-like.json") |> Poison.decode! + |> Map.put("object", activity.data["object"]["id"]) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == "http://mastodon.example.org/users/admin" + assert data["type"] == "Like" + assert data["id"] == "http://mastodon.example.org/users/admin#likes/2" + assert data["object"] == activity.data["object"]["id"] end end