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