TwitterAPI: Fix mentions.
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
index af6b5befce6bca012db51f1aff80ce65483d112f..e53957fbf5f5bbb2cee9fc8cbe3ff9c50013a9e8 100644 (file)
@@ -14,6 +14,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     object
     |> Map.put("actor", object["attributedTo"])
     |> fix_attachments
+    |> fix_context
+  end
+
+  def fix_context(object) do
+    object
+    |> Map.put("context", object["conversation"])
   end
 
   def fix_attachments(object) do
@@ -31,7 +37,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   # - tags
   # - emoji
   def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
-    with %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
+    with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
+         %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
       object = fix_object(data["object"])
       params = %{
         to: data["to"],
@@ -48,6 +55,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
       ActivityPub.create(params)
     else
+      %Activity{} = activity -> {:ok, activity}
       _e -> :error
     end
   end
@@ -100,9 +108,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   """
   def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
     object = object
+    |> set_sensitive
+    |> add_hashtags
     |> add_mention_tags
     |> add_attributed_to
     |> prepare_attachments
+    |> set_conversation
 
     data = data
     |> Map.put("object", object)
@@ -111,15 +122,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     {:ok, data}
   end
 
-  def prepare_outgoing(%{"type" => type} = data) when type in ["Follow", "Accept", "Like", "Announce"] do
+  def prepare_outgoing(%{"type" => type} = data) do
     data = data
     |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
 
     {:ok, data}
   end
 
+  def add_hashtags(object) do
+    tags = (object["tag"] || [])
+    |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
+
+    object
+    |> Map.put("tag", tags)
+  end
+
   def add_mention_tags(object) do
-    mentions = object["to"]
+    recipients = object["to"] ++ (object["cc"] || [])
+    mentions = recipients
     |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
     |> Enum.filter(&(&1))
     |> Enum.map(fn(user) -> %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"} end)
@@ -130,6 +150,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     |> Map.put("tag", tags ++ mentions)
   end
 
+  def set_conversation(object) do
+    Map.put(object, "conversation", object["context"])
+  end
+
+  def set_sensitive(object) do
+    tags = object["tag"] || []
+    Map.put(object, "sensitive", "nsfw" in tags)
+  end
+
   def add_attributed_to(object) do
     attributedTo = object["attributedTo"] || object["actor"]