OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Http\Responses\Contracts;
|
||||
|
||||
use Illuminate\Contracts\Support\Responsable;
|
||||
|
||||
/**
|
||||
* Marker contract for Leantime's first-class HTTP response types.
|
||||
*
|
||||
* Response types live in app/Core/Http/Responses and are returned directly from
|
||||
* controllers; Laravel's router converts them to a Symfony Response via toResponse().
|
||||
* Extending Laravel's Responsable keeps the behaviour fully framework-native while
|
||||
* giving Leantime a single place to discover/centralise every response type it offers
|
||||
* (e.g. ImageResponse today; a JsonRpcResponse, etc. can follow the same pattern).
|
||||
*
|
||||
* @see \Illuminate\Contracts\Support\Responsable
|
||||
*/
|
||||
interface LeantimeResponseInterface extends Responsable {}
|
||||
53
app/Core/Http/Responses/ImageResponse.php
Normal file
53
app/Core/Http/Responses/ImageResponse.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Http\Responses;
|
||||
|
||||
use Leantime\Core\Http\Responses\Contracts\LeantimeResponseInterface;
|
||||
use SVG\SVG;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Response type for avatar/profile images served by the domain image controllers
|
||||
* (Users\Controllers\ProfileImage, Projects\Controllers\ProjectImage).
|
||||
*
|
||||
* The source is either a generated SVG avatar, an already-built file Response (an
|
||||
* uploaded image streamed by the file service), or a filesystem path. Controllers
|
||||
* return `new ImageResponse($source)` and Laravel renders it via toResponse().
|
||||
*/
|
||||
class ImageResponse implements LeantimeResponseInterface
|
||||
{
|
||||
/**
|
||||
* @param SVG|Response|string $image Generated SVG, a pre-built file Response, or a filesystem path
|
||||
*/
|
||||
public function __construct(private SVG|Response|string $image) {}
|
||||
|
||||
/**
|
||||
* Build the cacheable image response.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function toResponse($request): Response
|
||||
{
|
||||
if ($this->image instanceof SVG) {
|
||||
return $this->withCacheHeaders(new Response($this->image->toXMLString()), 'image/svg+xml');
|
||||
}
|
||||
|
||||
if ($this->image instanceof Response) {
|
||||
return $this->image;
|
||||
}
|
||||
|
||||
return $this->withCacheHeaders(new Response(file_get_contents($this->image)), 'application/octet-stream');
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the public, 24h cache headers shared by every image variant.
|
||||
*/
|
||||
private function withCacheHeaders(Response $response, string $contentType): Response
|
||||
{
|
||||
$response->headers->set('Content-type', $contentType);
|
||||
$response->headers->set('Pragma', 'public');
|
||||
$response->headers->set('Cache-Control', 'max-age=86400');
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
69
app/Core/Http/Responses/JsonRpcErrorResponse.php
Normal file
69
app/Core/Http/Responses/JsonRpcErrorResponse.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Http\Responses;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Leantime\Core\Exceptions\Contracts\LeantimeExceptionInterface;
|
||||
use Leantime\Core\Http\Responses\Contracts\LeantimeResponseInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The JSON-RPC 2.0 error envelope as a first-class response type.
|
||||
*
|
||||
* Centralizes the `{jsonrpc, error: {code, message, data}, id}` wire format and is the single
|
||||
* place that turns a thrown exception into a client-facing JSON-RPC error — without leaking
|
||||
* internal detail. A LeantimeException maps to its own code/message/data; any other throwable
|
||||
* is reported as a generic server error (the caller is responsible for logging it).
|
||||
*
|
||||
* Note: JSON-RPC carries the error in-band, so the HTTP status stays 200; real HTTP status
|
||||
* codes belong on the web/REST surfaces (handled by the global ExceptionHandler), not here.
|
||||
*
|
||||
* @see https://www.jsonrpc.org/specification#error_object
|
||||
*/
|
||||
class JsonRpcErrorResponse implements LeantimeResponseInterface
|
||||
{
|
||||
public function __construct(
|
||||
private int $code,
|
||||
private string $message,
|
||||
private mixed $data = null,
|
||||
private int|string|null $id = 0,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Build an error envelope from a thrown exception.
|
||||
*
|
||||
* A LeantimeException is trusted to expose a client-safe code, message and data.
|
||||
* Anything else is collapsed to a generic server error so internal messages/stack
|
||||
* detail never reach the client; log such throwables at the call site.
|
||||
*/
|
||||
public static function fromException(Throwable $e, int|string|null $id = 0): self
|
||||
{
|
||||
if ($e instanceof LeantimeExceptionInterface) {
|
||||
return new self(
|
||||
$e->getRpcCode(),
|
||||
$e->getClientMessage(),
|
||||
$e->getErrorData() ?: null,
|
||||
$id,
|
||||
);
|
||||
}
|
||||
|
||||
return new self(-32000, 'Server error', null, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function toResponse($request): Response
|
||||
{
|
||||
return new JsonResponse([
|
||||
'jsonrpc' => '2.0',
|
||||
'error' => [
|
||||
'code' => $this->code,
|
||||
'message' => $this->message,
|
||||
'data' => $this->data,
|
||||
],
|
||||
'id' => $this->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
45
app/Core/Http/Responses/JsonRpcResponse.php
Normal file
45
app/Core/Http/Responses/JsonRpcResponse.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Http\Responses;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Leantime\Core\Http\Responses\Contracts\LeantimeResponseInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* The JSON-RPC 2.0 success envelope as a first-class response type.
|
||||
*
|
||||
* Centralizes the `{jsonrpc, result, id}` wire format (previously inlined in the Jsonrpc
|
||||
* controller) in the HTTP layer behind LeantimeResponseInterface, alongside ImageResponse.
|
||||
*
|
||||
* @see https://www.jsonrpc.org/specification#response_object
|
||||
*/
|
||||
class JsonRpcResponse implements LeantimeResponseInterface
|
||||
{
|
||||
/**
|
||||
* @param mixed $result The service return value (any JSON value per spec §5).
|
||||
* @param int|string|null $id The request id; null indicates a notification.
|
||||
*/
|
||||
public function __construct(
|
||||
private mixed $result,
|
||||
private int|string|null $id = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*/
|
||||
public function toResponse($request): Response
|
||||
{
|
||||
// A request without an id is a JSON-RPC notification and MUST NOT be responded to.
|
||||
// @see https://www.jsonrpc.org/specification#notification
|
||||
if ($this->id === null) {
|
||||
return new Response('', Response::HTTP_OK);
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'jsonrpc' => '2.0',
|
||||
'result' => $this->result,
|
||||
'id' => $this->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user