ActivityPub: Refactor create function.
[akkoma] / lib / pleroma / web / ostatus / handlers / note_handler.ex
index 0b06f8f02d17c1fc4709bd503578d15572536146..7b7ed2d5a4a09078b1dd5761849867deefd918e7 100644 (file)
@@ -52,6 +52,17 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
     |> Enum.filter(&(&1))
   end
 
+  def get_emoji(entry) do
+    try do
+      :xmerl_xpath.string('//link[@rel="emoji"]', entry)
+      |> Enum.reduce(%{}, fn(emoji, acc) ->
+        Map.put(acc, XML.string_from_xpath("@name", emoji), XML.string_from_xpath("@href", emoji))
+      end)
+    rescue
+      _e -> nil
+    end
+  end
+
   def make_to_list(actor, mentions) do
     [
       actor.follower_address
@@ -83,6 +94,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
          [author] <- :xmerl_xpath.string('//author[1]', doc),
          {:ok, actor} <- OStatus.find_make_or_update_user(author),
          content_html <- OStatus.get_content(entry),
+         cw <- OStatus.get_cw(entry),
          inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry),
          inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo),
          inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo,
@@ -92,15 +104,16 @@ defmodule Pleroma.Web.OStatus.NoteHandler do
          mentions <- get_mentions(entry),
          to <- make_to_list(actor, mentions),
          date <- XML.string_from_xpath("//published", entry),
-         note <- CommonAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, []),
+         note <- CommonAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, [], cw),
          note <- note |> Map.put("id", id) |> Map.put("tag", tags),
          note <- note |> Map.put("published", date),
+         note <- note |> Map.put("emoji", get_emoji(entry)),
          note <- add_external_url(note, entry),
          # TODO: Handle this case in make_note_data
          note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note)
       do
-      res = ActivityPub.create(to, actor, context, note, %{}, date, false)
-      User.update_note_count(actor)
+      res = ActivityPub.create(%{to: to, actor: actor, context: context, object: note, published: date, local: false})
+      User.increase_note_count(actor)
       res
     else
       %Activity{} = activity -> {:ok, activity}