From 7ab94ae22d559c26920372774b7c96552901f1cc Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 30 Mar 2018 15:49:10 +0200 Subject: [PATCH] TwitterApiController: Use ActivityView. --- lib/pleroma/web/twitter_api/twitter_api.ex | 1 - .../web/twitter_api/twitter_api_controller.ex | 66 +++++++++++++------ .../web/twitter_api/views/activity_view.ex | 10 +++ 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 4eac8a8fd..b3b505be2 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -277,7 +277,6 @@ def search(user, %{"q" => query} = params) do ) activities = Repo.all(q) - activities_to_statuses(activities, %{for: user}) end defp activities_to_statuses(activities, opts) do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a8bcae5ba..fb09deada 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -19,7 +19,7 @@ def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_da {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do conn - |> json(ActivityRepresenter.to_map(activity, %{user: user})) + |> json(ActivityView.render("activity.json", activity: activity, for: user)) else _ -> empty_status_reply(conn) end @@ -44,27 +44,41 @@ defp extract_media_ids(status_data) do end def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do - statuses = TwitterAPI.fetch_public_and_external_statuses(user, params) - {:ok, json} = Jason.encode(statuses) + params = + params + |> Map.put("type", ["Create", "Announce", "Follow"]) + |> Map.put("blocking_user", user) + + activities = ActivityPub.fetch_public_activities(params) conn - |> json_reply(200, json) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) end def public_timeline(%{assigns: %{user: user}} = conn, params) do - statuses = TwitterAPI.fetch_public_statuses(user, params) - {:ok, json} = Jason.encode(statuses) + params = + params + |> Map.put("type", ["Create", "Announce", "Follow"]) + |> Map.put("local_only", true) + |> Map.put("blocking_user", user) + + activities = ActivityPub.fetch_public_activities(params) conn - |> json_reply(200, json) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) end def friends_timeline(%{assigns: %{user: user}} = conn, params) do - statuses = TwitterAPI.fetch_friend_statuses(user, params) - {:ok, json} = Jason.encode(statuses) + params = + params + |> Map.put("type", ["Create", "Announce", "Follow", "Like"]) + |> Map.put("blocking_user", user) + |> Map.put("user", user) + + activities = ActivityPub.fetch_activities([user.ap_id | user.following], params) conn - |> json_reply(200, json) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) end def show_user(conn, params) do @@ -83,11 +97,16 @@ def show_user(conn, params) do def user_timeline(%{assigns: %{user: user}} = conn, params) do case TwitterAPI.get_user(user, params) do {:ok, target_user} -> - params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true}) - statuses = TwitterAPI.fetch_user_statuses(user, params) + params = + params + |> Map.put("type", ["Create", "Announce"]) + |> Map.put("actor_id", target_user.ap_id) + |> Map.put("whole_db", true) + + activities = ActivityPub.fetch_public_activities(params) conn - |> json_reply(200, statuses |> Jason.encode!()) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) {:error, msg} -> bad_request_reply(conn, msg) @@ -95,11 +114,10 @@ def user_timeline(%{assigns: %{user: user}} = conn, params) do end def mentions_timeline(%{assigns: %{user: user}} = conn, params) do - statuses = TwitterAPI.fetch_mentions(user, params) - {:ok, json} = Jason.encode(statuses) + activities = ActivityPub.fetch_activities([user.ap_id], params) conn - |> json_reply(200, json) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) end def follow(%{assigns: %{user: user}} = conn, params) do @@ -160,10 +178,16 @@ def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do id = String.to_integer(id) - response = Jason.encode!(TwitterAPI.fetch_conversation(user, id)) - conn - |> json_reply(200, response) + with context when is_binary(context) <- TwitterAPI.conversation_id_to_context(id), + activities <- + ActivityPub.fetch_activities_for_context(context, %{ + "blocking_user" => user, + "user" => user + }) do + conn + |> render(ActivityView, "index.json", %{activities: activities, for: user}) + end end def upload(conn, %{"media" => media}) do @@ -337,8 +361,10 @@ def update_profile(%{assigns: %{user: user}} = conn, params) do end def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do + activities = TwitterAPI.search(user, params) + conn - |> json(TwitterAPI.search(user, params)) + |> render(ActivityView, "index.json", %{activities: activities, for: user}) end defp bad_request_reply(conn, error_message) do diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 7c2501e16..3e69af3e3 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -3,11 +3,21 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do alias Pleroma.Web.CommonAPI.Utils alias Pleroma.User alias Pleroma.Web.TwitterAPI.UserView + alias Pleroma.Web.TwitterAPI.ActivityView alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter alias Pleroma.Activity alias Pleroma.Formatter + def render("index.json", opts) do + render_many( + opts.activities, + ActivityView, + "activity.json", + opts + ) + end + def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do user = User.get_cached_by_ap_id(activity.data["actor"]) created_at = activity.data["published"] |> Utils.date_to_asctime()