TwitterApiController: Use ActivityView.

This commit is contained in:
lain 2018-03-30 15:49:10 +02:00
parent e90f8f6c4b
commit 7ab94ae22d
3 changed files with 56 additions and 21 deletions

View file

@ -277,7 +277,6 @@ def search(user, %{"q" => query} = params) do
) )
activities = Repo.all(q) activities = Repo.all(q)
activities_to_statuses(activities, %{for: user})
end end
defp activities_to_statuses(activities, opts) do defp activities_to_statuses(activities, opts) do

View file

@ -19,7 +19,7 @@ def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_da
{:ok, activity} <- {:ok, activity} <-
TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do
conn conn
|> json(ActivityRepresenter.to_map(activity, %{user: user})) |> json(ActivityView.render("activity.json", activity: activity, for: user))
else else
_ -> empty_status_reply(conn) _ -> empty_status_reply(conn)
end end
@ -44,27 +44,41 @@ defp extract_media_ids(status_data) do
end end
def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do def public_and_external_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_public_and_external_statuses(user, params) params =
{:ok, json} = Jason.encode(statuses) params
|> Map.put("type", ["Create", "Announce", "Follow"])
|> Map.put("blocking_user", user)
activities = ActivityPub.fetch_public_activities(params)
conn conn
|> json_reply(200, json) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end end
def public_timeline(%{assigns: %{user: user}} = conn, params) do def public_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_public_statuses(user, params) params =
{:ok, json} = Jason.encode(statuses) params
|> Map.put("type", ["Create", "Announce", "Follow"])
|> Map.put("local_only", true)
|> Map.put("blocking_user", user)
activities = ActivityPub.fetch_public_activities(params)
conn conn
|> json_reply(200, json) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end end
def friends_timeline(%{assigns: %{user: user}} = conn, params) do def friends_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_friend_statuses(user, params) params =
{:ok, json} = Jason.encode(statuses) 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 conn
|> json_reply(200, json) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end end
def show_user(conn, params) do 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 def user_timeline(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.get_user(user, params) do case TwitterAPI.get_user(user, params) do
{:ok, target_user} -> {:ok, target_user} ->
params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true}) params =
statuses = TwitterAPI.fetch_user_statuses(user, 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 conn
|> json_reply(200, statuses |> Jason.encode!()) |> render(ActivityView, "index.json", %{activities: activities, for: user})
{:error, msg} -> {:error, msg} ->
bad_request_reply(conn, msg) bad_request_reply(conn, msg)
@ -95,11 +114,10 @@ def user_timeline(%{assigns: %{user: user}} = conn, params) do
end end
def mentions_timeline(%{assigns: %{user: user}} = conn, params) do def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_mentions(user, params) activities = ActivityPub.fetch_activities([user.ap_id], params)
{:ok, json} = Jason.encode(statuses)
conn conn
|> json_reply(200, json) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end end
def follow(%{assigns: %{user: user}} = conn, params) do 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 def fetch_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
id = String.to_integer(id) id = String.to_integer(id)
response = Jason.encode!(TwitterAPI.fetch_conversation(user, id))
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 conn
|> json_reply(200, response) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end
end end
def upload(conn, %{"media" => media}) do def upload(conn, %{"media" => media}) do
@ -337,8 +361,10 @@ def update_profile(%{assigns: %{user: user}} = conn, params) do
end end
def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do
activities = TwitterAPI.search(user, params)
conn conn
|> json(TwitterAPI.search(user, params)) |> render(ActivityView, "index.json", %{activities: activities, for: user})
end end
defp bad_request_reply(conn, error_message) do defp bad_request_reply(conn, error_message) do

View file

@ -3,11 +3,21 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.TwitterAPI.UserView alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.TwitterAPI.ActivityView
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Formatter 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 def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
user = User.get_cached_by_ap_id(activity.data["actor"]) user = User.get_cached_by_ap_id(activity.data["actor"])
created_at = activity.data["published"] |> Utils.date_to_asctime() created_at = activity.data["published"] |> Utils.date_to_asctime()