Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / twitter_api / views / activity_view.ex
1 defmodule Pleroma.Web.TwitterAPI.ActivityView do
2 use Pleroma.Web, :view
3 alias Pleroma.Web.CommonAPI.Utils
4 alias Pleroma.User
5 alias Pleroma.Web.TwitterAPI.UserView
6 alias Pleroma.Web.TwitterAPI.TwitterAPI
7 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
8 alias Pleroma.Activity
9 alias Pleroma.Formatter
10
11 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
12 user = User.get_by_ap_id(activity.data["actor"])
13 created_at = activity.data["published"] |> Utils.date_to_asctime
14 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
15
16 text = "#{user.nickname} retweeted a status."
17
18 # retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
19 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
20
21 %{
22 "id" => activity.id,
23 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
24 "statusnet_html" => text,
25 "text" => text,
26 "is_local" => activity.local,
27 "is_post_verb" => false,
28 "uri" => "tag:#{activity.data["id"]}:objectType=note",
29 "created_at" => created_at,
30 "retweeted_status" => retweeted_status,
31 "statusnet_conversation_id" => conversation_id(announced_activity),
32 "external_url" => activity.data["id"],
33 "activity_type" => "repeat"
34 }
35 end
36
37 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
38 user = User.get_cached_by_ap_id(activity.data["actor"])
39 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
40 created_at = activity.data["published"]
41 |> Utils.date_to_asctime
42
43 text = "#{user.nickname} favorited a status."
44
45 %{
46 "id" => activity.id,
47 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
48 "statusnet_html" => text,
49 "text" => text,
50 "is_local" => activity.local,
51 "is_post_verb" => false,
52 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
53 "created_at" => created_at,
54 "in_reply_to_status_id" => liked_activity.id,
55 "external_url" => activity.data["id"],
56 "activity_type" => "like"
57 }
58 end
59
60 def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
61 actor = get_in(activity.data, ["actor"])
62 user = User.get_cached_by_ap_id(actor)
63
64 created_at = object["published"] |> Utils.date_to_asctime
65 like_count = object["like_count"] || 0
66 announcement_count = object["announcement_count"] || 0
67 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
68 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
69
70 attentions = activity.recipients
71 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
72 |> Enum.filter(&(&1))
73 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
74
75 conversation_id = conversation_id(activity)
76
77 tags = activity.data["object"]["tag"] || []
78 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
79
80 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
81
82 summary = activity.data["object"]["summary"]
83 content = object["content"]
84 content = if !!summary and summary != "" do
85 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
86 else
87 content
88 end
89
90 html = HtmlSanitizeEx.basic_html(content)
91 |> Formatter.emojify(object["emoji"])
92
93 %{
94 "id" => activity.id,
95 "uri" => activity.data["object"]["id"],
96 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
97 "statusnet_html" => html,
98 "text" => HtmlSanitizeEx.strip_tags(content),
99 "is_local" => activity.local,
100 "is_post_verb" => true,
101 "created_at" => created_at,
102 "in_reply_to_status_id" => object["inReplyToStatusId"],
103 "statusnet_conversation_id" => conversation_id,
104 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
105 "attentions" => attentions,
106 "fave_num" => like_count,
107 "repeat_num" => announcement_count,
108 "favorited" => !!favorited,
109 "repeated" => !!repeated,
110 "external_url" => object["external_url"] || object["id"],
111 "tags" => tags,
112 "activity_type" => "post",
113 "possibly_sensitive" => possibly_sensitive
114 }
115 end
116
117 defp conversation_id(activity) do
118 with context when not is_nil(context) <- activity.data["context"] do
119 TwitterAPI.context_to_conversation_id(context)
120 else _e -> nil
121 end
122 end
123 end