summaryrefslogtreecommitdiffstats
path: root/lib/Model
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2019-01-24 10:26:44 -0100
committerMaxence Lange <maxence@artificial-owl.com>2019-01-24 10:50:03 -0100
commitaccf7e424d23e8f343f4d0ecf053f65ab2800e77 (patch)
tree99e67d61c175b218a5c624e0f5eb74c0a827f102 /lib/Model
parentfd2ef9e6cec67b6a50b91293d97831d3631d9f26 (diff)
implementing caching for incoming request
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Model')
-rw-r--r--lib/Model/ActivityPub/ACore.php36
-rw-r--r--lib/Model/StreamQueue.php264
2 files changed, 294 insertions, 6 deletions
diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php
index 4cac3361..1a2f3cfa 100644
--- a/lib/Model/ActivityPub/ACore.php
+++ b/lib/Model/ActivityPub/ACore.php
@@ -32,6 +32,7 @@ namespace OCA\Social\Model\ActivityPub;
use daita\MySmallPhpTools\Traits\TArrayTools;
use daita\MySmallPhpTools\Traits\TPathTools;
+use daita\MySmallPhpTools\Traits\TStringTools;
use JsonSerializable;
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
use OCA\Social\Exceptions\InvalidOriginException;
@@ -45,6 +46,7 @@ class ACore extends Item implements JsonSerializable {
use TArrayTools;
+ use TStringTools;
use TPathTools;
@@ -64,6 +66,9 @@ class ACore extends Item implements JsonSerializable {
/** @var null Item */
private $parent = null;
+ /** @var string */
+ private $requestToken = '';
+
/** @var array */
private $entries = [];
@@ -93,6 +98,30 @@ class ACore extends Item implements JsonSerializable {
/**
+ * @return string
+ */
+ public function getRequestToken(): string {
+ if ($this->isRoot()) {
+ return $this->requestToken;
+ } else {
+ return $this->getRoot()
+ ->getRequestToken();
+ }
+ }
+
+ /**
+ * @param string $token
+ *
+ * @return ACore
+ */
+ public function setRequestToken(string $token): ACore {
+ $this->requestToken = $token;
+
+ return $this;
+ }
+
+
+ /**
* @param ACore $parent
*
* @return ACore
@@ -237,12 +266,7 @@ class ACore extends Item implements JsonSerializable {
$base = $this->withoutEndSlash($this->withBeginSlash($base));
}
- $uuid = sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff),
- mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
- );
-
+ $uuid = $this->uuid();
$this->setId($url . $base . '/' . $uuid);
}
diff --git a/lib/Model/StreamQueue.php b/lib/Model/StreamQueue.php
new file mode 100644
index 00000000..1fcf1fe4
--- /dev/null
+++ b/lib/Model/StreamQueue.php
@@ -0,0 +1,264 @@
+<?php
+declare(strict_types=1);
+
+
+/**
+ * Nextcloud - Social Support
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Maxence Lange <maxence@artificial-owl.com>
+ * @copyright 2018, Maxence Lange <maxence@artificial-owl.com>
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+namespace OCA\Social\Model;
+
+
+use daita\MySmallPhpTools\Traits\TArrayTools;
+use DateTime;
+use JsonSerializable;
+
+
+/**
+ * Class StreamQueue
+ *
+ * @package OCA\Social\Model
+ */
+class StreamQueue implements JsonSerializable {
+
+
+ use TArrayTools;
+
+
+ const TYPE_CACHE = 'Cache';
+ const TYPE_VERIFY = 'Signature';
+
+ const STATUS_STANDBY = 0;
+ const STATUS_RUNNING = 1;
+ const STATUS_SUCCESS = 9;
+
+
+ /** @var integer */
+ private $id = 0;
+
+ /** @var string */
+ private $token = '';
+
+ /** @var string */
+ private $streamId = '';
+
+ /** @var string */
+ private $type = '';
+
+ /** @var int */
+ private $status = 0;
+
+ /** @var int */
+ private $tries = 0;
+
+ /** @var int */
+ private $last = 0;
+
+
+ /**
+ * StreamQueue constructor.
+ *
+ * @param string $token
+ * @param string $type
+ * @param string $streamId
+ */
+ public function __construct(string $token = '', string $type = '', string $streamId = '') {
+ $this->token = $token;
+ $this->type = $type;
+ $this->streamId = $streamId;
+ }
+
+
+ /**
+ * @return int
+ */
+ public function getId(): int {
+ return $this->id;
+ }
+
+ /**
+ * @param int $id
+ *
+ * @return StreamQueue
+ */
+ public function setId(int $id): StreamQueue {
+ $this->id = $id;
+
+ return $this;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getToken(): string {
+ return $this->token;
+ }
+
+ /**
+ * @param string $token
+ *
+ * @return StreamQueue
+ */
+ public function setToken(string $token): StreamQueue {
+ $this->token = $token;
+
+ return $this;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getStreamId(): string {
+ return $this->streamId;
+ }
+
+ /**
+ * @param string $streamId
+ *
+ * @return StreamQueue
+ */
+ public function setStreamId(string $streamId): StreamQueue {
+ $this->streamId = $streamId;
+
+ return $this;
+ }
+
+
+ /**
+ * @return string
+ */
+ public function getType(): string {
+ return $this->type;
+ }
+
+ /**
+ * @param string $type
+ *
+ * @return StreamQueue
+ */
+ public function setType(string $type): StreamQueue {
+ $this->type = $type;
+
+ return $this;
+ }
+
+
+ /**
+ * @return int
+ */
+ public function getStatus(): int {
+ return $this->status;
+ }
+
+ /**
+ * @param int $status
+ *
+ * @return StreamQueue
+ */
+ public function setStatus(int $status): StreamQueue {
+ $this->status = $status;
+
+ return $this;
+ }
+
+
+ /**
+ * @return int
+ */
+ public function getTries(): int {
+ return $this->tries;
+ }
+
+ /**
+ * @param int $tries
+ *
+ * @return StreamQueue
+ */
+ public function setTries(int $tries): StreamQueue {
+ $this->tries = $tries;
+
+ return $this;
+ }
+
+
+ /**
+ * @return int
+ */
+ public function getLast(): int {
+ return $this->last;
+ }
+
+ /**
+ * @param int $last
+ *
+ * @return StreamQueue
+ */
+ public function setLast(int $last): StreamQueue {
+ $this->last = $last;
+
+ return $this;
+ }
+
+
+ /**
+ * @param array $data
+ */
+ public function importFromDatabase(array $data) {
+ $this->setId($this->getInt('id', $data, 0));
+ $this->setToken($this->get('token', $data, ''));
+ $this->setStreamId($this->get('stream_id', $data, ''));
+ $this->setType($this->get('type', $data, ''));
+ $this->setStatus($this->getInt('status', $data, 0));
+ $this->setTries($this->getInt('tries', $data, 0));
+
+ $last = $this->get('last', $data, '');
+ if ($last === '') {
+ $this->setLast(0);
+ } else {
+ $dTime = new DateTime($last);
+ $this->setLast($dTime->getTimestamp());
+ }
+ }
+
+
+ /**
+ * @return array
+ */
+ public function jsonSerialize(): array {
+ return [
+ 'id' => $this->getId(),
+ 'token' => $this->getToken(),
+ 'streamId' => $this->getStreamId(),
+ 'type' => $this->getType(),
+ 'status' => $this->getStatus(),
+ 'tries' => $this->getTries(),
+ 'last' => $this->getLast()
+ ];
+ }
+
+}
+