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