Blame view
sources/apps/activity/lib/hooks.php
10.9 KB
|
d1bafeea1
|
1 2 3 4 5 |
<?php /** * ownCloud - Activities App * |
|
6d9380f96
|
6 |
* @author Frank Karlitschek, Joas Schilling |
|
d1bafeea1
|
7 8 9 10 11 12 13 14 15 16 17 18 |
* @copyright 2013 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library 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. * |
|
6d9380f96
|
19 |
* You should have received a copy of the GNU Affero General Public |
|
d1bafeea1
|
20 21 22 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ |
|
d1bafeea1
|
23 24 25 26 27 28 |
namespace OCA\Activity;
/**
* @brief The class to handle the filesystem hooks
*/
class Hooks {
|
|
d1bafeea1
|
29 30 31 32 33 |
/**
* @brief Registers the filesystem hooks for basic filesystem operations.
* All other events has to be triggered by the apps.
*/
public static function register() {
|
|
6d9380f96
|
34 35 36 37 |
\OCP\Util::connectHook('OC_Filesystem', 'post_create', 'OCA\Activity\Hooks', 'fileCreate');
\OCP\Util::connectHook('OC_Filesystem', 'post_update', 'OCA\Activity\Hooks', 'fileUpdate');
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Activity\Hooks', 'fileDelete');
\OCP\Util::connectHook('OCP\Share', 'post_shared', 'OCA\Activity\Hooks', 'share');
|
|
d1bafeea1
|
38 |
|
|
6d9380f96
|
39 |
\OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Activity\Hooks', 'deleteUser');
|
|
d1bafeea1
|
40 |
|
|
6d9380f96
|
41 42 43 44 45 46 |
// hooking up the activity manager
$am = \OC::$server->getActivityManager();
$am->registerConsumer(function() {
return new Consumer();
});
}
|
|
d1bafeea1
|
47 |
|
|
6d9380f96
|
48 49 50 51 52 53 54 |
/**
* @brief Store the create hook events
* @param array $params The hook params
*/
public static function fileCreate($params) {
self::addNotificationsForFileAction($params['path'], Data::TYPE_SHARE_CREATED, 'created_self', 'created_by');
}
|
|
d1bafeea1
|
55 |
|
|
6d9380f96
|
56 57 58 59 60 61 |
/**
* @brief Store the update hook events
* @param array $params The hook params
*/
public static function fileUpdate($params) {
self::addNotificationsForFileAction($params['path'], Data::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
|
|
d1bafeea1
|
62 63 64 |
} /** |
|
6d9380f96
|
65 |
* @brief Store the delete hook events |
|
d1bafeea1
|
66 67 |
* @param array $params The hook params */ |
|
6d9380f96
|
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
public static function fileDelete($params) {
self::addNotificationsForFileAction($params['path'], Data::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_by');
}
/**
* Creates the entries for file actions on $file_path
*
* @param string $filePath The file that is being changed
* @param int $activityType The activity type
* @param string $subject The subject for the actor
* @param string $subjectBy The subject for other users (with "by $actor")
*/
public static function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) {
// Do not add activities for .part-files
if (substr($filePath, -5) === '.part') {
return;
}
$affectedUsers = self::getUserPathsFromPath($filePath);
$filteredStreamUsers = UserSettings::filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
$filteredEmailUsers = UserSettings::filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
foreach ($affectedUsers as $user => $path) {
if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
continue;
|
|
d1bafeea1
|
93 |
} |
|
d1bafeea1
|
94 |
|
|
6d9380f96
|
95 96 97 98 99 100 101 102 103 104 |
if ($user === \OCP\User::getUser()) {
if (!UserSettings::getUserSetting(\OCP\User::getUser(), 'setting', 'self')) {
continue;
}
$userSubject = $subject;
$userParams = array($path);
} else {
$userSubject = $subjectBy;
$userParams = array($path, \OCP\User::getUser());
|
|
d1bafeea1
|
105 |
} |
|
6d9380f96
|
106 107 108 109 110 111 112 113 |
self::addNotificationsForUser( $user, $userSubject, $userParams, $path, true, !empty($filteredStreamUsers[$user]), !empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0, $activityType, Data::PRIORITY_HIGH ); |
|
d1bafeea1
|
114 |
} |
|
d1bafeea1
|
115 116 117 |
} /** |
|
6d9380f96
|
118 119 120 121 |
* Returns a "username => path" map for all affected users * * @param string $path * @return array |
|
d1bafeea1
|
122 |
*/ |
|
6d9380f96
|
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
public static function getUserPathsFromPath($path) {
list($file_path, $uidOwner) = self::getSourcePathAndOwner($path);
return \OCP\Share::getUsersSharingFile($file_path, $uidOwner, true, true);
}
/**
* Return the source
*
* @param string $path
* @return array
*/
public static function getSourcePathAndOwner($path) {
$uidOwner = \OC\Files\Filesystem::getOwner($path);
if ($uidOwner != \OCP\User::getUser()) {
\OC\Files\Filesystem::initMountPoints($uidOwner);
$info = \OC\Files\Filesystem::getFileInfo($path);
$ownerView = new \OC\Files\View('/'.$uidOwner.'/files');
$path = $ownerView->getPath($info['fileid']);
|
|
d1bafeea1
|
142 |
} |
|
6d9380f96
|
143 |
return array($path, $uidOwner); |
|
d1bafeea1
|
144 145 146 |
} /** |
|
6d9380f96
|
147 |
* @brief Manage sharing events |
|
d1bafeea1
|
148 149 |
* @param array $params The hook params */ |
|
6d9380f96
|
150 151 152 153 154 155 156 157 158 159 160 161 162 |
public static function share($params) {
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
if ($params['shareWith']) {
if ($params['shareType'] == \OCP\Share::SHARE_TYPE_USER) {
self::shareFileOrFolderWithUser($params);
} else if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) {
self::shareFileOrFolderWithGroup($params);
}
} else {
self::shareFileOrFolder($params);
}
}
}
|
|
d1bafeea1
|
163 |
|
|
6d9380f96
|
164 165 166 167 168 169 170 |
/**
* @brief Sharing a file or folder with a user
* @param array $params The hook params
*/
public static function shareFileOrFolderWithUser($params) {
// User performing the share
self::shareNotificationForSharer('shared_user_self', $params['shareWith'], $params['fileSource'], $params['itemType']);
|
|
d1bafeea1
|
171 |
|
|
6d9380f96
|
172 173 174 175 176 177 178 179 |
// New shared user $path = $params['fileTarget']; self::addNotificationsForUser( $params['shareWith'], 'shared_with_by', array($path, \OCP\User::getUser()), $path, ($params['itemType'] === 'file'), UserSettings::getUserSetting($params['shareWith'], 'stream', Data::TYPE_SHARED), UserSettings::getUserSetting($params['shareWith'], 'email', Data::TYPE_SHARED) ? UserSettings::getUserSetting($params['shareWith'], 'setting', 'batchtime') : 0 ); |
|
d1bafeea1
|
180 181 182 |
} /** |
|
6d9380f96
|
183 |
* @brief Sharing a file or folder with a group |
|
d1bafeea1
|
184 185 |
* @param array $params The hook params */ |
|
6d9380f96
|
186 187 188 |
public static function shareFileOrFolderWithGroup($params) {
// User performing the share
self::shareNotificationForSharer('shared_group_self', $params['shareWith'], $params['fileSource'], $params['itemType']);
|
|
d1bafeea1
|
189 |
|
|
6d9380f96
|
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
// Members of the new group
$affectedUsers = array();
$usersInGroup = \OC_Group::usersInGroup($params['shareWith']);
foreach ($usersInGroup as $user) {
$affectedUsers[$user] = $params['fileTarget'];
}
// Remove the triggering user, we already managed his notifications
unset($affectedUsers[\OCP\User::getUser()]);
if (empty($affectedUsers)) {
return;
}
$filteredStreamUsersInGroup = UserSettings::filterUsersBySetting($usersInGroup, 'stream', Data::TYPE_SHARED);
$filteredEmailUsersInGroup = UserSettings::filterUsersBySetting($usersInGroup, 'email', Data::TYPE_SHARED);
// Check when there was a naming conflict and the target is different
// for some of the users
$query = \OCP\DB::prepare('SELECT `share_with`, `file_target` FROM `*PREFIX*share` WHERE `parent` = ? ');
$result = $query->execute(array($params['id']));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCA\Activity\Hooks::shareFileOrFolderWithGroup', \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
} else {
while ($row = $result->fetchRow()) {
$affectedUsers[$row['share_with']] = $row['file_target'];
}
}
foreach ($affectedUsers as $user => $path) {
if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
continue;
|
|
d1bafeea1
|
222 |
} |
|
6d9380f96
|
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
self::addNotificationsForUser(
$user, 'shared_with_by', array($path, \OCP\User::getUser()),
$path, ($params['itemType'] === 'file'),
!empty($filteredStreamUsersInGroup[$user]),
!empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0
);
}
}
/**
* Add notifications for the user that shares a file/folder
*
* @param string $subject
* @param string $shareWith
* @param int $fileSource
* @param string $itemType
*/
public static function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
// User performing the share
if (UserSettings::getUserSetting(\OCP\User::getUser(), 'setting', 'self')) {
$file_path = \OC\Files\Filesystem::getPath($fileSource);
self::addNotificationsForUser(
\OCP\User::getUser(), $subject, array($file_path, $shareWith),
$file_path, ($itemType === 'file'),
UserSettings::getUserSetting(\OCP\User::getUser(), 'stream', Data::TYPE_SHARED),
UserSettings::getUserSetting(\OCP\User::getUser(), 'email', Data::TYPE_SHARED) ? UserSettings::getUserSetting(\OCP\User::getUser(), 'setting', 'batchtime') : 0
);
|
|
d1bafeea1
|
252 |
} |
|
6d9380f96
|
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
}
/**
* Adds the activity and email for a user when the settings require it
*
* @param string $user
* @param string $subject
* @param array $subjectParams
* @param string $path
* @param bool $isFile If the item is a file, we link to the parent directory
* @param bool $streamSetting
* @param int $emailSetting
* @param string $type
* @param int $priority
*/
protected static function addNotificationsForUser($user, $subject, $subjectParams, $path, $isFile, $streamSetting, $emailSetting, $type = Data::TYPE_SHARED, $priority = Data::PRIORITY_MEDIUM) {
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($isFile) ? dirname($path) : $path,
));
|
|
d1bafeea1
|
272 |
|
|
6d9380f96
|
273 274 275 276 277 278 279 280 281 282 |
// Add activity to stream
if ($streamSetting) {
Data::send('files', $subject, $subjectParams, '', array(), $path, $link, $user, $type, $priority);
}
// Add activity to mail queue
if ($emailSetting) {
$latestSend = time() + $emailSetting;
Data::storeMail('files', $subject, $subjectParams, $user, $type, $latestSend);
}
|
|
d1bafeea1
|
283 |
} |
|
6d9380f96
|
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
/**
* @brief Sharing a file or folder via link/public
* @param array $params The hook params
*/
public static function shareFileOrFolder($params) {
if (UserSettings::getUserSetting(\OCP\User::getUser(), 'setting', 'self') &&
UserSettings::getUserSetting(\OCP\User::getUser(), 'stream', Data::TYPE_SHARED)) {
$path = \OC\Files\Filesystem::getPath($params['fileSource']);
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
Data::send('files', 'shared_link_self', array($path), '', array(), $path, $link, \OCP\User::getUser(), Data::TYPE_SHARED, Data::PRIORITY_MEDIUM);
}
}
|
|
d1bafeea1
|
300 |
|
|
6d9380f96
|
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
/**
* Delete remaining activities and emails when a user is deleted
* @param array $params The hook params
*/
public static function deleteUser($params) {
// Delete activity entries
$data = new Data(
\OC::$server->getActivityManager()
);
$data->deleteActivities(array('affecteduser' => $params['uid']));
// Delete entries from mail queue
$query = \OCP\DB::prepare(
'DELETE FROM `*PREFIX*activity_mq` '
. ' WHERE `amq_affecteduser` = ?');
$query->execute(array($params['uid']));
}
|
|
d1bafeea1
|
318 |
} |