Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/help-test
[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
29 mentions = opts[:mentioned] || []
30
31 attentions = activity.data["to"]
32 |> Enum.map(fn (ap_id) -> Enum.find(mentions, fn(user) -> ap_id == user.ap_id end) end)
33 |> Enum.filter(&(&1))
34 |> Enum.map(fn (user) -> UserRepresenter.to_map(user, opts) end)
35
36 %{
37 "id" => activity.id,
38 "user" => UserRepresenter.to_map(user, opts),
39 "attentions" => [],
40 "statusnet_html" => content,
41 "text" => HtmlSanitizeEx.strip_tags(content),
42 "is_local" => true,
43 "is_post_verb" => true,
44 "created_at" => created_at,
45 "in_reply_to_status_id" => activity.data["object"]["inReplyToStatusId"],
46 "statusnet_conversation_id" => activity.data["object"]["statusnetConversationId"],
47 "attachments" => (activity.data["object"]["attachment"] || []) |> ObjectRepresenter.enum_to_list(opts),
48 "attentions" => attentions
49 }
50 end
51
52 defp date_to_asctime(date) do
53 with {:ok, date, _offset} <- date |> DateTime.from_iso8601 do
54 Calendar.Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")
55 else e ->
56 ""
57 end
58 end
59 end