Merge branch 'captcha' 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"] do
113 notification = %Notification{user_id: user.id, activity: activity}
114 {:ok, notification} = Repo.insert(notification)
115 Pleroma.Web.Streamer.stream("user", notification)
116 Pleroma.Web.Push.send(notification)
117 notification
118 end
119 end
120
121 def get_notified_from_activity(activity, local_only \\ true)
122
123 def get_notified_from_activity(
124 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
125 local_only
126 )
127 when type in ["Create", "Like", "Announce", "Follow"] do
128 recipients =
129 []
130 |> maybe_notify_to_recipients(activity)
131 |> maybe_notify_mentioned_recipients(activity)
132 |> Enum.uniq()
133
134 User.get_users_from_set(recipients, local_only)
135 end
136
137 def get_notified_from_activity(_, _local_only), do: []
138
139 defp maybe_notify_to_recipients(
140 recipients,
141 %Activity{data: %{"to" => to, "type" => _type}} = _activity
142 ) do
143 recipients ++ to
144 end
145
146 defp maybe_notify_mentioned_recipients(
147 recipients,
148 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
149 )
150 when type == "Create" do
151 object = Object.normalize(data["object"])
152
153 object_data =
154 cond do
155 !is_nil(object) ->
156 object.data
157
158 is_map(data["object"]) ->
159 data["object"]
160
161 true ->
162 %{}
163 end
164
165 tagged_mentions = maybe_extract_mentions(object_data)
166
167 recipients ++ tagged_mentions
168 end
169
170 defp maybe_notify_mentioned_recipients(recipients, _), do: recipients
171
172 defp maybe_extract_mentions(%{"tag" => tag}) do
173 tag
174 |> Enum.filter(fn x -> is_map(x) end)
175 |> Enum.filter(fn x -> x["type"] == "Mention" end)
176 |> Enum.map(fn x -> x["href"] end)
177 end
178
179 defp maybe_extract_mentions(_), do: []
180 end