Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
send mails to inherited members on GS
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl authored and backportbot[bot] committed Jul 28, 2021
commit 7ac5b3348959e2bb5ee2bdf322fb75f2c3c904b5
17 changes: 15 additions & 2 deletions lib/Listeners/Files/CreatingShareSendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
use Exception;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Events\Files\CreatingFileShareEvent;
use OCA\Circles\Exceptions\FederatedItemException;
use OCA\Circles\Exceptions\RemoteInstanceException;
use OCA\Circles\Exceptions\RemoteNotFoundException;
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\ShareWrapperNotFoundException;
use OCA\Circles\Exceptions\UnknownRemoteException;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\ShareWrapper;
use OCA\Circles\Service\ConfigService;
Expand Down Expand Up @@ -94,6 +100,13 @@ public function __construct(

/**
* @param Event $event
*
* @throws FederatedItemException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws UnknownRemoteException
*/
public function handle(Event $event): void {
if (!$event instanceof CreatingFileShareEvent) {
Expand All @@ -105,7 +118,7 @@ public function handle(Event $event): void {
$federatedEvent = $event->getFederatedEvent();

$result = [];
foreach ($circle->getInheritedMembers() as $member) {
foreach ($circle->getInheritedMembers(false, true) as $member) {
if ($member->getUserType() !== Member::TYPE_MAIL
&& $member->getUserType() !== Member::TYPE_CONTACT) {
continue;
Expand Down Expand Up @@ -136,6 +149,6 @@ public function handle(Event $event): void {
];
}

$event->getFederatedEvent()->setResultEntry('shares', $result);
$event->getFederatedEvent()->setResultEntry('info', $result);
}
}
8 changes: 7 additions & 1 deletion lib/Listeners/Files/MemberAddedSendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ public function handle(Event $event): void {
// }
}

$this->sendMailService->generateMail($circle, $member, $shares, $mails);
if ($member->hasInvitedBy()) {
$author = $member->getInvitedBy()->getDisplayName();
} else {
$author = 'someone';
}

$this->sendMailService->generateMail($author, $circle, $member, $shares, $mails);
}
}
}
43 changes: 28 additions & 15 deletions lib/Listeners/Files/ShareCreatedSendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use OCA\Circles\Model\ShareWrapper;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\ContactService;
use OCA\Circles\Service\RemoteStreamService;
use OCA\Circles\Service\SendMailService;
use OCA\Circles\Service\ShareTokenService;
use OCA\Circles\Service\ShareWrapperService;
Expand All @@ -68,6 +69,9 @@ class ShareCreatedSendMail implements IEventListener {
/** @var ShareTokenService */
private $shareTokenService;

/** @var RemoteStreamService */
private $remoteStreamService;

/** @var SendMailService */
private $sendMailService;

Expand All @@ -83,19 +87,22 @@ class ShareCreatedSendMail implements IEventListener {
*
* @param ShareWrapperService $shareWrapperService
* @param ShareTokenService $shareTokenService
* @param RemoteStreamService $remoteStreamService
* @param SendMailService $sendMailService
* @param ContactService $contactService
* @param ConfigService $configService
*/
public function __construct(
ShareWrapperService $shareWrapperService,
ShareTokenService $shareTokenService,
RemoteStreamService $remoteStreamService,
SendMailService $sendMailService,
ContactService $contactService,
ConfigService $configService
) {
$this->shareWrapperService = $shareWrapperService;
$this->shareTokenService = $shareTokenService;
$this->remoteStreamService = $remoteStreamService;
$this->sendMailService = $sendMailService;
$this->contactService = $contactService;
$this->configService = $configService;
Expand All @@ -109,7 +116,6 @@ public function __construct(
*
* @throws FederatedItemException
* @throws RemoteInstanceException
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws RequestBuilderException
* @throws UnknownRemoteException
Expand All @@ -121,7 +127,7 @@ public function handle(Event $event): void {

$circle = $event->getCircle();

foreach ($circle->getInheritedMembers(false, false) as $member) {
foreach ($circle->getInheritedMembers(false, true) as $member) {
if ($member->getUserType() !== Member::TYPE_MAIL
&& $member->getUserType() !== Member::TYPE_CONTACT) {
continue;
Expand All @@ -130,30 +136,37 @@ public function handle(Event $event): void {
$mails = [];
$share = null;
foreach ($event->getResults() as $origin => $item) {
$shares = $item->gData('shares');
if (!$shares->hasKey($member->getId())) {
$info = $item->gData('info');
if (!$info->hasKey($member->getId())) {
continue;
}

$data = $shares->gData($member->getId());

// TODO: this must be run even if there is no shares, and only if the $origin === $member->getInstance() and its aliases
$mails = array_merge($mails, $data->gArray('mails'));
$data = $info->gData($member->getId());
try {
if (($this->configService->isLocalInstance($member->getInstance())
&& $this->configService->isLocalInstance($origin))
|| $this->remoteStreamService->isFromSameInstance($origin, $member->getInstance())) {
$mails = $data->gArray('mails');
}
} catch (RemoteNotFoundException $e) {
continue;
}

// TODO: is it safe to use $origin to compare getInstance() ?
// TODO: do we need to check the $origin ?
// TODO: Solution would be to check the origin based on aliases using RemoteInstanceService
// if ($member->getUserType() === Member::TYPE_CONTACT && $member->getInstance() === $origin) {
try {
/** @var ShareWrapper $share */
$share = $data->gObj('share', ShareWrapper::class);
} catch (Exception $e) {
}

// }
}

if (!is_null($share)) {
$this->sendMailService->generateMail($circle, $member, [$share], $mails);
if ($share->hasInitiator()) {
$author = $share->getInitiator()->getDisplayName();
} else {
$author = 'someone';
}

$this->sendMailService->generateMail($author, $circle, $member, [$share], $mails);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions lib/Service/RemoteStreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,41 @@ public function addRemoteInstance(
}


/**
* @param string $address
*
* @return RemoteInstance
* @throws RemoteNotFoundException
*/
public function getRemoteInstanceFromAddress(string $address): RemoteInstance {
$remotes = $this->remoteRequest->getAllInstances();
foreach ($remotes as $remote) {
if ($remote->getInstance() === $address || in_array($address, $remote->getAliases())) {
return $remote;
}
}

throw new RemoteNotFoundException();
}


/**
* @param string $instance
* @param string $check
*
* @return bool
* @throws RemoteNotFoundException
*/
public function isFromSameInstance(string $instance, string $check): bool {
$remote = $this->getRemoteInstanceFromAddress($instance);
if ($remote->getInstance() === $check || in_array($check, $remote->getAliases())) {
return true;
}

return false;
}


/**
* Confirm the Auth of a RemoteInstance, based on the result from a request
*
Expand Down
15 changes: 5 additions & 10 deletions lib/Service/SendMailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ public function __construct(


/**
* @param string $author
* @param Circle $circle
* @param Member $member
* @param ShareWrapper[] $shares
* @param array $mails
*/
public function generateMail(Circle $circle, Member $member, array $shares, array $mails): void {
public function generateMail(string $author, Circle $circle, Member $member, array $shares, array $mails): void {
if (empty($shares)) {
return;
}
Expand All @@ -96,12 +97,6 @@ public function generateMail(Circle $circle, Member $member, array $shares, arra
return;
}

if ($member->hasInvitedBy()) {
$invitedBy = $member->getInvitedBy()->getDisplayName();
} else {
$invitedBy = 'someone';
}

$links = [];
foreach ($shares as $share) {
$links[] = [
Expand All @@ -111,15 +106,15 @@ public function generateMail(Circle $circle, Member $member, array $shares, arra
}

$template = $this->generateMailExitingShares(
$invitedBy,
$author,
$circle->getDisplayName(),
sizeof($links) > 1
);

$this->fillMailExistingShares($template, $links);
foreach ($mails as $mail) {
try {
$this->sendMailExistingShares($template, $invitedBy, $mail, sizeof($links) > 1);
$this->sendMailExistingShares($template, $author, $mail, sizeof($links) > 1);
} catch (Exception $e) {
}
}
Expand Down