2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-03-27 20:24:19 +00:00
|
|
|
defmodule Pleroma.Web.TwitterAPI.ActivityView do
|
|
|
|
use Pleroma.Web, :view
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Formatter
|
|
|
|
alias Pleroma.HTML
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-02-27 15:52:03 +00:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2018-03-27 20:24:19 +00:00
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
2019-01-27 12:16:08 +00:00
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Web.TwitterAPI.ActivityView
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Web.TwitterAPI.UserView
|
2018-03-27 20:24:19 +00:00
|
|
|
|
2018-04-02 12:46:56 +00:00
|
|
|
import Ecto.Query
|
2018-12-14 16:24:33 +00:00
|
|
|
require Logger
|
2018-04-02 12:46:56 +00:00
|
|
|
|
2018-04-02 13:28:35 +00:00
|
|
|
defp query_context_ids([]), do: []
|
2018-04-02 14:27:36 +00:00
|
|
|
|
2018-04-02 12:46:56 +00:00
|
|
|
defp query_context_ids(contexts) do
|
2018-04-02 14:27:36 +00:00
|
|
|
query = from(o in Object, where: fragment("(?)->>'id' = ANY(?)", o.data, ^contexts))
|
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp query_users([]), do: []
|
|
|
|
|
|
|
|
defp query_users(user_ids) do
|
|
|
|
query = from(user in User, where: user.ap_id in ^user_ids)
|
2018-04-02 12:46:56 +00:00
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp collect_context_ids(activities) do
|
2018-05-04 21:16:02 +00:00
|
|
|
_contexts =
|
2018-04-02 14:27:36 +00:00
|
|
|
activities
|
|
|
|
|> Enum.reject(& &1.data["context_id"])
|
|
|
|
|> Enum.map(fn %{data: data} ->
|
|
|
|
data["context"]
|
|
|
|
end)
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> query_context_ids()
|
|
|
|
|> Enum.reduce(%{}, fn %{data: %{"id" => ap_id}, id: id}, acc ->
|
|
|
|
Map.put(acc, ap_id, id)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp collect_users(activities) do
|
|
|
|
activities
|
|
|
|
|> Enum.map(fn activity ->
|
|
|
|
case activity.data do
|
|
|
|
data = %{"type" => "Follow"} ->
|
|
|
|
[data["actor"], data["object"]]
|
|
|
|
|
|
|
|
data ->
|
|
|
|
[data["actor"]]
|
|
|
|
end ++ activity.recipients
|
2018-04-02 12:46:56 +00:00
|
|
|
end)
|
2018-04-02 14:27:36 +00:00
|
|
|
|> List.flatten()
|
|
|
|
|> Enum.uniq()
|
|
|
|
|> query_users()
|
|
|
|
|> Enum.reduce(%{}, fn user, acc ->
|
|
|
|
Map.put(acc, user.ap_id, user)
|
2018-04-02 12:46:56 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-04-02 14:27:36 +00:00
|
|
|
defp get_context_id(%{data: %{"context_id" => context_id}}, _) when not is_nil(context_id),
|
|
|
|
do: context_id
|
|
|
|
|
2018-04-02 13:17:09 +00:00
|
|
|
defp get_context_id(%{data: %{"context" => nil}}, _), do: nil
|
2018-04-02 14:27:36 +00:00
|
|
|
|
2018-04-02 12:46:56 +00:00
|
|
|
defp get_context_id(%{data: %{"context" => context}}, options) do
|
|
|
|
cond do
|
|
|
|
id = options[:context_ids][context] -> id
|
2019-03-21 23:17:53 +00:00
|
|
|
true -> Utils.context_to_conversation_id(context)
|
2018-04-02 12:46:56 +00:00
|
|
|
end
|
|
|
|
end
|
2018-04-02 14:27:36 +00:00
|
|
|
|
2018-04-02 13:17:09 +00:00
|
|
|
defp get_context_id(_, _), do: nil
|
2018-04-02 12:46:56 +00:00
|
|
|
|
2018-04-02 14:27:36 +00:00
|
|
|
defp get_user(ap_id, opts) do
|
|
|
|
cond do
|
|
|
|
user = opts[:users][ap_id] ->
|
|
|
|
user
|
|
|
|
|
|
|
|
String.ends_with?(ap_id, "/followers") ->
|
|
|
|
nil
|
|
|
|
|
|
|
|
ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
|
|
|
|
nil
|
|
|
|
|
2019-01-07 11:41:31 +00:00
|
|
|
user = User.get_cached_by_ap_id(ap_id) ->
|
|
|
|
user
|
|
|
|
|
|
|
|
user = User.get_by_guessed_nickname(ap_id) ->
|
|
|
|
user
|
|
|
|
|
2018-04-02 14:27:36 +00:00
|
|
|
true ->
|
2019-01-16 14:13:09 +00:00
|
|
|
User.error_user(ap_id)
|
2018-04-02 14:27:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-30 13:49:10 +00:00
|
|
|
def render("index.json", opts) do
|
2018-04-02 12:46:56 +00:00
|
|
|
context_ids = collect_context_ids(opts.activities)
|
2018-04-02 14:27:36 +00:00
|
|
|
users = collect_users(opts.activities)
|
|
|
|
|
|
|
|
opts =
|
|
|
|
opts
|
|
|
|
|> Map.put(:context_ids, context_ids)
|
|
|
|
|> Map.put(:users, users)
|
2018-04-02 12:46:56 +00:00
|
|
|
|
2019-01-27 18:15:35 +00:00
|
|
|
safe_render_many(
|
2018-03-30 13:49:10 +00:00
|
|
|
opts.activities,
|
|
|
|
ActivityView,
|
|
|
|
"activity.json",
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-30 13:32:43 +00:00
|
|
|
def render("activity.json", %{activity: %{data: %{"type" => "Delete"}} = activity} = opts) do
|
2018-04-02 14:27:36 +00:00
|
|
|
user = get_user(activity.data["actor"], opts)
|
2018-03-30 13:32:43 +00:00
|
|
|
created_at = activity.data["published"] |> Utils.date_to_asctime()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => activity.id,
|
|
|
|
"uri" => activity.data["object"],
|
|
|
|
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
|
|
|
"attentions" => [],
|
|
|
|
"statusnet_html" => "deleted notice {{tag",
|
|
|
|
"text" => "deleted notice {{tag",
|
|
|
|
"is_local" => activity.local,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"created_at" => created_at,
|
|
|
|
"in_reply_to_status_id" => nil,
|
|
|
|
"external_url" => activity.data["id"],
|
|
|
|
"activity_type" => "delete"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-30 13:17:13 +00:00
|
|
|
def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
|
2018-04-02 14:27:36 +00:00
|
|
|
user = get_user(activity.data["actor"], opts)
|
2018-03-30 13:17:13 +00:00
|
|
|
created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
|
|
|
|
created_at = created_at |> Utils.date_to_asctime()
|
|
|
|
|
2018-04-02 14:27:36 +00:00
|
|
|
followed = get_user(activity.data["object"], opts)
|
2018-03-30 13:17:13 +00:00
|
|
|
text = "#{user.nickname} started following #{followed.nickname}"
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => activity.id,
|
|
|
|
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
|
|
|
"attentions" => [],
|
|
|
|
"statusnet_html" => text,
|
|
|
|
"text" => text,
|
|
|
|
"is_local" => activity.local,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"created_at" => created_at,
|
|
|
|
"in_reply_to_status_id" => nil,
|
|
|
|
"external_url" => activity.data["id"],
|
|
|
|
"activity_type" => "follow"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-30 12:46:36 +00:00
|
|
|
def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
|
2018-04-02 14:27:36 +00:00
|
|
|
user = get_user(activity.data["actor"], opts)
|
2018-03-30 13:01:53 +00:00
|
|
|
created_at = activity.data["published"] |> Utils.date_to_asctime()
|
2019-01-21 06:14:20 +00:00
|
|
|
announced_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
|
2018-03-30 12:46:36 +00:00
|
|
|
|
|
|
|
text = "#{user.nickname} retweeted a status."
|
|
|
|
|
|
|
|
retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => activity.id,
|
|
|
|
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
|
|
|
"statusnet_html" => text,
|
|
|
|
"text" => text,
|
|
|
|
"is_local" => activity.local,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"uri" => "tag:#{activity.data["id"]}:objectType=note",
|
|
|
|
"created_at" => created_at,
|
|
|
|
"retweeted_status" => retweeted_status,
|
2018-04-02 12:46:56 +00:00
|
|
|
"statusnet_conversation_id" => get_context_id(announced_activity, opts),
|
2018-03-30 12:46:36 +00:00
|
|
|
"external_url" => activity.data["id"],
|
|
|
|
"activity_type" => "repeat"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-30 11:49:09 +00:00
|
|
|
def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
|
2018-04-02 14:27:36 +00:00
|
|
|
user = get_user(activity.data["actor"], opts)
|
2019-01-21 06:14:20 +00:00
|
|
|
liked_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
|
2018-08-27 14:07:26 +00:00
|
|
|
liked_activity_id = if liked_activity, do: liked_activity.id, else: nil
|
2018-03-30 13:01:53 +00:00
|
|
|
|
|
|
|
created_at =
|
|
|
|
activity.data["published"]
|
|
|
|
|> Utils.date_to_asctime()
|
2018-03-30 11:49:09 +00:00
|
|
|
|
|
|
|
text = "#{user.nickname} favorited a status."
|
|
|
|
|
2018-12-14 06:24:18 +00:00
|
|
|
favorited_status =
|
|
|
|
if liked_activity,
|
|
|
|
do: render("activity.json", Map.merge(opts, %{activity: liked_activity})),
|
|
|
|
else: nil
|
|
|
|
|
2018-08-27 14:07:26 +00:00
|
|
|
%{
|
2018-03-30 11:49:09 +00:00
|
|
|
"id" => activity.id,
|
|
|
|
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
|
|
|
"statusnet_html" => text,
|
|
|
|
"text" => text,
|
|
|
|
"is_local" => activity.local,
|
|
|
|
"is_post_verb" => false,
|
|
|
|
"uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
|
|
|
|
"created_at" => created_at,
|
2018-12-14 06:24:18 +00:00
|
|
|
"favorited_status" => favorited_status,
|
2018-08-27 14:07:26 +00:00
|
|
|
"in_reply_to_status_id" => liked_activity_id,
|
2018-03-30 11:49:09 +00:00
|
|
|
"external_url" => activity.data["id"],
|
|
|
|
"activity_type" => "like"
|
2018-08-27 14:07:26 +00:00
|
|
|
}
|
2018-03-30 11:49:09 +00:00
|
|
|
end
|
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
def render(
|
|
|
|
"activity.json",
|
|
|
|
%{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
|
|
|
|
) do
|
2018-04-02 14:27:36 +00:00
|
|
|
user = get_user(activity.data["actor"], opts)
|
2018-03-27 20:24:19 +00:00
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
created_at = object["published"] |> Utils.date_to_asctime()
|
2018-03-27 20:24:19 +00:00
|
|
|
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"] || [])
|
2019-01-09 12:54:19 +00:00
|
|
|
pinned = activity.id in user.info.pinned_activities
|
2018-03-27 20:24:19 +00:00
|
|
|
|
2018-03-30 13:01:53 +00:00
|
|
|
attentions =
|
2019-01-24 20:30:43 +00:00
|
|
|
[]
|
|
|
|
|> Utils.maybe_notify_to_recipients(activity)
|
|
|
|
|> Utils.maybe_notify_mentioned_recipients(activity)
|
2018-04-02 14:27:36 +00:00
|
|
|
|> Enum.map(fn ap_id -> get_user(ap_id, opts) end)
|
2018-03-30 13:01:53 +00:00
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
|
2018-03-27 20:24:19 +00:00
|
|
|
|
2018-04-02 12:46:56 +00:00
|
|
|
conversation_id = get_context_id(activity, opts)
|
2018-03-27 20:24:19 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-06-24 05:33:34 +00:00
|
|
|
{summary, content} = render_content(object)
|
2018-03-30 13:01:53 +00:00
|
|
|
|
|
|
|
html =
|
2018-12-14 09:41:55 +00:00
|
|
|
content
|
2019-04-05 12:19:44 +00:00
|
|
|
|> HTML.get_cached_scrubbed_html_for_activity(
|
2019-01-04 23:19:46 +00:00
|
|
|
User.html_filter_policy(opts[:for]),
|
|
|
|
activity,
|
2019-04-05 12:19:44 +00:00
|
|
|
"twitterapi:content"
|
2019-01-04 23:19:46 +00:00
|
|
|
)
|
2018-03-30 13:01:53 +00:00
|
|
|
|> Formatter.emojify(object["emoji"])
|
2018-03-27 20:24:19 +00:00
|
|
|
|
2018-12-17 19:41:36 +00:00
|
|
|
text =
|
2018-12-18 14:32:41 +00:00
|
|
|
if content do
|
|
|
|
content
|
|
|
|
|> String.replace(~r/<br\s?\/?>/, "\n")
|
2019-04-05 12:19:44 +00:00
|
|
|
|> HTML.get_cached_stripped_html_for_activity(activity, "twitterapi:content")
|
2019-03-22 21:10:50 +00:00
|
|
|
else
|
|
|
|
""
|
2018-12-18 14:32:41 +00:00
|
|
|
end
|
2018-12-17 19:41:36 +00:00
|
|
|
|
2018-10-25 02:54:29 +00:00
|
|
|
reply_parent = Activity.get_in_reply_to_activity(activity)
|
|
|
|
|
2018-10-25 04:04:04 +00:00
|
|
|
reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
|
2018-10-25 02:54:29 +00:00
|
|
|
|
2019-01-06 10:16:40 +00:00
|
|
|
summary = HTML.strip_tags(summary)
|
|
|
|
|
2019-01-28 06:04:54 +00:00
|
|
|
card =
|
|
|
|
StatusView.render(
|
|
|
|
"card.json",
|
|
|
|
Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
|
|
|
|
)
|
2019-01-27 12:16:08 +00:00
|
|
|
|
2018-03-27 20:24:19 +00:00
|
|
|
%{
|
|
|
|
"id" => activity.id,
|
|
|
|
"uri" => activity.data["object"]["id"],
|
|
|
|
"user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
|
|
|
|
"statusnet_html" => html,
|
2018-12-17 19:41:36 +00:00
|
|
|
"text" => text,
|
2018-03-27 20:24:19 +00:00
|
|
|
"is_local" => activity.local,
|
|
|
|
"is_post_verb" => true,
|
|
|
|
"created_at" => created_at,
|
|
|
|
"in_reply_to_status_id" => object["inReplyToStatusId"],
|
2018-10-25 04:04:04 +00:00
|
|
|
"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,
|
|
|
|
"in_reply_to_user_id" => reply_user && reply_user.id,
|
2018-03-27 20:24:19 +00:00
|
|
|
"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,
|
2019-01-09 12:54:19 +00:00
|
|
|
"pinned" => pinned,
|
2018-03-27 20:24:19 +00:00
|
|
|
"external_url" => object["external_url"] || object["id"],
|
|
|
|
"tags" => tags,
|
|
|
|
"activity_type" => "post",
|
2018-05-20 16:01:24 +00:00
|
|
|
"possibly_sensitive" => possibly_sensitive,
|
2019-01-27 12:16:08 +00:00
|
|
|
"visibility" => StatusView.get_visibility(object),
|
2019-01-06 10:16:40 +00:00
|
|
|
"summary" => summary,
|
2019-01-27 12:16:08 +00:00
|
|
|
"summary_html" => summary |> Formatter.emojify(object["emoji"]),
|
2019-02-27 15:52:03 +00:00
|
|
|
"card" => card,
|
|
|
|
"muted" => CommonAPI.thread_muted?(user, activity) || User.mutes?(opts[:for], user)
|
2018-03-27 20:24:19 +00:00
|
|
|
}
|
|
|
|
end
|
2018-06-24 05:33:34 +00:00
|
|
|
|
2018-12-14 16:24:33 +00:00
|
|
|
def render("activity.json", %{activity: unhandled_activity}) do
|
|
|
|
Logger.warn("#{__MODULE__} unhandled activity: #{inspect(unhandled_activity)}")
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-06-24 05:33:34 +00:00
|
|
|
def render_content(%{"type" => "Note"} = object) do
|
|
|
|
summary = object["summary"]
|
2018-07-12 16:12:54 +00:00
|
|
|
|
2018-06-24 05:33:34 +00:00
|
|
|
content =
|
|
|
|
if !!summary and summary != "" do
|
|
|
|
"<p>#{summary}</p>#{object["content"]}"
|
|
|
|
else
|
|
|
|
object["content"]
|
|
|
|
end
|
|
|
|
|
|
|
|
{summary, content}
|
|
|
|
end
|
|
|
|
|
2018-12-23 13:58:45 +00:00
|
|
|
def render_content(%{"type" => object_type} = object)
|
|
|
|
when object_type in ["Article", "Page", "Video"] do
|
2018-06-24 05:33:34 +00:00
|
|
|
summary = object["name"] || object["summary"]
|
2018-07-12 16:12:54 +00:00
|
|
|
|
2018-06-24 05:33:34 +00:00
|
|
|
content =
|
2018-11-01 14:07:27 +00:00
|
|
|
if !!summary and summary != "" and is_bitstring(object["url"]) do
|
2018-06-24 05:33:34 +00:00
|
|
|
"<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
|
|
|
|
else
|
|
|
|
object["content"]
|
|
|
|
end
|
|
|
|
|
|
|
|
{summary, content}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_content(object) do
|
|
|
|
summary = object["summary"] || "Unhandled activity type: #{object["type"]}"
|
|
|
|
content = "<p>#{summary}</p>#{object["content"]}"
|
|
|
|
|
|
|
|
{summary, content}
|
|
|
|
end
|
2018-03-27 20:24:19 +00:00
|
|
|
end
|