summaryrefslogtreecommitdiffstats
path: root/apps/dav
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2016-10-13 12:16:43 +0200
committerJoas Schilling <coding@schilljs.com>2016-11-03 12:07:58 +0100
commit43b46bcc6aa491ffcaa8ee6d8214285136d250cc (patch)
tree263a6e3e4ddcc42e528556617866fe7a46fce820 /apps/dav
parent07f6747305d23dfb16ad3c63b968f974771aa145 (diff)
downloadnextcloud-server-43b46bcc6aa491ffcaa8ee6d8214285136d250cc.tar.gz
nextcloud-server-43b46bcc6aa491ffcaa8ee6d8214285136d250cc.zip
Activities for events and todos
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/dav')
-rw-r--r--apps/dav/lib/CalDAV/Activity/Backend.php125
-rw-r--r--apps/dav/lib/CalDAV/Activity/Extension.php66
-rw-r--r--apps/dav/lib/CalDAV/CalDavBackend.php4
3 files changed, 189 insertions, 6 deletions
diff --git a/apps/dav/lib/CalDAV/Activity/Backend.php b/apps/dav/lib/CalDAV/Activity/Backend.php
index 9d931e0f559..6e4da340c9a 100644
--- a/apps/dav/lib/CalDAV/Activity/Backend.php
+++ b/apps/dav/lib/CalDAV/Activity/Backend.php
@@ -30,6 +30,7 @@ use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
+use Sabre\VObject\Reader;
/**
* Class Backend
@@ -70,7 +71,7 @@ class Backend {
* @param array $properties
*/
public function addCalendar($calendarId, array $properties) {
- $this->triggerActivity(Extension::SUBJECT_ADD, $calendarId, $properties);
+ $this->triggerCalendarActivity(Extension::SUBJECT_ADD, $calendarId, $properties);
}
/**
@@ -80,7 +81,7 @@ class Backend {
* @param array $properties
*/
public function updateCalendar($calendarId, array $properties) {
- $this->triggerActivity(Extension::SUBJECT_UPDATE, $calendarId, $properties);
+ $this->triggerCalendarActivity(Extension::SUBJECT_UPDATE, $calendarId, $properties);
}
/**
@@ -89,10 +90,9 @@ class Backend {
* @param int $calendarId
*/
public function deleteCalendar($calendarId) {
- $this->triggerActivity(Extension::SUBJECT_DELETE, $calendarId);
+ $this->triggerCalendarActivity(Extension::SUBJECT_DELETE, $calendarId);
}
-
/**
* Creates activities for all related users when a calendar was touched
*
@@ -100,7 +100,7 @@ class Backend {
* @param int $calendarId
* @param array $changedProperties
*/
- protected function triggerActivity($action, $calendarId, array $changedProperties = []) {
+ protected function triggerCalendarActivity($action, $calendarId, array $changedProperties = []) {
$properties = $this->calDavBackend->getCalendarById($calendarId);
if (!isset($properties['principaluri'])) {
return;
@@ -363,6 +363,119 @@ class Backend {
}
/**
+ * Creates activities when a calendar object was created
+ *
+ * @param int $calendarId
+ * @param string $objectUri
+ */
+ public function addCalendarObject($calendarId, $objectUri) {
+ $this->triggerCalendarObjectActivity(Extension::SUBJECT_OBJECT_ADD, $calendarId, $objectUri);
+ }
+
+ /**
+ * Creates activities when a calendar object was updated
+ *
+ * @param int $calendarId
+ * @param string $objectUri
+ */
+ public function updateCalendarObject($calendarId, $objectUri) {
+ $this->triggerCalendarObjectActivity(Extension::SUBJECT_OBJECT_UPDATE, $calendarId, $objectUri);
+ }
+
+ /**
+ * Creates activities when a calendar object was deleted
+ *
+ * @param int $calendarId
+ * @param string $objectUri
+ */
+ public function deleteCalendarObject($calendarId, $objectUri) {
+ $this->triggerCalendarObjectActivity(Extension::SUBJECT_OBJECT_DELETE, $calendarId, $objectUri);
+ }
+
+ /**
+ * Creates activities for all related users when a calendar was touched
+ *
+ * @param string $action
+ * @param int $calendarId
+ * @param string $objectUri
+ */
+ protected function triggerCalendarObjectActivity($action, $calendarId, $objectUri) {
+ $properties = $this->calDavBackend->getCalendarById($calendarId);
+ if (!isset($properties['principaluri'])) {
+ return;
+ }
+
+ $principal = explode('/', $properties['principaluri']);
+ $owner = $principal[2];
+
+ $currentUser = $this->userSession->getUser();
+ if ($currentUser instanceof IUser) {
+ $currentUser = $currentUser->getUID();
+ } else {
+ $currentUser = $owner;
+ }
+
+ $object = $this->getObjectNameAndType($calendarId, $objectUri);
+ $action = $action . '_' . $object['type'];
+
+ if ($object['type'] === 'todo' && strpos($action, Extension::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'COMPLETED') {
+ $action .= '_completed';
+ } else if ($object['type'] === 'todo' && strpos($action, Extension::SUBJECT_OBJECT_UPDATE) === 0 && $object['status'] === 'NEEDS-ACTION') {
+ $action .= '_needs_action';
+ }
+
+ $event = $this->activityManager->generateEvent();
+ $event->setApp('dav')
+ ->setObject(Extension::CALENDAR, $calendarId)
+ ->setType(Extension::CALENDAR)
+ ->setAuthor($currentUser);
+
+ $users = $this->getUsersForCalendar($calendarId);
+ $users[] = $owner;
+
+ foreach ($users as $user) {
+ $event->setAffectedUser($user)
+ ->setSubject(
+ $user === $currentUser ? $action . '_self' : $action,
+ [
+ $currentUser,
+ $properties['{DAV:}displayname'],
+ $object['name'],
+ ]
+ );
+ $this->activityManager->publish($event);
+ }
+ }
+
+ /**
+ * @param int $calendarId
+ * @param string $objectUri
+ * @return string[]|bool
+ */
+ protected function getObjectNameAndType($calendarId, $objectUri) {
+ $data = $this->calDavBackend->getCalendarObject($calendarId, $objectUri);
+
+ $vObject = Reader::read($data['calendardata']);
+ $component = $componentType = null;
+ foreach($vObject->getComponents() as $component) {
+ if (in_array($component->name, ['VEVENT', 'VTODO'])) {
+ $componentType = $component->name;
+ break;
+ }
+ }
+
+ if (!$componentType) {
+ // Calendar objects must have a VEVENT or VTODO component
+ return false;
+ }
+
+ if ($componentType === 'VEVENT') {
+ return ['name' => (string) $component->SUMMARY, 'type' => 'event'];
+ }
+ return ['name' => (string) $component->SUMMARY, 'type' => 'todo', 'status' => (string) $component->STATUS];
+ }
+
+ /**
* Get all users that have access to a given calendar
*
* @param int $calendarId
@@ -391,6 +504,6 @@ class Backend {
}
}
- return $users;
+ return array_unique($users);
}
}
diff --git a/apps/dav/lib/CalDAV/Activity/Extension.php b/apps/dav/lib/CalDAV/Activity/Extension.php
index 9100bb7e184..d3b0871363e 100644
--- a/apps/dav/lib/CalDAV/Activity/Extension.php
+++ b/apps/dav/lib/CalDAV/Activity/Extension.php
@@ -40,6 +40,10 @@ class Extension implements IExtension {
const SUBJECT_UNSHARE_USER = 'calendar_user_unshare';
const SUBJECT_UNSHARE_GROUP = 'calendar_group_unshare';
+ const SUBJECT_OBJECT_ADD = 'object_add';
+ const SUBJECT_OBJECT_UPDATE = 'object_update';
+ const SUBJECT_OBJECT_DELETE = 'object_delete';
+
/** @var IFactory */
protected $languageFactory;
@@ -138,6 +142,7 @@ class Extension 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_SHARE_USER:
return (string) $l->t('%1$s shared calendar %2$s with you', $params);
case self::SUBJECT_SHARE_USER . '_you':
@@ -152,6 +157,7 @@ class Extension implements IExtension {
return (string) $l->t('%3$s unshared calendar %2$s from %1$s', $params);
case self::SUBJECT_UNSHARE_USER . '_self':
return (string) $l->t('%1$s unshared calendar %2$s from themselves', $params);
+
case self::SUBJECT_SHARE_GROUP . '_you':
return (string) $l->t('You shared calendar %2$s with group %1$s', $params);
case self::SUBJECT_SHARE_GROUP . '_by':
@@ -160,6 +166,42 @@ class Extension implements IExtension {
return (string) $l->t('You unshared calendar %2$s from group %1$s', $params);
case self::SUBJECT_UNSHARE_GROUP . '_by':
return (string) $l->t('%3$s unshared calendar %2$s from group %1$s', $params);
+
+ case self::SUBJECT_OBJECT_ADD . '_event':
+ return (string) $l->t('%1$s created event %3$s in calendar %2$s', $params);
+ case self::SUBJECT_OBJECT_ADD . '_event_self':
+ return (string) $l->t('You created event %3$s in calendar %2$s', $params);
+ case self::SUBJECT_OBJECT_DELETE . '_event':
+ return (string) $l->t('%1$s deleted event %3$s from calendar %2$s', $params);
+ case self::SUBJECT_OBJECT_DELETE . '_event_self':
+ return (string) $l->t('You deleted event %3$s from calendar %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_event':
+ return (string) $l->t('%1$s updated event %3$s in calendar %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_event_self':
+ return (string) $l->t('You updated event %3$s in calendar %2$s', $params);
+
+ case self::SUBJECT_OBJECT_ADD . '_todo':
+ return (string) $l->t('%1$s created todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_ADD . '_todo_self':
+ return (string) $l->t('You created todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_DELETE . '_todo':
+ return (string) $l->t('%1$s deleted todo %3$s from list %2$s', $params);
+ case self::SUBJECT_OBJECT_DELETE . '_todo_self':
+ return (string) $l->t('You deleted todo %3$s from list %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_todo':
+ return (string) $l->t('%1$s updated todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
+ return (string) $l->t('You updated todo %3$s in list %2$s', $params);
+
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
+ return (string) $l->t('%1$s solved todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
+ return (string) $l->t('You solved todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
+ return (string) $l->t('%1$s reopened todo %3$s in list %2$s', $params);
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
+ return (string) $l->t('You reopened todo %3$s in list %2$s', $params);
+
}
return false;
@@ -214,6 +256,30 @@ class Extension implements IExtension {
//1 => 'calendar',
2 => 'username',
];
+
+ case self::SUBJECT_OBJECT_ADD . '_event':
+ case self::SUBJECT_OBJECT_ADD . '_event_self':
+ case self::SUBJECT_OBJECT_DELETE . '_event':
+ case self::SUBJECT_OBJECT_DELETE . '_event_self':
+ case self::SUBJECT_OBJECT_UPDATE . '_event':
+ case self::SUBJECT_OBJECT_UPDATE . '_event_self':
+
+ case self::SUBJECT_OBJECT_ADD . '_todo':
+ case self::SUBJECT_OBJECT_ADD . '_todo_self':
+ case self::SUBJECT_OBJECT_DELETE . '_todo':
+ case self::SUBJECT_OBJECT_DELETE . '_todo_self':
+ case self::SUBJECT_OBJECT_UPDATE . '_todo':
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
+
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
+ case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
+ return [
+ 0 => 'username',
+ //1 => 'calendar',
+ //2 => 'object',
+ ];
}
}
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php
index fa01d27befc..660114e462a 100644
--- a/apps/dav/lib/CalDAV/CalDavBackend.php
+++ b/apps/dav/lib/CalDAV/CalDavBackend.php
@@ -892,6 +892,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
])
->execute();
+ $this->activityBackend->addCalendarObject($calendarId, $objectUri);
$this->addChange($calendarId, $objectUri, 1);
return '"' . $extraData['etag'] . '"';
@@ -933,6 +934,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri)))
->execute();
+ $this->activityBackend->updateCalendarObject($calendarId, $objectUri);
$this->addChange($calendarId, $objectUri, 2);
return '"' . $extraData['etag'] . '"';
@@ -965,6 +967,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return void
*/
function deleteCalendarObject($calendarId, $objectUri) {
+ $this->activityBackend->deleteCalendarObject($calendarId, $objectUri);
+
$stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?');
$stmt->execute([$calendarId, $objectUri]);