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