2019-01-21 16:12:19 +00:00
|
|
|
<?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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-01-24 11:28:12 +00:00
|
|
|
namespace OCA\Social\Model\ActivityPub;
|
2019-01-21 16:12:19 +00:00
|
|
|
|
|
|
|
|
2019-09-13 21:56:40 +00:00
|
|
|
use daita\MySmallPhpTools\IQueryRow;
|
2019-01-24 11:28:12 +00:00
|
|
|
use daita\MySmallPhpTools\Model\Cache;
|
|
|
|
use daita\MySmallPhpTools\Model\CacheItem;
|
|
|
|
use DateTime;
|
2019-04-08 16:52:03 +00:00
|
|
|
use Exception;
|
2019-01-21 16:12:19 +00:00
|
|
|
use JsonSerializable;
|
2019-04-08 16:52:03 +00:00
|
|
|
use OCA\Social\Model\StreamAction;
|
2019-06-20 23:59:40 +00:00
|
|
|
use OCA\Social\Traits\TDetails;
|
2019-01-21 16:12:19 +00:00
|
|
|
|
|
|
|
|
2019-09-13 21:56:40 +00:00
|
|
|
/**
|
|
|
|
* Class Stream
|
|
|
|
*
|
|
|
|
* @package OCA\Social\Model\ActivityPub
|
|
|
|
*/
|
2019-09-12 12:38:03 +00:00
|
|
|
class Stream extends ACore implements IQueryRow, JsonSerializable {
|
2019-01-21 16:12:19 +00:00
|
|
|
|
|
|
|
|
2019-06-20 23:59:40 +00:00
|
|
|
use TDetails;
|
|
|
|
|
|
|
|
|
2019-05-29 17:47:09 +00:00
|
|
|
const TYPE = 'Stream';
|
|
|
|
|
|
|
|
|
2019-01-24 11:28:12 +00:00
|
|
|
const TYPE_PUBLIC = 'public';
|
|
|
|
const TYPE_UNLISTED = 'unlisted';
|
|
|
|
const TYPE_FOLLOWERS = 'followers';
|
|
|
|
const TYPE_DIRECT = 'direct';
|
2019-06-20 23:59:40 +00:00
|
|
|
const TYPE_ANNOUNCE = 'announce';
|
2019-01-24 11:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
2019-05-17 14:33:35 +00:00
|
|
|
private $activityId = '';
|
2019-01-24 11:28:12 +00:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $content = '';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $attributedTo = '';
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $inReplyTo = '';
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $sensitive = false;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $conversation = '';
|
|
|
|
|
|
|
|
/** @var Cache */
|
|
|
|
private $cache = null;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $publishedTime = 0;
|
2019-01-21 16:12:19 +00:00
|
|
|
|
2019-04-08 16:52:03 +00:00
|
|
|
/** @var StreamAction */
|
|
|
|
private $action = null;
|
|
|
|
|
2019-08-15 13:56:29 +00:00
|
|
|
/** @var string */
|
|
|
|
private $timeline = '';
|
|
|
|
|
2019-05-17 17:33:21 +00:00
|
|
|
/** @var bool */
|
2020-06-12 12:35:33 +00:00
|
|
|
private $filterDuplicate = false;
|
2019-05-17 17:33:21 +00:00
|
|
|
|
2019-01-21 16:12:19 +00:00
|
|
|
|
2019-06-20 23:59:40 +00:00
|
|
|
/**
|
|
|
|
* Stream constructor.
|
|
|
|
*
|
|
|
|
* @param null $parent
|
|
|
|
*/
|
2019-01-21 16:12:19 +00:00
|
|
|
public function __construct($parent = null) {
|
|
|
|
parent::__construct($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-01-24 11:28:12 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getActivityId(): string {
|
|
|
|
return $this->activityId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $activityId
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setActivityId(string $activityId): Stream {
|
|
|
|
$this->activityId = $activityId;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent(): string {
|
|
|
|
return $this->content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $content
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setContent(string $content): Stream {
|
|
|
|
$this->content = $content;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAttributedTo(): string {
|
|
|
|
return $this->attributedTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $attributedTo
|
|
|
|
*
|
|
|
|
* @return Stream
|
2019-01-21 16:12:19 +00:00
|
|
|
*/
|
2019-01-24 11:28:12 +00:00
|
|
|
public function setAttributedTo(string $attributedTo): Stream {
|
|
|
|
$this->attributedTo = $attributedTo;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInReplyTo(): string {
|
|
|
|
return $this->inReplyTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $inReplyTo
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setInReplyTo(string $inReplyTo): Stream {
|
|
|
|
$this->inReplyTo = $inReplyTo;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSensitive(): bool {
|
|
|
|
return $this->sensitive;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $sensitive
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setSensitive(bool $sensitive): Stream {
|
|
|
|
$this->sensitive = $sensitive;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getConversation(): string {
|
|
|
|
return $this->conversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $conversation
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setConversation(string $conversation): Stream {
|
|
|
|
$this->conversation = $conversation;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPublishedTime(): int {
|
|
|
|
return $this->publishedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $time
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setPublishedTime(int $time): Stream {
|
|
|
|
$this->publishedTime = $time;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function convertPublished() {
|
2019-05-17 10:19:45 +00:00
|
|
|
try {
|
|
|
|
$dTime = new DateTime($this->getPublished());
|
|
|
|
$this->setPublishedTime($dTime->getTimestamp());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2019-01-24 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-05-29 20:56:42 +00:00
|
|
|
public function hasCache(): bool {
|
2019-01-24 11:28:12 +00:00
|
|
|
return ($this->cache !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Cache
|
|
|
|
*/
|
|
|
|
public function getCache(): Cache {
|
2019-01-21 16:12:19 +00:00
|
|
|
return $this->cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-24 11:28:12 +00:00
|
|
|
* @param Cache $cache
|
2019-01-21 16:12:19 +00:00
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
2019-01-24 11:28:12 +00:00
|
|
|
public function setCache(Cache $cache): Stream {
|
2019-01-21 16:12:19 +00:00
|
|
|
$this->cache = $cache;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-24 11:28:12 +00:00
|
|
|
public function addCacheItem(string $url): Stream {
|
|
|
|
$cacheItem = new CacheItem($url);
|
|
|
|
|
2019-05-29 20:56:42 +00:00
|
|
|
if (!$this->hasCache()) {
|
2019-01-24 11:28:12 +00:00
|
|
|
$this->setCache(new Cache());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getCache()
|
|
|
|
->addItem($cacheItem);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-08 16:52:03 +00:00
|
|
|
/**
|
|
|
|
* @return StreamAction
|
|
|
|
*/
|
|
|
|
public function getAction(): StreamAction {
|
|
|
|
return $this->action;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param StreamAction $action
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setAction(StreamAction $action): Stream {
|
|
|
|
$this->action = $action;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasAction(): bool {
|
|
|
|
return ($this->action !== null);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-15 13:56:29 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTimeline(): string {
|
|
|
|
return $this->timeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $timeline
|
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
|
|
|
public function setTimeline(string $timeline): self {
|
|
|
|
$this->timeline = $timeline;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 17:33:21 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
public function isFilterDuplicate(): bool {
|
|
|
|
return $this->filterDuplicate;
|
2019-05-17 17:33:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 12:35:33 +00:00
|
|
|
* @param bool $filterDuplicate
|
2019-05-17 17:33:21 +00:00
|
|
|
*
|
|
|
|
* @return Stream
|
|
|
|
*/
|
2020-06-12 12:35:33 +00:00
|
|
|
public function setFilterDuplicate(bool $filterDuplicate): Stream {
|
|
|
|
$this->filterDuplicate = $filterDuplicate;
|
2019-05-17 17:33:21 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-08 16:52:03 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2019-01-24 11:28:12 +00:00
|
|
|
public function import(array $data) {
|
|
|
|
parent::import($data);
|
|
|
|
|
|
|
|
$this->setInReplyTo($this->validate(self::AS_ID, 'inReplyTo', $data, ''));
|
|
|
|
$this->setAttributedTo($this->validate(self::AS_ID, 'attributedTo', $data, ''));
|
|
|
|
$this->setSensitive($this->getBool('sensitive', $data, false));
|
2019-01-26 11:57:07 +00:00
|
|
|
$this->setObjectId($this->get('object', $data, ''));
|
2019-01-24 11:28:12 +00:00
|
|
|
$this->setConversation($this->validate(self::AS_ID, 'conversation', $data, ''));
|
|
|
|
$this->setContent($this->get('content', $data, ''));
|
|
|
|
$this->convertPublished();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function importFromDatabase(array $data) {
|
|
|
|
parent::importFromDatabase($data);
|
|
|
|
|
2019-05-06 14:32:31 +00:00
|
|
|
try {
|
|
|
|
$dTime = new DateTime($this->get('published_time', $data, 'yesterday'));
|
|
|
|
$this->setPublishedTime($dTime->getTimestamp());
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2019-01-24 11:28:12 +00:00
|
|
|
|
|
|
|
$this->setActivityId($this->validate(self::AS_ID, 'activity_id', $data, ''));
|
2019-01-26 11:57:07 +00:00
|
|
|
$this->setContent($this->validate(self::AS_STRING, 'content', $data, ''));
|
|
|
|
$this->setObjectId($this->validate(self::AS_ID, 'object_id', $data, ''));
|
2019-01-24 11:28:12 +00:00
|
|
|
$this->setAttributedTo($this->validate(self::AS_ID, 'attributed_to', $data, ''));
|
|
|
|
$this->setInReplyTo($this->validate(self::AS_ID, 'in_reply_to', $data));
|
2019-06-20 23:59:40 +00:00
|
|
|
$this->setDetailsAll($this->getArray('details', $data, []));
|
2020-06-12 12:35:33 +00:00
|
|
|
$this->setFilterDuplicate($this->getBool('filter_duplicate', $data, false));
|
2019-01-24 11:28:12 +00:00
|
|
|
|
|
|
|
$cache = new Cache();
|
|
|
|
$cache->import($this->getArray('cache', $data, []));
|
|
|
|
$this->setCache($cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-21 16:12:19 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2020-08-25 03:06:43 +00:00
|
|
|
public function exportAsActivityPub(): array {
|
2019-01-24 11:28:12 +00:00
|
|
|
$result = array_merge(
|
2020-08-25 03:06:43 +00:00
|
|
|
parent::exportAsActivityPub(),
|
2019-01-24 11:28:12 +00:00
|
|
|
[
|
|
|
|
'content' => $this->getContent(),
|
2019-02-23 00:04:00 +00:00
|
|
|
'attributedTo' => ($this->getAttributedTo() !== '') ? $this->getUrlSocial()
|
2019-09-13 21:56:40 +00:00
|
|
|
. $this->getAttributedTo() : '',
|
2019-01-24 11:28:12 +00:00
|
|
|
'inReplyTo' => $this->getInReplyTo(),
|
|
|
|
'sensitive' => $this->isSensitive(),
|
|
|
|
'conversation' => $this->getConversation()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-08-25 03:06:43 +00:00
|
|
|
// TODO: use exportFormat
|
2019-01-21 16:12:19 +00:00
|
|
|
if ($this->isCompleteDetails()) {
|
2019-01-26 11:57:07 +00:00
|
|
|
$result = array_merge(
|
2019-01-21 16:12:19 +00:00
|
|
|
$result,
|
2019-01-24 11:28:12 +00:00
|
|
|
[
|
2019-06-20 23:59:40 +00:00
|
|
|
'details' => $this->getDetailsAll(),
|
2019-04-08 16:52:03 +00:00
|
|
|
'action' => ($this->hasAction()) ? $this->getAction() : [],
|
2019-05-29 20:56:42 +00:00
|
|
|
'cache' => ($this->hasCache()) ? $this->getCache() : '',
|
2019-02-23 00:04:00 +00:00
|
|
|
'publishedTime' => $this->getPublishedTime()
|
2019-01-24 11:28:12 +00:00
|
|
|
]
|
2019-01-21 16:12:19 +00:00
|
|
|
);
|
2019-06-20 23:59:40 +00:00
|
|
|
|
2019-09-27 13:03:25 +00:00
|
|
|
// $result['cc'] = '';
|
|
|
|
// $result['bcc'] = '';
|
|
|
|
// $result['to'] = '';
|
2019-01-21 16:12:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 00:04:00 +00:00
|
|
|
$this->cleanArray($result);
|
|
|
|
|
2019-01-21 16:12:19 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-25 03:06:43 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function exportAsLocal(): array {
|
|
|
|
$result = array_merge(
|
|
|
|
parent::exportAsLocal(),
|
|
|
|
[]);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|