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