Add basic activity view.
authorlain <lain@soykaf.club>
Tue, 27 Mar 2018 20:24:19 +0000 (22:24 +0200)
committerlain <lain@soykaf.club>
Tue, 27 Mar 2018 20:24:19 +0000 (22:24 +0200)
lib/pleroma/web/twitter_api/twitter_api_controller.ex
lib/pleroma/web/twitter_api/views/activity_view.ex [new file with mode: 0644]
test/web/twitter_api/twitter_api_controller_test.exs

index 085113f267692bdea9ae29016e4f1e18e98d8190..a3c98a245e423bc572c3567691d601243e9f4e22 100644 (file)
@@ -1,6 +1,6 @@
 defmodule Pleroma.Web.TwitterAPI.Controller do
   use Pleroma.Web, :controller
-  alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
+  alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
   alias Pleroma.Web.CommonAPI
   alias Pleroma.{Repo, Activity, User}
@@ -140,10 +140,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   end
 
   def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    response = Jason.encode!(TwitterAPI.fetch_status(user, id))
-
-    conn
-    |> json_reply(200, response)
+    with %Activity{} = activity <- Repo.get(Activity, id),
+         true <- ActivityPub.visible_for_user?(activity, user) do
+      render conn, ActivityView, "activity.json", %{activity: activity, for: user}
+    end
   end
 
   def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
new file mode 100644 (file)
index 0000000..76b8cb1
--- /dev/null
@@ -0,0 +1,72 @@
+defmodule Pleroma.Web.TwitterAPI.ActivityView do
+  use Pleroma.Web, :view
+  alias Pleroma.Web.CommonAPI.Utils
+  alias Pleroma.User
+  alias Pleroma.Web.TwitterAPI.UserView
+  alias Pleroma.Web.TwitterAPI.TwitterAPI
+  alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
+  alias Pleroma.Formatter
+
+  def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
+    actor = get_in(activity.data, ["actor"])
+    user = User.get_cached_by_ap_id(actor)
+
+    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"] || [])
+
+    attentions = activity.recipients
+    |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
+    |> Enum.filter(&(&1))
+    |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
+
+    conversation_id = conversation_id(activity)
+
+    tags = activity.data["object"]["tag"] || []
+    possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
+
+    tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
+
+    summary = activity.data["object"]["summary"]
+    content = object["content"]
+    content = if !!summary and summary != "" do
+      "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
+    else
+      content
+    end
+
+    html = HtmlSanitizeEx.basic_html(content) |> Formatter.emojify(object["emoji"])
+
+    %{
+      "id" => activity.id,
+      "uri" => activity.data["object"]["id"],
+      "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
+      "statusnet_html" => html,
+      "text" => HtmlSanitizeEx.strip_tags(content),
+      "is_local" => activity.local,
+      "is_post_verb" => true,
+      "created_at" => created_at,
+      "in_reply_to_status_id" => object["inReplyToStatusId"],
+      "statusnet_conversation_id" => conversation_id,
+      "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
+      "attentions" => attentions,
+      "fave_num" => like_count,
+      "repeat_num" => announcement_count,
+      "favorited" => !!favorited,
+      "repeated" => !!repeated,
+      "external_url" => object["external_url"] || object["id"],
+      "tags" => tags,
+      "activity_type" => "post",
+      "possibly_sensitive" => possibly_sensitive
+    }
+  end
+
+  defp conversation_id(activity) do
+    with context when not is_nil(context) <- activity.data["context"] do
+      TwitterAPI.context_to_conversation_id(context)
+    else _e -> nil
+    end
+  end
+end
index d3040f0dce3de6b8828f63184397dc7cf2e9e551..eef639112e92ddb2e39c3690a9f91411784e647c 100644 (file)
@@ -90,8 +90,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
 
   describe "GET /statuses/show/:id.json" do
     test "returns one status", %{conn: conn} do
-      {:ok, user} = UserBuilder.insert
-      {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user})
+      user = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"})
       actor = Repo.get_by!(User, ap_id: activity.data["actor"])
 
       conn = conn