Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into fork
Browse files Browse the repository at this point in the history
Signed-off-by: marcin mikołajczak <[email protected]>
  • Loading branch information
mkljczk committed May 17, 2024
2 parents adbd400 + e944b15 commit e5510d4
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 241 deletions.
1 change: 1 addition & 0 deletions changelog.d/instance-rules.add
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add instance rules
1 change: 1 addition & 0 deletions changelog.d/strip-object-actor.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Strip actor property from objects before federating
1 change: 1 addition & 0 deletions changelog.d/web_push_filtered.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Web Push notifications are no longer generated for muted/blocked threads and users.
2 changes: 2 additions & 0 deletions lib/pleroma/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Pleroma.Constants do

const(object_internal_fields,
do: [
"actor",
"reactions",
"reaction_count",
"likes",
Expand All @@ -20,6 +21,7 @@ defmodule Pleroma.Constants do
"deleted_activity_id",
"pleroma_internal",
"generator",
"rules",
"assigned_account",
"rules",
"content_type",
Expand Down
86 changes: 37 additions & 49 deletions lib/pleroma/notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,20 @@ defmodule Pleroma.Notification do
end
end

@spec create_notifications(Activity.t(), keyword()) :: {:ok, [Notification.t()] | []}
def create_notifications(activity, options \\ [])
@spec create_notifications(Activity.t()) :: {:ok, [Notification.t()] | []}
def create_notifications(activity)

def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity, options) do
def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
object = Object.normalize(activity, fetch: false)

if object && object.data["type"] == "Answer" do
{:ok, []}
else
do_create_notifications(activity, options)
do_create_notifications(activity)
end
end

def create_notifications(%Activity{data: %{"type" => type}} = activity, options)
def create_notifications(%Activity{data: %{"type" => type}} = activity)
when type in [
"Follow",
"Like",
Expand All @@ -393,41 +393,29 @@ defmodule Pleroma.Notification do
"Accept",
"Join"
] do
do_create_notifications(activity, options)
do_create_notifications(activity)
end

def create_notifications(_, _), do: {:ok, []}
def create_notifications(_), do: {:ok, []}

defp do_create_notifications(%Activity{} = activity, options) do
do_send = Keyword.get(options, :do_send, true)
defp do_create_notifications(%Activity{} = activity) do
enabled_participants = get_notified_participants_from_activity(activity)

{enabled_participants, disabled_participants} =
get_notified_participants_from_activity(activity)
enabled_receivers = get_notified_from_activity(activity) -- enabled_participants

potential_participants = enabled_participants ++ disabled_participants

{enabled_receivers, disabled_receivers} = get_notified_from_activity(activity)

potential_receivers = (enabled_receivers ++ disabled_receivers) -- potential_participants

{enabled_subscribers, disabled_subscribers} = get_notified_subscribers_from_activity(activity)

potential_subscribers =
(enabled_subscribers ++ disabled_subscribers) --
(potential_participants ++ potential_receivers)
enabled_subscribers =
get_notified_subscribers_from_activity(activity) --
(enabled_participants ++ enabled_receivers)

notifications =
(Enum.map(potential_receivers, fn user ->
do_send = do_send && user in enabled_receivers
create_notification(activity, user, do_send: do_send)
(Enum.map(enabled_receivers, fn user ->
create_notification(activity, user)
end) ++
Enum.map(potential_subscribers, fn user ->
do_send = do_send && user in enabled_subscribers
create_notification(activity, user, do_send: do_send, type: "status")
Enum.map(enabled_subscribers, fn user ->
create_notification(activity, user, type: "status")
end) ++
Enum.map(potential_participants, fn user ->
do_send = do_send && user in enabled_participants
create_notification(activity, user, do_send: do_send, type: "pleroma:event_update")
Enum.map(enabled_participants, fn user ->
create_notification(activity, user, type: "pleroma:event_update")
end))
|> Enum.reject(&is_nil/1)

Expand Down Expand Up @@ -493,7 +481,6 @@ defmodule Pleroma.Notification do

# TODO move to sql, too.
def create_notification(%Activity{} = activity, %User{} = user, opts \\ []) do
do_send = Keyword.get(opts, :do_send, true)
type = Keyword.get(opts, :type, type_from_activity(activity))

unless skip?(activity, user, opts) do
Expand All @@ -508,11 +495,6 @@ defmodule Pleroma.Notification do
|> Marker.multi_set_last_read_id(user, "notifications")
|> Repo.transaction()

if do_send do
Streamer.stream(["user", "user:notification"], notification)
Push.send(notification)
end

notification
end
end
Expand Down Expand Up @@ -594,10 +576,7 @@ defmodule Pleroma.Notification do
|> exclude_relationship_restricted_ap_ids(activity)
|> exclude_thread_muter_ap_ids(activity)

notification_enabled_users =
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)

{notification_enabled_users, potential_receivers -- notification_enabled_users}
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
end

def get_notified_from_activity(_, _local_only), do: {[], []}
Expand All @@ -615,10 +594,7 @@ defmodule Pleroma.Notification do
potential_receivers =
User.get_users_from_set(notification_enabled_ap_ids, local_only: local_only)

notification_enabled_users =
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)

{notification_enabled_users, potential_receivers -- notification_enabled_users}
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
end

def get_notified_subscribers_from_activity(_, _), do: {[], []}
Expand All @@ -636,10 +612,7 @@ defmodule Pleroma.Notification do
potential_receivers =
User.get_users_from_set(notification_enabled_ap_ids, local_only: local_only)

notification_enabled_users =
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)

{notification_enabled_users, potential_receivers -- notification_enabled_users}
Enum.filter(potential_receivers, fn u -> u.ap_id in notification_enabled_ap_ids end)
end

def get_notified_participants_from_activity(_, _), do: {[], []}
Expand Down Expand Up @@ -780,6 +753,7 @@ defmodule Pleroma.Notification do
def skip?(%Activity{} = activity, %User{} = user, opts) do
[
:self,
:internal,
:invisible,
:block_from_strangers,
:recently_followed,
Expand All @@ -799,6 +773,12 @@ defmodule Pleroma.Notification do
end
end

def skip?(:internal, %Activity{} = activity, _user, _opts) do
actor = activity.data["actor"]
user = User.get_cached_by_ap_id(actor)
User.internal?(user)
end

def skip?(:invisible, %Activity{} = activity, _user, _opts) do
actor = activity.data["actor"]
user = User.get_cached_by_ap_id(actor)
Expand Down Expand Up @@ -885,4 +865,12 @@ defmodule Pleroma.Notification do
)
|> Repo.update_all(set: [seen: true])
end

@spec send(list(Notification.t())) :: :ok
def send(notifications) do
Enum.each(notifications, fn notification ->
Streamer.stream(["user", "user:notification"], notification)
Push.send(notification)
end)
end
end
10 changes: 10 additions & 0 deletions lib/pleroma/web/activity_pub/activity_pub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do

defp restrict_rule(query, _), do: query

defp restrict_rule(query, %{rule_id: rule_id}) do
from(
activity in query,
where: fragment("(?)->'rules' \\? (?)", activity.data, ^rule_id)
)
end

defp restrict_rule(query, _), do: query

defp exclude_poll_votes(query, %{include_poll_votes: true}), do: query

defp exclude_poll_votes(query, _) do
Expand Down Expand Up @@ -1515,6 +1524,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> restrict_announce_object_actor(opts)
|> restrict_object(opts)
|> restrict_filtered(opts)
|> restrict_rule(opts)
|> restrict_quote_url(opts)
|> restrict_rule(opts)
|> maybe_restrict_deactivated_users(opts)
Expand Down
32 changes: 19 additions & 13 deletions lib/pleroma/web/activity_pub/side_effects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
alias Pleroma.Web.ActivityPub.Builder
alias Pleroma.Web.ActivityPub.Pipeline
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.Push
alias Pleroma.Web.Streamer
alias Pleroma.Workers.EventReminderWorker
alias Pleroma.Workers.PollWorker
Expand Down Expand Up @@ -115,7 +114,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
nil
end

{:ok, notifications} = Notification.create_notifications(object, do_send: false)
{:ok, notifications} = Notification.create_notifications(object)

meta =
meta
Expand Down Expand Up @@ -174,7 +173,11 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
liked_object = Object.get_by_ap_id(object.data["object"])
Utils.add_like_to_object(object, liked_object)

Notification.create_notifications(object)
{:ok, notifications} = Notification.create_notifications(object)

meta =
meta
|> add_notifications(notifications)

{:ok, object, meta}
end
Expand All @@ -192,7 +195,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
def handle(%{data: %{"type" => "Create"}} = activity, meta) do
with {:ok, object, meta} <- handle_object_creation(meta[:object_data], activity, meta),
%User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
{:ok, notifications} = Notification.create_notifications(activity, do_send: false)
{:ok, notifications} = Notification.create_notifications(activity)
{:ok, _user} = ActivityPub.increase_note_count_if_public(user, object)
{:ok, _user} = ActivityPub.update_last_status_at_if_public(user, object)

Expand Down Expand Up @@ -250,11 +253,13 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do

Utils.add_announce_to_object(object, announced_object)

if !User.internal?(user) do
Notification.create_notifications(object)
{:ok, notifications} = Notification.create_notifications(object)

ap_streamer().stream_out(object)
end
if !User.internal?(user), do: ap_streamer().stream_out(object)

meta =
meta
|> add_notifications(notifications)

{:ok, object, meta}
end
Expand All @@ -275,7 +280,11 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
reacted_object = Object.get_by_ap_id(object.data["object"])
Utils.add_emoji_reaction_to_object(object, reacted_object)

Notification.create_notifications(object)
{:ok, notifications} = Notification.create_notifications(object)

meta =
meta
|> add_notifications(notifications)

{:ok, object, meta}
end
Expand Down Expand Up @@ -679,10 +688,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do

defp send_notifications(meta) do
Keyword.get(meta, :notifications, [])
|> Enum.each(fn notification ->
Streamer.stream(["user", "user:notification"], notification)
Push.send(notification)
end)
|> Notification.send()

meta
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pleroma/web/admin_api/views/report_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
}),
state: report.data["state"],
notes: render(__MODULE__, "index_notes.json", %{notes: report.report_notes}),
assigned_account: assigned_account,
rules: rules(Map.get(report.data, "rules", nil))
rules: rules(Map.get(report.data, "rules", nil)),
assigned_account: assigned_account
}
end

Expand Down
1 change: 1 addition & 0 deletions lib/pleroma/web/api_spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ defmodule Pleroma.Web.ApiSpec do
"Frontend management",
"Instance configuration",
"Instance documents",
"Instance rule managment",
"Invites",
"MediaProxy cache",
"OAuth application management",
Expand Down
8 changes: 4 additions & 4 deletions lib/pleroma/web/api_spec/operations/admin/report_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ defmodule Pleroma.Web.ApiSpec.Admin.ReportOperation do
}
}
},
assigned_account:
account_admin()
|> Map.put(:nullable, true),
rules: %Schema{
type: :array,
items: %Schema{
Expand All @@ -211,7 +208,10 @@ defmodule Pleroma.Web.ApiSpec.Admin.ReportOperation do
hint: %Schema{type: :string, nullable: true}
}
}
}
},
assigned_account:
account_admin()
|> Map.put(:nullable, true)
}
}
end
Expand Down
6 changes: 3 additions & 3 deletions lib/pleroma/web/mastodon_api/views/instance_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do

defp common_information(instance) do
%{
title: Keyword.get(instance, :name),
version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.compat_version()})",
languages: Keyword.get(instance, :languages, ["en"]),
rules: render(__MODULE__, "rules.json")
rules: render(__MODULE__, "rules.json"),
title: Keyword.get(instance, :name),
version: "#{@mastodon_api_level} (compatible; #{Pleroma.Application.compat_version()})"
}
end

Expand Down
5 changes: 3 additions & 2 deletions lib/pleroma/workers/notification_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ defmodule Pleroma.Workers.NotificationWorker do
@impl Oban.Worker
@spec perform(Oban.Job.t()) :: {:error, :activity_not_found} | {:ok, [Pleroma.Notification.t()]}
def perform(%Job{args: %{"op" => "create", "activity_id" => activity_id}}) do
with %Activity{} = activity <- find_activity(activity_id) do
Notification.create_notifications(activity)
with %Activity{} = activity <- find_activity(activity_id),
{:ok, notifications} <- Notification.create_notifications(activity) do
Notification.send(notifications)
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/create-chat-message.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"actor": "http://2hu.gensokyo/users/raymoo",
"id": "http://2hu.gensokyo/objects/1",
"actor": "http://mastodon.example.org/users/admin",
"id": "http://mastodon.example.org/objects/1",
"object": {
"attributedTo": "http://2hu.gensokyo/users/raymoo",
"attributedTo": "http://mastodon.example.org/users/admin",
"content": "You expected a cute girl? Too bad. <script>alert('XSS')</script>",
"id": "http://2hu.gensokyo/objects/2",
"id": "http://mastodon.example.org/objects/2",
"published": "2020-02-12T14:08:20Z",
"to": [
"http://2hu.gensokyo/users/marisa"
Expand Down
Loading

0 comments on commit e5510d4

Please sign in to comment.