Favorite changes.
[akkoma] / lib / pleroma / web / twitter_api / representers / activity_representer.ex
1 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
2 use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
3 alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ObjectRepresenter}
4 alias Pleroma.Activity
5
6
7 def to_map(%Activity{data: %{"type" => "Like"}} = activity, %{user: user, liked_activity: liked_activity} = opts) do
8 created_at = get_in(activity.data, ["published"])
9 |> date_to_asctime
10
11 text = "#{user.nickname} favorited a status."
12
13 %{
14 "id" => activity.id,
15 "user" => UserRepresenter.to_map(user, opts),
16 "statusnet_html" => text, # TODO: add summary
17 "text" => text,
18 "is_local" => true,
19 "is_post_verb" => false,
20 "uri" => "tag:#{activity.data["id"]}:objectType=Favourite",
21 "created_at" => created_at,
22 "in_reply_to_status_id" => liked_activity.id,
23 }
24 end
25
26 def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = opts) do
27 created_at = get_in(activity.data, ["published"])
28 |> date_to_asctime
29
30 %{
31 "id" => activity.id,
32 "user" => UserRepresenter.to_map(user, opts),
33 "attentions" => [],
34 "statusnet_html" => "", # TODO: add summary
35 "text" => "",
36 "is_local" => true,
37 "is_post_verb" => false,
38 "created_at" => created_at,
39 "in_reply_to_status_id" => nil,
40 }
41 end
42
43 def to_map(%Activity{} = activity, %{user: user} = opts) do
44 content = get_in(activity.data, ["object", "content"])
45 created_at = get_in(activity.data, ["object", "published"])
46 |> date_to_asctime
47 like_count = get_in(activity.data, ["object", "like_count"]) || 0
48 favorited = opts[:for] && opts[:for].ap_id in (activity.data["object"]["likes"] || [])
49
50 mentions = opts[:mentioned] || []
51
52 attentions = activity.data["to"]
53 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
54 |> Enum.filter(&(&1))
55 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
56
57 %{
58 "id" => activity.id,
59 "user" => UserRepresenter.to_map(user, opts),
60 "attentions" => [],
61 "statusnet_html" => content,
62 "text" => HtmlSanitizeEx.strip_tags(content),
63 "is_local" => true,
64 "is_post_verb" => true,
65 "created_at" => created_at,
66 "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
67 "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
68 "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
69 "attentions" => attentions,
70 "fave_num" => like_count,
71 "favorited" => !!favorited
72 }
73 end
74
75 defp date_to_asctime(date) do
76 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
77 Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
78 else _e ->
79 ""
80 end
81 end
82 end