Merge branch 'fix/tusky-dm' 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}
8 alias Pleroma.Web.CommonAPI.Utils
9 import Ecto.Query
10
11 schema "notifications" do
12 field(:seen, :boolean, default: false)
13 belongs_to(:user, User, type: Pleroma.FlakeId)
14 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
15
16 timestamps()
17 end
18
19 # TODO: Make generic and unify (see activity_pub.ex)
20 defp restrict_max(query, %{"max_id" => max_id}) do
21 from(activity in query, where: activity.id < ^max_id)
22 end
23
24 defp restrict_max(query, _), do: query
25
26 defp restrict_since(query, %{"since_id" => since_id}) do
27 from(activity in query, where: activity.id > ^since_id)
28 end
29
30 defp restrict_since(query, _), do: query
31
32 def for_user(user, opts \\ %{}) do
33 query =
34 from(
35 n in Notification,
36 where: n.user_id == ^user.id,
37 order_by: [desc: n.id],
38 preload: [:activity],
39 limit: 20
40 )
41
42 query =
43 query
44 |> restrict_since(opts)
45 |> restrict_max(opts)
46
47 Repo.all(query)
48 end
49
50 def set_read_up_to(%{id: user_id} = _user, id) do
51 query =
52 from(
53 n in Notification,
54 where: n.user_id == ^user_id,
55 where: n.id <= ^id,
56 update: [
57 set: [seen: true]
58 ]
59 )
60
61 Repo.update_all(query, [])
62 end
63
64 def get(%{id: user_id} = _user, id) do
65 query =
66 from(
67 n in Notification,
68 where: n.id == ^id,
69 preload: [:activity]
70 )
71
72 notification = Repo.one(query)
73
74 case notification do
75 %{user_id: ^user_id} ->
76 {:ok, notification}
77
78 _ ->
79 {:error, "Cannot get notification"}
80 end
81 end
82
83 def clear(user) do
84 from(n in Notification, where: n.user_id == ^user.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{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"] or
114 (activity.data["type"] == "Follow" and
115 Enum.any?(Notification.for_user(user), fn notif ->
116 notif.activity.data["type"] == "Follow" and
117 notif.activity.data["actor"] == activity.data["actor"]
118 end)) do
119 notification = %Notification{user_id: user.id, activity: activity}
120 {:ok, notification} = Repo.insert(notification)
121 Pleroma.Web.Streamer.stream("user", notification)
122 Pleroma.Web.Push.send(notification)
123 notification
124 end
125 end
126
127 def get_notified_from_activity(activity, local_only \\ true)
128
129 def get_notified_from_activity(
130 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
131 local_only
132 )
133 when type in ["Create", "Like", "Announce", "Follow"] do
134 recipients =
135 []
136 |> Utils.maybe_notify_to_recipients(activity)
137 |> Utils.maybe_notify_mentioned_recipients(activity)
138 |> Enum.uniq()
139
140 User.get_users_from_set(recipients, local_only)
141 end
142
143 def get_notified_from_activity(_, _local_only), do: []
144 end