Allow to bite users

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
Signed-off-by: limepotato <limepot@protonmail.ch>
This commit is contained in:
marcin mikołajczak 2024-08-27 17:33:03 +02:00 committed by limepotato
parent 050bc74437
commit 53416190a9
20 changed files with 363 additions and 36 deletions

View file

@ -72,6 +72,7 @@ defmodule Pleroma.Notification do
pleroma:report
reblog
poll
bite
}
def changeset(%Notification{} = notification, attrs) do
@ -402,7 +403,7 @@ defmodule Pleroma.Notification do
end
def create_notifications(%Activity{data: %{"type" => type}} = activity, options)
when type in ["Follow", "Like", "Announce", "Move", "EmojiReact", "Flag", "Update"] do
when type in ["Follow", "Like", "Announce", "Move", "EmojiReact", "Flag", "Update", "Bite"] do
do_create_notifications(activity, options)
end
@ -459,6 +460,9 @@ defmodule Pleroma.Notification do
"Update" ->
"update"
"Bite" ->
"bite"
t ->
raise "No notification type for activity type #{t}"
end
@ -532,7 +536,8 @@ defmodule Pleroma.Notification do
"Move",
"EmojiReact",
"Flag",
"Update"
"Update",
"Bite"
] do
potential_receiver_ap_ids = get_potential_receiver_ap_ids(activity)

View file

@ -406,4 +406,15 @@ defmodule Pleroma.Web.ActivityPub.Builder do
defp pinned_url(nickname) when is_binary(nickname) do
url(~p[/users/#{nickname}/collections/featured])
end
def bite(%User{} = biting, %User{} = bitten) do
{:ok,
%{
"id" => Utils.generate_activity_id(),
"target" => bitten.ap_id,
"actor" => biting.ap_id,
"type" => "Bite",
"to" => [bitten.ap_id]
}, []}
end
end

View file

@ -22,6 +22,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
alias Pleroma.Web.ActivityPub.ObjectValidators.AnswerValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.BiteValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
@ -160,7 +161,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
def validate(%{"type" => type} = object, meta)
when type in ~w[Accept Reject Follow Update Like EmojiReact Announce
Answer] do
Answer Bite] do
validator =
case type do
"Accept" -> AcceptRejectValidator
@ -171,6 +172,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
"EmojiReact" -> EmojiReactValidator
"Announce" -> AnnounceValidator
"Answer" -> AnswerValidator
"Bite" -> BiteValidator
end
with {:ok, object} <-

View file

@ -33,7 +33,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do
|> validate_required([:type, :actor, :to, :cc, :object])
|> validate_inclusion(:type, ["Accept", "Reject"])
|> validate_actor_presence()
|> validate_object_presence(allowed_types: ["Follow"])
|> validate_object_presence(allowed_types: ["Follow", "Bite"])
|> validate_accept_reject_rights()
end
@ -46,8 +46,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do
def validate_accept_reject_rights(cng) do
with object_id when is_binary(object_id) <- get_field(cng, :object),
%Activity{data: %{"object" => followed_actor}} <- Activity.get_by_ap_id(object_id),
true <- followed_actor == get_field(cng, :actor) do
%Activity{} = activity <- Activity.get_by_ap_id(object_id),
true <- validate_actor(activity, get_field(cng, :actor)) do
cng
else
_e ->
@ -56,6 +56,14 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AcceptRejectValidator do
end
end
defp validate_actor(%Activity{data: %{"type" => "Follow", "object" => followed_actor}}, actor) do
followed_actor == actor
end
defp validate_actor(%Activity{data: %{"type" => "Bite", "target" => biten_actor}}, actor) do
biten_actor == actor
end
defp maybe_fetch_object(%{"object" => %{} = object} = activity) do
# If we don't have an ID, we may have to fetch the object
if Map.has_key?(object, "id") do

View file

@ -0,0 +1,49 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.BiteValidator do
use Ecto.Schema
alias Pleroma.EctoType.ActivityPub.ObjectValidators
import Ecto.Changeset
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
@primary_key false
embedded_schema do
quote do
unquote do
import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
message_fields()
activity_fields()
end
end
field(:target, ObjectValidators.ObjectID)
end
def cast_data(data) do
%__MODULE__{}
|> cast(data |> fix_object(), __schema__(:fields))
end
defp fix_object(data) do
Map.put(data, "object", data["target"])
end
defp validate_data(cng) do
cng
|> validate_required([:id, :type, :actor, :to, :target])
|> validate_inclusion(:type, ["Bite"])
|> validate_actor_presence()
|> validate_actor_presence(field_name: :target)
end
def cast_and_validate(data) do
data
|> cast_data
|> validate_data
end
end

View file

@ -40,23 +40,16 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
# - Sends a notification
@impl true
def handle(
%{
data: %{
"actor" => actor,
"type" => "Accept",
"object" => follow_activity_id
}
} = object,
%{data: %{"actor" => actor, "type" => "Accept", "object" => activity_id}} = object,
meta
) do
with %Activity{actor: follower_id} = follow_activity <-
Activity.get_by_ap_id(follow_activity_id),
%User{} = followed <- User.get_cached_by_ap_id(actor),
%User{} = follower <- User.get_cached_by_ap_id(follower_id),
{:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
{:ok, _follower, followed} <-
FollowingRelationship.update(follower, followed, :follow_accept) do
Notification.update_notification_type(followed, follow_activity)
with %Activity{} = activity <-
Activity.get_by_ap_id(activity_id) do
handle_accepted(activity, actor)
if activity.data["type"] === "Join" do
Notification.create_notifications(object)
end
end
{:ok, object, meta}
@ -72,18 +65,14 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
data: %{
"actor" => actor,
"type" => "Reject",
"object" => follow_activity_id
"object" => activity_id
}
} = object,
meta
) do
with %Activity{actor: follower_id} = follow_activity <-
Activity.get_by_ap_id(follow_activity_id),
%User{} = followed <- User.get_cached_by_ap_id(actor),
%User{} = follower <- User.get_cached_by_ap_id(follower_id),
{:ok, _follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject") do
FollowingRelationship.update(follower, followed, :follow_reject)
Notification.dismiss(follow_activity)
with %Activity{} = activity <-
Activity.get_by_ap_id(activity_id) do
handle_rejected(activity, actor)
end
{:ok, object, meta}
@ -397,12 +386,93 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
end
end
# Task this handles
# - Bites
# - Sends a notification
@impl true
def handle(
%{
data: %{
"id" => bite_id,
"type" => "Bite",
"target" => bitten_user,
"actor" => biting_user
}
} = object,
meta
) do
with %User{} = biting <- User.get_cached_by_ap_id(biting_user),
%User{} = bitten <- User.get_cached_by_ap_id(bitten_user),
{:previous_bite, previous_bite} <-
{:previous_bite, Utils.fetch_latest_bite(biting, bitten, object)},
{:reverse_bite, reverse_bite} <-
{:reverse_bite, Utils.fetch_latest_bite(bitten, biting)},
{:can_bite, true, _} <- {:can_bite, can_bite?(previous_bite, reverse_bite), bitten} do
if bitten.local do
{:ok, accept_data, _} = Builder.accept(bitten, object)
{:ok, _activity, _} = Pipeline.common_pipeline(accept_data, local: true)
end
if reverse_bite do
Notification.dismiss(reverse_bite)
end
{:ok, notifications} = Notification.create_notifications(object)
meta
|> add_notifications(notifications)
else
{:can_bite, false, bitten} ->
{:ok, reject_data, _} = Builder.reject(bitten, object)
{:ok, _activity, _} = Pipeline.common_pipeline(reject_data, local: true)
meta
_ ->
meta
end
updated_object = Activity.get_by_ap_id(bite_id)
{:ok, updated_object, meta}
end
# Nothing to do
@impl true
def handle(object, meta) do
{:ok, object, meta}
end
defp handle_accepted(
%Activity{actor: follower_id, data: %{"type" => "Follow"}} = follow_activity,
actor
) do
with %User{} = followed <- User.get_cached_by_ap_id(actor),
%User{} = follower <- User.get_cached_by_ap_id(follower_id),
{:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
{:ok, _follower, followed} <-
FollowingRelationship.update(follower, followed, :follow_accept) do
Notification.update_notification_type(followed, follow_activity)
end
end
defp handle_accepted(_, _), do: nil
defp handle_rejected(
%Activity{actor: follower_id, data: %{"type" => "Follow"}} = follow_activity,
actor
) do
with %User{} = followed <- User.get_cached_by_ap_id(actor),
%User{} = follower <- User.get_cached_by_ap_id(follower_id),
{:ok, _follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject") do
FollowingRelationship.update(follower, followed, :follow_reject)
Notification.dismiss(follow_activity)
end
end
defp handle_rejected(%Activity{data: %{"type" => "Bite"}} = bite_activity, _actor) do
Notification.dismiss(bite_activity)
end
defp handle_update_user(
%{data: %{"type" => "Update", "object" => updated_object}} = object,
meta
@ -587,4 +657,12 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|> send_notifications()
|> send_streamables()
end
defp can_bite?(nil, _), do: true
defp can_bite?(_, nil), do: false
defp can_bite?(previous_bite, reverse_bite) do
NaiveDateTime.diff(previous_bite.inserted_at, reverse_bite.inserted_at) < 0
end
end

View file

@ -519,7 +519,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end
defp handle_incoming_normalised(%{"type" => type} = data, _options)
when type in ~w{Like EmojiReact Announce Add Remove} do
when type in ~w{Like EmojiReact Announce Add Remove Bite} do
with :ok <- ObjectValidator.fetch_actor_and_object(data),
{:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
{:ok, activity}
@ -533,7 +533,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
%{"type" => type} = data,
_options
)
when type in ~w{Update Block Follow Accept Reject} do
when type in ~w{Update Block Follow Accept Reject Bite} do
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
{:ok, activity, _} <-
Pipeline.common_pipeline(data, local: false) do

View file

@ -888,4 +888,36 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> where([a, object: o], fragment("(?)->>'type' = 'Answer'", o.data))
|> Repo.all()
end
def make_bite_data(biting, bitten, activity_id) do
%{
"type" => "Bite",
"actor" => biting.ap_id,
"to" => [bitten.ap_id],
"target" => bitten.ap_id
}
|> Maps.put_if_present("id", activity_id)
end
def fetch_latest_bite(
%User{ap_id: biting_ap_id},
%{ap_id: bitten_ap_id},
exclude_activity \\ nil
) do
"Bite"
|> Activity.Queries.by_type()
|> where(actor: ^biting_ap_id)
|> maybe_exclude_activity_id(exclude_activity)
|> Activity.Queries.by_object_id(bitten_ap_id)
|> order_by([activity], fragment("? desc nulls last", activity.id))
|> limit(1)
|> Repo.one()
end
defp maybe_exclude_activity_id(query, nil), do: query
defp maybe_exclude_activity_id(query, %Activity{id: activity_id}) do
query
|> where([a], a.id != ^activity_id)
end
end

View file

@ -79,7 +79,7 @@ defmodule Pleroma.Web.ApiSpec do
"x-tagGroups": [
%{
"name" => "Accounts",
"tags" => ["Account actions", "Retrieve account information"]
"tags" => ["Account actions", "Bites", "Retrieve account information"]
},
%{
"name" => "Administration",

View file

@ -0,0 +1,33 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ApiSpec.BiteOperation do
alias OpenApiSpex.Operation
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.ApiError
@spec open_api_operation(atom) :: Operation.t()
def open_api_operation(action) do
operation = String.to_existing_atom("#{action}_operation")
apply(__MODULE__, operation, [])
end
def bite_operation do
%Operation{
tags: ["Bites"],
summary: "Bite",
operationId: "BiteController.bite",
security: [%{"oAuth" => ["write:bites"]}],
description: "Bite the given account",
parameters: [
Operation.parameter(:id, :query, :string, "Bitten account ID")
],
responses: %{
200 => Operation.response("Empty object", "application/json", %Schema{type: :object}),
400 => Operation.response("Error", "application/json", ApiError),
404 => Operation.response("Error", "application/json", ApiError)
}
}
end
end

View file

@ -177,7 +177,8 @@ defmodule Pleroma.Web.ApiSpec.NotificationOperation do
"pleroma:report",
"move",
"follow_request",
"poll"
"poll",
"bite"
],
description: """
The type of event that resulted in the notification.
@ -190,6 +191,7 @@ defmodule Pleroma.Web.ApiSpec.NotificationOperation do
- `move` - Someone moved their account
- `pleroma:emoji_reaction` - Someone reacted with emoji to your status
- `pleroma:report` - Someone was reported
- `bite` - Someone bit you
"""
}
end

View file

@ -578,4 +578,11 @@ defmodule Pleroma.Web.CommonAPI do
nil
end
end
def bite(biting, bitten) do
with {:ok, bite_data, _} <- Builder.bite(biting, bitten),
{:ok, activity, _} <- Pipeline.common_pipeline(bite_data, local: true) do
{:ok, biting, bitten, activity}
end
end
end

View file

@ -0,0 +1,39 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.BiteController do
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, only: [assign_account_by_id: 2, json_response: 3]
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.Plugs.OAuthScopesPlug
# alias Pleroma.Web.Plugs.RateLimiter
plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
plug(OAuthScopesPlug, %{scopes: ["write:bite"]} when action == :bite)
# plug(RateLimiter, [name: :relations_actions] when action in @relationship_actions)
# plug(RateLimiter, [name: :app_account_creation] when action == :create)
plug(:assign_account_by_id)
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.BiteOperation
@doc "POST /api/v1/bite"
def bite(%{assigns: %{user: %{id: id}, account: %{id: id}}}, _params) do
{:error, "Can not bite yourself"}
end
def bite(%{assigns: %{user: biting, account: bitten}} = conn, _) do
with {:ok, _, _, _} <- CommonAPI.bite(biting, bitten) do
json_response(conn, :ok, %{})
else
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end
end

View file

@ -52,6 +52,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationController do
pleroma:emoji_reaction
poll
update
bite
}
def index(%{assigns: %{user: user}} = conn, params) do
params =

View file

@ -90,7 +90,8 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
"akkoma:machine_translation"
end,
"custom_emoji_reactions",
"pleroma:get:main/ostatus"
"pleroma:get:main/ostatus",
"pleroma:bites"
]
|> Enum.filter(& &1)
end

View file

@ -128,7 +128,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
"pleroma:report" ->
put_report(response, activity)
type when type in ["follow", "follow_request"] ->
type when type in ["follow", "follow_request", "bite"] ->
response
end
end

View file

@ -80,6 +80,10 @@ defmodule Pleroma.Web.Nodeinfo.Nodeinfo do
bubble: !Config.restrict_unauthenticated_access?(:timelines, :bubble)
},
federatedTimelineAvailable: Config.get([:instance, :federated_timeline_available], true)
},
operations: %{
"com.shinolabs.api.bite": ["1.0.0"],
"jetzt.mia.ns.activitypub.accept.bite": ["1.0.0"]
}
}
end

View file

@ -631,6 +631,8 @@ defmodule Pleroma.Web.Router do
get("/followed_tags", TagController, :show_followed)
get("/preferences", AccountController, :preferences)
post("/bite", BiteController, :bite)
end
scope "/api/web", Pleroma.Web do

View file

@ -0,0 +1,52 @@
defmodule Pleroma.Repo.Migrations.AddBiteToNotificationsEnum do
use Ecto.Migration
@disable_ddl_transaction true
def up do
"""
alter type notification_type add value 'bite'
"""
|> execute()
end
# 20220605185734_add_update_to_notifications_enum.exs
def down do
alter table(:notifications) do
modify(:type, :string)
end
"""
delete from notifications where type = 'bite'
"""
|> execute()
"""
drop type if exists notification_type
"""
|> execute()
"""
create type notification_type as enum (
'follow',
'follow_request',
'mention',
'move',
'pleroma:emoji_reaction',
'pleroma:chat_mention',
'reblog',
'favourite',
'pleroma:report',
'poll',
'update'
)
"""
|> execute()
"""
alter table notifications
alter column type type notification_type using (type::notification_type)
"""
|> execute()
end
end

View file

@ -54,7 +54,8 @@
"backgroundUrl": {
"@id": "sharkey:backgroundUrl",
"@type": "@id"
}
},
"Bite": "https://ns.mia.jetzt/as#Bite"
}
]
}