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
7 alias Pleroma.{User, Activity, Notification, Repo}
8 alias Pleroma.Web.CommonAPI.Utils
9 alias Pleroma.Web.ThreadMute
12 schema "notifications" do
13 field(:seen, :boolean, default: false)
14 belongs_to(:user, User, type: Pleroma.FlakeId)
15 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
20 # TODO: Make generic and unify (see activity_pub.ex)
21 defp restrict_max(query, %{"max_id" => max_id}) do
22 from(activity in query, where: activity.id < ^max_id)
25 defp restrict_max(query, _), do: query
27 defp restrict_since(query, %{"since_id" => since_id}) do
28 from(activity in query, where: activity.id > ^since_id)
31 defp restrict_since(query, _), do: query
33 def for_user(user, opts \\ %{}) do
37 where: n.user_id == ^user.id,
38 order_by: [desc: n.id],
39 join: activity in assoc(n, :activity),
40 preload: [activity: activity],
46 |> restrict_since(opts)
52 def set_read_up_to(%{id: user_id} = _user, id) do
56 where: n.user_id == ^user_id,
63 Repo.update_all(query, [])
66 def get(%{id: user_id} = _user, id) do
71 join: activity in assoc(n, :activity),
72 preload: [activity: activity]
75 notification = Repo.one(query)
78 %{user_id: ^user_id} ->
82 {:error, "Cannot get notification"}
87 from(n in Notification, where: n.user_id == ^user.id)
91 def dismiss(%{id: user_id} = _user, id) do
92 notification = Repo.get(Notification, id)
95 %{user_id: ^user_id} ->
96 Repo.delete(notification)
99 {:error, "Cannot dismiss notification"}
103 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
104 when type in ["Create", "Like", "Announce", "Follow"] do
105 users = get_notified_from_activity(activity)
107 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
111 def create_notifications(_), do: {:ok, []}
113 # TODO move to sql, too.
114 def create_notification(%Activity{} = activity, %User{} = user) do
115 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
116 ThreadMute.muted?(user, activity) or user.ap_id == activity.data["actor"] or
117 (activity.data["type"] == "Follow" and
118 Enum.any?(Notification.for_user(user), fn notif ->
119 notif.activity.data["type"] == "Follow" and
120 notif.activity.data["actor"] == activity.data["actor"]
122 notification = %Notification{user_id: user.id, activity: activity}
123 {:ok, notification} = Repo.insert(notification)
124 Pleroma.Web.Streamer.stream("user", notification)
125 Pleroma.Web.Push.send(notification)
130 def get_notified_from_activity(activity, local_only \\ true)
132 def get_notified_from_activity(
133 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
136 when type in ["Create", "Like", "Announce", "Follow"] do
139 |> Utils.maybe_notify_to_recipients(activity)
140 |> Utils.maybe_notify_mentioned_recipients(activity)
143 User.get_users_from_set(recipients, local_only)
146 def get_notified_from_activity(_, _local_only), do: []