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