-
Notifications
You must be signed in to change notification settings - Fork 12
/
ActivityController.php
134 lines (119 loc) · 3.87 KB
/
ActivityController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace craft\webhooks\controllers;
use Craft;
use craft\db\Paginator;
use craft\db\Query;
use craft\helpers\DateTimeHelper;
use craft\helpers\Db;
use craft\helpers\Json;
use craft\web\twig\variables\Paginate;
use craft\webhooks\assets\activity\ActivityAsset;
use craft\webhooks\Plugin;
use yii\web\BadRequestHttpException;
use yii\web\Response;
/**
* Activity Controller
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0.0
*/
class ActivityController extends BaseController
{
/**
* Shows the webhook activity page
*
* @return Response
*/
public function actionIndex(): Response
{
Craft::$app->getView()->registerAssetBundle(ActivityAsset::class);
$query = (new Query())
->select(['r.*', 'w.name'])
->from(['{{%webhookrequests}} r'])
->leftJoin('{{%webhooks}} w', '[[w.id]] = [[r.webhookId]]')
->orderBy(['id' => SORT_DESC]);
$paginator = new Paginator($query, [
'currentPage' => $this->request->getPageNum(),
]);
$requests = $paginator->getPageResults();
foreach ($requests as &$request) {
$request['host'] = parse_url($request['url'], PHP_URL_HOST);
$request['dateRequested'] = DateTimeHelper::toDateTime($request['dateRequested']);
}
return $this->renderTemplate('webhooks/_activity/index', [
'requests' => $requests,
'pageInfo' => Paginate::create($paginator),
]);
}
/**
* Clears the requests table.
*
* @return Response
* @since 2.3.0
*/
public function actionClear(): Response
{
Db::delete('{{%webhookrequests}}', ['status' => Plugin::STATUS_DONE]);
return $this->redirect('webhooks/activity');
}
/**
* Returns request details modal HTML.
*
* @return Response
* @throws BadRequestHttpException
*/
public function actionDetails(): Response
{
$requestId = $this->request->getRequiredBodyParam('requestId');
$request = Plugin::getInstance()->getRequestData($requestId);
if ($request['requestHeaders'] && $request['requestBody'] && $this->_isJson($request['requestHeaders'])) {
$request['requestBody'] = $this->_prettyJson($request['requestBody']);
}
if ($request['responseHeaders'] && $request['responseBody'] && $this->_isJson($request['responseHeaders'])) {
$request['responseBody'] = $this->_prettyJson($request['responseBody']);
}
return $this->asJson([
'html' => Craft::$app->getView()->renderTemplate('webhooks/_activity/details', [
'request' => $request,
]),
]);
}
/**
* Returns request details modal HTML.
*
* @return Response
* @throws BadRequestHttpException
*/
public function actionRedeliver(): Response
{
$requestId = $this->request->getRequiredBodyParam('requestId');
Plugin::getInstance()->sendRequest($requestId);
return $this->runAction('details');
}
/**
* Returns whether a Content-Type header exists in the given list of headers, and is set to application/json.
*
* @param array $headers
* @return bool
*/
private function _isJson(array $headers): bool
{
foreach ($headers as $name => $value) {
if (strtolower($name) === 'content-type') {
$value = is_array($value) ? reset($value) : $value;
return strtolower(trim($value)) === 'application/json';
}
}
return false;
}
/**
* Prettifies a JSON blob.
*
* @param string $json
* @return string
*/
private function _prettyJson(string $json): string
{
return Json::encode(Json::decode($json), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
}