fixing verification on source

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/37/head
Maxence Lange 2018-11-19 11:10:24 -01:00
rodzic 596669104e
commit 31b729f5d4
4 zmienionych plików z 59 dodań i 27 usunięć

Wyświetl plik

@ -33,7 +33,6 @@ namespace OCA\Social\Db;
use OCA\Social\Exceptions\FollowDoesNotExistException; use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Model\ActivityPub\Follow; use OCA\Social\Model\ActivityPub\Follow;
use OCA\Social\Model\ActivityPub\Person;
/** /**
@ -74,22 +73,22 @@ class FollowsRequest extends FollowsRequestBuilder {
/** /**
* @param Person $actor * @param string $actorId
* @param Person $remote * @param string $remoteActorId
* *
* @return Follow * @return Follow
* @throws FollowDoesNotExistException * @throws FollowDoesNotExistException
*/ */
public function getByPersons(Person $actor, Person $remote) { public function getByPersons(string $actorId, string $remoteActorId) {
$qb = $this->getFollowsSelectSql(); $qb = $this->getFollowsSelectSql();
$this->limitToActorId($qb, $actor->getId()); $this->limitToActorId($qb, $actorId);
$this->limitToObjectId($qb, $remote->getId()); $this->limitToObjectId($qb, $remoteActorId);
$cursor = $qb->execute(); $cursor = $qb->execute();
$data = $cursor->fetch(); $data = $cursor->fetch();
$cursor->closeCursor(); $cursor->closeCursor();
if ($data === false) { if ($data === false) {
$this->miscService->log('does not exisst ?');
throw new FollowDoesNotExistException(); throw new FollowDoesNotExistException();
} }
@ -108,5 +107,17 @@ class FollowsRequest extends FollowsRequestBuilder {
} }
/**
* @param Follow $follow
*/
public function deleteByPersons(Follow $follow) {
$qb = $this->getFollowsDeleteSql();
$this->limitToActorId($qb, $follow->getActorId());
$this->limitToObjectId($qb, $follow->getObjectId());
$qb->execute();
}
} }

Wyświetl plik

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

Wyświetl plik

@ -32,6 +32,7 @@ namespace OCA\Social\Model\ActivityPub;
use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TArrayTools;
use JsonSerializable; use JsonSerializable;
use OCA\Social\Exceptions\ActivityCantBeVerifiedException;
use OCA\Social\Model\InstancePath; use OCA\Social\Model\InstancePath;
use OCA\Social\Service\ICoreService; use OCA\Social\Service\ICoreService;
@ -184,20 +185,23 @@ abstract class ACore implements JsonSerializable {
/** /**
* @param string $url * @param string $url
* *
* @return bool * @throws ActivityCantBeVerifiedException
*/ */
public function verify(string $url): bool { public function verify(string $url) {
if (parse_url($this->getId(), PHP_URL_HOST) !== $url1 = parse_url($this->getId());
parse_url($url, PHP_URL_HOST)) $url2 = parse_url($url);
return false;
\OC::$server->getLogger()->log(2, '#### ' . json_encode(parse_url($this->getId(), PHP_URL_PORT))); if ($this->get('host', $url1, '1') !== $this->get('host', $url2, '2')) {
// if (parse_url($this->getId(), PHP_URL_PORT) !== throw new ActivityCantBeVerifiedException('activity cannot be verified');
// parse_url($url, PHP_URL_HOST)) }
// return false;
//
return true; if ($this->get('scheme', $url1, '1') !== $this->get('scheme', $url2, '2')) {
throw new ActivityCantBeVerifiedException('activity cannot be verified');
}
if ($this->getInt('port', $url1, 1) !== $this->getInt('port', $url2, 1)) {
throw new ActivityCantBeVerifiedException('activity cannot be verified');
}
} }

Wyświetl plik

@ -106,8 +106,9 @@ class FollowService implements ICoreService {
$follow->setObjectId($remoteActor->getId()); $follow->setObjectId($remoteActor->getId());
try { try {
$this->followsRequest->getByPersons($actor, $remoteActor); $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId());
} catch (FollowDoesNotExistException $e) { } catch (FollowDoesNotExistException $e) {
$this->miscService->log('CREATE NEW ONE !');
$this->followsRequest->save($follow); $this->followsRequest->save($follow);
$follow->addInstancePath(new InstancePath($remoteActor->getInbox())); $follow->addInstancePath(new InstancePath($remoteActor->getInbox()));
@ -126,7 +127,7 @@ class FollowService implements ICoreService {
$remoteActor = $this->personService->getFromAccount($account); $remoteActor = $this->personService->getFromAccount($account);
try { try {
$follow = $this->followsRequest->getByPersons($actor, $remoteActor); $follow = $this->followsRequest->getByPersons($actor->getId(), $remoteActor->getId());
$this->followsRequest->delete($follow); $this->followsRequest->delete($follow);
} catch (FollowDoesNotExistException $e) { } catch (FollowDoesNotExistException $e) {
} }
@ -182,23 +183,31 @@ class FollowService implements ICoreService {
/** @var Follow $follow */ /** @var Follow $follow */
if ($follow->isRoot()) { if ($follow->isRoot()) {
$this->followsRequest->save($follow); $follow->verify($follow->getActorId());
$this->confirmFollowRequest($follow); try {
$this->followsRequest->getByPersons($follow->getActorId(), $follow->getObjectId());
} catch (FollowDoesNotExistException $e) {
$this->followsRequest->save($follow);
$this->confirmFollowRequest($follow);
}
} else { } else {
$parent = $follow->getParent(); $parent = $follow->getParent();
if ($parent->isRoot() === false) { if ($parent->isRoot() === false) {
return; return;
} }
if ($parent->getType() === Undo::TYPE && $parent->verify($follow->getActorId())) { if ($parent->getType() === Undo::TYPE) {
$this->followsRequest->delete($follow); $parent->verify($follow->getActorId());
$this->followsRequest->deleteByPersons($follow);
} }
if ($parent->getType() === Reject::TYPE && $parent->verify($follow->getObjectId())) { if ($parent->getType() === Reject::TYPE) {
$this->followsRequest->delete($follow); $parent->verify($follow->getObjectId());
$this->followsRequest->deleteByPersons($follow);
} }
if ($parent->getType() === Accept::TYPE && $parent->verify($follow->getObjectId())) { if ($parent->getType() === Accept::TYPE) {
$parent->verify($follow->getObjectId());
$this->followsRequest->accepted($follow); $this->followsRequest->accepted($follow);
} }