Skip to content

Commit

Permalink
feat(CI): Add age functionality to cheats app
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Apr 30, 2024
1 parent 0a71332 commit 1f8ab2a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions tests/integration/spreedcheats/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
return [
'ocs' => [
['name' => 'Api#resetSpreed', 'url' => '/', 'verb' => 'DELETE'],
['name' => 'Api#ageChat', 'url' => '/age', 'verb' => 'POST'],
],
];
73 changes: 62 additions & 11 deletions tests/integration/spreedcheats/lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\SpreedCheats\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand All @@ -16,22 +17,14 @@
use OCP\Share\IShare;

class ApiController extends OCSController {
/** @var IDBConnection */
private $db;

public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
IDBConnection $db
protected IDBConnection $db,
) {
parent::__construct($appName, $request);
$this->db = $db;
}

/**
* @NoCSRFRequired
*
* @return DataResponse
*/
public function resetSpreed(): DataResponse {
$delete = $this->db->getQueryBuilder();
$delete->delete('talk_attachments')->executeStatement();
Expand Down Expand Up @@ -110,4 +103,62 @@ public function resetSpreed(): DataResponse {

return new DataResponse();
}

public function ageChat(string $token, int $hours): DataResponse {
$query = $this->db->getQueryBuilder();
$query->select('id')
->from('talk_rooms')
->where($query->expr()->eq('token', $query->createNamedParameter($token)));

$result = $query->executeQuery();
$roomId = (int) $result->fetchOne();
$result->closeCursor();

if (!$roomId) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

$update = $this->db->getQueryBuilder();
$update->update('comments')
->set('creation_timestamp', $update->createParameter('creation_timestamp'))
->set('expire_date', $update->createParameter('expire_date'))
->set('meta_data', $update->createParameter('meta_data'))
->where($update->expr()->eq('id', $update->createParameter('id')));

$query = $this->db->getQueryBuilder();
$query->select('id', 'creation_timestamp', 'expire_date', 'meta_data')
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter('chat')))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($roomId)));

$result = $query->executeQuery();
while ($row = $result->fetch()) {
$creationTimestamp = new \DateTime($row['creation_timestamp']);
$creationTimestamp->sub(new \DateInterval('PT' . $hours . 'H'));

$expireDate = null;
if ($row['expire_date']) {
$expireDate = new \DateTime($row['expire_date']);
$expireDate->sub(new \DateInterval('PT' . $hours . 'H'));
}

$metaData = 'null';
if ($row['meta_data'] !== 'null') {
$metaData = json_decode($row['meta_data'], true);
if (isset($metaData['last_edited_time'])) {
$metaData['last_edited_time'] -= $hours * 3600;
}
$metaData = json_encode($metaData);
}

$update->setParameter('id', $row['id']);
$update->setParameter('creation_timestamp', $creationTimestamp, IQueryBuilder::PARAM_DATE);
$update->setParameter('expire_date', $expireDate, IQueryBuilder::PARAM_DATE);
$update->setParameter('meta_data', $metaData);
$update->executeStatement();
}
$result->closeCursor();

return new DataResponse();
}
}

0 comments on commit 1f8ab2a

Please sign in to comment.