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, Object}
10 schema "notifications" do
11 field(:seen, :boolean, default: false)
12 belongs_to(:user, Pleroma.User)
13 belongs_to(:activity, Pleroma.Activity)
18 # TODO: Make generic and unify (see activity_pub.ex)
19 defp restrict_max(query, %{"max_id" => max_id}) do
20 from(activity in query, where: activity.id < ^max_id)
23 defp restrict_max(query, _), do: query
25 defp restrict_since(query, %{"since_id" => since_id}) do
26 from(activity in query, where: activity.id > ^since_id)
29 defp restrict_since(query, _), do: query
31 def for_user(user, opts \\ %{}) do
35 where: n.user_id == ^user.id,
36 order_by: [desc: n.id],
43 |> restrict_since(opts)
49 def set_read_up_to(%{id: user_id} = _user, id) do
53 where: n.user_id == ^user_id,
60 Repo.update_all(query, [])
63 def get(%{id: user_id} = _user, id) do
71 notification = Repo.one(query)
74 %{user_id: ^user_id} ->
78 {:error, "Cannot get notification"}
83 from(n in Notification, where: n.user_id == ^user.id)
87 def dismiss(%{id: user_id} = _user, id) do
88 notification = Repo.get(Notification, id)
91 %{user_id: ^user_id} ->
92 Repo.delete(notification)
95 {:error, "Cannot dismiss notification"}
99 def create_notifications(%Activity{id: _, data: %{"to" => _, "type" => type}} = activity)
100 when type in ["Create", "Like", "Announce", "Follow"] do
101 users = get_notified_from_activity(activity)
103 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
107 def create_notifications(_), do: {:ok, []}
109 # TODO move to sql, too.
110 def create_notification(%Activity{} = activity, %User{} = user) do
111 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
112 user.ap_id == activity.data["actor"] or
113 (activity.data["type"] == "Follow" and
114 Enum.any?(Notification.for_user(user), fn notif ->
115 notif.activity.data["type"] == "Follow" and
116 notif.activity.data["actor"] == activity.data["actor"]
118 notification = %Notification{user_id: user.id, activity: activity}
119 {:ok, notification} = Repo.insert(notification)
120 Pleroma.Web.Streamer.stream("user", notification)
121 Pleroma.Web.Push.send(notification)
126 def get_notified_from_activity(activity, local_only \\ true)
128 def get_notified_from_activity(
129 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
132 when type in ["Create", "Like", "Announce", "Follow"] do
135 |> maybe_notify_to_recipients(activity)
136 |> maybe_notify_mentioned_recipients(activity)
139 User.get_users_from_set(recipients, local_only)
142 def get_notified_from_activity(_, _local_only), do: []
144 defp maybe_notify_to_recipients(
146 %Activity{data: %{"to" => to, "type" => _type}} = _activity
151 defp maybe_notify_mentioned_recipients(
153 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
155 when type == "Create" do
156 object = Object.normalize(data["object"])
163 is_map(data["object"]) ->
170 tagged_mentions = maybe_extract_mentions(object_data)
172 recipients ++ tagged_mentions
175 defp maybe_notify_mentioned_recipients(recipients, _), do: recipients
177 defp maybe_extract_mentions(%{"tag" => tag}) do
179 |> Enum.filter(fn x -> is_map(x) end)
180 |> Enum.filter(fn x -> x["type"] == "Mention" end)
181 |> Enum.map(fn x -> x["href"] end)
184 defp maybe_extract_mentions(_), do: []