Skip to content

Commit

Permalink
fixing non-GS issues
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed May 12, 2020
1 parent 17b4565 commit ca6a990
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 178 deletions.
174 changes: 17 additions & 157 deletions lib/GlobalScale/FileShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@
use OC;
use OC\Share20\Share;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Exceptions\GSStatusException;
use OCA\Circles\Exceptions\TokenDoesNotExistException;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\GlobalScale\GSEvent;
use OCA\Circles\Model\GlobalScale\GSShare;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\SharesToken;
use OCA\Circles\Model\SharingFrame;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\MiscService;
use OCP\Files\NotFoundException;
use OCP\IL10N;
Expand Down Expand Up @@ -75,17 +73,8 @@ class FileShare extends AGlobalScaleEvent {
* @param GSEvent $event
* @param bool $localCheck
* @param bool $mustBeChecked
*
* @throws GSStatusException
*/
public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = false): void {
// TODO: might be a bad idea, all process of the sharing should be managed from here.
// Even if we are not in a GS setup.
// The reason is that if a mail needs to be send, all mail address associated to the circle needs to be retrieved
if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
return;
}

// if event/file is local, we generate a federate share for the same circle on other instances
if ($event->getSource() !== $this->configService->getLocalCloudId()) {
return;
Expand Down Expand Up @@ -114,18 +103,9 @@ public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChe

/**
* @param GSEvent $event
*
* @throws GSStatusException
*/
public function manage(GSEvent $event): void {
// TODO: might be a bad idea, all process of the sharing should be managed from here.
// Even if we are not in a GS setup.
// The reason is that if a mail needs to be send, all mail address associated to the circle needs to be retrieved
if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
return;
}

// TODO - if event is local - generate mails sur be sent to TYPE_MAILS
$circle = $event->getCircle();

// if event is not local, we create a federated file to the right instance of Nextcloud, using the right token
if ($event->getSource() !== $this->configService->getLocalCloudId()) {
Expand All @@ -146,9 +126,18 @@ public function manage(GSEvent $event): void {
$gsShare->setMountPoint($filename);

$this->gsSharesRequest->create($gsShare);
} else {
// if the event is local, we send mail to mail-as-members
$members = $this->membersRequest->forceGetMembers(
$circle->getUniqueId(), Member::LEVEL_MEMBER, Member::TYPE_MAIL, true
);

foreach ($members as $member) {
$this->sendShareToContact($event, $circle, $member->getMemberId(), [$member->getUserId()]);
}
}

$circle = $event->getCircle();
// we also fill the event's result for further things, like contact-as-members
$members = $this->membersRequest->forceGetMembers(
$circle->getUniqueId(), Member::LEVEL_MEMBER, Member::TYPE_CONTACT, true
);
Expand All @@ -169,22 +158,13 @@ public function manage(GSEvent $event): void {
*/
public function result(array $events): void {
$event = null;
// $mails = $cloudIds = [];
$contacts = [];
foreach (array_keys($events) as $instance) {
$event = $events[$instance];
$contacts = array_merge(
$contacts, $event->getResult()
->gArray('contacts')
);
// $mails = array_merge(
// $mails, $event->getResult()
// ->gArray('mails')
// );
// $cloudIds = array_merge(
// $cloudIds, $event->getResult()
// ->gArray('cloudIds')
// );
}

if ($event === null || !$event->hasCircle()) {
Expand All @@ -193,51 +173,26 @@ public function result(array $events): void {

$circle = $event->getCircle();

// we check mail address that were already filled
// $mails = $this->getMailAddressFromCircle($circle->getUniqueId());


// foreach ($members as $member) {
// $mails[] = $member->getUserId();
// }


foreach ($contacts as $contact) {
$this->sendShareToContact($event, $circle, $contact);
$this->sendShareToContact($event, $circle, $contact['memberId'], $contact['emails']);
}


// TODO - making this not needed - should force the async on this EVENT as it should be initiated in manage()
// $contacts = $this->membersRequest->forceGetMembers(
// $circle->getUniqueId(), Member::LEVEL_MEMBER, Member::TYPE_CONTACT
// );
//
// foreach ($contacts as $contact) {
// $mails = array_merge($mails, $this->getMailsFromContact($contact->getUserId()));
// $cloudIds = array_merge($cloudIds, $this->getCloudIdsFromContact($contact->getUserId()));
// }
//
// $mails = array_values(array_unique($mails));
// $this->sendShareToMails($event, $mails);

// $cloudIds = array_values(array_unique($cloudIds));
// $this->sendShareToCloudIds($event, $cloudIds);
}


/**
* @param GSEvent $event
* @param Circle $circle
* @param array $contact
* @param string $memberId
* @param array $emails
*/
private function sendShareToContact(GSEvent $event, Circle $circle, array $contact) {
private function sendShareToContact(GSEvent $event, Circle $circle, string $memberId, array $emails) {
$password = '';
if ($this->configService->enforcePasswordProtection()) {
$password = $this->miscService->token(15);
}

try {
$member = $this->membersRequest->forceGetMemberById($contact['memberId']);
$member = $this->membersRequest->forceGetMemberById($memberId);
$share = $this->getShareFromData($event->getData());
} catch (Exception $e) {
return;
Expand All @@ -250,19 +205,9 @@ private function sendShareToContact(GSEvent $event, Circle $circle, array $conta
return;
}

foreach ($contact['emails'] as $mail) {
foreach ($emails as $mail) {
$this->sharedByMail($circle, $share, $mail, $sharesToken, $password);
}

// $mails = [$member->getUserId()];
// if ($member->getType() === Member::TYPE_CONTACT) {
// $mails = $this->getMailsFromContact($member->getUserId());
// }
//
// foreach ($mails as $mail) {
// $this->sharedByMail($circle, $share, $mail, $sharesToken, $password);
// }

}


Expand Down Expand Up @@ -494,91 +439,6 @@ function(Member $member) {
}


//
// /**
// * @param GSEvent $event
// *
// * @throws BroadcasterIsNotCompatibleException
// * ` */
// private function generateFederatedShare(GSEvent $event) {
// $data = $event->getData();
// $frame = SharingFrame::fromJSON(json_encode($data->gAll()));
//
// try {
// $broadcaster = \OC::$server->query((string)$frame->getHeader('broadcast'));
// if (!($broadcaster instanceof IBroadcaster)) {
// throw new BroadcasterIsNotCompatibleException();
// }
//
// $frameCircle = $frame->getCircle();
// $circle = $this->circlesRequest->forceGetCircle($frameCircle->getUniqueId());
// } catch (QueryException | CircleDoesNotExistException $e) {
// return;
// }
//
// $this->feedBroadcaster($broadcaster, $frame, $circle);
// }

//
// /**
// * @param IBroadcaster $broadcaster
// * @param SharingFrame $frame
// * @param Circle $circle
// */
// private function feedBroadcaster(IBroadcaster $broadcaster, SharingFrame $frame, Circle $circle) {
// $broadcaster->init();
//
// if ($circle->getType() !== Circle::CIRCLES_PERSONAL) {
// $broadcaster->createShareToCircle($frame, $circle);
// }
//
// $members =
// $this->membersRequest->forceGetMembers($circle->getUniqueId(), Member::LEVEL_MEMBER, 0, true);
// foreach ($members as $member) {
// $this->parseMember($member);
//
// if ($member->isBroadcasting()) {
// $broadcaster->createShareToMember($frame, $member);
// }
//
// if ($member->getInstance() !== '') {
// $this->miscService->log('#### GENERATE FEDERATED CIRCLES SHARE ' . $member->getInstance());
// }
// }
// }

//
// /**
// * @param Member $member
// */
// private function parseMember(Member &$member) {
// $this->parseMemberFromContact($member);
// }


// /**
// * on Type Contact, we convert the type to MAIL and retrieve the first mail of the list.
// * If no email, we set the member as not broadcasting.
// *
// * @param Member $member
// */
// private function parseMemberFromContact(Member &$member) {
// if ($member->getType() !== Member::TYPE_CONTACT) {
// return;
// }
//
// $contact = MiscService::getContactData($member->getUserId());
// if (!key_exists('EMAIL', $contact)) {
// $member->broadcasting(false);
//
// return;
// }
//
// $member->setType(Member::TYPE_MAIL);
// $member->setUserId(array_shift($contact['EMAIL']));
// }


/**
* @param SimpleDataStore $data
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Search/GlobalScaleUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function search($search) {
try {
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
} catch (GSStatusException $e) {
return;
return [];
}

$request = new Request('/users', Request::TYPE_GET);
Expand Down
3 changes: 1 addition & 2 deletions lib/Search/LocalGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class LocalGroups implements ISearch {
* {@inheritdoc}
*/
public function search($search) {

$result = [];
$groupManager = \OC::$server->getGroupManager();

Expand All @@ -48,4 +47,4 @@ public function search($search) {
return $result;
}

}
}
1 change: 0 additions & 1 deletion lib/Service/BroadcastService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public function __construct(
* @throws Exception
*/
public function broadcastFrame(SharingFrame $frame) {

if ($frame->getHeader('broadcast') === null) {
return;
}
Expand Down
33 changes: 16 additions & 17 deletions lib/Service/GlobalScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
use daita\MySmallPhpTools\Exceptions\RequestServerException;
use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Model\SimpleDataStore;
use daita\MySmallPhpTools\Traits\TRequest;
use daita\MySmallPhpTools\Traits\TStringTools;
use OC;
Expand All @@ -45,7 +44,6 @@
use OCA\Circles\Exceptions\GSKeyException;
use OCA\Circles\Exceptions\GSStatusException;
use OCA\Circles\GlobalScale\AGlobalScaleEvent;
use OCA\Circles\Model\Circle;
use OCA\Circles\Model\GlobalScale\GSEvent;
use OCA\Circles\Model\GlobalScale\GSWrapper;
use OCP\AppFramework\QueryException;
Expand Down Expand Up @@ -104,9 +102,9 @@ public function __construct(
* @throws GSStatusException
*/
public function asyncBroadcast(GSEvent $event): void {
if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
return;
}
// if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
// return;
// }

$wrapper = new GSWrapper();
$wrapper->setEvent($event);
Expand Down Expand Up @@ -197,21 +195,23 @@ public function checkEvent(GSEvent $event): void {
* @param bool $all
*
* @return array
* @throws GSStatusException
*/
public function getInstances(bool $all = false): array {
/** @var string $lookup */
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);

$request = new Request('/instances', Request::TYPE_GET);
$request->setAddressFromUrl($lookup);

try {
$instances = $this->retrieveJson($request);
} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
$request = new Request('/instances', Request::TYPE_GET);
$request->setAddressFromUrl($lookup);

try {
$instances = $this->retrieveJson($request);
} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
$this->miscService->log('Issue while retrieving instances from lookup: ' . $e->getMessage());

return [];
return [];
}
} catch (GSStatusException $e) {
$instances = [$this->configService->getLocalCloudId()];
}

if ($all) {
Expand All @@ -221,6 +221,5 @@ public function getInstances(bool $all = false): array {
return array_diff($instances, $this->configService->getTrustedDomains());
}



}

0 comments on commit ca6a990

Please sign in to comment.