Skip to content

Commit

Permalink
Add hash support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schendel committed Sep 13, 2024
1 parent 35ec457 commit ac5e063
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ We now have created a basic Twack-component and you now the general concepts how

## Changelog

### Changes in 2.3.3(2024-09-12)

- Add hash to page requests
- Return 204 (No Content) if matching hash is sent via GET param

### Changes in 2.3.1(2024-03-04)

- Use AppApi getAjaxOf() if available
Expand Down
2 changes: 1 addition & 1 deletion Twack.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getModuleInfo() {
return [
'title' => 'Twack',
'author' => 'Sebastian Schendel',
'version' => '2.3.2',
'version' => '2.3.3',
'summary' => 'Reusable components for your ProcessWire-templates.',
'singular' => true,
'autoload' => true,
Expand Down
21 changes: 13 additions & 8 deletions TwackApiAccess.class.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php

namespace ProcessWire;

class TwackApiAccess {
public static function pageIDRequest($data) {
$data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']);
$page = wire('pages')->get('id=' . $data->id);
return self::pageRequest($page, '');
return self::pageRequest($page, '', $data);
}

public static function dashboardRequest() {
public static function dashboardRequest($data) {
$page = wire('pages')->get('/');
return self::pageRequest($page, '');
return self::pageRequest($page, '', $data);
}

public static function pagePathRequest($data) {
Expand All @@ -26,19 +25,19 @@ public static function pagePathRequest($data) {
if ($value !== $path) {
continue;
}
return self::pageRequest($rootPage, $key);
return self::pageRequest($rootPage, $key, $data);
}
}

$info = wire('pages')->pathFinder()->get($path);
if (!empty($info['language']['name'])) {
return self::pageRequest($page, $info['language']['name']);
return self::pageRequest($page, $info['language']['name'], $data);
}

return self::pageRequest($page, '');
return self::pageRequest($page, '', $data);
}

protected static function pageRequest(Page $page, $languageFromPath) {
protected static function pageRequest(Page $page, $languageFromPath, $data) {
if (!wire('modules')->isInstalled('Twack')) {
throw new InternalServererrorException('Twack module not found.');
}
Expand Down Expand Up @@ -75,6 +74,12 @@ protected static function pageRequest(Page $page, $languageFromPath) {

$ajaxOutput = $page->render();
$results = json_decode($ajaxOutput, true);
$results['hash'] = md5($ajaxOutput);

if (!empty($data->hash) && !empty($results['hash']) && $results['hash'] === $data->hash) {
throw new AppApiException('No new contents', 204, ['errorcode' => 'no_new_contents']);
}

return $results;
}

Expand Down

0 comments on commit ac5e063

Please sign in to comment.