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 dismiss(%{id: user_id} = _user, id) do
102 notification = Repo.get(Notification, id)
105 %{user_id: ^user_id} ->
106 Repo.delete(notification)
109 {:error, "Cannot dismiss notification"}
113 def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
114 when type in ["Create", "Like", "Announce", "Follow"] do
115 users = get_notified_from_activity(activity)
117 notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
121 def create_notifications(_), do: {:ok, []}
123 # TODO move to sql, too.
124 def create_notification(%Activity{} = activity, %User{} = user) do
125 unless skip?(activity, user) do
126 notification = %Notification{user_id: user.id, activity: activity}
127 {:ok, notification} = Repo.insert(notification)
128 Pleroma.Web.Streamer.stream("user", notification)
129 Pleroma.Web.Push.send(notification)
134 def get_notified_from_activity(activity, local_only \\ true)
136 def get_notified_from_activity(
137 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
140 when type in ["Create", "Like", "Announce", "Follow"] do
143 |> Utils.maybe_notify_to_recipients(activity)
144 |> Utils.maybe_notify_mentioned_recipients(activity)
145 |> Utils.maybe_notify_subscribers(activity)
148 User.get_users_from_set(recipients, local_only)
151 def get_notified_from_activity(_, _local_only), do: []
153 def skip?(activity, user) do
154 [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
155 |> Enum.any?(&skip?(&1, activity, user))
158 def skip?(:self, activity, user) do
159 activity.data["actor"] == user.ap_id
162 def skip?(:blocked, activity, user) do
163 actor = activity.data["actor"]
164 User.blocks?(user, %{ap_id: actor})
167 def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
170 def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
173 def skip?(:muted, activity, user) do
174 actor = activity.data["actor"]
176 User.mutes?(user, %{ap_id: actor}) or
177 CommonAPI.thread_muted?(user, activity)
183 %{info: %{notification_settings: %{"followers" => false}}} = user
185 actor = activity.data["actor"]
186 follower = User.get_cached_by_ap_id(actor)
187 User.following?(follower, user)
190 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
191 actor = activity.data["actor"]
192 followed = User.get_by_ap_id(actor)
193 User.following?(user, followed)
196 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
197 actor = activity.data["actor"]
199 Notification.for_user(user)
201 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
206 def skip?(_, _, _), do: false