1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Notification do
10 alias Pleroma.Notification
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.CommonAPI
17 schema "notifications" do
18 field(:seen, :boolean, default: false)
19 belongs_to(:user, User, type: Pleroma.FlakeId)
20 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
25 # TODO: Make generic and unify (see activity_pub.ex)
26 defp restrict_max(query, %{"max_id" => max_id}) do
27 from(activity in query, where: activity.id < ^max_id)
30 defp restrict_max(query, _), do: query
32 defp restrict_since(query, %{"since_id" => since_id}) do
33 from(activity in query, where: activity.id > ^since_id)
36 defp restrict_since(query, _), do: query
38 def for_user(user, opts \\ %{}) do
41 where: n.user_id == ^user.id,
42 order_by: [desc: n.id],
43 join: activity in assoc(n, :activity),
44 preload: [activity: activity],
48 "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",
52 |> restrict_since(opts)
57 def set_read_up_to(%{id: user_id} = _user, id) do
61 where: n.user_id == ^user_id,
68 Repo.update_all(query, [])
71 def get(%{id: user_id} = _user, id) do
76 join: activity in assoc(n, :activity),
77 preload: [activity: activity]
80 notification = Repo.one(query)
83 %{user_id: ^user_id} ->
87 {:error, "Cannot get notification"}
92 from(n in Notification, where: n.user_id == ^user.id)
96 def dismiss(%{id: user_id} = _user, id) do
97 notification = Repo.get(Notification, id)
100 %{user_id: ^user_id} ->
101 Repo.delete(notification)
104 {:error, "Cannot dismiss notification"}
108 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
109 when type in ["Create", "Like", "Announce", "Follow"] do
110 users = get_notified_from_activity(activity)
112 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
116 def create_notifications(_), do: {:ok, []}
118 # TODO move to sql, too.
119 def create_notification(%Activity{} = activity, %User{} = user) do
120 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
121 CommonAPI.thread_muted?(user, activity) or user.ap_id == activity.data["actor"] or
122 (activity.data["type"] == "Follow" and
123 Enum.any?(Notification.for_user(user), fn notif ->
124 notif.activity.data["type"] == "Follow" and
125 notif.activity.data["actor"] == activity.data["actor"]
127 notification = %Notification{user_id: user.id, activity: activity}
128 {:ok, notification} = Repo.insert(notification)
129 Pleroma.Web.Streamer.stream("user", notification)
130 Pleroma.Web.Push.send(notification)
135 def get_notified_from_activity(activity, local_only \\ true)
137 def get_notified_from_activity(
138 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
141 when type in ["Create", "Like", "Announce", "Follow"] do
144 |> Utils.maybe_notify_to_recipients(activity)
145 |> Utils.maybe_notify_mentioned_recipients(activity)
148 User.get_users_from_set(recipients, local_only)
151 def get_notified_from_activity(_, _local_only), do: []