kopia lustrzana https://github.com/nextcloud/social
Merge pull request #23 from nextcloud-gmbh/add-post-typd
type can be public', 'direct', 'followers', 'unlisted'pull/24/head
commit
dbf808bfb6
lib
Command
Service
|
@ -32,7 +32,6 @@ namespace OCA\Social\Command;
|
|||
|
||||
use Exception;
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Social\Model\InstancePath;
|
||||
use OCA\Social\Model\Post;
|
||||
use OCA\Social\Service\ActivityPub\NoteService;
|
||||
use OCA\Social\Service\ActivityService;
|
||||
|
@ -104,7 +103,11 @@ class NoteCreate extends Base {
|
|||
'replyTo', 'r', InputOption::VALUE_OPTIONAL, 'in reply to an existing thread'
|
||||
)
|
||||
->addOption(
|
||||
'to', 't', InputOption::VALUE_OPTIONAL, 'to (default Public)'
|
||||
'to', 't', InputOption::VALUE_OPTIONAL, 'mentioning people'
|
||||
)
|
||||
->addOption(
|
||||
'type', 'y', InputOption::VALUE_OPTIONAL,
|
||||
'type: public (default), followers, unlisted, direct'
|
||||
)
|
||||
->addArgument('userid', InputArgument::REQUIRED, 'userId of the author')
|
||||
->addArgument('content', InputArgument::REQUIRED, 'content of the post')
|
||||
|
@ -124,11 +127,13 @@ class NoteCreate extends Base {
|
|||
$content = $input->getArgument('content');
|
||||
$to = $input->getOption('to');
|
||||
$replyTo = $input->getOption('replyTo');
|
||||
$type = $input->getOption('type');
|
||||
|
||||
$post = new Post($userId);
|
||||
$post->setContent($content);
|
||||
$post->setType(($type === null) ? '' : $type);
|
||||
$post->setReplyTo(($replyTo === null) ? '' : $replyTo);
|
||||
$post->addTo($to);
|
||||
$post->addTo(($to === null) ? '' : $to);
|
||||
|
||||
$result = $this->postService->createPost($post, $activity);
|
||||
|
||||
|
|
|
@ -186,6 +186,8 @@ class ActivityPubController extends Controller {
|
|||
$this->activityService->checkRequest($this->request);
|
||||
$body = file_get_contents('php://input');
|
||||
|
||||
// $this->miscService->log('Body: ' . $body);
|
||||
|
||||
$activity = $this->importService->import($body);
|
||||
|
||||
try {
|
||||
|
|
|
@ -115,6 +115,7 @@ class LocalController extends Controller {
|
|||
$post->setReplyTo($this->get('replyTo', $data, ''));
|
||||
$post->setTo($this->getArray('to', $data, []));
|
||||
$post->addTo($this->get('to', $data, ''));
|
||||
$post->setType($this->get('type', $data, NoteService::TYPE_PUBLIC));
|
||||
|
||||
$result = $this->postService->createPost($post);
|
||||
|
||||
|
|
|
@ -435,6 +435,12 @@ abstract class ACore implements JsonSerializable {
|
|||
}
|
||||
|
||||
|
||||
public function addCc(string $cc): Acore {
|
||||
$this->cc[] = $cc;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
@ -33,6 +33,12 @@ namespace OCA\Social\Model;
|
|||
use daita\MySmallPhpTools\Traits\TArrayTools;
|
||||
use JsonSerializable;
|
||||
|
||||
|
||||
/**
|
||||
* Class Post
|
||||
*
|
||||
* @package OCA\Social\Model
|
||||
*/
|
||||
class Post implements JsonSerializable {
|
||||
|
||||
|
||||
|
@ -51,7 +57,16 @@ class Post implements JsonSerializable {
|
|||
/** @var string */
|
||||
private $content;
|
||||
|
||||
public function __construct($userId = '') {
|
||||
/** @var string */
|
||||
private $type;
|
||||
|
||||
|
||||
/**
|
||||
* Post constructor.
|
||||
*
|
||||
* @param string $userId
|
||||
*/
|
||||
public function __construct(string $userId = '') {
|
||||
$this->userId = $userId;
|
||||
}
|
||||
|
||||
|
@ -65,13 +80,15 @@ class Post implements JsonSerializable {
|
|||
|
||||
/**
|
||||
* @param string $to
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function addTo(string $to) {
|
||||
if ($to === '') {
|
||||
return;
|
||||
public function addTo(string $to): Post {
|
||||
if ($to !== '') {
|
||||
$this->to[] = $to;
|
||||
}
|
||||
|
||||
$this->to[] = $to;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,9 +100,13 @@ class Post implements JsonSerializable {
|
|||
|
||||
/**
|
||||
* @param array $to
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function setTo(array $to) {
|
||||
public function setTo(array $to): Post {
|
||||
$this->to = $to;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
@ -98,9 +119,31 @@ class Post implements JsonSerializable {
|
|||
|
||||
/**
|
||||
* @param string $replyTo
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function setReplyTo(string $replyTo) {
|
||||
public function setReplyTo(string $replyTo): Post {
|
||||
$this->replyTo = $replyTo;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType(): string {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*
|
||||
* @return Post
|
||||
*/
|
||||
public function setType(string $type): Post {
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,7 +170,8 @@ class Post implements JsonSerializable {
|
|||
'userId' => $this->getUserId(),
|
||||
'to' => $this->getTo(),
|
||||
'replyTo' => $this->getReplyTo(),
|
||||
'content' => $this->getContent()
|
||||
'content' => $this->getContent(),
|
||||
'type' => $this->getType()
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -49,6 +49,12 @@ use OCA\Social\Service\MiscService;
|
|||
class NoteService implements ICoreService {
|
||||
|
||||
|
||||
const TYPE_PUBLIC = 'public';
|
||||
const TYPE_UNLISTED = 'unlisted';
|
||||
const TYPE_FOLLOWERS = 'followers';
|
||||
const TYPE_DIRECT = 'direct';
|
||||
|
||||
|
||||
/** @var NotesRequest */
|
||||
private $notesRequest;
|
||||
|
||||
|
@ -95,11 +101,13 @@ class NoteService implements ICoreService {
|
|||
* @param string $userId
|
||||
* @param string $content
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @return Note
|
||||
* @throws ActorDoesNotExistException
|
||||
* @throws NoUserException
|
||||
*/
|
||||
public function generateNote(string $userId, string $content) {
|
||||
public function generateNote(string $userId, string $content, string $type) {
|
||||
$note = new Note();
|
||||
$actor = $this->actorService->getActorFromUserId($userId);
|
||||
|
||||
|
@ -108,7 +116,8 @@ class NoteService implements ICoreService {
|
|||
$note->setAttributedTo(
|
||||
$this->configService->getRoot() . '@' . $actor->getPreferredUsername()
|
||||
);
|
||||
$note->setTo(ActivityService::TO_PUBLIC);
|
||||
|
||||
$this->setRecipient($note, $actor, $type);
|
||||
$note->setContent($content);
|
||||
|
||||
$note->saveAs($this);
|
||||
|
@ -119,18 +128,53 @@ class NoteService implements ICoreService {
|
|||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param Person $actor
|
||||
* @param string $type
|
||||
*/
|
||||
private function setRecipient(Note $note, Person $actor, string $type) {
|
||||
switch ($type) {
|
||||
case self::TYPE_UNLISTED:
|
||||
$note->setTo($actor->getFollowers());
|
||||
$note->addCc(ActivityService::TO_PUBLIC);
|
||||
break;
|
||||
|
||||
case self::TYPE_FOLLOWERS:
|
||||
$note->setTo($actor->getFollowers());
|
||||
break;
|
||||
|
||||
case self::TYPE_DIRECT:
|
||||
break;
|
||||
|
||||
default:
|
||||
$note->setTo(ActivityService::TO_PUBLIC);
|
||||
$note->addCc($actor->getFollowers());
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param string $type
|
||||
* @param string $account
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function assignTo(Note $note, string $account) {
|
||||
public function addRecipient(Note $note, string $type, string $account) {
|
||||
if ($account === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$actor = $this->personService->getFromAccount($account);
|
||||
|
||||
$note->addToArray($actor->getId());
|
||||
if ($type === self::TYPE_DIRECT) {
|
||||
$note->addToArray($actor->getId());
|
||||
} else {
|
||||
$note->addCc($actor->getId());
|
||||
}
|
||||
|
||||
$note->addTag(
|
||||
[
|
||||
'type' => 'Mention',
|
||||
|
@ -144,17 +188,18 @@ class NoteService implements ICoreService {
|
|||
|
||||
/**
|
||||
* @param Note $note
|
||||
* @param string $type
|
||||
* @param array $accounts
|
||||
*
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function assignToArray(Note $note, array $accounts) {
|
||||
public function addRecipients(Note $note, string $type, array $accounts) {
|
||||
if ($accounts === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($accounts as $account) {
|
||||
$this->assignTo($note, $account);
|
||||
$this->addRecipient($note, $type, $account);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -94,7 +94,6 @@ class PersonService implements ICoreService {
|
|||
try {
|
||||
$actor = $this->cacheActorsRequest->getFromAccount($account);
|
||||
} catch (CacheActorDoesNotExistException $e) {
|
||||
|
||||
$object = $this->instanceService->retrieveAccount($account);
|
||||
$actor = new Person();
|
||||
$actor->import($object);
|
||||
|
|
|
@ -78,9 +78,11 @@ class PostService {
|
|||
*/
|
||||
public function createPost(Post $post, ACore &$activity = null) {
|
||||
$note =
|
||||
$this->noteService->generateNote($post->getUserId(), $post->getContent());
|
||||
$this->noteService->generateNote(
|
||||
$post->getUserId(), $post->getContent(), $post->getType()
|
||||
);
|
||||
|
||||
$this->noteService->assignToArray($note, $post->getTo());
|
||||
$this->noteService->addRecipients($note, $post->getType(), $post->getTo());
|
||||
$this->noteService->replyTo($note, $post->getReplyTo());
|
||||
|
||||
return $this->activityService->createActivity(
|
||||
|
|
Ładowanie…
Reference in New Issue