Skip to content

Commit

Permalink
allow doi and info hash paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc09h committed Jun 1, 2024
1 parent 4747268 commit 75e2638
Show file tree
Hide file tree
Showing 36 changed files with 286 additions and 148 deletions.
1 change: 1 addition & 0 deletions app/Models/Collages.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Collages extends ObjectCrud
"categoryId" => "categoryId",
"userId" => "userId",
"title" => "title",
"slug" => "slug",
"description" => "description",
"tags" => "tags", # json
"torrentCount" => "torrentCount",
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public static function createIfNotExists(int|string $contentId, string $contentT
"id" => $app->dbNew->shortUuid(),
"contentId" => $contentId,
"contentType" => $contentType,
"userId" => 0, # created by the system
"userId" => $app->user->core["id"] ?? 0, # created by the system
"subject" => "Conversation",
];

Expand Down
13 changes: 13 additions & 0 deletions app/Models/Literature.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class Literature extends ObjectCrud
*/
public function read(int|string $id = null): void
{
$app = App::go();

# is it a doi?
$doi = preg_match("/{$app->env->regexDoi}/i", strval($id));
if ($doi) {
$query = "select id from literature where doi = ?";
$id = $app->dbNew->single($query, [$id]);

if (!$id) {
throw new Exception("not found");
}
}

# parent read
parent::read($id);

Expand Down
14 changes: 14 additions & 0 deletions app/Models/ObjectCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public function create(array $data = []): void
$transform["id"] = $app->dbNew->shortUuid();
}

# convert empty values to null
foreach ($transform as $key => $value) {
if (empty($value)) {
$transform[$key] = null;
}
}

# perform an upsert
$upsert = $app->dbNew->upsert($this->table, $transform);

Expand Down Expand Up @@ -166,6 +173,13 @@ public function update(array $data = []): void
$column = $app->dbNew->determineIdentifier($this->id);
$transform[$column] = $this->id;

# convert empty values to null
foreach ($transform as $key => $value) {
if (empty($value)) {
$transform[$key] = null;
}
}

# perform an upsert
$upsert = $app->dbNew->upsert($this->table, $transform);
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Requests extends ObjectCrud
"lastVote" => "lastVote",
"identifier" => "identifier",
"title" => "title",
"slug" => "slug",
"subject" => "subject",
"object" => "object",
"description" => "description",
Expand Down
31 changes: 27 additions & 4 deletions app/Models/TorrentGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class TorrentGroups extends ObjectCrud
"revisionId" => "revisionId",
"identifier" => "identifier",
"title" => "title",
"slug" => "slug",
"subject" => "subject",
"object" => "object",
"workgroup" => "workgroup",
Expand Down Expand Up @@ -108,12 +109,34 @@ public function read(int|string $id = null): void
*/
public function update(array $data = []): void
{
throw new Exception("not implemented");
$app = App::go();

/** */
# can the owner update?
if ($this->isOwner && $app->user->cant(["torrents" => "update"])) {
throw new Exception("forbidden");
}

# encode the json fields
$data["tags"] = json_encode($data["tags"] ?? []);
# can someone else moderate?
if (!$this->isOwner && $app->user->cant(["torrents" => "moderate"])) {
throw new Exception("forbidden");
}

# validate the data
$data = [
"id" => $this->id,
"categoryId" => $this->categoryId,
"revisionId" => $this->revisionId + 1,
"identifier" => $data["identifier"] ?? $this->identifier,
"title" => $data["title"] ?? $this->title,
"subject" => $data["subject"] ?? $this->subject,
"object" => $data["object"] ?? $this->object,
"workgroup" => $data["workgroup"] ?? $this->workgroup,
"location" => $data["location"] ?? $this->location,
"year" => $data["year"] ?? $this->year,
"description" => $data["description"] ?? $this->description,
"picture" => $data["picture"] ?? $this->picture,
"tags" => json_encode($data["tags"] ?? $this->tags),
];

# parent update
parent::update($data);
Expand Down
13 changes: 12 additions & 1 deletion app/Models/Torrents.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,18 @@ class Torrents extends ObjectCrud
*/
public function read(int|string $id = null): void
{
$app = \Gazelle\App::go();
$app = App::go();

# is it an info hash?
$infoHash = ctype_xdigit(strval($id)) && strlen(strval($id)) === 40;
if ($infoHash) {
$query = "select id from torrents where hex(info_hash) = ?";
$id = $app->dbNew->single($query, [$id]);

if (!$id) {
throw new Exception("not found");
}
}

# parent read
parent::read($id);
Expand Down
1 change: 1 addition & 0 deletions app/Models/Wiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Wiki extends ObjectCrud
"userId" => "userId",
"revision" => "revision",
"title" => "title",
"slug" => "slug",
"body" => "body",
"minimumReadClass" => "minimumReadClass",
"minimumEditClass" => "minimumEditClass",
Expand Down
10 changes: 5 additions & 5 deletions database/migrations/20240522203651_chihaya_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function change(): void

foreach ($ref as $row) {
$query = "insert into approved_clients (peer_id, description) values (?, ?)";
$app->dbNew->do($query, [ $row["peer_id"], $row["description"] ]);
$app->dbNew->do($query, [ $row["peer_id"], $row["vstring"] ]);
}

/*
Expand Down Expand Up @@ -131,8 +131,8 @@ public function change(): void
$query = "
create table transfer_history
(
uid int not null,
fid int not null,
uid bigint unsigned not null,
fid bigint unsigned not null,
uploaded bigint default 0 not null,
downloaded bigint default 0 not null,
seeding tinyint default 0 not null,
Expand Down Expand Up @@ -182,8 +182,8 @@ public function change(): void
(
last_announce int unsigned default 0 not null,
starttime int unsigned default 0 not null,
uid int unsigned not null,
fid int unsigned not null,
uid bigint unsigned not null,
fid bigint unsigned not null,
ip int unsigned not null,
client_id mediumint unsigned not null,
uploaded bigint unsigned default 0 not null,
Expand Down
Binary file modified public/images/noartwork.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions resources/scss/global/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,15 @@ img {
color: white;
}
}
}

// sidebar: needs to be its own file
#sidebar {
#groupPicture {
text-align: center;

img {
max-height: 20rem;
}
}
}
19 changes: 19 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@


/**
* web routes
*
* Other variations of CRUD include:
*
* - ABCD (add, browse, change, delete)
* - CRUDL (create, read, update, delete, list)
* - BREAD (browse, read, edit, add, delete)
* - DAVE (delete, add, view, edit)[7]
* - CRAP (create, replicate, append, process)
*
* So our basic route logic should be:
*
* - browse, e.g., /torrents
* - create, e.g., /torrents/add
* - read, e.g., /torrents/666
* - update, e.g., /torrents/666/edit
* - delete, e.g., /torrents/666/delete (no interface)
*
* @see https://flightphp.com/learn
* @see https://en.wikipedia.org/wiki/Create,_read,_update_and_delete
*/

# require the route files
Expand Down
4 changes: 2 additions & 2 deletions routes/web/collages.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


# create
Flight::route("/collages/create", function () {
Flight::route("/collages/add", function () {
$app = Gazelle\App::go();
$app->middleware(["collages" => "create"]);
require_once "{$app->env->serverRoot}/sections/collages/updateOrCreate.php";
Expand All @@ -33,7 +33,7 @@


# update
Flight::route("/collages/@identifier/update", function ($id) {
Flight::route("/collages/@identifier/edit", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["collages" => "update"]);
require_once "{$app->env->serverRoot}/sections/collages/updateOrCreate.php";
Expand Down
2 changes: 1 addition & 1 deletion routes/web/conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

# createMessage
Flight::route("/conversations/createMessage", function () {
Flight::route("/conversations/add-message", function () {
$app = Gazelle\App::go();
$app->middleware(["conversations" => "create"]);
require_once "{$app->env->serverRoot}/sections/conversations/createMessage.php";
Expand Down
38 changes: 26 additions & 12 deletions routes/web/creators.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,41 @@
* creators
*/

# browse
Flight::route("/creators", function () {
$app = Gazelle\App::go();
$app->middleware(["creators" => "read"]);
require_once "{$app->env->serverRoot}/sections/creators/browse.php";
});

# details
Flight::route("/creators/@identifier", function ($identifier = null) {

# create
Flight::route("/creators/add", function () {
$app = Gazelle\App::go();
$app->middleware(["creators" => "create"]);
require_once "{$app->env->serverRoot}/sections/creators/create.php";
});


# read
Flight::route("/creators/@id", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["creators" => "read"]);
require_once "{$app->env->serverRoot}/sections/creators/details.php";
});

/*
# edit
Flight::route("/requests/edit/@identifier", function ($identifier = null) {

# update
Flight::route("/creators/@id/edit", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["requests" => "update"]);
require_once "{$app->env->serverRoot}/sections/requests/updateOrCreate.php";
$app->middleware(["creators" => "update"]);
require_once "{$app->env->serverRoot}/sections/creators/updateOrCreate.php";
});


# new
Flight::route("/requests/new", function () {
# delete
Flight::route("/creators/@id/delete", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["requests" => "create"]);
require_once "{$app->env->serverRoot}/sections/requests/updateOrCreate.php";
$app->middleware(["creators" => "delete"]);
require_once "{$app->env->serverRoot}/sections/creators/delete.php";
});
*/
29 changes: 15 additions & 14 deletions routes/web/literature.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,49 @@
* literature
*/

/*
# browse
Flight::route("/literature", function () {
$app = Gazelle\App::go();
$app->middleware(["literature" => "read"]);
require_once "{$app->env->serverRoot}/sections/literature/browse.php";
});
*/

/*

# create
Flight::route("/literature/create", function () {
Flight::route("/literature/add", function () {
$app = Gazelle\App::go();
$app->middleware(["literature" => "create"]);
require_once "{$app->env->serverRoot}/sections/literature/updateOrCreate.php";
});
*/


# read
Flight::route("/literature/@identifier", function ($id) {
# read: id
Flight::route("/literature/@id", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["literature" => "read"]);
require_once "{$app->env->serverRoot}/sections/literature/details.php";
});


# read: doi
Flight::route("/literature(/@prefix/@suffix)", function ($prefix, $suffix) {
$app = Gazelle\App::go();
#$app->middleware(["literature" => "read"]);
$app->middleware(["literature" => "read"]);
require_once "{$app->env->serverRoot}/sections/literature/details.php";
});


/*
# update
Flight::route("/literature/@identifier/update", function ($id) {
Flight::route("/literature/@id/edit", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["literature" => "update"]);
require_once "{$app->env->serverRoot}/sections/literature/updateOrCreate.php";
});
*/


/*
# delete
Flight::route("/literature/@identifier/delete", function ($id) {
Flight::route("/literature/@id/delete", function ($id) {
$app = Gazelle\App::go();
$app->middleware(["literature" => "delete"]);
require_once "{$app->env->serverRoot}/sections/literature/delete.php";
});
*/
Loading

0 comments on commit 75e2638

Please sign in to comment.