Skip to content

Commit

Permalink
refactor user quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
Spine committed Oct 15, 2022
1 parent f074f30 commit f974d99
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
20 changes: 11 additions & 9 deletions app/Comment/AbstractComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ abstract class AbstractComment extends \Gazelle\BaseObject {
protected $viewer;

protected const PAGE_TOTAL = '%s_comments_%d';
protected const CATALOG = '%s_comments_%d_catalogue_%d';

abstract public function page(): string;
abstract public function pageUrl(): string;
Expand Down Expand Up @@ -136,17 +135,20 @@ public function load() {

// Cache catalogue from which the page is selected
$CatalogueID = (int)floor(TORRENT_COMMENTS_PER_PAGE * ($this->pageNum - 1) / THREAD_CATALOGUE);
$catKey = sprintf(self::CATALOG, $page, $pageId, $CatalogueID);
if (($Catalogue = self::$cache->get_value($catKey)) === false) {
$catKey = sprintf(\Gazelle\Manager\Comment::CATALOG, $page, $pageId, $CatalogueID);
$Catalogue = self::$cache->get_value($catKey);
if ($Catalogue === false) {
self::$db->prepared_query("
SELECT c.ID,
c.AuthorID,
c.AddedTime,
c.Body,
c.EditedUserID,
c.EditedTime,
u.Username
u.Username,
a.Username AS author_name
FROM comments AS c
LEFT JOIN users_main AS a ON (a.ID = c.AuthorID)
LEFT JOIN users_main AS u ON (u.ID = c.EditedUserID)
WHERE c.Page = ? AND c.PageID = ?
ORDER BY c.ID
Expand Down Expand Up @@ -253,8 +255,9 @@ public function modify(): bool {
self::$cache->deleteMulti([
"edit_{$page}_" . $this->id,
"{$page}_comments_" . $this->pageId,
"{$page}_comments_{$this->pageId}_catalogue_"
. (int)floor((($commentPage - 1) * TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE),
sprintf(\Gazelle\Manager\Comment::CATALOG, $page, $this->pageId,
(int)floor((($commentPage - 1) * TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE)
),
]);

if ($page == 'collages') {
Expand Down Expand Up @@ -302,12 +305,11 @@ public function remove(): bool {
"{$page}_comments_" . $this->pageId,
]);

//We need to clear all subsequential catalogues as they've all been bumped with the absence of this post
// We need to clear all subsequential catalogues as they've all been bumped with the absence of this post
$current = floor((TORRENT_COMMENTS_PER_PAGE * $commentPage - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
$last = floor((TORRENT_COMMENTS_PER_PAGE * $commentPages - TORRENT_COMMENTS_PER_PAGE) / THREAD_CATALOGUE);
$keyStem = "{$page}_comments_{$this->pageId}_catalogue_";
for ($i = $current; $i <= $last; ++$i) {
self::$cache->delete_value($keyStem . $i);
self::$cache->delete_value(sprintf(\Gazelle\Manager\Comment::CATALOG, $page, $this->pageId, $i));
}

if ($page === 'collages') {
Expand Down
8 changes: 5 additions & 3 deletions app/Manager/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Comment extends \Gazelle\Base {

const CATALOG = '%s_comments_%d_cat_%d';

protected function className(string $page): string {
switch ($page) {
case 'artist':
Expand Down Expand Up @@ -51,7 +53,7 @@ public function create(int $userId, string $page, int $pageId, string $body) {
", TORRENT_COMMENTS_PER_PAGE, TORRENT_COMMENTS_PER_PAGE, THREAD_CATALOGUE, $page, $pageId
);
self::$cache->deleteMulti([
"{$page}_comments_{$pageId}_catalogue_{$catalogueId}",
sprintf(self::CATALOG, $page, $pageId, $catalogueId),
"{$page}_comments_{$pageId}"
]);
if ($page == 'collages') {
Expand Down Expand Up @@ -96,7 +98,7 @@ public function merge(string $page, int $pageId, int $targetPageId) {
$subscription->move($page, $pageId, $targetPageId);

for ($i = 0; $i <= $last; ++$i) {
self::$cache->delete_value($page . "_comments_$targetPageId" . "_catalogue_$i");
self::$cache->delete_value(sprintf(self::CATALOG, $page, $targetPageId, $i));
}
self::$cache->delete_value($page."_comments_$targetPageId");
}
Expand Down Expand Up @@ -138,7 +140,7 @@ public function remove(string $page, int $pageId) {

// Clear cache
for ($i = 0; $i <= $last; ++$i) {
self::$cache->delete_value($page . '_comments_' . $pageId . '_catalogue_' . $i);
self::$cache->delete_value(sprintf(self::CATALOG, $page, $pageId, $i));
}
self::$cache->delete_value($page . '_comments_' . $pageId);

Expand Down
39 changes: 15 additions & 24 deletions app/User/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ public function page(int $limit, int $offset): array {
);

$page = [];
$forumMan = new \Gazelle\Manager\Forum;
$postMan = new \Gazelle\Manager\ForumPost;
$forumMan = new \Gazelle\Manager\Forum;
$postMan = new \Gazelle\Manager\ForumPost;
$releaseType = new \Gazelle\ReleaseType;
$tgMan = new \Gazelle\Manager\TGroup;

foreach ($quoteList as $q) {
$context = [];
switch ($q['Page']) {
Expand Down Expand Up @@ -214,38 +216,27 @@ public function page(int $limit, int $offset): array {
continue 2;
}
$request = $requestList[$q['PageID']];
switch (CATEGORY[$request['CategoryID'] - 1]) {
case 'Music':
$link = \Artists::display_artists(\Requests::get_artists($q['PageID']))
. sprintf('<a href="requests.php?action=view&amp;id=%d" dir="ltr">%s [%d]</a>',
$q['PageID'], $request['Title'], $request['Year']);
break;
case 'Audiobooks':
case 'Comedy':
$link = sprintf('<a href="requests.php?action=view&amp;id=%d" dir="ltr">%s [%d]</a>',
$q['PageID'], $request['Title'], $request['Year']);
break;
default:
$link = sprintf('<a href="requests.php?action=view&amp;id=%d" dir="ltr">%s</a>',
$q['PageID'], $request['Title']);
break;
}
$ahref = "a href=\"requests.php?action=view&amp;id={$q['PageID']}\" dir=\"ltr\"";
$context = [
'jump' => "requests.php?action=view&amp;id={$q['PageID']}&amp;postid={$q['PostID']}#post{$q['PostID']}",
'link' => $link,
'link' => match(CATEGORY[$request['CategoryID'] - 1]) {
'Audiobooks',
'Music' => \Artists::display_artists(\Requests::get_artists($q['PageID']))
. "<$ahref>{$request['Title']} [{$request['Year']}]</a>",
'Comedy' => "<$ahref>{$request['Title']} [{$request['Year']}]</a>",
default => "<$ahref>{$request['Title']}</a>",
},
'title' => 'Request',
];
break;
case 'torrents':
if (!isset($torrentList[$q['PageID']])) {
$tgroup = $tgMan->findById($q['PageID']);
if (is_null($tgroup)) {
continue 2;
}
$group = $torrentList[$q['PageID']];
$context = [
'jump' => "torrents.php?id={$q['PageID']}&amp;postid={$q['PostID']}#post{$q['PostID']}",
'link' => \Artists::display_artists($group['ExtendedArtists'])
. sprintf('<a href="torrents.php?id=%d" dir="ltr">%s [%d] [%s]</a>',
$q['PageID'], $group['Name'], $group['Year'], $releaseType->findNameById($group['ReleaseType'])),
'link' => $tgroup->link(),
'title' => 'Torrent',
];
break;
Expand Down
2 changes: 1 addition & 1 deletion templates/comment/thread.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div style="float: left;"><a class="post_id" href="{{ "#{url}&postid=#{post.ID}#post#{post.ID}" }}">#{{ post.ID }}</a>
<strong>{{ post.AuthorID|user_full }}</strong> {{ post.AddedTime|time_diff }}
<span id="postcontrol-{{ post.ID }}">
- <a href="#quickpost" onclick="Quote('{{ post.ID }}','{{ post.Username }}', true);" class="brackets">Quote</a>
- <a href="#quickpost" onclick="Quote('{{ post.ID }}','{{ post.author_name }}', true);" class="brackets">Quote</a>
{% if (viewer.permitted('site_moderate_forums')) or (post.AuthorID == viewer.Id) %}
- <a href="#post{{ post.ID }}" onclick="Edit_Form('{{ post.ID }}','{{ key }}');" class="brackets">Edit</a>
{% endif %}
Expand Down

0 comments on commit f974d99

Please sign in to comment.