-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.php
167 lines (138 loc) · 4.83 KB
/
App.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Truonglv\Api;
use XF;
use function md5;
use function strval;
use function is_array;
use function array_keys;
use function json_encode;
use XF\Entity\Attachment;
use XF\Mvc\Entity\Entity;
use function call_user_func;
use InvalidArgumentException;
use Truonglv\Api\Data\Reaction;
use XF\Api\Result\EntityResult;
use Truonglv\Api\Util\Encryption;
use Truonglv\Api\Repository\AlertQueueRepository;
class App
{
const HEADER_KEY_APP_VERSION = 'HTTP_XF_TAPI_VERSION';
const HEADER_KEY_API_KEY = 'HTTP_XF_TAPI_KEY';
const HEADER_KEY_ACCESS_TOKEN = 'HTTP_XF_TAPI_TOKEN';
const KEY_LINK_PROXY_ACCESS_TOKEN = 'token';
const KEY_LINK_PROXY_DATE = 'date';
const KEY_LINK_PROXY_TARGET_URL = 'url';
const KEY_LINK_PROXY_INPUT_DATA = '_d';
const KEY_LINK_PROXY_INPUT_SIGNATURE = '_s';
const QUOTE_PLACEHOLDER_TEMPLATE = '[QUOTE={content_type},{content_id}]';
const PAYMENT_PROVIDER_ANDROID = 'tapi_iap_android';
const PAYMENT_PROVIDER_IOS = 'tapi_iap_ios';
/**
* @var bool
*/
public static $enableLogging = false;
/**
* @var string
*/
public static $defaultPushNotificationService = 'Truonglv\Api:FirebaseCloudMessaging';
/**
* @var \XF\Http\Request|null
*/
protected static $request;
public static function setRequest(?\XF\Http\Request $request): void
{
self::$request = $request;
}
public static function getRequest(): \XF\Http\Request
{
return self::$request !== null ? self::$request : XF::app()->request();
}
public static function canViewResources(): bool
{
$addOnRepo = XF::repository(XF\Repository\AddOnRepository::class);
$addOns = $addOnRepo->getEnabledAddOns();
if (isset($addOns['XFRM'])) {
/** @var mixed $callable */
$callable = [XF::visitor(), 'canViewResources'];
return is_callable($callable) && call_user_func($callable) === true;
}
return false;
}
/**
* @param string $targetUrl
* @return string
*/
public static function buildLinkProxy(string $targetUrl): string
{
$accessToken = static::getRequest()->getServer(static::HEADER_KEY_ACCESS_TOKEN);
$payload = [
self::KEY_LINK_PROXY_ACCESS_TOKEN => $accessToken,
self::KEY_LINK_PROXY_DATE => XF::$time,
self::KEY_LINK_PROXY_TARGET_URL => $targetUrl,
];
$encoded = strval(json_encode($payload));
try {
$encrypted = Encryption::encrypt($encoded, XF::app()->options()->tApi_encryptKey);
} catch (InvalidArgumentException $e) {
return $targetUrl;
}
return XF::app()->router('public')
->buildLink('canonical:misc/tapi-goto', null, [
self::KEY_LINK_PROXY_INPUT_DATA => $encrypted,
self::KEY_LINK_PROXY_INPUT_SIGNATURE => md5($encoded)
]);
}
/**
* @return array
*/
public static function getSupportAlertContentTypes(): array
{
$alertQueueRepo = XF::repository(AlertQueueRepository::class);
return $alertQueueRepo->getSupportedAlertContentTypes();
}
/**
* @param Attachment $attachment
* @return string
*/
public static function buildAttachmentLink(Attachment $attachment): string
{
return XF::app()->router('public')->buildLink('canonical:attachments', $attachment);
}
/**
* @param EntityResult $result
* @param Entity $entity
* @param string $reactionKey
* @return void
*/
public static function setupApiResultReactions(EntityResult $result, Entity $entity, string $reactionKey = 'reactions'): void
{
/** @var callable $callable */
$callable = [$entity, 'getVisitorReactionId'];
$visitorReactedId = call_user_func($callable);
$reacted = [];
$entityReactions = $entity->get($reactionKey . '_');
if (is_array($entityReactions)) {
/** @var Reaction $reactionData */
$reactionData = XF::app()->data('Truonglv\Api:Reaction');
$reactions = $reactionData->getReactions();
foreach (array_keys($entityReactions) as $reactionId) {
$count = $entityReactions[$reactionId];
if ($visitorReactedId > 0 && $visitorReactedId == $reactionId) {
$count -= 1;
}
if (isset($reactions[$reactionId]) && $count > 0) {
$reacted[] = [
'image' => $reactions[$reactionId]['imageUrl'],
'total' => $count
];
}
}
}
$result->tapi_reactions = $reacted;
}
public static function alertQueueRepo(): AlertQueueRepository
{
$repo = XF::repository(AlertQueueRepository::class);
return $repo;
}
}