Merge branch 'fix/multiple-follow-notifications-from-same-user' into 'develop'
[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, Object}
8 import Ecto.Query
9
10 schema "notifications" do
11 field(:seen, :boolean, default: false)
12 belongs_to(:user, Pleroma.User)
13 belongs_to(:activity, Pleroma.Activity)
14
15 timestamps()
16 end
17
18 # TODO: Make generic and unify (see activity_pub.ex)
19 defp restrict_max(query, %{"max_id" => max_id}) do
20 from(activity in query, where: activity.id < ^max_id)
21 end
22
23 defp restrict_max(query, _), do: query
24
25 defp restrict_since(query, %{"since_id" => since_id}) do
26 from(activity in query, where: activity.id > ^since_id)
27 end
28
29 defp restrict_since(query, _), do: query
30
31 def for_user(user, opts \\ %{}) do
32 query =
33 from(
34 n in Notification,
35 where: n.user_id == ^user.id,
36 order_by: [desc: n.id],
37 preload: [:activity],
38 limit: 20
39 )
40
41 query =
42 query
43 |> restrict_since(opts)
44 |> restrict_max(opts)
45
46 Repo.all(query)
47 end
48
49 def set_read_up_to(%{id: user_id} = _user, id) do
50 query =
51 from(
52 n in Notification,
53 where: n.user_id == ^user_id,
54 where: n.id <= ^id,
55 update: [
56 set: [seen: true]
57 ]
58 )
59
60 Repo.update_all(query, [])
61 end
62
63 def get(%{id: user_id} = _user, id) do
64 query =
65 from(
66 n in Notification,
67 where: n.id == ^id,
68 preload: [:activity]
69 )
70
71 notification = Repo.one(query)
72
73 case notification do
74 %{user_id: ^user_id} ->
75 {:ok, notification}
76
77 _ ->
78 {:error, "Cannot get notification"}
79 end
80 end
81
82 def clear(user) do
83 from(n in Notification, where: n.user_id == ^user.id)
84 |> Repo.delete_all()
85 end
86
87 def dismiss(%{id: user_id} = _user, id) do
88 notification = Repo.get(Notification, id)
89
90 case notification do
91 %{user_id: ^user_id} ->
92 Repo.delete(notification)
93
94 _ ->
95 {:error, "Cannot dismiss notification"}
96 end
97 end
98
99 def create_notifications(%Activity{id: _, data: %{"to" => _, "type" => type}} = activity)
100 when type in ["Create", "Like", "Announce", "Follow"] do
101 users = get_notified_from_activity(activity)
102
103 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
104 {:ok, notifications}
105 end
106
107 def create_notifications(_), do: {:ok, []}
108
109 # TODO move to sql, too.
110 def create_notification(%Activity{} = activity, %User{} = user) do
111 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
112 user.ap_id == activity.data["actor"] or
113 (activity.data["type"] == "Follow" and
114 Enum.any?(Notification.for_user(user), fn notif ->
115 notif.activity.data["type"] == "Follow" and
116 notif.activity.data["actor"] == activity.data["actor"]
117 end)) do
118 notification = %Notification{user_id: user.id, activity: activity}
119 {:ok, notification} = Repo.insert(notification)
120 Pleroma.Web.Streamer.stream("user", notification)
121 Pleroma.Web.Push.send(notification)
122 notification
123 end
124 end
125
126 def get_notified_from_activity(activity, local_only \\ true)
127
128 def get_notified_from_activity(
129 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
130 local_only
131 )
132 when type in ["Create", "Like", "Announce", "Follow"] do
133 recipients =
134 []
135 |> maybe_notify_to_recipients(activity)
136 |> maybe_notify_mentioned_recipients(activity)
137 |> Enum.uniq()
138
139 User.get_users_from_set(recipients, local_only)
140 end
141
142 def get_notified_from_activity(_, _local_only), do: []
143
144 defp maybe_notify_to_recipients(
145 recipients,
146 %Activity{data: %{"to" => to, "type" => _type}} = _activity
147 ) do
148 recipients ++ to
149 end
150
151 defp maybe_notify_mentioned_recipients(
152 recipients,
153 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
154 )
155 when type == "Create" do
156 object = Object.normalize(data["object"])
157
158 object_data =
159 cond do
160 !is_nil(object) ->
161 object.data
162
163 is_map(data["object"]) ->
164 data["object"]
165
166 true ->
167 %{}
168 end
169
170 tagged_mentions = maybe_extract_mentions(object_data)
171
172 recipients ++ tagged_mentions
173 end
174
175 defp maybe_notify_mentioned_recipients(recipients, _), do: recipients
176
177 defp maybe_extract_mentions(%{"tag" => tag}) do
178 tag
179 |> Enum.filter(fn x -> is_map(x) end)
180 |> Enum.filter(fn x -> x["type"] == "Mention" end)
181 |> Enum.map(fn x -> x["href"] end)
182 end
183
184 defp maybe_extract_mentions(_), do: []
185 end