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
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)
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)
24 defp restrict_max(query, _), do: query
26 defp restrict_since(query, %{"since_id" => since_id}) do
27 from(activity in query, where: activity.id > ^since_id)
30 defp restrict_since(query, _), do: query
32 def for_user(user, opts \\ %{}) do
36 where: n.user_id == ^user.id,
37 order_by: [desc: n.id],
38 join: activity in assoc(n, :activity),
39 preload: [activity: activity],
45 |> restrict_since(opts)
51 def set_read_up_to(%{id: user_id} = _user, id) do
55 where: n.user_id == ^user_id,
62 Repo.update_all(query, [])
65 def get(%{id: user_id} = _user, id) do
70 join: activity in assoc(n, :activity),
71 preload: [activity: activity]
74 notification = Repo.one(query)
77 %{user_id: ^user_id} ->
81 {:error, "Cannot get notification"}
86 from(n in Notification, where: n.user_id == ^user.id)
90 def dismiss(%{id: user_id} = _user, id) do
91 notification = Repo.get(Notification, id)
94 %{user_id: ^user_id} ->
95 Repo.delete(notification)
98 {:error, "Cannot dismiss notification"}
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)
106 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
110 def create_notifications(_), do: {:ok, []}
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"]
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)
129 def get_notified_from_activity(activity, local_only \\ true)
131 def get_notified_from_activity(
132 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
135 when type in ["Create", "Like", "Announce", "Follow"] do
138 |> Utils.maybe_notify_to_recipients(activity)
139 |> Utils.maybe_notify_mentioned_recipients(activity)
142 User.get_users_from_set(recipients, local_only)
145 def get_notified_from_activity(_, _local_only), do: []