7c2501e161bdd790265edadf51db1a3d1f0cc179
[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" => "Delete"}} = activity} = opts) do
12 user = User.get_cached_by_ap_id(activity.data["actor"])
13 created_at = activity.data["published"] |> Utils.date_to_asctime()
14
15 %{
16 "id" => activity.id,
17 "uri" => activity.data["object"],
18 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
19 "attentions" => [],
20 "statusnet_html" => "deleted notice {{tag",
21 "text" => "deleted notice {{tag",
22 "is_local" => activity.local,
23 "is_post_verb" => false,
24 "created_at" => created_at,
25 "in_reply_to_status_id" => nil,
26 "external_url" => activity.data["id"],
27 "activity_type" => "delete"
28 }
29 end
30
31 def render("activity.json", %{activity: %{data: %{"type" => "Follow"}} = activity} = opts) do
32 user = User.get_cached_by_ap_id(activity.data["actor"])
33 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
34 created_at = created_at |> Utils.date_to_asctime()
35
36 followed = User.get_cached_by_ap_id(activity.data["object"])
37 text = "#{user.nickname} started following #{followed.nickname}"
38
39 %{
40 "id" => activity.id,
41 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
42 "attentions" => [],
43 "statusnet_html" => text,
44 "text" => text,
45 "is_local" => activity.local,
46 "is_post_verb" => false,
47 "created_at" => created_at,
48 "in_reply_to_status_id" => nil,
49 "external_url" => activity.data["id"],
50 "activity_type" => "follow"
51 }
52 end
53
54 def render("activity.json", %{activity: %{data: %{"type" => "Announce"}} = activity} = opts) do
55 user = User.get_by_ap_id(activity.data["actor"])
56 created_at = activity.data["published"] |> Utils.date_to_asctime()
57 announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
58
59 text = "#{user.nickname} retweeted a status."
60
61 retweeted_status = render("activity.json", Map.merge(opts, %{activity: announced_activity}))
62
63 %{
64 "id" => activity.id,
65 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
66 "statusnet_html" => text,
67 "text" => text,
68 "is_local" => activity.local,
69 "is_post_verb" => false,
70 "uri" => "tag:#{activity.data["id"]}:objectType=note",
71 "created_at" => created_at,
72 "retweeted_status" => retweeted_status,
73 "statusnet_conversation_id" => conversation_id(announced_activity),
74 "external_url" => activity.data["id"],
75 "activity_type" => "repeat"
76 }
77 end
78
79 def render("activity.json", %{activity: %{data: %{"type" => "Like"}} = activity} = opts) do
80 user = User.get_cached_by_ap_id(activity.data["actor"])
81 liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
82
83 created_at =
84 activity.data["published"]
85 |> Utils.date_to_asctime()
86
87 text = "#{user.nickname} favorited a status."
88
89 %{
90 "id" => activity.id,
91 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
92 "statusnet_html" => text,
93 "text" => text,
94 "is_local" => activity.local,
95 "is_post_verb" => false,
96 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
97 "created_at" => created_at,
98 "in_reply_to_status_id" => liked_activity.id,
99 "external_url" => activity.data["id"],
100 "activity_type" => "like"
101 }
102 end
103
104 def render(
105 "activity.json",
106 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
107 ) do
108 actor = get_in(activity.data, ["actor"])
109 user = User.get_cached_by_ap_id(actor)
110
111 created_at = object["published"] |> Utils.date_to_asctime()
112 like_count = object["like_count"] || 0
113 announcement_count = object["announcement_count"] || 0
114 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
115 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
116
117 attentions =
118 activity.recipients
119 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
120 |> Enum.filter(& &1)
121 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
122
123 conversation_id = conversation_id(activity)
124
125 tags = activity.data["object"]["tag"] || []
126 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
127
128 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
129
130 summary = activity.data["object"]["summary"]
131 content = object["content"]
132
133 content =
134 if !!summary and summary != "" do
135 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
136 else
137 content
138 end
139
140 html =
141 HtmlSanitizeEx.basic_html(content)
142 |> Formatter.emojify(object["emoji"])
143
144 %{
145 "id" => activity.id,
146 "uri" => activity.data["object"]["id"],
147 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
148 "statusnet_html" => html,
149 "text" => HtmlSanitizeEx.strip_tags(content),
150 "is_local" => activity.local,
151 "is_post_verb" => true,
152 "created_at" => created_at,
153 "in_reply_to_status_id" => object["inReplyToStatusId"],
154 "statusnet_conversation_id" => conversation_id,
155 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
156 "attentions" => attentions,
157 "fave_num" => like_count,
158 "repeat_num" => announcement_count,
159 "favorited" => !!favorited,
160 "repeated" => !!repeated,
161 "external_url" => object["external_url"] || object["id"],
162 "tags" => tags,
163 "activity_type" => "post",
164 "possibly_sensitive" => possibly_sensitive
165 }
166 end
167
168 defp conversation_id(activity) do
169 with context when not is_nil(context) <- activity.data["context"] do
170 TwitterAPI.context_to_conversation_id(context)
171 else
172 _e -> nil
173 end
174 end
175 end