twitterapi: more object normalization work
authorWilliam Pitcock <nenolod@dereferenced.org>
Sun, 25 Nov 2018 20:14:00 +0000 (20:14 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Tue, 4 Dec 2018 04:52:09 +0000 (04:52 +0000)
lib/pleroma/web/twitter_api/representers/activity_representer.ex
lib/pleroma/web/twitter_api/views/activity_view.ex

index fbd33f07ed1297dfe148c6b9e8e4a727c40bd86c..4b4e202b32d42f6fdf3790d5a85b78676f30ec20 100644 (file)
@@ -3,7 +3,7 @@
 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
   use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
   alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
-  alias Pleroma.{Activity, User}
+  alias Pleroma.{Activity, User, Object}
   alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
   alias Pleroma.Web.CommonAPI.Utils
   alias Pleroma.Formatter
@@ -144,11 +144,13 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
         %Activity{data: %{"object" => %{"content" => content} = object}} = activity,
         %{user: user} = opts
       ) do
-    created_at = object["published"] |> Utils.date_to_asctime()
-    like_count = object["like_count"] || 0
-    announcement_count = object["announcement_count"] || 0
-    favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
-    repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
+    object = Object.normalize(object)
+
+    created_at = object.data["published"] |> Utils.date_to_asctime()
+    like_count = object.data["like_count"] || 0
+    announcement_count = object.data["announcement_count"] || 0
+    favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
+    repeated = opts[:for] && opts[:for].ap_id in (object.data["announcements"] || [])
 
     mentions = opts[:mentioned] || []
 
@@ -160,8 +162,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
 
     conversation_id = conversation_id(activity)
 
-    tags = activity.data["object"]["tag"] || []
-    possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
+    tags = object.data["tag"] || []
+    possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
 
     tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
 
@@ -169,16 +171,16 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
 
     html =
       HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
-      |> Formatter.emojify(object["emoji"])
+      |> Formatter.emojify(object.data["emoji"])
 
     video =
       if object["type"] == "Video" do
-        vid = [object]
+        vid = [object.data]
       else
         []
       end
 
-    attachments = (object["attachment"] || []) ++ video
+    attachments = (object.data["attachment"] || []) ++ video
 
     reply_parent = Activity.get_in_reply_to_activity(activity)
 
@@ -186,14 +188,14 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
 
     %{
       "id" => activity.id,
-      "uri" => activity.data["object"]["id"],
+      "uri" => object.data["id"],
       "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
       "statusnet_html" => html,
       "text" => HTML.strip_tags(content),
       "is_local" => activity.local,
       "is_post_verb" => true,
       "created_at" => created_at,
-      "in_reply_to_status_id" => object["inReplyToStatusId"],
+      "in_reply_to_status_id" => object.data["inReplyToStatusId"],
       "in_reply_to_screen_name" => reply_user && reply_user.nickname,
       "in_reply_to_profileurl" => User.profile_url(reply_user),
       "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
@@ -205,12 +207,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       "repeat_num" => announcement_count,
       "favorited" => to_boolean(favorited),
       "repeated" => to_boolean(repeated),
-      "external_url" => object["external_url"] || object["id"],
+      "external_url" => object.data["external_url"] || object.data["id"],
       "tags" => tags,
       "activity_type" => "post",
       "possibly_sensitive" => possibly_sensitive,
-      "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
-      "summary" => object["summary"]
+      "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object.data),
+      "summary" => object.data["summary"]
     }
   end
 
index 83e8fb765b3d4a3cd1a735763b3cfd467258d372..7839fe878f0985c093429c2924a63b08ad706620 100644 (file)
@@ -225,8 +225,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
 
     conversation_id = get_context_id(activity, opts)
 
-    tags = activity.data["object"]["tag"] || []
-    possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
+    tags = object.data["tag"] || []
+    possibly_sensitive = object.data["sensitive"] || Enum.member?(tags, "nsfw")
 
     tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
 
@@ -242,7 +242,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
 
     %{
       "id" => activity.id,
-      "uri" => activity.data["object"]["id"],
+      "uri" => object.data["id"],
       "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
       "statusnet_html" => html,
       "text" => HTML.strip_tags(content),