Merge branch 'develop' of git.pleroma.social:pleroma/pleroma 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(notifications, Pleroma.Web.TwitterAPI.NotificationView, "notification.json", for: user)
27 end
28
29 def render("notification.json", %{notification: %Notification{id: id, seen: seen, activity: activity, inserted_at: created_at}, for: user} = opts) do
30 ntype = case activity.data["type"] do
31 "Create" -> "mention"
32 "Like" -> "like"
33 "Announce" -> "repeat"
34 "Follow" -> "follow"
35 end
36 from = get_user(activity.data["actor"], opts)
37
38 %{
39 "id" => id,
40 "ntype" => ntype,
41 "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}),
42 "from_profile" => UserView.render("show.json", %{user: from, for: user}),
43 "is_seen" => (if seen, do: 1, else: 0),
44 "created_at" => created_at |> Utils.format_naive_asctime()
45 }
46 end
47 end