kopia lustrzana https://github.com/nextcloud/social
managing notes
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/40/head
rodzic
86e90ffef8
commit
62f713f434
|
@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
|
||||||
use Exception;
|
use Exception;
|
||||||
use OCA\Social\AppInfo\Application;
|
use OCA\Social\AppInfo\Application;
|
||||||
use OCA\Social\Db\NotesRequest;
|
use OCA\Social\Db\NotesRequest;
|
||||||
|
use OCA\Social\Exceptions\UnknownItemException;
|
||||||
use OCA\Social\Service\ActivityPub\FollowService;
|
use OCA\Social\Service\ActivityPub\FollowService;
|
||||||
use OCA\Social\Service\ActivityService;
|
use OCA\Social\Service\ActivityService;
|
||||||
use OCA\Social\Service\ActorService;
|
use OCA\Social\Service\ActorService;
|
||||||
|
@ -125,6 +126,7 @@ class ActivityPubController extends Controller {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$actor = $this->actorService->getActor($username);
|
$actor = $this->actorService->getActor($username);
|
||||||
|
|
||||||
// $actor->setTopLevel(true);
|
// $actor->setTopLevel(true);
|
||||||
|
|
||||||
return $this->directSuccess($actor);
|
return $this->directSuccess($actor);
|
||||||
|
@ -161,7 +163,23 @@ class ActivityPubController extends Controller {
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function sharedInbox(): Response {
|
public function sharedInbox(): Response {
|
||||||
return $this->success([]);
|
|
||||||
|
try {
|
||||||
|
$this->activityService->checkRequest($this->request);
|
||||||
|
|
||||||
|
$body = file_get_contents('php://input');
|
||||||
|
$this->miscService->log('Shared Inbox: ' . $body);
|
||||||
|
|
||||||
|
$activity = $this->importService->import($body);
|
||||||
|
try {
|
||||||
|
$this->importService->parse($activity);
|
||||||
|
} catch (UnknownItemException $e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->success([]);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return $this->fail($e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -186,12 +204,12 @@ class ActivityPubController extends Controller {
|
||||||
$this->activityService->checkRequest($this->request);
|
$this->activityService->checkRequest($this->request);
|
||||||
$body = file_get_contents('php://input');
|
$body = file_get_contents('php://input');
|
||||||
|
|
||||||
$this->miscService->log('Body: ' . $body);
|
$this->miscService->log('Inbox: ' . $body);
|
||||||
|
|
||||||
$activity = $this->importService->import($body);
|
$activity = $this->importService->import($body);
|
||||||
try {
|
try {
|
||||||
$this->importService->save($activity);
|
$this->importService->parse($activity);
|
||||||
} catch (Exception $e) {
|
} catch (UnknownItemException $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->success([]);
|
return $this->success([]);
|
||||||
|
@ -248,6 +266,7 @@ class ActivityPubController extends Controller {
|
||||||
try {
|
try {
|
||||||
$actor = $this->actorService->getActor($username);
|
$actor = $this->actorService->getActor($username);
|
||||||
$followers = $this->followService->getFollowers($actor);
|
$followers = $this->followService->getFollowers($actor);
|
||||||
|
|
||||||
// $followers->setTopLevel(true);
|
// $followers->setTopLevel(true);
|
||||||
|
|
||||||
return $this->directSuccess($followers);
|
return $this->directSuccess($followers);
|
||||||
|
|
|
@ -31,6 +31,7 @@ namespace OCA\Social\Db;
|
||||||
|
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use OCA\Social\Exceptions\NoteNotFoundException;
|
||||||
use OCA\Social\Model\ActivityPub\Note;
|
use OCA\Social\Model\ActivityPub\Note;
|
||||||
use OCA\Social\Service\ActivityService;
|
use OCA\Social\Service\ActivityService;
|
||||||
use OCA\Social\Service\ConfigService;
|
use OCA\Social\Service\ConfigService;
|
||||||
|
@ -61,52 +62,72 @@ class NotesRequest extends NotesRequestBuilder {
|
||||||
* @param Note $note
|
* @param Note $note
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
* @throws \Exception
|
|
||||||
*/
|
*/
|
||||||
public function save(Note $note): int {
|
public function save(Note $note): int {
|
||||||
try {
|
$dTime = new DateTime();
|
||||||
|
$dTime->setTimestamp($note->getPublishedTime());
|
||||||
|
|
||||||
$dTime = new DateTime();
|
$qb = $this->getNotesInsertSql();
|
||||||
$dTime->setTimestamp($note->getPublishedTime());
|
$qb->setValue('id', $qb->createNamedParameter($note->getId()))
|
||||||
|
->setValue('to', $qb->createNamedParameter($note->getTo()))
|
||||||
|
->setValue(
|
||||||
|
'to_array', $qb->createNamedParameter(
|
||||||
|
json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->setValue(
|
||||||
|
'cc', $qb->createNamedParameter(
|
||||||
|
json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->setValue(
|
||||||
|
'bcc', $qb->createNamedParameter(
|
||||||
|
json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES
|
||||||
|
)
|
||||||
|
)
|
||||||
|
->setValue('content', $qb->createNamedParameter($note->getContent()))
|
||||||
|
->setValue('summary', $qb->createNamedParameter($note->getSummary()))
|
||||||
|
->setValue('published', $qb->createNamedParameter($note->getPublished()))
|
||||||
|
->setValue(
|
||||||
|
'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE)
|
||||||
|
)
|
||||||
|
->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo()))
|
||||||
|
->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo()))
|
||||||
|
->setValue('source', $qb->createNamedParameter($note->getSource()))
|
||||||
|
->setValue(
|
||||||
|
'creation',
|
||||||
|
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
||||||
|
);
|
||||||
|
|
||||||
$qb = $this->getNotesInsertSql();
|
$qb->execute();
|
||||||
$qb->setValue('id', $qb->createNamedParameter($note->getId()))
|
|
||||||
->setValue('to', $qb->createNamedParameter($note->getTo()))
|
|
||||||
->setValue(
|
|
||||||
'to_array', $qb->createNamedParameter(
|
|
||||||
json_encode($note->getToArray(), JSON_UNESCAPED_SLASHES)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->setValue(
|
|
||||||
'cc', $qb->createNamedParameter(
|
|
||||||
json_encode($note->getCcArray(), JSON_UNESCAPED_SLASHES)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->setValue(
|
|
||||||
'bcc', $qb->createNamedParameter(
|
|
||||||
json_encode($note->getBccArray()), JSON_UNESCAPED_SLASHES
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->setValue('content', $qb->createNamedParameter($note->getContent()))
|
|
||||||
->setValue('summary', $qb->createNamedParameter($note->getSummary()))
|
|
||||||
->setValue('published', $qb->createNamedParameter($note->getPublished()))
|
|
||||||
->setValue(
|
|
||||||
'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE)
|
|
||||||
)
|
|
||||||
->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo()))
|
|
||||||
->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo()))
|
|
||||||
->setValue('source', $qb->createNamedParameter($note->getSource()))
|
|
||||||
->setValue(
|
|
||||||
'creation',
|
|
||||||
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
|
|
||||||
);
|
|
||||||
|
|
||||||
$qb->execute();
|
return $qb->getLastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
return $qb->getLastInsertId();
|
|
||||||
} catch (\Exception $e) {
|
/**
|
||||||
throw $e;
|
* @param string $id
|
||||||
|
*
|
||||||
|
* @return Note
|
||||||
|
* @throws NoteNotFoundException
|
||||||
|
*/
|
||||||
|
public function getFromId(string $id): Note {
|
||||||
|
if ($id === '') {
|
||||||
|
throw new NoteNotFoundException();
|
||||||
|
};
|
||||||
|
|
||||||
|
$qb = $this->getNotesSelectSql();
|
||||||
|
$this->limitToIdString($qb, $id);
|
||||||
|
|
||||||
|
$cursor = $qb->execute();
|
||||||
|
$data = $cursor->fetch();
|
||||||
|
$cursor->closeCursor();
|
||||||
|
|
||||||
|
if ($data === false) {
|
||||||
|
throw new NoteNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->parseNotesSelectSql($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -153,4 +174,15 @@ class NotesRequest extends NotesRequestBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $id
|
||||||
|
*/
|
||||||
|
public function deleteNoteById(string $id) {
|
||||||
|
$qb = $this->getNotesDeleteSql();
|
||||||
|
$this->limitToIdString($qb, $id);
|
||||||
|
|
||||||
|
$qb->execute();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\Social\Exceptions;
|
||||||
|
|
||||||
|
class NoteNotFoundException extends \Exception {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -33,7 +33,9 @@ namespace OCA\Social\Service\ActivityPub;
|
||||||
use Exception;
|
use Exception;
|
||||||
use OC\User\NoUserException;
|
use OC\User\NoUserException;
|
||||||
use OCA\Social\Db\NotesRequest;
|
use OCA\Social\Db\NotesRequest;
|
||||||
|
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
|
||||||
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
||||||
|
use OCA\Social\Exceptions\NoteNotFoundException;
|
||||||
use OCA\Social\Exceptions\RequestException;
|
use OCA\Social\Exceptions\RequestException;
|
||||||
use OCA\Social\Exceptions\SocialAppConfigException;
|
use OCA\Social\Exceptions\SocialAppConfigException;
|
||||||
use OCA\Social\Model\ActivityPub\ACore;
|
use OCA\Social\Model\ActivityPub\ACore;
|
||||||
|
@ -226,9 +228,9 @@ class NoteService implements ICoreService {
|
||||||
*
|
*
|
||||||
* @param ACore $note
|
* @param ACore $note
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws ActivityCantBeVerifiedException
|
||||||
*/
|
*/
|
||||||
public function save(ACore $note) {
|
public function parse(ACore $note) {
|
||||||
/** @var Note $note */
|
/** @var Note $note */
|
||||||
if ($note->isRoot()) {
|
if ($note->isRoot()) {
|
||||||
return;
|
return;
|
||||||
|
@ -241,9 +243,21 @@ class NoteService implements ICoreService {
|
||||||
|
|
||||||
if ($parent->getType() === Create::TYPE) {
|
if ($parent->getType() === Create::TYPE) {
|
||||||
$parent->verify(($note->getAttributedTo()));
|
$parent->verify(($note->getAttributedTo()));
|
||||||
$this->notesRequest->save($note);
|
try {
|
||||||
|
$this->notesRequest->getFromId($note->getId());
|
||||||
|
} catch (NoteNotFoundException $e) {
|
||||||
|
$this->notesRequest->save($note);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ACore $item
|
||||||
|
*/
|
||||||
|
public function delete(ACore $item) {
|
||||||
|
/** @var Note $item */
|
||||||
|
$this->notesRequest->deleteNoteById($item->getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Exception;
|
use Exception;
|
||||||
use OCA\Social\Db\ActorsRequest;
|
use OCA\Social\Db\ActorsRequest;
|
||||||
|
use OCA\Social\Db\NotesRequest;
|
||||||
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
use OCA\Social\Exceptions\ActorDoesNotExistException;
|
||||||
use OCA\Social\Exceptions\InvalidResourceException;
|
use OCA\Social\Exceptions\InvalidResourceException;
|
||||||
use OCA\Social\Exceptions\RequestException;
|
use OCA\Social\Exceptions\RequestException;
|
||||||
|
@ -65,6 +66,9 @@ class ActivityService {
|
||||||
/** @var ActorsRequest */
|
/** @var ActorsRequest */
|
||||||
private $actorsRequest;
|
private $actorsRequest;
|
||||||
|
|
||||||
|
/** @var NotesRequest */
|
||||||
|
private $notesRequest;
|
||||||
|
|
||||||
/** @var ActorService */
|
/** @var ActorService */
|
||||||
private $actorService;
|
private $actorService;
|
||||||
|
|
||||||
|
@ -88,6 +92,7 @@ class ActivityService {
|
||||||
* ActivityService constructor.
|
* ActivityService constructor.
|
||||||
*
|
*
|
||||||
* @param ActorsRequest $actorsRequest
|
* @param ActorsRequest $actorsRequest
|
||||||
|
* @param NotesRequest $notesRequest
|
||||||
* @param CurlService $curlService
|
* @param CurlService $curlService
|
||||||
* @param ActorService $actorService
|
* @param ActorService $actorService
|
||||||
* @param PersonService $personService
|
* @param PersonService $personService
|
||||||
|
@ -96,13 +101,15 @@ class ActivityService {
|
||||||
* @param MiscService $miscService
|
* @param MiscService $miscService
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ActorsRequest $actorsRequest, CurlService $curlService, ActorService $actorService,
|
ActorsRequest $actorsRequest, NotesRequest $notesRequest, CurlService $curlService,
|
||||||
|
ActorService $actorService,
|
||||||
PersonService $personService, InstanceService $instanceService,
|
PersonService $personService, InstanceService $instanceService,
|
||||||
ConfigService $configService,
|
ConfigService $configService,
|
||||||
MiscService $miscService
|
MiscService $miscService
|
||||||
) {
|
) {
|
||||||
$this->curlService = $curlService;
|
$this->curlService = $curlService;
|
||||||
$this->actorsRequest = $actorsRequest;
|
$this->actorsRequest = $actorsRequest;
|
||||||
|
$this->notesRequest = $notesRequest;
|
||||||
$this->actorService = $actorService;
|
$this->actorService = $actorService;
|
||||||
$this->personService = $personService;
|
$this->personService = $personService;
|
||||||
$this->instanceService = $instanceService;
|
$this->instanceService = $instanceService;
|
||||||
|
@ -111,12 +118,6 @@ class ActivityService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function test() {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Person $actor
|
* @param Person $actor
|
||||||
* @param ACore $item
|
* @param ACore $item
|
||||||
|
@ -126,6 +127,7 @@ class ActivityService {
|
||||||
* @return array
|
* @return array
|
||||||
* @throws RequestException
|
* @throws RequestException
|
||||||
* @throws SocialAppConfigException
|
* @throws SocialAppConfigException
|
||||||
|
* @throws ActorDoesNotExistException
|
||||||
*/
|
*/
|
||||||
public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null
|
public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null
|
||||||
): array {
|
): array {
|
||||||
|
@ -153,12 +155,41 @@ class ActivityService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $id
|
||||||
|
*
|
||||||
|
* @return ACore
|
||||||
|
* @throws InvalidResourceException
|
||||||
|
*/
|
||||||
|
public function getItem(string $id): ACore {
|
||||||
|
if ($id === '') {
|
||||||
|
throw new InvalidResourceException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$requests = [
|
||||||
|
$this->notesRequest
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($requests as $request) {
|
||||||
|
try {
|
||||||
|
$toDelete = $request->getFromId($id);
|
||||||
|
|
||||||
|
return $toDelete;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidResourceException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ACore $activity
|
* @param ACore $activity
|
||||||
* @param int $type
|
* @param int $type
|
||||||
*
|
*
|
||||||
* @throws RequestException
|
* @throws RequestException
|
||||||
* @throws SocialAppConfigException
|
* @throws SocialAppConfigException
|
||||||
|
* @throws ActorDoesNotExistException
|
||||||
*/
|
*/
|
||||||
public function manageRequest(ACore $activity, int $type) {
|
public function manageRequest(ACore $activity, int $type) {
|
||||||
$result = $this->request($activity, $type);
|
$result = $this->request($activity, $type);
|
||||||
|
@ -175,6 +206,7 @@ class ActivityService {
|
||||||
* @return array
|
* @return array
|
||||||
* @throws RequestException
|
* @throws RequestException
|
||||||
* @throws SocialAppConfigException
|
* @throws SocialAppConfigException
|
||||||
|
* @throws ActorDoesNotExistException
|
||||||
*/
|
*/
|
||||||
public function request(ACore &$activity, int $type) {
|
public function request(ACore &$activity, int $type) {
|
||||||
$this->setupCore($activity);
|
$this->setupCore($activity);
|
||||||
|
@ -206,7 +238,7 @@ class ActivityService {
|
||||||
): array {
|
): array {
|
||||||
$document = json_encode($activity);
|
$document = json_encode($activity);
|
||||||
$date = gmdate(self::DATE_FORMAT);
|
$date = gmdate(self::DATE_FORMAT);
|
||||||
$localActor = $this->getActorFromActivity($activity);
|
$localActor = $this->getActorFromItem($activity);
|
||||||
|
|
||||||
$localActorLink =
|
$localActorLink =
|
||||||
$this->configService->getUrlRoot() . '@' . $localActor->getPreferredUsername();
|
$this->configService->getUrlRoot() . '@' . $localActor->getPreferredUsername();
|
||||||
|
@ -255,49 +287,6 @@ class ActivityService {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * @param Core $activity
|
|
||||||
// *
|
|
||||||
// * @return array
|
|
||||||
// */
|
|
||||||
// private function getHostsFromActivity(Core $activity) {
|
|
||||||
//
|
|
||||||
// $hosts = [];
|
|
||||||
// $hosts[] = $this->getHostFromUriId($activity->getTo());
|
|
||||||
// foreach ($activity->getToArray() as $to) {
|
|
||||||
// $hosts[] = $this->getHostFromUriId($to);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if ($activity instanceof Note) {
|
|
||||||
// /** @var Note $activity */
|
|
||||||
// $hosts[] = $this->getHostFromUriId($activity->getInReplyTo());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// $hosts = $this->cleaningHosts($hosts);
|
|
||||||
//
|
|
||||||
// return $hosts;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// /**
|
|
||||||
// * @param array $hosts
|
|
||||||
// *
|
|
||||||
// * @return array
|
|
||||||
// */
|
|
||||||
// private function cleaningHosts(array $hosts) {
|
|
||||||
// $ret = [];
|
|
||||||
// foreach ($hosts as $host) {
|
|
||||||
// if ($host === '') {
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// $ret[] = $host;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return $ret;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ACore $activity
|
* @param ACore $activity
|
||||||
*
|
*
|
||||||
|
@ -305,7 +294,7 @@ class ActivityService {
|
||||||
* @throws SocialAppConfigException
|
* @throws SocialAppConfigException
|
||||||
* @throws ActorDoesNotExistException
|
* @throws ActorDoesNotExistException
|
||||||
*/
|
*/
|
||||||
private function getActorFromActivity(Acore $activity): Person {
|
private function getActorFromItem(Acore $activity): Person {
|
||||||
if ($activity->gotActor()) {
|
if ($activity->gotActor()) {
|
||||||
return $activity->getActor();
|
return $activity->getActor();
|
||||||
}
|
}
|
||||||
|
@ -413,7 +402,7 @@ class ActivityService {
|
||||||
|
|
||||||
$coreService = $activity->savingAs();
|
$coreService = $activity->savingAs();
|
||||||
if ($coreService !== null) {
|
if ($coreService !== null) {
|
||||||
$coreService->save($activity);
|
$coreService->parse($activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($activity->gotObject()) {
|
if ($activity->gotObject()) {
|
||||||
|
|
Ładowanie…
Reference in New Issue