Merge branch 'hj-happiness-improvement' 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 join: activity in assoc(n, :activity),
39 preload: [activity: activity],
40 limit: 20
41 )
42
43 query =
44 query
45 |> restrict_since(opts)
46 |> restrict_max(opts)
47
48 Repo.all(query)
49 end
50
51 def set_read_up_to(%{id: user_id} = _user, id) do
52 query =
53 from(
54 n in Notification,
55 where: n.user_id == ^user_id,
56 where: n.id <= ^id,
57 update: [
58 set: [seen: true]
59 ]
60 )
61
62 Repo.update_all(query, [])
63 end
64
65 def get(%{id: user_id} = _user, id) do
66 query =
67 from(
68 n in Notification,
69 where: n.id == ^id,
70 join: activity in assoc(n, :activity),
71 preload: [activity: activity]
72 )
73
74 notification = Repo.one(query)
75
76 case notification do
77 %{user_id: ^user_id} ->
78 {:ok, notification}
79
80 _ ->
81 {:error, "Cannot get notification"}
82 end
83 end
84
85 def clear(user) do
86 from(n in Notification, where: n.user_id == ^user.id)
87 |> Repo.delete_all()
88 end
89
90 def dismiss(%{id: user_id} = _user, id) do
91 notification = Repo.get(Notification, id)
92
93 case notification do
94 %{user_id: ^user_id} ->
95 Repo.delete(notification)
96
97 _ ->
98 {:error, "Cannot dismiss notification"}
99 end
100 end
101
102 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
103 when type in ["Create", "Like", "Announce", "Follow"] do
104 users = get_notified_from_activity(activity)
105
106 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
107 {:ok, notifications}
108 end
109
110 def create_notifications(_), do: {:ok, []}
111
112 # TODO move to sql, too.
113 def create_notification(%Activity{} = activity, %User{} = user) do
114 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
115 user.ap_id == activity.data["actor"] or
116 (activity.data["type"] == "Follow" and
117 Enum.any?(Notification.for_user(user), fn notif ->
118 notif.activity.data["type"] == "Follow" and
119 notif.activity.data["actor"] == activity.data["actor"]
120 end)) do
121 notification = %Notification{user_id: user.id, activity: activity}
122 {:ok, notification} = Repo.insert(notification)
123 Pleroma.Web.Streamer.stream("user", notification)
124 Pleroma.Web.Push.send(notification)
125 notification
126 end
127 end
128
129 def get_notified_from_activity(activity, local_only \\ true)
130
131 def get_notified_from_activity(
132 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
133 local_only
134 )
135 when type in ["Create", "Like", "Announce", "Follow"] do
136 recipients =
137 []
138 |> Utils.maybe_notify_to_recipients(activity)
139 |> Utils.maybe_notify_mentioned_recipients(activity)
140 |> Enum.uniq()
141
142 User.get_users_from_set(recipients, local_only)
143 end
144
145 def get_notified_from_activity(_, _local_only), do: []
146 end