Unshare user activities

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2016-10-07 12:29:22 +02:00
parent 90578327d5
commit 83d51afab1
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
2 changed files with 80 additions and 4 deletions

View File

@ -22,8 +22,6 @@
namespace OCA\DAV\CalDAV;
use OCP\Activity\IExtension;
use OCP\Activity\IManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
@ -40,6 +38,7 @@ class Activity implements IExtension {
const SUBJECT_ADD = 'calendar_add';
const SUBJECT_UPDATE = 'calendar_update';
const SUBJECT_DELETE = 'calendar_delete';
const SUBJECT_UNSHARE_USER = 'calendar_user_unshare';
/**
* Subject keys for translation of the subjections
@ -143,6 +142,12 @@ class Activity implements IExtension {
return (string) $l->t('%1$s updated calendar %2$s', $params);
case self::SUBJECT_UPDATE . '_self':
return (string) $l->t('You updated calendar %2$s', $params);
case self::SUBJECT_UNSHARE_USER:
return (string) $l->t('%1$s unshared calendar %2$s from you', $params);
case self::SUBJECT_UNSHARE_USER . '_you':
return (string) $l->t('You unshared calendar %2$s from %1$s', $params);
case self::SUBJECT_UNSHARE_USER . '_by':
return (string) $l->t('%1$s unshared calendar %2$s from themselves', $params);
}
return false;
@ -165,6 +170,9 @@ class Activity implements IExtension {
case self::SUBJECT_ADD:
case self::SUBJECT_DELETE:
case self::SUBJECT_UPDATE:
case self::SUBJECT_UNSHARE_USER:
case self::SUBJECT_UNSHARE_USER . '_you':
case self::SUBJECT_UNSHARE_USER . '_by':
return [
0 => 'username',
//1 => 'calendar',

View File

@ -26,6 +26,7 @@
namespace OCA\DAV\CalDAV;
use OCA\DAV\DAV\Sharing\IShareable;
use OCP\Activity\IEvent;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\DAV\DAV\Sharing\Backend;
@ -1658,6 +1659,9 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @param array $remove
*/
public function updateShares($shareable, $add, $remove) {
/** @var Calendar $shareable */
$this->triggerActivitySharing($shareable, $add, $remove);
$this->sharingBackend->updateShares($shareable, $add, $remove);
}
@ -1742,8 +1746,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
return;
}
$principaluri = explode('/', $properties['principaluri']);
$owner = array_pop($principaluri);
$principal = explode('/', $properties['principaluri']);
$owner = $principal[2];
$currentUser = $userSession->getUser();
if ($currentUser instanceof IUser) {
@ -1783,6 +1787,70 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
}
protected function triggerActivitySharing(Calendar $calendar, array $add, array $remove) {
$aM = \OC::$server->getActivityManager();
$userSession = \OC::$server->getUserSession();
$calendarId = $calendar->getResourceId();
//$shares = $this->sharingBackend->getShares($calendarId);
$properties = $this->getCalendarById($calendarId);
$principal = explode('/', $properties['principaluri']);
$owner = $principal[2];
$currentUser = $userSession->getUser();
if ($currentUser instanceof IUser) {
$currentUser = $currentUser->getUID();
} else {
$currentUser = $owner;
}
$event = $aM->generateEvent();
$event->setApp('dav')
->setObject(Activity::CALENDAR, $calendarId)
->setType(Activity::CALENDAR)
->setAuthor($currentUser);
foreach ($remove as $principal) {
// principal:principals/users/test
$parts = explode(':', $principal, 2);
if ($parts[0] !== 'principal') {
continue;
}
$principal = explode('/', $parts[1]);
if ($principal[1] === 'users') {
$this->triggerActivityUnshareUser($principal[2], $event, $properties);
if ($owner !== $principal[2]) {
$event->setAffectedUser($owner)
->setSubject(
$owner === $event->getAuthor() ? Activity::SUBJECT_UNSHARE_USER . '_you' : Activity::SUBJECT_UNSHARE_USER . '_by',
[
$principal[2],
$properties['{DAV:}displayname'],
]
);
$aM->publish($event);
}
}
}
}
protected function triggerActivityUnshareUser($user, IEvent $event, array $properties) {
$aM = \OC::$server->getActivityManager();
$event->setAffectedUser($user)
->setSubject(
$user === $event->getAuthor() ? Activity::SUBJECT_DELETE . '_self' : Activity::SUBJECT_UNSHARE_USER,
[
$event->getAuthor(),
$properties['{DAV:}displayname'],
]
);
$aM->publish($event);
}
/**
* Get all users that have access to a given calendar
*