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)
147 User.get_users_from_set(recipients, local_only)
150 def get_notified_from_activity(_, _local_only), do: []
152 def skip?(activity, user) do
153 [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
154 |> Enum.any?(&skip?(&1, activity, user))
157 def skip?(:self, activity, user) do
158 activity.data["actor"] == user.ap_id
161 def skip?(:blocked, activity, user) do
162 actor = activity.data["actor"]
163 User.blocks?(user, %{ap_id: actor})
166 def skip?(:local, %{local: true}, %{info: %{notification_settings: %{"local" => false}}}),
169 def skip?(:local, %{local: false}, %{info: %{notification_settings: %{"remote" => false}}}),
172 def skip?(:muted, activity, user) do
173 actor = activity.data["actor"]
175 User.mutes?(user, %{ap_id: actor}) or
176 CommonAPI.thread_muted?(user, activity)
182 %{info: %{notification_settings: %{"followers" => false}}} = user
184 actor = activity.data["actor"]
185 follower = User.get_cached_by_ap_id(actor)
186 User.following?(follower, user)
189 def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
190 actor = activity.data["actor"]
191 followed = User.get_by_ap_id(actor)
192 User.following?(user, followed)
195 def skip?(:recently_followed, %{data: %{"type" => "Follow"}} = activity, user) do
196 actor = activity.data["actor"]
198 Notification.for_user(user)
200 %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
205 def skip?(_, _, _), do: false