Merge pull request #40 from nextcloud-gmbh/delete

Deleting Notes from remote
pull/43/head
Maxence Lange 2018-11-20 11:55:01 -01:00 zatwierdzone przez GitHub
commit 7452cc7333
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
13 zmienionych plików z 530 dodań i 110 usunięć

Wyświetl plik

@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\UnknownItemException;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ActorService;
@ -125,6 +126,7 @@ class ActivityPubController extends Controller {
try {
$actor = $this->actorService->getActor($username);
// $actor->setTopLevel(true);
return $this->directSuccess($actor);
@ -161,7 +163,23 @@ class ActivityPubController extends Controller {
* @return 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);
$body = file_get_contents('php://input');
$this->miscService->log('Body: ' . $body);
$this->miscService->log('Inbox: ' . $body);
$activity = $this->importService->import($body);
try {
$this->importService->save($activity);
} catch (Exception $e) {
$this->importService->parse($activity);
} catch (UnknownItemException $e) {
}
return $this->success([]);
@ -248,6 +266,7 @@ class ActivityPubController extends Controller {
try {
$actor = $this->actorService->getActor($username);
$followers = $this->followService->getFollowers($actor);
// $followers->setTopLevel(true);
return $this->directSuccess($followers);

Wyświetl plik

@ -31,6 +31,7 @@ namespace OCA\Social\Db;
use DateTime;
use OCA\Social\Exceptions\NoteNotFoundException;
use OCA\Social\Model\ActivityPub\Note;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ConfigService;
@ -61,52 +62,72 @@ class NotesRequest extends NotesRequestBuilder {
* @param Note $note
*
* @return int
* @throws \Exception
*/
public function save(Note $note): int {
try {
$dTime = new DateTime();
$dTime->setTimestamp($note->getPublishedTime());
$dTime = new DateTime();
$dTime->setTimestamp($note->getPublishedTime());
$qb = $this->getNotesInsertSql();
$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->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();
$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();
}
}

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class NoteNotFoundException extends \Exception {
}

Wyświetl plik

@ -0,0 +1,82 @@
<?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/>.
*
*/
namespace OCA\Social\Model\ActivityPub\Activity;
use JsonSerializable;
use OCA\Social\Model\ActivityPub\ACore;
/**
* Class Delete
*
* @package OCA\Social\Model\ActivityPub\Activity
*/
class Delete extends ACore implements JsonSerializable {
const TYPE = 'Delete';
/**
* Activity constructor.
*
* @param ACore $parent
*/
public function __construct($parent = null) {
parent::__construct($parent);
$this->setType(self::TYPE);
}
/**
* @param array $data
*/
public function import(array $data) {
parent::import($data);
$this->setActorId($this->get('actor', $data, ''));
}
/**
* @return array
*/
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
]
);
}
}

Wyświetl plik

@ -0,0 +1,82 @@
<?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/>.
*
*/
namespace OCA\Social\Model\ActivityPub\Activity;
use JsonSerializable;
use OCA\Social\Model\ActivityPub\ACore;
/**
* Class Tombstone
*
* @package OCA\Social\Model\ActivityPub\Activity
*/
class Tombstone extends ACore implements JsonSerializable {
const TYPE = 'Tombstone';
/**
* Undo constructor.
*
* @param ACore $parent
*/
public function __construct($parent = null) {
parent::__construct($parent);
$this->setType(self::TYPE);
}
/**
* @param array $data
*/
public function import(array $data) {
parent::import($data);
}
/**
* @return array
*/
public function jsonSerialize(): array {
return array_merge(
parent::jsonSerialize(),
[
]
);
}
}

Wyświetl plik

@ -0,0 +1,127 @@
<?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/>.
*
*/
namespace OCA\Social\Service\ActivityPub;
use Exception;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\UnknownItemException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Delete;
use OCA\Social\Service\ActivityService;
use OCA\Social\Service\ICoreService;
use OCA\Social\Service\MiscService;
class DeleteService implements ICoreService {
/** @var NotesRequest */
private $notesRequest;
/** @var ActivityService */
private $activityService;
/** @var NoteService */
private $noteService;
/** @var MiscService */
private $miscService;
/**
* UndoService constructor.
*
* @param NotesRequest $notesRequest
* @param ActivityService $activityService
* @param NoteService $noteService
* @param MiscService $miscService
*/
public function __construct(
NotesRequest $notesRequest, ActivityService $activityService, NoteService $noteService,
MiscService $miscService
) {
$this->notesRequest = $notesRequest;
$this->activityService = $activityService;
$this->noteService = $noteService;
$this->miscService = $miscService;
}
/**
* @param ACore $delete
*
* @throws UnknownItemException
*/
public function parse(ACore $delete) {
if ($delete->gotObject()) {
$id = $delete->getObject()
->getId();
} else {
$id = $delete->getObjectId();
}
/** @var Delete $delete */
try {
$item = $this->activityService->getItem($id);
switch ($item->getType()) {
case 'Note':
$service = $this->noteService;
break;
default:
throw new UnknownItemException();
}
try {
$service->delete($item);
} catch (Exception $e) {
$this->miscService->log(
2, 'Cannot delete ' . $delete->getType() . ': ' . $e->getMessage()
);
}
} catch (InvalidResourceException $e) {
}
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
}
}

Wyświetl plik

@ -179,7 +179,7 @@ class FollowService implements ICoreService {
*
* @throws Exception
*/
public function save(ACore $follow) {
public function parse(ACore $follow) {
/** @var Follow $follow */
if ($follow->isRoot()) {
$follow->verify($follow->getActorId());
@ -214,5 +214,11 @@ class FollowService implements ICoreService {
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
}
}

Wyświetl plik

@ -33,7 +33,9 @@ namespace OCA\Social\Service\ActivityPub;
use Exception;
use OC\User\NoUserException;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\NoteNotFoundException;
use OCA\Social\Exceptions\RequestException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Model\ActivityPub\ACore;
@ -226,9 +228,9 @@ class NoteService implements ICoreService {
*
* @param ACore $note
*
* @throws Exception
* @throws ActivityCantBeVerifiedException
*/
public function save(ACore $note) {
public function parse(ACore $note) {
/** @var Note $note */
if ($note->isRoot()) {
return;
@ -241,9 +243,21 @@ class NoteService implements ICoreService {
if ($parent->getType() === Create::TYPE) {
$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());
}

Wyświetl plik

@ -93,7 +93,7 @@ class PersonService implements ICoreService {
try {
$actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES));
$this->save($actor, true);
$this->parse($actor, true);
} catch (Exception $e) {
}
}
@ -138,7 +138,7 @@ class PersonService implements ICoreService {
}
try {
$this->save($actor);
$this->parse($actor);
} catch (Exception $e) {
throw new InvalidResourceException($e->getMessage());
}
@ -174,7 +174,7 @@ class PersonService implements ICoreService {
}
try {
$this->save($actor);
$this->parse($actor);
} catch (Exception $e) {
throw new InvalidResourceException($e->getMessage());
}
@ -202,10 +202,21 @@ class PersonService implements ICoreService {
*
* @throws Exception
*/
public function save(ACore $person, bool $local = false) {
public function parse(ACore $person, bool $local = false) {
/** @var Person $person */
if ($person->isRoot() === false) {
return;
}
$this->cacheActorsRequest->save($person, $local);
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
}
}

Wyświetl plik

@ -59,7 +59,14 @@ class UndoService implements ICoreService {
*
* @throws Exception
*/
public function save(ACore $undo) {
public function parse(ACore $undo) {
}
/**
* @param ACore $item
*/
public function delete(ACore $item) {
}
}

Wyświetl plik

@ -35,6 +35,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
use Exception;
use OCA\Social\Db\ActorsRequest;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\RequestException;
@ -65,6 +66,9 @@ class ActivityService {
/** @var ActorsRequest */
private $actorsRequest;
/** @var NotesRequest */
private $notesRequest;
/** @var ActorService */
private $actorService;
@ -88,6 +92,7 @@ class ActivityService {
* ActivityService constructor.
*
* @param ActorsRequest $actorsRequest
* @param NotesRequest $notesRequest
* @param CurlService $curlService
* @param ActorService $actorService
* @param PersonService $personService
@ -96,13 +101,15 @@ class ActivityService {
* @param MiscService $miscService
*/
public function __construct(
ActorsRequest $actorsRequest, CurlService $curlService, ActorService $actorService,
ActorsRequest $actorsRequest, NotesRequest $notesRequest, CurlService $curlService,
ActorService $actorService,
PersonService $personService, InstanceService $instanceService,
ConfigService $configService,
MiscService $miscService
) {
$this->curlService = $curlService;
$this->actorsRequest = $actorsRequest;
$this->notesRequest = $notesRequest;
$this->actorService = $actorService;
$this->personService = $personService;
$this->instanceService = $instanceService;
@ -111,12 +118,6 @@ class ActivityService {
}
public function test() {
}
/**
* @param Person $actor
* @param ACore $item
@ -126,6 +127,7 @@ class ActivityService {
* @return array
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function createActivity(Person $actor, ACore $item, int $type, ACore &$activity = null
): 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 int $type
*
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function manageRequest(ACore $activity, int $type) {
$result = $this->request($activity, $type);
@ -175,6 +206,7 @@ class ActivityService {
* @return array
* @throws RequestException
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
public function request(ACore &$activity, int $type) {
$this->setupCore($activity);
@ -206,7 +238,7 @@ class ActivityService {
): array {
$document = json_encode($activity);
$date = gmdate(self::DATE_FORMAT);
$localActor = $this->getActorFromActivity($activity);
$localActor = $this->getActorFromItem($activity);
$localActorLink =
$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
*
@ -305,7 +294,7 @@ class ActivityService {
* @throws SocialAppConfigException
* @throws ActorDoesNotExistException
*/
private function getActorFromActivity(Acore $activity): Person {
private function getActorFromItem(Acore $activity): Person {
if ($activity->gotActor()) {
return $activity->getActor();
}
@ -413,7 +402,7 @@ class ActivityService {
$coreService = $activity->savingAs();
if ($coreService !== null) {
$coreService->save($activity);
$coreService->parse($activity);
}
if ($activity->gotObject()) {

Wyświetl plik

@ -27,13 +27,32 @@ declare(strict_types=1);
*
*/
namespace OCA\Social\Service;
use OCA\Social\Model\ActivityPub\ACore;
/**
* Interface ICoreService
*
* @package OCA\Social\Service
*/
interface ICoreService {
public function save(ACore $core);
/**
* @param ACore $item
*/
public function parse(ACore $item);
/**
* @param ACore $item
*/
public function delete(ACore $item);
}

Wyświetl plik

@ -37,10 +37,13 @@ use OCA\Social\Exceptions\UnknownItemException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Accept;
use OCA\Social\Model\ActivityPub\Activity\Create;
use OCA\Social\Model\ActivityPub\Activity\Delete;
use OCA\Social\Model\ActivityPub\Activity\Reject;
use OCA\Social\Model\ActivityPub\Activity\Tombstone;
use OCA\Social\Model\ActivityPub\Follow;
use OCA\Social\Model\ActivityPub\Note;
use OCA\Social\Model\ActivityPub\Activity\Undo;
use OCA\Social\Service\ActivityPub\DeleteService;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityPub\NoteService;
use OCA\Social\Service\ActivityPub\UndoService;
@ -61,6 +64,9 @@ class ImportService {
/** @var FollowService */
private $followService;
/** @var DeleteService */
private $deleteService;
/** @var MiscService */
private $miscService;
@ -71,15 +77,18 @@ class ImportService {
* @param NoteService $noteService
* @param UndoService $undoService
* @param FollowService $followService
* @param DeleteService $deleteService
* @param MiscService $miscService
*/
public function __construct(
NoteService $noteService, UndoService $undoService, FollowService $followService,
DeleteService $deleteService,
MiscService $miscService
) {
$this->noteService = $noteService;
$this->undoService = $undoService;
$this->followService = $followService;
$this->deleteService = $deleteService;
$this->miscService = $miscService;
}
@ -113,6 +122,14 @@ class ImportService {
$item = new Create($root);
break;
case Delete::TYPE:
$item = new Delete($root);
break;
case Tombstone::TYPE:
$item = new Tombstone($root);
break;
case Note::TYPE:
$item = new Note($root);
break;
@ -155,10 +172,13 @@ class ImportService {
*
* @throws UnknownItemException
*/
public function save(Acore $activity) {
public function parse(Acore $activity) {
if ($activity->gotObject()) {
$this->save($activity->getObject());
try {
$this->parse($activity->getObject());
} catch (UnknownItemException $e) {
}
}
switch ($activity->getType()) {
@ -166,6 +186,10 @@ class ImportService {
// $service = $this;
// break;
case Delete::TYPE:
$service = $this->deleteService;
break;
// case Undo::TYPE:
// $service = $this->undoService;
// break;
@ -191,10 +215,10 @@ class ImportService {
}
try {
$service->save($activity);
$service->parse($activity);
} catch (Exception $e) {
$this->miscService->log(
2, 'Cannot save ' . $activity->getType() . ': ' . $e->getMessage()
2, 'Cannot parse ' . $activity->getType() . ': ' . $e->getMessage()
);
}
}