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