1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
3 alias Pleroma.Web.CommonAPI.Utils
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.TwitterAPI
7 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
9 alias Pleroma.Formatter
11 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
14 created_at = activity.data["published"]
15 |> Utils.date_to_asctime
17 text = "#{user.nickname} favorited a status."
21 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
22 "statusnet_html" => text,
24 "is_local" => activity.local,
25 "is_post_verb" => false,
26 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
27 "created_at" => created_at,
28 "in_reply_to_status_id" => liked_activity.id,
29 "external_url" => activity.data["id"],
30 "activity_type" => "like"
34 def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
35 actor = get_in(activity.data, ["actor"])
36 user = User.get_cached_by_ap_id(actor)
38 created_at = object["published"] |> Utils.date_to_asctime
39 like_count = object["like_count"] || 0
40 announcement_count = object["announcement_count"] || 0
41 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
42 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
44 attentions = activity.recipients
45 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
47 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
49 conversation_id = conversation_id(activity)
51 tags = activity.data["object"]["tag"] || []
52 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
54 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
56 summary = activity.data["object"]["summary"]
57 content = object["content"]
58 content = if !!summary and summary != "" do
59 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
64 html = HtmlSanitizeEx.basic_html(content)
65 |> Formatter.emojify(object["emoji"])
69 "uri" => activity.data["object"]["id"],
70 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
71 "statusnet_html" => html,
72 "text" => HtmlSanitizeEx.strip_tags(content),
73 "is_local" => activity.local,
74 "is_post_verb" => true,
75 "created_at" => created_at,
76 "in_reply_to_status_id" => object["inReplyToStatusId"],
77 "statusnet_conversation_id" => conversation_id,
78 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
79 "attentions" => attentions,
80 "fave_num" => like_count,
81 "repeat_num" => announcement_count,
82 "favorited" => !!favorited,
83 "repeated" => !!repeated,
84 "external_url" => object["external_url"] || object["id"],
86 "activity_type" => "post",
87 "possibly_sensitive" => possibly_sensitive
91 defp conversation_id(activity) do
92 with context when not is_nil(context) <- activity.data["context"] do
93 TwitterAPI.context_to_conversation_id(context)