Add liking to ActivityPub.
authorRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 13:50:05 +0000 (15:50 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 13 Apr 2017 13:50:05 +0000 (15:50 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index 75e4101f281d8a7b41e43891993a0407f2075cad..10efa2c9d4ad902709ba42c4e1800684e07e1463 100644 (file)
@@ -1,10 +1,12 @@
 defmodule Pleroma.Web.ActivityPub.ActivityPub do
   alias Pleroma.Repo
-  alias Pleroma.{Activity, Object, Upload}
+  alias Pleroma.{Activity, Object, Upload, User}
   import Ecto.Query
 
   def insert(map) when is_map(map) do
-    map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
+    map = map
+    |> Map.put_new_lazy("id", &generate_activity_id/0)
+    |> Map.put_new_lazy("published", &make_date/0)
 
     map = if is_map(map["object"]) do
       object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
@@ -17,6 +19,30 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     Repo.insert(%Activity{data: map})
   end
 
+  def like(%User{ap_id: ap_id}, object = %Object{data: %{ "id" => id}}) do
+    data = %{
+      "type" => "Like",
+      "actor" => ap_id,
+      "object" => id
+    }
+
+    {:ok, activity} = insert(data)
+    like_count = (object.data["like_count"] || 0) + 1
+    new_data = object.data |> Map.put("like_count", like_count)
+    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)
+
+    {:ok, activity, object}
+  end
+
   def generate_activity_id do
     generate_id("activities")
   end
@@ -74,4 +100,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     data = Upload.store(file)
     Repo.insert(%Object{data: data})
   end
+
+  defp make_date do
+    DateTime.utc_now() |> DateTime.to_iso8601
+  end
 end
index 5cfd46238b1077eabcadf1655f68188af9c4c34c..2bdd439b022e9ec0d8eb10f94b289b5205cb1a41 100644 (file)
@@ -4,6 +4,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   alias Pleroma.{Activity, Object}
   alias Pleroma.Builders.ActivityBuilder
 
+  import Pleroma.Factory
+
   describe "insertion" do
     test "inserts a given map into the activity database, giving it an id if it has none." do
       data = %{
@@ -110,6 +112,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
+  describe "like an object" do
+    test "adds a like activity to the db" do
+      note_activity = insert(:note_activity)
+      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+      user = insert(:user)
+      user_two = insert(:user)
+
+      {:ok, like_activity, object} = ActivityPub.like(user, object)
+
+      assert like_activity.data["actor"] == user.ap_id
+      assert like_activity.data["type"] == "Like"
+      assert like_activity.data["object"] == object.data["id"]
+      assert object.data["like_count"] == 1
+
+      [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
+      assert note_activity.data["object"]["like_count"] == 1
+
+      {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
+      assert object.data["like_count"] == 2
+    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"}