414ed4731fa81bdb7e6d3e89cf836a6272bf4b7f
[akkoma] / lib / pleroma / web / twitter_api / views / notification_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.NotificationView do
6 use Pleroma.Web, :view
7 alias Pleroma.{Notification, User}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.TwitterAPI.{ActivityView, UserView}
10
11 defp get_user(ap_id, opts) do
12 cond do
13 user = opts[:users][ap_id] ->
14 user
15
16 String.ends_with?(ap_id, "/followers") ->
17 nil
18
19 ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
20 nil
21
22 true ->
23 User.get_cached_by_ap_id(ap_id)
24 end
25 end
26
27 def render("notification.json", %{notifications: notifications, for: user}) do
28 render_many(
29 notifications,
30 Pleroma.Web.TwitterAPI.NotificationView,
31 "notification.json",
32 for: user
33 )
34 end
35
36 def render(
37 "notification.json",
38 %{
39 notification: %Notification{
40 id: id,
41 seen: seen,
42 activity: activity,
43 inserted_at: created_at
44 },
45 for: user
46 } = opts
47 ) do
48 ntype =
49 case activity.data["type"] do
50 "Create" -> "mention"
51 "Like" -> "like"
52 "Announce" -> "repeat"
53 "Follow" -> "follow"
54 end
55
56 from = get_user(activity.data["actor"], opts)
57
58 %{
59 "id" => id,
60 "ntype" => ntype,
61 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
62 "from_profile" => UserView.render("show.json", %{user: from, for: user}),
63 "is_seen" => if(seen, do: 1, else: 0),
64 "created_at" => created_at |> Utils.format_naive_asctime()
65 }
66 end
67 end