Use graphqlite-like API

This commit is contained in:
Shish 2023-02-04 13:27:27 +00:00
parent d5bc9ffb7c
commit 180f21e29e
7 changed files with 57 additions and 47 deletions

View file

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GQLA\Expose; use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
/** /**
* Class Image * Class Image
@ -15,27 +17,27 @@ use GQLA\Expose;
* image per se, but could be a video, sound file, or any * image per se, but could be a video, sound file, or any
* other supported upload type. * other supported upload type.
*/ */
#[Expose(name: "Post")] #[Type(name: "Post")]
class Image class Image
{ {
public const IMAGE_DIR = "images"; public const IMAGE_DIR = "images";
public const THUMBNAIL_DIR = "thumbs"; public const THUMBNAIL_DIR = "thumbs";
#[Expose] #[Field]
public ?int $id = null; public ?int $id = null;
#[Expose] #[Field]
public int $height = 0; public int $height = 0;
#[Expose] #[Field]
public int $width = 0; public int $width = 0;
#[Expose] #[Field]
public string $hash; public string $hash;
#[Expose] #[Field]
public int $filesize; public int $filesize;
#[Expose] #[Field]
public string $filename; public string $filename;
#[Expose] #[Field]
private string $ext; private string $ext;
#[Expose] #[Field]
private string $mime; private string $mime;
/** @var ?string[] */ /** @var ?string[] */
@ -84,7 +86,7 @@ class Image
} }
} }
#[Expose(extends: "Query", name: "post_by_id")] #[Query(name: "post_by_id")]
public static function by_id(int $id): ?Image public static function by_id(int $id): ?Image
{ {
global $database; global $database;
@ -154,7 +156,7 @@ class Image
* #param string[] $tags * #param string[] $tags
* #return Image[] * #return Image[]
*/ */
#[Expose(extends: "Query", name: "posts", type: "[Post]", args: ["tags" => "[string]"])] #[Query(name: "posts", type: "[Post]", args: ["tags" => "[string]"])]
public static function find_images(?int $start = 0, ?int $limit = null, array $tags=[]): array public static function find_images(?int $start = 0, ?int $limit = null, array $tags=[]): array
{ {
$result = self::find_images_internal($start, $limit, $tags); $result = self::find_images_internal($start, $limit, $tags);
@ -360,7 +362,7 @@ class Image
/** /**
* Find the User who owns this Image * Find the User who owns this Image
*/ */
#[Expose(name: "owner")] #[Field(name: "owner")]
public function get_owner(): User public function get_owner(): User
{ {
return User::by_id($this->owner_id); return User::by_id($this->owner_id);
@ -459,9 +461,9 @@ class Image
/** /**
* Get this image's tags as an array. * Get this image's tags as an array.
* *
* #return string[] * @return String[]
*/ */
#[Expose(name: "tags", type: "[string]")] #[Field(name: "tags", type: "[string]")]
public function get_tag_array(): array public function get_tag_array(): array
{ {
global $database; global $database;
@ -489,7 +491,7 @@ class Image
/** /**
* Get the URL for the full size image * Get the URL for the full size image
*/ */
#[Expose(name: "image_link")] #[Field(name: "image_link")]
public function get_image_link(): string public function get_image_link(): string
{ {
return $this->get_link(ImageConfig::ILINK, '_images/$hash/$id%20-%20$tags.$ext', 'image/$id.$ext'); return $this->get_link(ImageConfig::ILINK, '_images/$hash/$id%20-%20$tags.$ext', 'image/$id.$ext');
@ -498,7 +500,7 @@ class Image
/** /**
* Get the nicely formatted version of the file name * Get the nicely formatted version of the file name
*/ */
#[Expose(name: "nice_name")] #[Field(name: "nice_name")]
public function get_nice_image_name(): string public function get_nice_image_name(): string
{ {
$plte = new ParseLinkTemplateEvent('$id - $tags.$ext', $this); $plte = new ParseLinkTemplateEvent('$id - $tags.$ext', $this);
@ -509,7 +511,7 @@ class Image
/** /**
* Get the URL for the thumbnail * Get the URL for the thumbnail
*/ */
#[Expose(name: "thumb_link")] #[Field(name: "thumb_link")]
public function get_thumb_link(): string public function get_thumb_link(): string
{ {
global $config; global $config;
@ -544,7 +546,7 @@ class Image
* Get the tooltip for this image, formatted according to the * Get the tooltip for this image, formatted according to the
* configured template. * configured template.
*/ */
#[Expose(name: "tooltip")] #[Field(name: "tooltip")]
public function get_tooltip(): string public function get_tooltip(): string
{ {
global $config; global $config;
@ -557,7 +559,7 @@ class Image
* Get the info for this image, formatted according to the * Get the info for this image, formatted according to the
* configured template. * configured template.
*/ */
#[Expose(name: "info")] #[Field(name: "info")]
public function get_info(): string public function get_info(): string
{ {
global $config; global $config;

View file

@ -4,14 +4,16 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GQLA\Expose; use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
#[Expose(name: "TagUsage")] #[Type(name: "TagUsage")]
class TagUsage class TagUsage
{ {
#[Expose] #[Field]
public string $tag; public string $tag;
#[Expose] #[Field]
public int $uses; public int $uses;
public function __construct(string $tag, int $uses) public function __construct(string $tag, int $uses)
@ -20,7 +22,7 @@ class TagUsage
$this->uses = $uses; $this->uses = $uses;
} }
#[Expose(extends: "Query", name: "tags", type: '[TagUsage]')] #[Query(name: "tags", type: '[TagUsage]')]
public static function tags(string $search, int $limit=10): array public static function tags(string $search, int $limit=10): array
{ {
global $cache, $database; global $cache, $database;

View file

@ -96,7 +96,7 @@ $_shm_event_count = 0;
/** /**
* Send an event to all registered Extensions. * Send an event to all registered Extensions.
* *
* @template T of Event * @template T of Event
* @param T $event * @param T $event
* @return T * @return T

View file

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GQLA\Expose; use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
function _new_user(array $row): User function _new_user(array $row): User
{ {
@ -19,12 +21,12 @@ function _new_user(array $row): User
* *
* The currently logged in user will always be accessible via the global variable $user. * The currently logged in user will always be accessible via the global variable $user.
*/ */
#[Expose(name: "User")] #[Type(name: "User")]
class User class User
{ {
#[Expose] #[Field]
public int $id; public int $id;
#[Expose] #[Field]
public string $name; public string $name;
public ?string $email; public ?string $email;
public string $join_date; public string $join_date;
@ -63,7 +65,7 @@ class User
} }
} }
#[Expose(extends: "Query")] #[Query]
public static function me(): User public static function me(): User
{ {
global $user; global $user;

View file

@ -4,7 +4,10 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GQLA\Expose; use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
use GQLA\Mutation;
require_once "vendor/ifixit/php-akismet/akismet.class.php"; require_once "vendor/ifixit/php-akismet/akismet.class.php";
@ -43,7 +46,7 @@ class CommentPostingException extends SCoreException
{ {
} }
#[Expose(name: "Comment")] #[Type(name: "Comment")]
class Comment class Comment
{ {
public ?User $owner; public ?User $owner;
@ -51,12 +54,12 @@ class Comment
public string $owner_name; public string $owner_name;
public ?string $owner_email; public ?string $owner_email;
public string $owner_class; public string $owner_class;
#[Expose] #[Field]
public string $comment; public string $comment;
public int $comment_id; public int $comment_id;
public int $image_id; public int $image_id;
public string $poster_ip; public string $poster_ip;
#[Expose] #[Field]
public string $posted; public string $posted;
public function __construct($row) public function __construct($row)
@ -83,7 +86,7 @@ class Comment
", ["owner_id"=>$user->id]); ", ["owner_id"=>$user->id]);
} }
#[Expose(name: "owner")] #[Field(name: "owner")]
public function get_owner(): User public function get_owner(): User
{ {
if (empty($this->owner)) { if (empty($this->owner)) {
@ -92,13 +95,13 @@ class Comment
return $this->owner; return $this->owner;
} }
#[Expose(extends: "Post", name: "comments", type: "[Comment]")] #[Field(extends: "Post", name: "comments", type: "[Comment]")]
public function get_comments(Image $post): array public function get_comments(Image $post): array
{ {
return CommentList::get_comments($post->id); return CommentList::get_comments($post->id);
} }
#[Expose(extends: "Mutation", name: "create_comment")] #[Mutation(name: "create_comment")]
public function create_comment(int $post_id, string $comment): bool public function create_comment(int $post_id, string $comment): bool
{ {
global $user; global $user;

View file

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GQLA\Expose; use GQLA\Type;
use GQLA\Field;
use GQLA\Query;
class SendPMEvent extends Event class SendPMEvent extends Event
{ {
@ -17,21 +19,20 @@ class SendPMEvent extends Event
} }
} }
#[Expose] #[Type]
class PM class PM
{ {
#[Expose] #[Field]
public int $id; public int $id;
public int $from_id; public int $from_id;
public string $from_ip; public string $from_ip;
public int $to_id; public int $to_id;
/** @var mixed */ public mixed $sent_date;
public $sent_date; #[Field]
#[Expose]
public string $subject; public string $subject;
#[Expose] #[Field]
public string $message; public string $message;
#[Expose] #[Field]
public bool $is_read; public bool $is_read;
public function __construct($from_id=0, string $from_ip="0.0.0.0", int $to_id=0, string $subject="A Message", string $message="Some Text", bool $read=false) public function __construct($from_id=0, string $from_ip="0.0.0.0", int $to_id=0, string $subject="A Message", string $message="Some Text", bool $read=false)

View file

@ -6,5 +6,5 @@ parameters:
- ../tests - ../tests
- ../themes/default - ../themes/default
ignoreErrors: ignoreErrors:
- '#Attribute class GQLA\\Expose#' - '#Attribute class GQLA\\#'
- '#Access to an undefined property Shimmie2\\Image::\$#' - '#Access to an undefined property Shimmie2\\Image::\$#'