it works!!
[akkoma] / lib / pleroma / notification.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.Notification do
6 use Ecto.Schema
7 alias Pleroma.{User, Activity, Notification, Repo}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.ThreadMute
10 import Ecto.Query
11
12 schema "notifications" do
13 field(:seen, :boolean, default: false)
14 belongs_to(:user, User, type: Pleroma.FlakeId)
15 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
16
17 timestamps()
18 end
19
20 # TODO: Make generic and unify (see activity_pub.ex)
21 defp restrict_max(query, %{"max_id" => max_id}) do
22 from(activity in query, where: activity.id < ^max_id)
23 end
24
25 defp restrict_max(query, _), do: query
26
27 defp restrict_since(query, %{"since_id" => since_id}) do
28 from(activity in query, where: activity.id > ^since_id)
29 end
30
31 defp restrict_since(query, _), do: query
32
33 def for_user(user, opts \\ %{}) do
34 query =
35 from(
36 n in Notification,
37 where: n.user_id == ^user.id,
38 order_by: [desc: n.id],
39 join: activity in assoc(n, :activity),
40 preload: [activity: activity],
41 limit: 20
42 )
43
44 query =
45 query
46 |> restrict_since(opts)
47 |> restrict_max(opts)
48
49 Repo.all(query)
50 end
51
52 def set_read_up_to(%{id: user_id} = _user, id) do
53 query =
54 from(
55 n in Notification,
56 where: n.user_id == ^user_id,
57 where: n.id <= ^id,
58 update: [
59 set: [seen: true]
60 ]
61 )
62
63 Repo.update_all(query, [])
64 end
65
66 def get(%{id: user_id} = _user, id) do
67 query =
68 from(
69 n in Notification,
70 where: n.id == ^id,
71 join: activity in assoc(n, :activity),
72 preload: [activity: activity]
73 )
74
75 notification = Repo.one(query)
76
77 case notification do
78 %{user_id: ^user_id} ->
79 {:ok, notification}
80
81 _ ->
82 {:error, "Cannot get notification"}
83 end
84 end
85
86 def clear(user) do
87 from(n in Notification, where: n.user_id == ^user.id)
88 |> Repo.delete_all()
89 end
90
91 def dismiss(%{id: user_id} = _user, id) do
92 notification = Repo.get(Notification, id)
93
94 case notification do
95 %{user_id: ^user_id} ->
96 Repo.delete(notification)
97
98 _ ->
99 {:error, "Cannot dismiss notification"}
100 end
101 end
102
103 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
104 when type in ["Create", "Like", "Announce", "Follow"] do
105 users = get_notified_from_activity(activity)
106
107 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
108 {:ok, notifications}
109 end
110
111 def create_notifications(_), do: {:ok, []}
112
113 # TODO move to sql, too.
114 def create_notification(%Activity{} = activity, %User{} = user) do
115 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
116 ThreadMute.muted?(user, activity) or
117 user.ap_id == activity.data["actor"] or
118 (activity.data["type"] == "Follow" and
119 Enum.any?(Notification.for_user(user), fn notif ->
120 notif.activity.data["type"] == "Follow" and
121 notif.activity.data["actor"] == activity.data["actor"]
122 end)) do
123 notification = %Notification{user_id: user.id, activity: activity}
124 {:ok, notification} = Repo.insert(notification)
125 Pleroma.Web.Streamer.stream("user", notification)
126 Pleroma.Web.Push.send(notification)
127 notification
128 end
129 end
130
131 def get_notified_from_activity(activity, local_only \\ true)
132
133 def get_notified_from_activity(
134 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
135 local_only
136 )
137 when type in ["Create", "Like", "Announce", "Follow"] do
138 recipients =
139 []
140 |> Utils.maybe_notify_to_recipients(activity)
141 |> Utils.maybe_notify_mentioned_recipients(activity)
142 |> Enum.uniq()
143
144 User.get_users_from_set(recipients, local_only)
145 end
146
147 def get_notified_from_activity(_, _local_only), do: []
148 end