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 User.blocks?(user, %{ap_id: activity.data["actor"]}) or
126 CommonAPI.thread_muted?(user, activity) or user.ap_id == activity.data["actor"] or
127 (activity.data["type"] == "Follow" and
128 Enum.any?(Notification.for_user(user), fn notif ->
129 notif.activity.data["type"] == "Follow" and
130 notif.activity.data["actor"] == activity.data["actor"]
132 notification = %Notification{user_id: user.id, activity: activity}
133 {:ok, notification} = Repo.insert(notification)
134 Pleroma.Web.Streamer.stream("user", notification)
135 Pleroma.Web.Push.send(notification)
140 def get_notified_from_activity(activity, local_only \\ true)
142 def get_notified_from_activity(
143 %Activity{data: %{"to" => _, "type" => type} = _data} = activity,
146 when type in ["Create", "Like", "Announce", "Follow"] do
149 |> Utils.maybe_notify_to_recipients(activity)
150 |> Utils.maybe_notify_mentioned_recipients(activity)
153 User.get_users_from_set(recipients, local_only)
156 def get_notified_from_activity(_, _local_only), do: []