ae5be60ce9e32eeea048d81bbb752ea9538757ad
[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.Formatter
9
10 def render("activity.json", %{activity: %{data: %{"type" => "Create", "object" => object}} = activity} = opts) do
11 actor = get_in(activity.data, ["actor"])
12 user = User.get_cached_by_ap_id(actor)
13
14 created_at = object["published"] |> Utils.date_to_asctime
15 like_count = object["like_count"] || 0
16 announcement_count = object["announcement_count"] || 0
17 favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
18 repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
19
20 attentions = activity.recipients
21 |> Enum.map(fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end)
22 |> Enum.filter(&(&1))
23 |> Enum.map(fn (user) -> UserView.render("show.json", %{user: user, for: opts[:for]}) end)
24
25 conversation_id = conversation_id(activity)
26
27 tags = activity.data["object"]["tag"] || []
28 possibly_sensitive = activity.data["object"]["sensitive"] || Enum.member?(tags, "nsfw")
29
30 tags = if possibly_sensitive, do: Enum.uniq(["nsfw" | tags]), else: tags
31
32 summary = activity.data["object"]["summary"]
33 content = object["content"]
34 content = if !!summary and summary != "" do
35 "<span>#{activity.data["object"]["summary"]}</span><br />#{content}</span>"
36 else
37 content
38 end
39
40 html = HtmlSanitizeEx.basic_html(content)
41 |> Formatter.emojify(object["emoji"])
42
43 %{
44 "id" => activity.id,
45 "uri" => activity.data["object"]["id"],
46 "user" => UserView.render("show.json", %{user: user, for: opts[:for]}),
47 "statusnet_html" => html,
48 "text" => HtmlSanitizeEx.strip_tags(content),
49 "is_local" => activity.local,
50 "is_post_verb" => true,
51 "created_at" => created_at,
52 "in_reply_to_status_id" => object["inReplyToStatusId"],
53 "statusnet_conversation_id" => conversation_id,
54 "attachments" => (object["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
55 "attentions" => attentions,
56 "fave_num" => like_count,
57 "repeat_num" => announcement_count,
58 "favorited" => !!favorited,
59 "repeated" => !!repeated,
60 "external_url" => object["external_url"] || object["id"],
61 "tags" => tags,
62 "activity_type" => "post",
63 "possibly_sensitive" => possibly_sensitive
64 }
65 end
66
67 defp conversation_id(activity) do
68 with context when not is_nil(context) <- activity.data["context"] do
69 TwitterAPI.context_to_conversation_id(context)
70 else _e -> nil
71 end
72 end
73 end