1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 query = from(n in Notification, where: n.user_id == ^user.id)
85 Repo.delete_all(query)
88 def dismiss(%{id: user_id} = _user, id) do
89 notification = Repo.get(Notification, id)
92 %{user_id: ^user_id} ->
93 Repo.delete(notification)
96 {:error, "Cannot dismiss notification"}
100 def create_notifications(%Activity{id: _, data: %{"to" => _, "type" => type}} = activity)
101 when type in ["Create", "Like", "Announce", "Follow"] do
102 users = get_notified_from_activity(activity)
104 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
108 def create_notifications(_), do: {:ok, []}
110 # TODO move to sql, too.
111 def create_notification(%Activity{} = activity, %User{} = user) do
112 unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
113 user.ap_id == activity.data["actor"] do
114 notification = %Notification{user_id: user.id, activity: activity}
115 {:ok, notification} = Repo.insert(notification)
116 Pleroma.Web.Streamer.stream("user", notification)
117 Pleroma.Web.Push.send(notification)
122 def get_notified_from_activity(activity, local_only \\ true)
124 def get_notified_from_activity(
125 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
128 when type in ["Create", "Like", "Announce", "Follow"] do
131 |> maybe_notify_to_recipients(activity)
132 |> maybe_notify_mentioned_recipients(activity)
135 User.get_users_from_set(recipients, local_only)
138 def get_notified_from_activity(_, _local_only), do: []
140 defp maybe_notify_to_recipients(
142 %Activity{data: %{"to" => to, "type" => _type}} = _activity
147 defp maybe_notify_mentioned_recipients(
149 %Activity{data: %{"to" => _to, "type" => type} = data} = _activity
151 when type == "Create" do
152 object = Object.normalize(data["object"])
159 is_map(data["object"]) ->
166 tagged_mentions = maybe_extract_mentions(object_data)
168 recipients ++ tagged_mentions
171 defp maybe_notify_mentioned_recipients(recipients, _), do: recipients
173 defp maybe_extract_mentions(%{"tag" => tag}) do
175 |> Enum.filter(fn x -> is_map(x) end)
176 |> Enum.filter(fn x -> x["type"] == "Mention" end)
177 |> Enum.map(fn x -> x["href"] end)
180 defp maybe_extract_mentions(_), do: []