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