1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 # THIS MODULE IS DEPRECATED! DON'T USE IT!
6 # USE THE Pleroma.Web.TwitterAPI.Views.ActivityView MODULE!
7 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
8 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
9 alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
10 alias Pleroma.{Activity, User}
11 alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView}
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Formatter
15 alias Pleroma.Web.MastodonAPI
16 alias Pleroma.Web.MastodonAPI.StatusView
18 defp user_by_ap_id(user_list, ap_id) do
19 Enum.find(user_list, fn %{ap_id: user_id} -> ap_id == user_id end)
23 %Activity{data: %{"type" => "Announce", "actor" => actor, "published" => created_at}} =
25 %{users: users, announced_activity: announced_activity} = opts
27 user = user_by_ap_id(users, actor)
28 created_at = created_at |> Utils.date_to_asctime()
30 text = "#{user.nickname} retweeted a status."
32 announced_user = user_by_ap_id(users, announced_activity.data["actor"])
33 retweeted_status = to_map(announced_activity, Map.merge(%{user: announced_user}, opts))
37 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
38 "statusnet_html" => text,
40 "is_local" => activity.local,
41 "is_post_verb" => false,
42 "uri" => "tag:#{activity.data["id"]}:objectType=note",
43 "created_at" => created_at,
44 "retweeted_status" => retweeted_status,
45 "statusnet_conversation_id" => conversation_id(announced_activity),
46 "external_url" => activity.data["id"],
47 "activity_type" => "repeat"
52 %Activity{data: %{"type" => "Like", "published" => created_at}} = activity,
53 %{user: user, liked_activity: liked_activity} = opts
55 created_at = created_at |> Utils.date_to_asctime()
57 text = "#{user.nickname} favorited a status."
61 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
62 "statusnet_html" => text,
64 "is_local" => activity.local,
65 "is_post_verb" => false,
66 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
67 "created_at" => created_at,
68 "in_reply_to_status_id" => liked_activity.id,
69 "external_url" => activity.data["id"],
70 "activity_type" => "like"
75 %Activity{data: %{"type" => "Follow", "object" => followed_id}} = activity,
78 created_at = activity.data["published"] || DateTime.to_iso8601(activity.inserted_at)
79 created_at = created_at |> Utils.date_to_asctime()
81 followed = User.get_cached_by_ap_id(followed_id)
82 text = "#{user.nickname} started following #{followed.nickname}"
86 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
88 "statusnet_html" => text,
90 "is_local" => activity.local,
91 "is_post_verb" => false,
92 "created_at" => created_at,
93 "in_reply_to_status_id" => nil,
94 "external_url" => activity.data["id"],
95 "activity_type" => "follow"
100 # Make this more proper. Just a placeholder to not break the frontend.
103 data: %{"type" => "Undo", "published" => created_at, "object" => undid_activity}
107 created_at = created_at |> Utils.date_to_asctime()
109 text = "#{user.nickname} undid the action at #{undid_activity["id"]}"
113 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
115 "statusnet_html" => text,
117 "is_local" => activity.local,
118 "is_post_verb" => false,
119 "created_at" => created_at,
120 "in_reply_to_status_id" => nil,
121 "external_url" => activity.data["id"],
122 "activity_type" => "undo"
127 %Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _}} =
131 created_at = created_at |> Utils.date_to_asctime()
135 "uri" => activity.data["object"],
136 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
138 "statusnet_html" => "deleted notice {{tag",
139 "text" => "deleted notice {{tag",
140 "is_local" => activity.local,
141 "is_post_verb" => false,
142 "created_at" => created_at,
143 "in_reply_to_status_id" => nil,
144 "external_url" => activity.data["id"],
145 "activity_type" => "delete"
150 %Activity{data: %{"object" => %{"content" => _content} = object}} = activity,
153 created_at = object["published"] |> Utils.date_to_asctime()
154 like_count = object["like_count"] || 0
155 announcement_count = object["announcement_count"] || 0
156 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
157 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
158 pinned = activity.id in user.info.pinned_activities
160 mentions = opts[:mentioned] || []
164 |> Utils.maybe_notify_to_recipients(activity)
165 |> Utils.maybe_notify_mentioned_recipients(activity)
166 |> Enum.map(fn ap_id -> Enum.find(mentions, fn user -> ap_id == user.ap_id end) end)
168 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
170 conversation_id = conversation_id(activity)
172 tags = activity.data["object"]["tag"] || []
173 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
175 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
177 {_summary, content} = ActivityView.render_content(object)
180 HTML.filter_tags(content, User.html_filter_policy(opts[:for]))
181 |> Formatter.emojify(object["emoji"])
183 attachments = object["attachment"] || []
185 reply_parent = Activity.get_in_reply_to_activity(activity)
187 reply_user = reply_parent && User.get_cached_by_ap_id(reply_parent.actor)
189 summary = HTML.strip_tags(object["summary"])
191 card = StatusView.render("card.json", MastodonAPI.get_status_card(activity.id))
195 "uri" => activity.data["object"]["id"],
196 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
197 "statusnet_html" => html,
198 "text" => HTML.strip_tags(content),
199 "is_local" => activity.local,
200 "is_post_verb" => true,
201 "created_at" => created_at,
202 "in_reply_to_status_id" => object["inReplyToStatusId"],
203 "in_reply_to_screen_name" => reply_user && reply_user.nickname,
204 "in_reply_to_profileurl" => User.profile_url(reply_user),
205 "in_reply_to_ostatus_uri" => reply_user && reply_user.ap_id,
206 "in_reply_to_user_id" => reply_user && reply_user.id,
207 "statusnet_conversation_id" => conversation_id,
208 "attachments" => attachments |> ObjectRepresenter.enum_to_list(opts),
209 "attentions" => attentions,
210 "fave_num" => like_count,
211 "repeat_num" => announcement_count,
212 "favorited" => to_boolean(favorited),
213 "repeated" => to_boolean(repeated),
215 "external_url" => object["external_url"] || object["id"],
217 "activity_type" => "post",
218 "possibly_sensitive" => possibly_sensitive,
219 "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object),
220 "summary" => summary,
221 "summary_html" => summary |> Formatter.emojify(object["emoji"]),
226 def conversation_id(activity) do
227 with context when not is_nil(context) <- activity.data["context"] do
228 TwitterAPI.context_to_conversation_id(context)
234 defp to_boolean(false) do
238 defp to_boolean(nil) do
242 defp to_boolean(_) do