-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: marcin mikołajczak <[email protected]>
- Loading branch information
Showing
21 changed files
with
324 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/pleroma/web/activity_pub/object_validators/bite_validator.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 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() | ||
end | ||
end | ||
|
||
field(:actor, ObjectValidators.ObjectID) | ||
field(:target, ObjectValidators.ObjectID) | ||
end | ||
|
||
def cast_data(data) do | ||
%__MODULE__{} | ||
|> cast(data, __schema__(:fields)) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lib/pleroma/web/mastodon_api/controllers/bite_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.