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