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