Merge pull request #42 from nextcloud-gmbh/delete-local

delete local notes
pull/43/head
Maxence Lange 2018-11-20 21:40:51 -01:00 zatwierdzone przez GitHub
commit f897770c1f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
17 zmienionych plików z 152 dodań i 65 usunięć

Wyświetl plik

@ -232,6 +232,13 @@
<type>timestamp</type>
</field>
<field>
<name>local</name>
<type>boolean</type>
<default>false</default>
<notnull>true</notnull>
</field>
</declaration>
</table>

Wyświetl plik

@ -5,7 +5,7 @@
<name>Social</name>
<summary>🎉 Nextcloud becomes part of the federated social networks!</summary>
<description><![CDATA[test]]></description>
<version>0.0.34</version>
<version>0.0.35</version>
<licence>agpl</licence>
<author mail="maxence@artificial-owl.com">Maxence Lange</author>
<author mail="jus@bitgrid.net">Julius Härtl</author>

Wyświetl plik

@ -36,7 +36,8 @@ return [
['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'],
['name' => 'Local#newPost', 'url' => '/api/v1/post', 'verb' => 'POST'],
['name' => 'Local#postCreate', 'url' => '/api/v1/post', 'verb' => 'POST'],
['name' => 'Local#postDelete', 'url' => '/api/v1/post', 'verb' => 'DELETE'],
['name' => 'Local#timeline', 'url' => '/api/v1/timeline', 'verb' => 'GET'],
['name' => 'Local#direct', 'url' => '/api/v1/direct', 'verb' => 'PUT'],
['name' => 'Local#accountsSearch', 'url' => '/api/v1/accounts/search', 'verb' => 'GET'],

8
composer.lock wygenerowano
Wyświetl plik

@ -12,12 +12,12 @@
"source": {
"type": "git",
"url": "https://github.com/daita/my-small-php-tools.git",
"reference": "d3e752060bde0206e6153b84ec1a7c8ff973fb75"
"reference": "1e70e4c2a6fee89577df1dd37052065e02c4b2e7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/d3e752060bde0206e6153b84ec1a7c8ff973fb75",
"reference": "d3e752060bde0206e6153b84ec1a7c8ff973fb75",
"url": "https://api.github.com/repos/daita/my-small-php-tools/zipball/1e70e4c2a6fee89577df1dd37052065e02c4b2e7",
"reference": "1e70e4c2a6fee89577df1dd37052065e02c4b2e7",
"shasum": ""
},
"require": {
@ -40,7 +40,7 @@
}
],
"description": "My small PHP Tools",
"time": "2018-11-16T10:15:50+00:00"
"time": "2018-11-20T22:24:36+00:00"
}
],
"packages-dev": [],

Wyświetl plik

@ -115,7 +115,7 @@ class AccountController extends Controller {
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}

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\SignatureException;
use OCA\Social\Exceptions\UnknownItemException;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityService;
@ -131,7 +132,7 @@ class ActivityPubController extends Controller {
return $this->directSuccess($actor);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -178,7 +179,7 @@ class ActivityPubController extends Controller {
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -214,7 +215,7 @@ class ActivityPubController extends Controller {
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -271,7 +272,7 @@ class ActivityPubController extends Controller {
return $this->directSuccess($followers);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}

Wyświetl plik

@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\TArrayTools;
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\AppInfo\Application;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Model\Post;
use OCA\Social\Service\ActivityPub\FollowService;
use OCA\Social\Service\ActivityPub\NoteService;
@ -121,7 +122,7 @@ class LocalController extends Controller {
*
* @return DataResponse
*/
public function newPost(array $data): DataResponse {
public function postCreate(array $data): DataResponse {
try {
$post = new Post($this->userId);
$post->setContent($this->get('content', $data, ''));
@ -134,7 +135,38 @@ class LocalController extends Controller {
return $this->success($result);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
/**
* Create a new post.
*
* // TODO: Delete the NoCSRF check
*
* @NoCSRFRequired
* @NoAdminRequired
* @NoSubAdminRequired
*
* @param string $id
*
* @return DataResponse
*/
public function postDelete(string $id): DataResponse {
try {
$note = $this->noteService->getNoteById($id);
$actor = $this->actorService->getActorFromUserId($this->userId);
if ($note->getAttributedTo() !== $actor->getId()) {
throw new InvalidResourceException('user have no rights');
}
$this->noteService->delete($note);
return $this->success();
} catch (Exception $e) {
return $this->fail($e);
}
}
@ -159,7 +191,7 @@ class LocalController extends Controller {
return $this->success($posts);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -178,7 +210,7 @@ class LocalController extends Controller {
return $this->success($posts);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -201,7 +233,7 @@ class LocalController extends Controller {
return $this->success(['accounts' => $accounts]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -225,7 +257,7 @@ class LocalController extends Controller {
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -249,7 +281,7 @@ class LocalController extends Controller {
return $this->success([]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}
@ -272,7 +304,7 @@ class LocalController extends Controller {
return $this->success(['actor' => $actor]);
} catch (Exception $e) {
return $this->fail($e->getMessage());
return $this->fail($e);
}
}

Wyświetl plik

@ -31,7 +31,6 @@ namespace OCA\Social\Db;
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Model\ActivityPub\Cache\CacheActor;
use OCA\Social\Model\ActivityPub\Person;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\MiscService;
@ -58,16 +57,14 @@ class CacheActorsRequest extends CacheActorsRequestBuilder {
* insert cache about an Actor in database.
*
* @param Person $actor
* @param bool $local
*
* @return int
*/
public function save(Person $actor, bool $local = false): int {
public function save(Person $actor): int {
$qb = $this->getCacheActorsInsertSql();
$qb->setValue('id', $qb->createNamedParameter($actor->getId()))
->setValue('account', $qb->createNamedParameter($actor->getAccount()))
->setValue('local', $qb->createNamedParameter(($local) ? '1' : '0'))
->setValue('local', $qb->createNamedParameter(($actor->isLocal()) ? '1' : '0'))
->setValue('following', $qb->createNamedParameter($actor->getFollowing()))
->setValue('followers', $qb->createNamedParameter($actor->getFollowers()))
->setValue('inbox', $qb->createNamedParameter($actor->getInbox()))

Wyświetl plik

@ -88,7 +88,6 @@ class FollowsRequest extends FollowsRequestBuilder {
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
$this->miscService->log('does not exisst ?');
throw new FollowDoesNotExistException();
}

Wyświetl plik

@ -94,6 +94,7 @@ class NotesRequest extends NotesRequestBuilder {
->setValue('attributed_to', $qb->createNamedParameter($note->getAttributedTo()))
->setValue('in_reply_to', $qb->createNamedParameter($note->getInReplyTo()))
->setValue('source', $qb->createNamedParameter($note->getSource()))
->setValue('local', $qb->createNamedParameter(($note->isLocal()) ? '1' : '0'))
->setValue(
'creation',
$qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE)
@ -111,7 +112,7 @@ class NotesRequest extends NotesRequestBuilder {
* @return Note
* @throws NoteNotFoundException
*/
public function getFromId(string $id): Note {
public function getNoteById(string $id): Note {
if ($id === '') {
throw new NoteNotFoundException();
};

Wyświetl plik

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

Wyświetl plik

@ -119,6 +119,10 @@ abstract class ACore implements JsonSerializable {
/** @var null ACore */
private $parent = null;
/** @var bool */
private $local = false;
/**
* Core constructor.
*
@ -562,6 +566,25 @@ abstract class ACore implements JsonSerializable {
}
/**
* @return bool
*/
public function isLocal(): bool {
return $this->local;
}
/**
* @param bool $local
*
* @return Person
*/
public function setLocal(bool $local): ACore {
$this->local = $local;
return $this;
}
/**
* @param ACore $parent
*
@ -656,6 +679,22 @@ abstract class ACore implements JsonSerializable {
return $this;
}
/**
* @param string $k
* @param bool $v
*
* @return ACore
*/
public function addEntryBool(string $k, bool $v): ACore {
if ($v === 0) {
return $this;
}
$this->entries[$k] = $v;
return $this;
}
/**
* @param string $k
* @param array $v
@ -761,6 +800,7 @@ abstract class ACore implements JsonSerializable {
$this->setActorId($this->get('actor', $data, ''));
$this->setObjectId($this->get('object', $data, ''));
$this->setSource($this->get('source', $data, ''));
$this->setLocal(($this->getInt('local', $data, 0) === 1));
}
@ -812,6 +852,8 @@ abstract class ACore implements JsonSerializable {
$this->addEntry('source', $this->getSource());
}
$this->addEntryBool('local', $this->isLocal());
return $this->getEntries();
}

Wyświetl plik

@ -189,7 +189,7 @@ class Note extends ACore implements JsonSerializable {
public function convertPublished() {
$dTime = new DateTime($this->getPublished());
$dTime->format(ActivityService::DATE_FORMAT);
$this->publishedTime = $dTime->getTimestamp();
$this->setPublishedTime($dTime->getTimestamp());
}

Wyświetl plik

@ -84,10 +84,6 @@ class Person extends ACore implements JsonSerializable {
/** @var string */
private $featured = '';
/** @var bool */
private $local = false;
/**
* Person constructor.
*
@ -342,25 +338,6 @@ class Person extends ACore implements JsonSerializable {
}
/**
* @return bool
*/
public function isLocal(): bool {
return $this->local;
}
/**
* @param bool $local
*
* @return Person
*/
public function setLocal(bool $local): Person {
$this->local = $local;
return $this;
}
/**
* @param array $data
*/
@ -377,8 +354,7 @@ class Person extends ACore implements JsonSerializable {
->setFollowing($this->get('following', $data, ''))
->setSharedInbox($this->get('shared_inbox', $data, ''))
->setFeatured($this->get('featured', $data, ''))
->setCreation($this->getInt('creation', $data, 0))
->setLocal(($this->getInt('local', $data, 0) === 1));
->setCreation($this->getInt('creation', $data, 0));
// if ($this->getPreferredUsername() === '') {
// $this->setType('Invalid');
@ -410,8 +386,7 @@ class Person extends ACore implements JsonSerializable {
'id' => $this->getId() . '#main-key',
'owner' => $this->getId(),
'publicKeyPem' => $this->getPublicKey()
],
'local' => $this->isLocal()
]
]
);
}

Wyświetl plik

@ -30,7 +30,6 @@ declare(strict_types=1);
namespace OCA\Social\Service\ActivityPub;
use Exception;
use OC\User\NoUserException;
use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
@ -124,6 +123,8 @@ class NoteService implements ICoreService {
$this->setRecipient($note, $actor, $type);
$note->setContent($content);
$note->convertPublished();
$note->setLocal(true);
$note->saveAs($this);
@ -244,7 +245,7 @@ class NoteService implements ICoreService {
if ($parent->getType() === Create::TYPE) {
$parent->verify(($note->getAttributedTo()));
try {
$this->notesRequest->getFromId($note->getId());
$this->notesRequest->getNoteById($note->getId());
} catch (NoteNotFoundException $e) {
$this->notesRequest->save($note);
}
@ -261,6 +262,17 @@ class NoteService implements ICoreService {
}
/**
* @param string $id
*
* @return Note
* @throws NoteNotFoundException
*/
public function getNoteById(string $id): Note {
return $this->notesRequest->getNoteById($id);
}
/**
* @param string $userId
*

Wyświetl plik

@ -92,8 +92,9 @@ class PersonService implements ICoreService {
}
try {
$actor->setLocal(true);
$actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES));
$this->parse($actor, true);
$this->parse($actor);
} catch (Exception $e) {
}
}
@ -198,17 +199,20 @@ class PersonService implements ICoreService {
* This method is called when saving the Follow object
*
* @param ACore $person
* @param bool $local
*
* @throws Exception
*/
public function parse(ACore $person, bool $local = false) {
public function parse(ACore $person) {
/** @var Person $person */
if ($person->isRoot() === false) {
return;
}
$this->cacheActorsRequest->save($person, $local);
if ($person->getId() === '') {
return;
}
$this->cacheActorsRequest->save($person);
}

Wyświetl plik

@ -30,6 +30,7 @@ declare(strict_types=1);
namespace OCA\Social\Service;
use daita\MySmallPhpTools\Exceptions\MalformedArrayException;
use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Traits\TArrayTools;
use DateTime;
@ -39,6 +40,7 @@ use OCA\Social\Db\NotesRequest;
use OCA\Social\Exceptions\ActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\RequestException;
use OCA\Social\Exceptions\SignatureException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Create;
@ -273,14 +275,17 @@ class ActivityService {
/**
* @param IRequest $request
*
* @throws Exception
* @throws InvalidResourceException
* @throws MalformedArrayException
* @throws RequestException
* @throws SignatureException
*/
public function checkRequest(IRequest $request) {
$dTime = new DateTime($request->getHeader('date'));
$dTime->format(self::DATE_FORMAT);
if ($dTime->getTimestamp() < (time() - self::DATE_DELAY)) {
throw new Exception('object is too old');
throw new SignatureException('object is too old');
}
$this->checkSignature($request);
@ -308,7 +313,10 @@ class ActivityService {
/**
* @param IRequest $request
*
* @throws Exception
* @throws InvalidResourceException
* @throws RequestException
* @throws SignatureException
* @throws MalformedArrayException
*/
private function checkSignature(IRequest $request) {
$signatureHeader = $request->getHeader('Signature');
@ -323,8 +331,8 @@ class ActivityService {
$publicKey = $this->retrieveKey($keyId);
if (openssl_verify($estimated, $signed, $publicKey, 'sha256') !== 1) {
throw new Exception('signature cannot be checked');
if ($publicKey === '' || openssl_verify($estimated, $signed, $publicKey, 'sha256') !== 1) {
throw new SignatureException('signature cannot be checked');
}
}