2018-09-28 11:41:24 +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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-13 09:40:27 +00:00
|
|
|
namespace OCA\Social\Model\ActivityPub\Object;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
2018-11-16 10:47:59 +00:00
|
|
|
use DateTime;
|
2019-01-19 12:00:25 +00:00
|
|
|
use Exception;
|
2018-09-28 11:41:24 +00:00
|
|
|
use JsonSerializable;
|
2019-01-19 12:00:25 +00:00
|
|
|
use OCA\Social\AP;
|
|
|
|
use OCA\Social\Exceptions\InvalidResourceEntryException;
|
|
|
|
use OCA\Social\Exceptions\ItemUnknownException;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2019-01-24 11:28:12 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Stream;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-12-19 01:13:33 +00:00
|
|
|
|
2019-01-21 16:12:19 +00:00
|
|
|
class Note extends Stream implements JsonSerializable {
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
const TYPE = 'Note';
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2019-01-19 12:00:25 +00:00
|
|
|
/** @var array */
|
|
|
|
private $attachments = [];
|
2019-03-05 15:11:16 +00:00
|
|
|
|
2019-01-04 19:34:19 +00:00
|
|
|
/** @var array */
|
|
|
|
private $hashtags = [];
|
2018-11-16 10:47:59 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note constructor.
|
|
|
|
*
|
2018-11-19 10:34:54 +00:00
|
|
|
* @param ACore $parent
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-19 10:34:54 +00:00
|
|
|
public function __construct($parent = null) {
|
|
|
|
parent::__construct($parent);
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
$this->setType(self::TYPE);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-19 12:00:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttachments(): array {
|
|
|
|
return $this->attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $attachments
|
|
|
|
*
|
|
|
|
* @return Note
|
|
|
|
*/
|
|
|
|
public function setAttachments(array $attachments): Note {
|
|
|
|
$this->attachments = $attachments;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
2019-01-04 19:34:19 +00:00
|
|
|
* @return array
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2019-01-04 19:34:19 +00:00
|
|
|
public function getHashtags(): array {
|
|
|
|
return $this->hashtags;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-04 19:34:19 +00:00
|
|
|
* @param array $hashtags
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
|
|
|
* @return Note
|
|
|
|
*/
|
2019-01-04 19:34:19 +00:00
|
|
|
public function setHashtags(array $hashtags): Note {
|
|
|
|
$this->hashtags = $hashtags;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-16 10:47:59 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-01-04 19:34:19 +00:00
|
|
|
public function fillHashtags() {
|
|
|
|
$tags = $this->getTags('Hashtag');
|
|
|
|
$hashtags = [];
|
|
|
|
foreach ($tags as $tag) {
|
2019-01-11 10:35:36 +00:00
|
|
|
$hashtag = $tag['name'];
|
|
|
|
if (substr($hashtag, 0, 1) === '#') {
|
|
|
|
$hashtag = substr($hashtag, 1);
|
|
|
|
}
|
|
|
|
$hashtags[] = $hashtag;
|
2019-01-04 19:34:19 +00:00
|
|
|
}
|
2018-11-16 10:47:59 +00:00
|
|
|
|
2019-01-04 19:34:19 +00:00
|
|
|
$this->setHashtags($hashtags);
|
2018-11-16 10:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-12 22:55:10 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function import(array $data) {
|
|
|
|
parent::import($data);
|
|
|
|
|
2019-01-19 12:00:25 +00:00
|
|
|
$this->importAttachments($this->getArray('attachment', $data, []));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function importAttachments(array $list) {
|
2019-01-19 12:13:27 +00:00
|
|
|
$new = [];
|
2019-01-19 12:00:25 +00:00
|
|
|
foreach ($list as $item) {
|
|
|
|
try {
|
|
|
|
$attachment = AP::$activityPub->getItemFromData($item, $this);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attachment->getType() !== Document::TYPE
|
|
|
|
&& $attachment->getType() !== Image::TYPE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$attachment->setUrl(
|
|
|
|
$this->validateEntryString(ACore::AS_URL, $attachment->getUrl())
|
|
|
|
);
|
|
|
|
} catch (InvalidResourceEntryException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($attachment->getUrl() === '') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$interface = AP::$activityPub->getInterfaceFromType($attachment->getType());
|
|
|
|
} catch (ItemUnknownException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$interface->save($attachment);
|
2019-01-19 12:13:27 +00:00
|
|
|
$new[] = $attachment;
|
2019-01-19 12:00:25 +00:00
|
|
|
}
|
2019-01-19 12:13:27 +00:00
|
|
|
|
|
|
|
$this->setAttachments($new);
|
2018-11-12 22:55:10 +00:00
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
|
2018-12-21 13:50:03 +00:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*/
|
|
|
|
public function importFromDatabase(array $data) {
|
|
|
|
parent::importFromDatabase($data);
|
|
|
|
|
2019-01-19 12:00:25 +00:00
|
|
|
$this->setAttachments($this->getArray('attachments', $data, []));
|
2019-02-23 00:04:00 +00:00
|
|
|
$this->setHashtags($this->getArray('hashtags', $data, []));
|
2018-12-21 13:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function jsonSerialize(): array {
|
2019-02-21 09:38:04 +00:00
|
|
|
$result = parent::jsonSerialize();
|
2019-01-19 12:13:27 +00:00
|
|
|
|
|
|
|
if ($this->isCompleteDetails()) {
|
2019-01-04 19:34:19 +00:00
|
|
|
$result['hashtags'] = $this->getHashtags();
|
2019-01-19 12:13:27 +00:00
|
|
|
$result['attachment'] = $this->getAttachments();
|
|
|
|
}
|
|
|
|
|
2019-02-23 00:04:00 +00:00
|
|
|
$this->cleanArray($result);
|
|
|
|
|
2019-01-19 12:13:27 +00:00
|
|
|
return $result;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|