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
9 alias Pleroma.Notification
11 alias Pleroma.Pagination
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.CommonAPI.Utils
20 schema "notifications" do
21 field(:seen, :boolean, default: false)
22 belongs_to(:user, User, type: Pleroma.FlakeId)
23 belongs_to(:activity, Activity, type: Pleroma.FlakeId)
28 def changeset(%Notification{} = notification, attrs) do
30 |> cast(attrs, [:seen])
33 def for_user_query(user) do
35 |> where(user_id: ^user.id)
36 |> join(:inner, [n], activity in assoc(n, :activity))
37 |> join(:left, [n, a], object in Object,
40 "(?->>'id') = COALESCE((? -> 'object'::text) ->> 'id'::text)",
45 |> preload([n, a, o], activity: {a, object: o})
48 def for_user(user, opts \\ %{}) do
51 |> Pagination.fetch_paginated(opts)
54 def set_read_up_to(%{id: user_id} = _user, id) do
58 where: n.user_id == ^user_id,
65 Repo.update_all(query, [])
68 def read_one(%User{} = user, notification_id) do
69 with {:ok, %Notification{} = notification} <- get(user, notification_id) do
71 |> changeset(%{seen: true})
76 def get(%{id: user_id} = _user, id) do
81 join: activity in assoc(n, :activity),
82 preload: [activity: activity]
85 notification = Repo.one(query)
88 %{user_id: ^user_id} ->
92 {:error, "Cannot get notification"}
97 from(n in Notification, where: n.user_id == ^user.id)
101 def destroy_multiple(%{id: user_id} = _user, ids) do
102 from(n in Notification,
104 where: n.user_id == ^user_id
109 def dismiss(%{id: user_id} = _user, id) do
110 notification = Repo.get(Notification, id)
113 %{user_id: ^user_id} ->
114 Repo.delete(notification)
117 {:error, "Cannot dismiss notification"}
121 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
122 when type in ["Create", "Like", "Announce", "Follow"] do
123 users = get_notified_from_activity(activity)
125 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
129 def create_notifications(_), do: {:ok, []}
131 # TODO move to sql, too.
132 def create_notification(%Activity{} = activity, %User{} = user) do
133 unless skip?(activity, user) do
134 notification = %Notification{user_id: user.id, activity: activity}
135 {:ok, notification} = Repo.insert(notification)
136 Pleroma.Web.Streamer.stream("user", notification)
137 Pleroma.Web.Push.send(notification)
142 def get_notified_from_activity(activity, local_only \\ true)
144 def get_notified_from_activity(
145 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
148 when type in ["Create", "Like", "Announce", "Follow"] do
151 |> Utils.maybe_notify_to_recipients(activity)
152 |> Utils.maybe_notify_mentioned_recipients(activity)
153 |> Utils.maybe_notify_subscribers(activity)
156 User.get_users_from_set(recipients, local_only)
159 def get_notified_from_activity(_, _local_only), do: []
161 def skip?(activity, user) do
162 [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
163 |> Enum.any?(&skip?(&1, activity, user))
166 def skip?(:self, activity, user) do
167 activity.data["actor"] == user.ap_id
170 def skip?(:blocked, activity, user) do
171 actor = activity.data["actor"]
172 User.blocks?(user, %{ap_id: actor})
175 def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
178 def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
181 def skip?(:muted, activity, user) do
182 actor = activity.data["actor"]
184 User.mutes?(user, %{ap_id: actor}) or CommonAPI.thread_muted?(user, activity)
190 %{info: %{notification_settings: %{"followers" => false}}} = user
192 actor = activity.data["actor"]
193 follower = User.get_cached_by_ap_id(actor)
194 User.following?(follower, user)
197 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
198 actor = activity.data["actor"]
199 followed = User.get_by_ap_id(actor)
200 User.following?(user, followed)
203 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
204 actor = activity.data["actor"]
206 Notification.for_user(user)
208 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
213 def skip?(_, _, _), do: false