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