Skip to content

Commit

Permalink
Merge pull request #729 from nextcloud/backport/728/stable22
Browse files Browse the repository at this point in the history
[stable22] Sending mail on new shares
  • Loading branch information
ArtificialOwl authored Jul 23, 2021
2 parents 63fa003 + 5b05833 commit 3d31633
Show file tree
Hide file tree
Showing 14 changed files with 660 additions and 117 deletions.
29 changes: 24 additions & 5 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use OC;
use OCA\Circles\Events\AddingCircleMemberEvent;
use OCA\Circles\Events\CircleMemberAddedEvent;
use OCA\Circles\Events\Files\CreatingFileShareEvent;
use OCA\Circles\Events\Files\FileShareCreatedEvent;
use OCA\Circles\Events\MembershipsCreatedEvent;
use OCA\Circles\Events\MembershipsRemovedEvent;
use OCA\Circles\Events\RemovingCircleMemberEvent;
Expand All @@ -48,9 +50,11 @@
use OCA\Circles\Listeners\Examples\ExampleMembershipsCreated;
use OCA\Circles\Listeners\Examples\ExampleMembershipsRemoved;
use OCA\Circles\Listeners\Examples\ExampleRequestingCircleMember;
use OCA\Circles\Listeners\Files\AddingMember as ListenerFilesAddingMember;
use OCA\Circles\Listeners\Files\MemberAdded as ListenerFilesMemberAdded;
use OCA\Circles\Listeners\Files\AddingMemberSendMail as ListenerFilesAddingMemberSendMail;
use OCA\Circles\Listeners\Files\CreatingShareSendMail as ListenerFilesCreatingShareSendMail;
use OCA\Circles\Listeners\Files\MemberAddedSendMail as ListenerFilesMemberAddedSendMail;
use OCA\Circles\Listeners\Files\RemovingMember as ListenerFilesRemovingMember;
use OCA\Circles\Listeners\Files\ShareCreatedSendMail as ListenerFilesShareCreatedSendMail;
use OCA\Circles\Listeners\GroupCreated;
use OCA\Circles\Listeners\GroupDeleted;
use OCA\Circles\Listeners\GroupMemberAdded;
Expand Down Expand Up @@ -137,11 +141,26 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserRemovedEvent::class, GroupMemberRemoved::class);

// Local Events (for Files/Shares/Notifications management)
$context->registerEventListener(AddingCircleMemberEvent::class, ListenerFilesAddingMember::class);
$context->registerEventListener(CircleMemberAddedEvent::class, ListenerFilesMemberAdded::class);
$context->registerEventListener(
AddingCircleMemberEvent::class,
ListenerFilesAddingMemberSendMail::class
);
$context->registerEventListener(
CircleMemberAddedEvent::class,
ListenerFilesMemberAddedSendMail::class
);
$context->registerEventListener(
CreatingFileShareEvent::class,
ListenerFilesCreatingShareSendMail::class
);
$context->registerEventListener(
FileShareCreatedEvent::class,
ListenerFilesShareCreatedSendMail::class
);
$context->registerEventListener(RemovingCircleMemberEvent::class, ListenerFilesRemovingMember::class);
$context->registerEventListener(
RequestingCircleMemberEvent::class, ListenerNotificationsRequestingMember::class
RequestingCircleMemberEvent::class,
ListenerNotificationsRequestingMember::class
);

// It seems that AccountManager use deprecated dispatcher, let's use a deprecated listener
Expand Down
81 changes: 81 additions & 0 deletions lib/Events/Files/CreatingFileShareEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php


declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* 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 2021
* @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\Circles\Events\Files;

use OCA\Circles\Events\CircleGenericEvent;
use OCA\Circles\Model\Federated\FederatedEvent;
use OCA\Circles\Model\Mount;

/**
* Class CreatingFileShareEvent
*
* @package OCA\Circles\Events\Files
*/
class CreatingFileShareEvent extends CircleGenericEvent {


/** @var Mount */
private $mount;


/**
* CreatingFileShareEvent constructor.
*
* @param FederatedEvent $federatedEvent
*/
public function __construct(FederatedEvent $federatedEvent) {
parent::__construct($federatedEvent);
}


/**
* @param Mount $mount
*/
public function setMount(Mount $mount): void {
$this->mount = $mount;
}

/**
* @return Mount
*/
public function getMount(): Mount {
return $this->mount;
}

/**
* @return bool
*/
public function hasMount(): bool {
return !is_null($this->mount);
}
}
55 changes: 55 additions & 0 deletions lib/Events/Files/FileShareCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* 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 2021
* @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\Circles\Events\Files;

use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
use OCA\Circles\Events\CircleResultGenericEvent;
use OCA\Circles\Model\Federated\FederatedEvent;

/**
* Class CreatingFileShareEvent
*
* @package OCA\Circles\Events\Files
*/
class FileShareCreatedEvent extends CircleResultGenericEvent {


/**
* FileShareCreatedEvent constructor.
*
* @param FederatedEvent $federatedEvent
* @param SimpleDataStore[] $result
*/
public function __construct(FederatedEvent $federatedEvent, array $result) {
parent::__construct($federatedEvent, $result);
}
}
26 changes: 17 additions & 9 deletions lib/FederatedItems/Files/FileShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class FileShare implements
* FileShare constructor.
*
* @param MountRequest $mountRequest
* @param EventService $eventService
* @param ConfigService $configService
*/
public function __construct(
Expand Down Expand Up @@ -105,18 +106,23 @@ public function verify(FederatedEvent $event): void {
* @throws CircleNotFoundException
*/
public function manage(FederatedEvent $event): void {
if ($this->configService->isLocalInstance($event->getOrigin())) {
return;
$mount = null;
if (!$this->configService->isLocalInstance($event->getOrigin())) {
/** @var ShareWrapper $wrappedShare */
$wrappedShare = $event->getParams()->gObj('wrappedShare', ShareWrapper::class);
$mount = new Mount();
$mount->fromShare($wrappedShare);
$mount->setMountId($this->token(15));

$this->mountRequest->save($mount);
}

/** @var ShareWrapper $wrappedShare */
$wrappedShare = $event->getParams()->gObj('wrappedShare', ShareWrapper::class);
$mount = new Mount();
$mount->fromShare($wrappedShare);
$mount->setMountId($this->token(15));
$this->eventService->fileShareCreating($event, $mount);

$this->mountRequest->save($mount);
$this->eventService->federatedShareCreated($wrappedShare, $mount);
// $this->eventService->federatedShareCreated($wrappedShare, $mount);


// $this->eventService->fileSharing($event);

// $this->mountRequest->create($mount);
// $circle = $event->getDeprecatedCircle();
Expand Down Expand Up @@ -172,6 +178,8 @@ public function manage(FederatedEvent $event): void {
* @param array $results
*/
public function result(FederatedEvent $event, array $results): void {
$this->eventService->fileShareCreated($event, $results);

// $event = null;
// $contacts = [];
// foreach (array_keys($events) as $instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Events\AddingCircleMemberEvent;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\ShareTokenAlreadyExistException;
use OCA\Circles\Model\Member;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\ContactService;
Expand All @@ -51,7 +50,7 @@
*
* @package OCA\Circles\Listeners\Files
*/
class AddingMember implements IEventListener {
class AddingMemberSendMail implements IEventListener {
use TStringTools;
use TNC22Logger;

Expand Down Expand Up @@ -126,7 +125,7 @@ public function handle(Event $event): void {
foreach ($shares as $share) {
try {
$shareToken = $this->shareTokenService->generateShareToken($share, $member);
} catch (ShareTokenAlreadyExistException $e) {
} catch (Exception $e) {
continue;
}

Expand All @@ -136,29 +135,10 @@ public function handle(Event $event): void {

$result[$member->getId()] = [
'shares' => $files,
'mails' => $this->getMailAddressesFromContact($member)
'mails' => $this->contactService->getMailAddressesFromMember($member)
];
}

$event->getFederatedEvent()->setResultEntry('files', $result);
}


/**
* @param Member $member
*
* @return array
*/
private function getMailAddressesFromContact(Member $member): array {
if ($member->getUserType() !== Member::TYPE_CONTACT
|| !$this->configService->isLocalInstance($member->getInstance())) {
return [];
}

try {
return $this->contactService->getMailAddresses($member->getUserId());
} catch (Exception $e) {
return [];
}
}
}
Loading

0 comments on commit 3d31633

Please sign in to comment.