6a5304049e46186bc27a18e6686e9b8c94ce99dc
[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" => "Follow"}} = activity, %{user: user} = opts) do
8 created_at = get_in(activity.data, ["published"])
9 |> date_to_asctime
10
11 %{
12 "id" => activity.id,
13 "user" => UserRepresenter.to_map(user, opts),
14 "attentions" => [],
15 "statusnet_html" => "", # TODO: add summary
16 "text" => "",
17 "is_local" => true,
18 "is_post_verb" => false,
19 "created_at" => created_at,
20 "in_reply_to_status_id" => nil,
21 }
22 end
23
24 def to_map(%Activity{} = activity, %{user: user} = opts) do
25 content = get_in(activity.data, ["object", "content"])
26 created_at = get_in(activity.data, ["object", "published"])
27 |> date_to_asctime
28 like_count = get_in(activity.data, ["object", "like_count"]) || 0
29
30 mentions = opts[:mentioned] || []
31
32 attentions = activity.data["to"]
33 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
34 |> Enum.filter(&(&1))
35 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
36
37 %{
38 "id" => activity.id,
39 "user" => UserRepresenter.to_map(user, opts),
40 "attentions" => [],
41 "statusnet_html" => content,
42 "text" => HtmlSanitizeEx.strip_tags(content),
43 "is_local" => true,
44 "is_post_verb" => true,
45 "created_at" => created_at,
46 "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
47 "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
48 "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
49 "attentions" => attentions,
50 "fave_num" => like_count
51 }
52 end
53
54 defp date_to_asctime(date) do
55 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
56 Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
57 else _e ->
58 ""
59 end
60 end
61 end