Format the code.
[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
41 created_at =
42 activity.data["published"]
43 |> Utils.date_to_asctime()
44
45 text = "#{user.nickname} favorited a status."
46
47 %{
48 "id" => activity.id,
49 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
50 "statusnet_html" => text,
51 "text" => text,
52 "is_local" => activity.local,
53 "is_post_verb" => false,
54 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
55 "created_at" => created_at,
56 "in_reply_to_status_id" => liked_activity.id,
57 "external_url" => activity.data["id"],
58 "activity_type" => "like"
59 }
60 end
61
62 def render(
63 "activity.json",
64 %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts
65 ) do
66 actor = get_in(activity.data, ["actor"])
67 user = User.get_cached_by_ap_id(actor)
68
69 created_at = object["published"] |> Utils.date_to_asctime()
70 like_count = object["like_count"] || 0
71 announcement_count = object["announcement_count"] || 0
72 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
73 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
74
75 attentions =
76 activity.recipients
77 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
78 |> Enum.filter(& &1)
79 |> Enum.map(fn user -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
80
81 conversation_id = conversation_id(activity)
82
83 tags = activity.data["object"]["tag"] || []
84 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
85
86 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
87
88 summary = activity.data["object"]["summary"]
89 content = object["content"]
90
91 content =
92 if !!summary and summary != "" do
93 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
94 else
95 content
96 end
97
98 html =
99 HtmlSanitizeEx.basic_html(content)
100 |> Formatter.emojify(object["emoji"])
101
102 %{
103 "id" => activity.id,
104 "uri" => activity.data["object"]["id"],
105 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
106 "statusnet_html" => html,
107 "text" => HtmlSanitizeEx.strip_tags(content),
108 "is_local" => activity.local,
109 "is_post_verb" => true,
110 "created_at" => created_at,
111 "in_reply_to_status_id" => object["inReplyToStatusId"],
112 "statusnet_conversation_id" => conversation_id,
113 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
114 "attentions" => attentions,
115 "fave_num" => like_count,
116 "repeat_num" => announcement_count,
117 "favorited" => !!favorited,
118 "repeated" => !!repeated,
119 "external_url" => object["external_url"] || object["id"],
120 "tags" => tags,
121 "activity_type" => "post",
122 "possibly_sensitive" => possibly_sensitive
123 }
124 end
125
126 defp conversation_id(activity) do
127 with context when not is_nil(context) <- activity.data["context"] do
128 TwitterAPI.context_to_conversation_id(context)
129 else
130 _e -> nil
131 end
132 end
133 end