Add context and inReplyTo.
authorRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 22:34:10 +0000 (23:34 +0100)
committerRoger Braun <roger@rogerbraun.net>
Thu, 23 Mar 2017 22:34:10 +0000 (23:34 +0100)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/twitter_api/twitter_api.ex
test/web/twitter_api/twitter_api_test.exs

index e7427affbf58eccbacf75889290b1bfb0b2c8c3d..d14ce35702c273ed5aec7f384099125bcb823c9d 100644 (file)
@@ -17,19 +17,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
 
   def generate_activity_id do
-    host =
-      Application.get_env(:pleroma, Pleroma.Web.Endpoint)
-      |> Keyword.fetch!(:url)
-      |> Keyword.fetch!(:host)
-    "https://#{host}/activities/#{Ecto.UUID.generate}"
+    generate_id("activities")
+  end
+
+  def generate_context_id do
+    generate_id("contexts")
   end
 
   def generate_object_id do
+    generate_id("objects")
+  end
+
+  def generate_id(type) do
     host =
       Application.get_env(:pleroma, Pleroma.Web.Endpoint)
       |> Keyword.fetch!(:url)
       |> Keyword.fetch!(:host)
-    "https://#{host}/objects/#{Ecto.UUID.generate}"
+    "https://#{host}/#{type}/#{Ecto.UUID.generate}"
   end
 
   def fetch_public_activities(opts \\ %{}) do
index 58fd51a62bcd63433a644115f6018582b33fb7fa..b982832480baea08c74e30af4bf2b6ad68e1f84d 100644 (file)
@@ -1,11 +1,12 @@
 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
-  alias Pleroma.User
+  alias Pleroma.{User, Activity, Repo}
   alias Pleroma.Web.ActivityPub.ActivityPub
-  alias Pleroma.Repo
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
   def create_status(user = %User{}, data = %{}) do
     date = DateTime.utc_now() |> DateTime.to_iso8601
+
+    context = ActivityPub.generate_context_id
     activity = %{
       "type" => "Create",
       "to" => [
@@ -16,11 +17,27 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
       "object" => %{
         "type" => "Note",
         "content" => data["status"],
-        "published" => date
+        "published" => date,
+        "context" => context
       },
-      "published" => date
+      "published" => date,
+      "context" => context
     }
 
+    # Wire up reply info.
+    activity = with inReplyToId when not is_nil(inReplyToId) <- data["in_reply_to_status_id"],
+                    inReplyTo <- Repo.get(Activity, inReplyToId),
+                    context <- inReplyTo.data["context"]
+               do
+               activity
+               |> put_in(["context"], context)
+               |> put_in(["object", "context"], context)
+               |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
+               |> put_in(["object", "inReplyToStatusId"], inReplyToId)
+               else _e ->
+                 activity
+               end
+
     ActivityPub.insert(activity)
   end
 
index f68a49f6a2695eaa84e2967a3f77389acb1c68c2..507db5488c631564ac22883030d7423f07022296 100644 (file)
@@ -18,6 +18,32 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert get_in(activity.data, ["actor"]) == User.ap_id(user)
     assert Enum.member?(get_in(activity.data, ["to"]), User.ap_followers(user))
     assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
+
+    # Add a context
+    assert is_binary(get_in(activity.data, ["context"]))
+    assert is_binary(get_in(activity.data, ["object", "context"]))
+  end
+
+  test "create a status that is a reply" do
+    user = UserBuilder.build
+    input = %{
+      "status" => "Hello again."
+    }
+
+    { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
+
+    input = %{
+      "status" => "Here's your (you).",
+      "in_reply_to_status_id" => activity.id
+    }
+
+    { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input)
+
+    assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"])
+    assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
+    assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"])
+    assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"])
+    assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id
   end
 
   test "fetch public statuses" do