2018-09-28 11:41:24 +00:00
|
|
|
<?php
|
2022-04-15 11:34:01 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
use JsonSerializable;
|
2023-03-14 21:17:57 +00:00
|
|
|
use OCA\Social\AP;
|
2019-07-11 15:00:05 +00:00
|
|
|
use OCA\Social\Exceptions\ItemAlreadyExistsException;
|
2023-03-14 21:17:57 +00:00
|
|
|
use OCA\Social\Exceptions\ItemNotFoundException;
|
2018-12-13 09:40:27 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\ACore;
|
2023-03-14 21:17:57 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Actor\Person;
|
2019-01-24 11:28:12 +00:00
|
|
|
use OCA\Social\Model\ActivityPub\Stream;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2019-01-21 16:12:19 +00:00
|
|
|
class Note extends Stream implements JsonSerializable {
|
2022-04-15 11:34:01 +00:00
|
|
|
public const TYPE = 'Note';
|
2018-11-19 10:34:54 +00:00
|
|
|
|
2022-04-15 11:01:18 +00:00
|
|
|
private array $hashtags = [];
|
2018-11-16 10:47:59 +00:00
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
public function __construct(ACore $parent = null) {
|
2018-11-19 10:34:54 +00:00
|
|
|
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-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
|
|
|
public function setHashtags(array $hashtags): Note {
|
|
|
|
$this->hashtags = $hashtags;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:17:57 +00:00
|
|
|
public function fillMentions(): void {
|
|
|
|
$personInterface = AP::$activityPub->getInterfaceFromType(Person::TYPE);
|
|
|
|
$mentions = [];
|
|
|
|
|
|
|
|
foreach ($this->getTags('Mention') as $item) {
|
|
|
|
$username = ltrim($this->get('name', $item), '@');
|
|
|
|
$mention = [
|
|
|
|
'id' => 0,
|
|
|
|
'username' => $username,
|
|
|
|
'url' => $this->get('href', $item),
|
|
|
|
'acct' => $username,
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var Person $actor */
|
|
|
|
$actor = $personInterface->getItemById($mention['url']);
|
|
|
|
$mention['id'] = (string)$actor->getNid();
|
|
|
|
$mention['username'] = $actor->getPreferredUsername();
|
|
|
|
$mention['acct'] = $actor->getAccount();
|
|
|
|
} catch (ItemNotFoundException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$mentions[] = $mention;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setDetailArray('mentions', $mentions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
public function fillHashtags(): void {
|
2019-01-04 19:34:19 +00:00
|
|
|
$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);
|
|
|
|
}
|
2020-08-22 02:41:38 +00:00
|
|
|
$hashtags[] = trim($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
|
|
|
/**
|
2019-07-11 15:00:05 +00:00
|
|
|
* @throws ItemAlreadyExistsException
|
2018-11-12 22:55:10 +00:00
|
|
|
*/
|
2022-04-15 16:13:33 +00:00
|
|
|
public function import(array $data): void {
|
2018-11-12 22:55:10 +00:00
|
|
|
parent::import($data);
|
|
|
|
|
2019-07-26 15:10:48 +00:00
|
|
|
$this->fillHashtags();
|
2023-03-14 21:17:57 +00:00
|
|
|
$this->fillMentions();
|
2019-01-19 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-15 16:13:33 +00:00
|
|
|
public function importFromDatabase(array $data): void {
|
2018-12-21 13:50:03 +00:00
|
|
|
parent::importFromDatabase($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
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|