Add announcements to activitypub.
authorRoger Braun <roger@rogerbraun.net>
Sat, 15 Apr 2017 10:11:20 +0000 (12:11 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sat, 15 Apr 2017 10:11:20 +0000 (12:11 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index 75de2e43c65625b2bce975ec471fdaab9ff6fcad..a38f63b0312db40c7c39df07ed75d85fa49972e0 100644 (file)
@@ -137,6 +137,30 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> Enum.reverse
   end
 
+  def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
+    data = %{
+      "type" => "Announce",
+      "actor" => ap_id,
+      "object" => id,
+      "to" => [User.ap_followers(user)]
+    }
+
+    {:ok, activity} = insert(data)
+
+    announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq
+
+    new_data = object.data
+    |> Map.put("announcement_count", length(announcements))
+    |> Map.put("announcements", announcements)
+
+    changeset = Ecto.Changeset.change(object, data: new_data)
+    {:ok, object} = Repo.update(changeset)
+
+    update_object_in_activities(object)
+
+    {:ok, activity, object}
+  end
+
   def fetch_activities_for_context(context) do
     query = from activity in Activity,
       where: fragment("? @> ?", activity.data, ^%{ context: context })
index 203dcaec29fc6ef4ed7b2e97d4aa6d36bb4a19e7..b5f81da0ce21fc61b178089528285c1a529b8bb7 100644 (file)
@@ -162,6 +162,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  describe "announcing an object" do
+    test "adds an announce activity to the db" do
+      note_activity = insert(:note_activity)
+      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+      user = insert(:user)
+
+      {:ok, announce_activity, object} = ActivityPub.announce(user, object)
+      assert object.data["announcement_count"] == 1
+      assert object.data["announcements"] == [user.ap_id]
+      assert announce_activity.data["to"] == [User.ap_followers(user)]
+      assert announce_activity.data["object"] == object.data["id"]
+      assert announce_activity.data["actor"] == user.ap_id
+    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"}