Merge pull request #1685 from owncloud/issue/1670

More activities events
This commit is contained in:
Vincent Petry 2014-05-13 12:46:50 +02:00
Родитель ef75fdc549 12b266eab2
Коммит f31b096d1d
8 изменённых файлов: 500 добавлений и 247 удалений

Просмотреть файл

@ -29,16 +29,11 @@ $notify_email = $notify_stream = array();
$l = OCP\Util::getL10N('activity');
$types = \OCA\Activity\Data::getNotificationTypes($l);
foreach ($types as $type => $data) {
if (!empty($_POST[$type . '_email'])) {
$notify_email = array_merge($notify_email, $data['types']);
}
if (!empty($_POST[$type . '_stream'])) {
$notify_stream = array_merge($notify_stream, $data['types']);
}
foreach ($types as $type => $desc) {
/*
OCP\Config::setUserValue(OCP\User::getUser(), 'activity', 'notify_email_' . $type, !empty($_POST[$type . '_email']));
*/
OCP\Config::setUserValue(OCP\User::getUser(), 'activity', 'notify_stream_' . $type, !empty($_POST[$type . '_stream']));
}
OCP\Config::setUserValue(OCP\User::getUser(), 'activity', 'notify_email', serialize($notify_email));
OCP\Config::setUserValue(OCP\User::getUser(), 'activity', 'notify_stream', serialize($notify_stream));
OC_JSON::success(array("data" => array( "message" => $l->t('Your settings have been updated.'))));

Просмотреть файл

@ -31,10 +31,9 @@
</field>
<field>
<name>type</name>
<type>integer</type>
<default>0</default>
<type>text</type>
<notnull>true</notnull>
<length>4</length>
<length>255</length>
</field>
<field>
<name>user</name>

101
appinfo/update.php Normal file
Просмотреть файл

@ -0,0 +1,101 @@
<?php
$installedVersion = OCP\Config::getAppValue('activity', 'installed_version');
if (version_compare($installedVersion, '1.1.6', '<')) {
$type_map = array(
1 => \OCA\Activity\Data::TYPE_SHARE_CHANGED,
2 => \OCA\Activity\Data::TYPE_SHARE_DELETED,
3 => \OCA\Activity\Data::TYPE_SHARE_CREATED,
4 => \OCA\Activity\Data::TYPE_SHARED,
5 => \OCA\Activity\Data::TYPE_SHARED,
6 => \OCA\Activity\Data::TYPE_SHARE_CHANGED,
7 => \OCA\Activity\Data::TYPE_SHARE_DELETED,
8 => \OCA\Activity\Data::TYPE_SHARE_CREATED,
9 => \OCA\Activity\Data::TYPE_SHARE_EXPIRED,
10 => \OCA\Activity\Data::TYPE_SHARE_RESHARED,
11 => \OCA\Activity\Data::TYPE_SHARE_RESHARED,
12 => \OCA\Activity\Data::TYPE_SHARE_DOWNLOADED,
13 => \OCA\Activity\Data::TYPE_SHARE_UPLOADED,
14 => \OCA\Activity\Data::TYPE_STORAGE_QUOTA_90,
15 => \OCA\Activity\Data::TYPE_STORAGE_FAILURE,
16 => \OCA\Activity\Data::TYPE_SHARE_UNSHARED,
);
foreach ($type_map as $old_type => $new_type) {
$query = \OC_DB::prepare('UPDATE `*PREFIX*activity` SET `type` = ? WHERE `type` = ?');
$query->execute(array($new_type, $old_type));
}
// fetch from DB
$query = \OC_DB::prepare(
'SELECT `userid`, `configvalue` '
. ' FROM `*PREFIX*preferences` '
. ' WHERE `appid` = ? AND `configkey` = ? '
);
$result = $query->execute(array('activity', 'notify_stream'));
$preference_map = array(
1 => \OCA\Activity\Data::TYPE_SHARE_CHANGED,
2 => \OCA\Activity\Data::TYPE_SHARE_DELETED,
3 => \OCA\Activity\Data::TYPE_SHARE_CREATED,
4 => \OCA\Activity\Data::TYPE_SHARED,
9 => \OCA\Activity\Data::TYPE_SHARE_EXPIRED,
10 => \OCA\Activity\Data::TYPE_SHARE_RESHARED,
12 => \OCA\Activity\Data::TYPE_SHARE_DOWNLOADED,
13 => \OCA\Activity\Data::TYPE_SHARE_UPLOADED,
14 => \OCA\Activity\Data::TYPE_STORAGE_QUOTA_90,
15 => \OCA\Activity\Data::TYPE_STORAGE_FAILURE,
16 => \OCA\Activity\Data::TYPE_SHARE_UNSHARED,
);
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, `configkey`, `configvalue`)' . ' VALUES ( ?, ?, ?, ? )');
while ($row = $result->fetchRow()) {
foreach ($preference_map as $old_type => $new_type) {
$query->execute(array(
$row['userid'],
'activity',
'notify_stream_' . $new_type,
in_array($old_type, unserialize($row['configvalue'])),
));
}
}
$query = \OC_DB::prepare('DELETE FROM `*PREFIX*preferences` WHERE `appid` = ? AND (`configkey` = ? OR `configkey` = ?)');
$query->execute(array('activity', 'notify_stream', 'notify_email'));
}
if (version_compare($installedVersion, '1.1.10', '<')) {
$subject_map = array(
'%s created' => 'created_self',
'%s created by %s' => 'created_by',
'%s changed' => 'changed_self',
'%s changed by %s' => 'changed_by',
'%s deleted' => 'deleted_self',
'%s deleted by %s' => 'deleted_by',
'You shared %s with %s' => 'shared_user_self',
'You shared %s with group %s' => 'shared_group_self',
'%s shared %s with you' => 'shared_with_by',
'You shared %s' => 'shared_link_self',
);
foreach ($subject_map as $old_subject => $new_subject) {
$query = \OC_DB::prepare('UPDATE `*PREFIX*activity` SET `subject` = ? WHERE `subject` = ?');
$query->execute(array($new_subject, $old_subject));
}
}
if (version_compare($installedVersion, '1.1.11', '<')) {
$query = \OC_DB::prepare(
'SELECT `activity_id`, `subjectparams` '
. 'FROM `*PREFIX*activity` '
. 'WHERE `subject` = ?');
$result = $query->execute(array('shared_with_by'));
while ($row = $result->fetchRow()) {
$subjectparams = unserialize($row['subjectparams']);
$username = array_shift($subjectparams);
array_push($subjectparams, $username);
$query = \OC_DB::prepare('UPDATE `*PREFIX*activity` SET `subjectparams` = ? WHERE `activity_id` = ?');
$query->execute(array(serialize($subjectparams), $row['activity_id']));
}
}

Просмотреть файл

@ -1 +1 @@
1.1.2
1.1.11

Просмотреть файл

@ -36,120 +36,55 @@ class Data
const PRIORITY_HIGH = 40;
const PRIORITY_VERYHIGH = 50;
const TYPE_SHARED = 4;
const TYPE_SHARED_BY = 5;
const TYPE_SHARE_EXPIRED = 9;
const TYPE_SHARE_UNSHARED = 16;
const TYPE_SHARED = 'shared';
const TYPE_SHARE_EXPIRED = 'share_expired';
const TYPE_SHARE_UNSHARED = 'share_unshared';
const TYPE_SHARE_CREATED = 3;
const TYPE_SHARE_CREATED_BY = 8;
const TYPE_SHARE_CHANGED = 1;
const TYPE_SHARE_CHANGED_BY = 6;
const TYPE_SHARE_DELETED = 2;
const TYPE_SHARE_DELETED_BY = 7;
const TYPE_SHARE_RESHARED = 10;
const TYPE_SHARE_RESHARED_BY = 11;
const TYPE_SHARE_CREATED = 'file_created';
const TYPE_SHARE_CHANGED = 'file_changed';
const TYPE_SHARE_DELETED = 'file_deleted';
const TYPE_SHARE_RESHARED = 'file_reshared';
const TYPE_SHARE_DOWNLOADED = 12;
const TYPE_SHARE_UPLOADED = 13;
const TYPE_SHARE_DOWNLOADED = 'file_downloaded';
const TYPE_SHARE_UPLOADED = 'file_uploaded';
const TYPE_STORAGE_QUOTA_90 = 14;
const TYPE_STORAGE_FAILURE = 15;
const TYPE_STORAGE_QUOTA_90 = 'storage_quota_90';
const TYPE_STORAGE_FAILURE = 'storage_failure';
public static function getNotificationTypes($l) {
return array(
'shared' => array(
'desc' => $l->t('A file or folder has been <strong>shared</strong>'),
'types' => array(
\OCA\Activity\Data::TYPE_SHARED,
\OCA\Activity\Data::TYPE_SHARED_BY,
),
),
// 'shared_unshared' => array(
// 'desc' => $l->t('Previously shared file or folder has been <strong>unshared</strong>'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_SHARE_UNSHARED,
// ),
// ),
// 'shared_expired' => array(
// 'desc' => $l->t('Expiration date of shared file or folder <strong>expired</strong>'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_SHARE_EXPIRED,
// ),
// ),
'share_created' => array(
'desc' => $l->t('A new file or folder has been <strong>created</strong> in a shared folder'),
'types' => array(
\OCA\Activity\Data::TYPE_SHARE_CREATED,
\OCA\Activity\Data::TYPE_SHARE_CREATED_BY,
),
),
'share_changed' => array(
'desc' => $l->t('A file or folder has been <strong>changed</strong> in a shared folder'),
'types' => array(
\OCA\Activity\Data::TYPE_SHARE_CHANGED,
\OCA\Activity\Data::TYPE_SHARE_CHANGED_BY,
),
),
'share_deleted' => array(
'desc' => $l->t('A file or folder has been <strong>deleted</strong> from a shared folder'),
'types' => array(
\OCA\Activity\Data::TYPE_SHARE_DELETED,
\OCA\Activity\Data::TYPE_SHARE_DELETED_BY,
),
),
// 'share_reshared' => array(
// 'desc' => $l->t('A file or folder has been <strong>reshared</strong>'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_SHARE_RESHARED,
// \OCA\Activity\Data::TYPE_SHARE_RESHARED_BY,
// ),
// ),
// 'share_downloaded' => array(
// 'desc' => $l->t('A file or folder shared via link has been <strong>downloaded</strong>'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_SHARE_DOWNLOADED,
// ),
// ),
// 'share_uploaded' => array(
// 'desc' => $l->t('A file has been <strong>uploaded</strong> into a folder shared via link'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_SHARE_UPLOADED,
// ),
// ),
// 'storage_quota_90' => array(
// 'desc' => $l->t('<strong>Storage usage</strong> is at 90%%'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_STORAGE_QUOTA_90,
// ),
// ),
// 'storage_failure' => array(
// 'desc' => $l->t('An <strong>external storage</strong> has an error'),
// 'types' => array(
// \OCA\Activity\Data::TYPE_STORAGE_FAILURE,
// ),
// ),
\OCA\Activity\Data::TYPE_SHARED => $l->t('A file or folder has been <strong>shared</strong>'),
// \OCA\Activity\Data::TYPE_SHARE_UNSHARED => $l->t('Previously shared file or folder has been <strong>unshared</strong>'),
// \OCA\Activity\Data::TYPE_SHARE_EXPIRED => $l->t('Expiration date of shared file or folder <strong>expired</strong>'),
\OCA\Activity\Data::TYPE_SHARE_CREATED => $l->t('A new file or folder has been <strong>created</strong> in a shared folder'),
\OCA\Activity\Data::TYPE_SHARE_CHANGED => $l->t('A file or folder has been <strong>changed</strong> in a shared folder'),
\OCA\Activity\Data::TYPE_SHARE_DELETED => $l->t('A file or folder has been <strong>deleted</strong> from a shared folder'),
// \OCA\Activity\Data::TYPE_SHARE_RESHARED => $l->t('A file or folder has been <strong>reshared</strong>'),
// \OCA\Activity\Data::TYPE_SHARE_DOWNLOADED => $l->t('A file or folder shared via link has been <strong>downloaded</strong>'),
// \OCA\Activity\Data::TYPE_SHARE_UPLOADED => $l->t('A file has been <strong>uploaded</strong> into a folder shared via link'),
// \OCA\Activity\Data::TYPE_STORAGE_QUOTA_90 => $l->t('<strong>Storage usage</strong> is at 90%%'),
// \OCA\Activity\Data::TYPE_STORAGE_FAILURE => $l->t('An <strong>external storage</strong> has an error'),
);
}
public static function getUserDefaultSetting($method) {
public static function getUserDefaultSetting($method, $type) {
$settings = self::getUserDefaultSettings($method);
return in_array($type, $settings);
}
public static function getUserDefaultSettings($method) {
$settings = array();
switch ($method) {
case 'stream':
$settings[] = Data::TYPE_SHARE_CREATED;
$settings[] = Data::TYPE_SHARE_CREATED_BY;
$settings[] = Data::TYPE_SHARE_CHANGED;
$settings[] = Data::TYPE_SHARE_CHANGED_BY;
$settings[] = Data::TYPE_SHARE_DELETED;
$settings[] = Data::TYPE_SHARE_DELETED_BY;
// $settings[] = Data::TYPE_SHARE_RESHARED;
// $settings[] = Data::TYPE_SHARE_RESHARED_BY;
//
// $settings[] = Data::TYPE_SHARE_DOWNLOADED;
case 'email':
$settings[] = Data::TYPE_SHARED;
$settings[] = Data::TYPE_SHARED_BY;
// $settings[] = Data::TYPE_SHARE_EXPIRED;
// $settings[] = Data::TYPE_SHARE_UNSHARED;
//
@ -196,6 +131,22 @@ class Data
return true;
}
public static function prepare_files_params($app, $text, $params, $file_position = false)
{
if ($app === 'files') {
$prepared_params = array();
foreach ($params as $i => $param) {
if ($file_position === $i) {
// Remove the path from the file string
$param = substr($param, strrpos($param, '/') + 1);
}
$prepared_params[] = $param;
}
return $prepared_params;
}
return $params;
}
/**
* @brief Translate an event string with the translations from the app where it was send from
* @param string $app The app where this event comes from
@ -204,10 +155,59 @@ class Data
* @return string translated
*/
public static function translation($app, $text, $params) {
$l = \OCP\Util::getL10N($app);
$result = $l->t($text, $params);
unset($l);
return($result);
if (!$text) {
return '';
}
if ($app === 'files') {
$l = \OCP\Util::getL10N('activity');
$params = self::prepare_files_params($app, $text, $params, 0);
if ($text === 'created_self') {
return $l->t('You created %1$s', $params);
}
else if ($text === 'created_by') {
return $l->t('%2$s created %1$s', $params);
}
else if ($text === 'changed_self') {
return $l->t('You changed %1$s', $params);
}
else if ($text === 'changed_by') {
return $l->t('%2$s changed %1$s', $params);
}
else if ($text === 'deleted_self') {
return $l->t('You deleted %1$s', $params);
}
else if ($text === 'deleted_by') {
return $l->t('%2$s deleted %1$s', $params);
}
else if ($text === 'shared_user_self') {
return $l->t('You shared %1$s with %2$s', $params);
}
else if ($text === 'shared_group_self') {
return $l->t('You shared %1$s with group %2$s', $params);
}
else if ($text === 'shared_with_by') {
return $l->t('%2$s shared %1$s with you', $params);
}
else if ($text === 'shared_link_self') {
return $l->t('You shared %1$s', $params);
}
return $l->t($text, $params);
} else {
$l = \OCP\Util::getL10N($app);
return $l->t($text, $params);
}
}
public static function getUserSetting($user, $method, $type) {
return \OCP\Config::getUserValue(
$user,
'activity',
'notify_' . $method . '_' . $type,
\OCA\Activity\Data::getUserDefaultSetting($method, $type)
);
}
/**
@ -216,22 +216,21 @@ class Data
* @return string Part of the SQL query limiting the activities
*/
public static function getUserNotificationTypesQuery($user, $method) {
$user_activities = unserialize(\OCP\Config::getUserValue(
$user, 'activity', 'notify_' . $method, serialize(self::getUserDefaultSetting($method))
));
$l = \OC_L10N::get('activity');
$types = \OCA\Activity\Data::getNotificationTypes($l);
// If the user selected to display no activities at all,
// we assume this was a mistake, so we display the default types.
if (empty($user_activities)) {
$user_activities = self::getUserDefaultSetting($method);
if (empty($user_activities)) {
// Default selection list is empty aswell.
// We don't want to display any activities then.
return '1 = 0';
foreach ($types as $type => $desc) {
if (self::getUserSetting($user, $method, $type)) {
$user_activities[] = $type;
}
}
return '`type` IN (' . implode(',', $user_activities) . ')';
// We don't want to display any activities
if (empty($user_activities)) {
return '1 = 0';
}
return "`type` IN ('" . implode("','", $user_activities) . "')";
}
/**
@ -255,10 +254,14 @@ class Data
$result = $query->execute(array($user));
$activity = array();
while ($row = $result->fetchRow()) {
$row['subject'] = Data::translation($row['app'],$row['subject'],unserialize($row['subjectparams']));
$row['message'] = Data::translation($row['app'],$row['message'],unserialize($row['messageparams']));
$activity[] = $row;
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCA\Activity\Data::read', \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
} else {
while ($row = $result->fetchRow()) {
$row['subject'] = Data::translation($row['app'],$row['subject'],unserialize($row['subjectparams']));
$row['message'] = Data::translation($row['app'],$row['message'],unserialize($row['messageparams']));
$activity[] = $row;
}
}
return $activity;
@ -285,10 +288,14 @@ class Data
$result = $query->execute(array($user, '%' . $txt . '%', '%' . $txt . '%', '%' . $txt . '%')); //$result = $query->execute(array($user,'%'.$txt.''));
$activity = array();
while ($row = $result->fetchRow()) {
$row['subject'] = Data::translation($row['app'],$row['subject'],unserialize($row['subjectparams']));
$row['message'] = Data::translation($row['app'],$row['message'],unserialize($row['messageparams']));
$activity[] = $row;
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCA\Activity\Data::search', \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
} else {
while ($row = $result->fetchRow()) {
$row['subject'] = Data::translation($row['app'],$row['subject'],unserialize($row['subjectparams']));
$row['message'] = Data::translation($row['app'],$row['message'],unserialize($row['messageparams']));
$activity[] = $row;
}
}
return $activity;

Просмотреть файл

@ -3,7 +3,7 @@
/**
* ownCloud - Activities App
*
* @author Frank Karlitschek
* @author Frank Karlitschek, Joas Schilling
* @copyright 2013 Frank Karlitschek frank@owncloud.org
*
* This library is free software; you can redistribute it and/or
@ -21,14 +21,12 @@
*
*/
namespace OCA\Activity;
/**
* @brief The class to handle the filesystem hooks
*/
class Hooks {
public static $createhookfired = false;
public static $createhookfile = '';
@ -37,87 +35,16 @@ class Hooks {
* All other events has to be triggered by the apps.
*/
public static function register() {
//Listen to create file signal
\OCP\Util::connectHook('OC_Filesystem', 'post_create', "OCA\Activity\Hooks", "file_create");
//Listen to delete file signal
\OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Activity\Hooks", "file_delete");
//Listen to write file signal
\OCP\Util::connectHook('OC_Filesystem', 'post_write', "OCA\Activity\Hooks", "file_write");
//Listen to share signal
\OCP\Util::connectHook('OCP\Share', 'post_shared', "OCA\Activity\Hooks", "share");
\OCP\Util::connectHook('OC_Filesystem', 'post_create', 'OCA\Activity\Hooks', 'file_create');
\OCP\Util::connectHook('OC_Filesystem', 'post_update', 'OCA\Activity\Hooks', 'file_update');
\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Activity\Hooks', 'file_delete');
\OCP\Util::connectHook('OCP\Share', 'post_shared', 'OCA\Activity\Hooks', 'share');
// hooking up the activity manager
if (property_exists('OC', 'server')) {
if (method_exists(\OC::$server, 'getActivityManager')) {
$am = \OC::$server->getActivityManager();
$am->registerConsumer(function() {
return new Consumer();
});
}
}
}
/**
* @brief Store the write hook events
* @param array $params The hook params
*/
public static function file_write($params) {
if( self::$createhookfired ) {
$params['path'] = self::$createhookfile;
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($params['path'])));
$subject = '%s created';// Add to l10n: $l->t('%s created');
Data::send('files', $subject, substr($params['path'], 1), '', array(), $params['path'], $link, \OCP\User::getUser(), 3);
if(substr($params['path'],0,8)=='/Shared/') {
$uidOwner = \OC\Files\Filesystem::getOwner($params['path']);
$realfile=substr($params['path'],7);
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($realfile)));
$subject = '%s created by %s';// Add to l10n: $l->t('%s created by %s');
Data::send('files', $subject, array($realfile,\OCP\User::getUser()), '', array(), $realfile, $link, $uidOwner, 8, Data::PRIORITY_HIGH);
}
self::$createhookfired = false;
self::$createhookfile = '';
} else {
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($params['path'])));
$subject = '%s changed';// Add to l10n: $l->t('%s changed');
Data::send('files', $subject, substr($params['path'], 1), '', array(), $params['path'], $link, \OCP\User::getUser(), 1);
if(substr($params['path'],0,8)=='/Shared/') {
$uidOwner = \OC\Files\Filesystem::getOwner($params['path']);
$realfile=substr($params['path'],7);
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($realfile)));
$subject = '%s changed by %s';// Add to l10n: $l->t('%s changed by %s');
Data::send('files', $subject, array($realfile,\OCP\User::getUser()), '', array(), $realfile, $link, $uidOwner, 6, Data::PRIORITY_HIGH);
}
}
}
/**
* @brief Store the delete hook events
* @param array $params The hook params
*/
public static function file_delete($params) {
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($params['path'])));
$subject = '%s deleted';// Add to l10n: $l->t('%s deleted');
Data::send('files', $subject, substr($params['path'], 1), '', array(), $params['path'], $link, \OCP\User::getUser(), 2);
if(substr($params['path'],0,8)=='/Shared/') {
$uidOwner = \OC\Files\Filesystem::getOwner($params['path']);
$realfile=substr($params['path'],7);
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($realfile)));
$subject = '%s deleted by %s';// Add to l10n: $l->t('%s deleted by %s');
Data::send('files', $subject, array($realfile,\OCP\User::getUser()), '', array(), $realfile, $link, $uidOwner, 7, Data::PRIORITY_HIGH);
}
$am = \OC::$server->getActivityManager();
$am->registerConsumer(function() {
return new Consumer();
});
}
/**
@ -125,41 +52,259 @@ class Hooks {
* @param array $params The hook params
*/
public static function file_create($params) {
// remember the create event for later consumption
self::$createhookfired = true;
self::$createhookfile = $params['path'];
self::add_hooks_for_files($params['path'], Data::TYPE_SHARE_CREATED, 'created_self', 'created_by');
}
/**
* @brief Store the share events
* @brief Store the update hook events
* @param array $params The hook params
*/
public static function file_update($params) {
self::add_hooks_for_files($params['path'], Data::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
}
/**
* @brief Store the delete hook events
* @param array $params The hook params
*/
public static function file_delete($params) {
self::add_hooks_for_files($params['path'], Data::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_self');
}
/**
* Creates the entries for file actions on $file_path
*
* @param string $file_path The file that is being changed
* @param int $activity_type The activity type
* @param string $subject The subject for the actor
* @param string $subject_by The subject for other users (with "by $actor")
*/
public static function add_hooks_for_files($file_path, $activity_type, $subject, $subject_by) {
// Do not add activities for .part-files
if (substr($file_path, -5) === '.part') {
return;
}
$affectedUsers = self::getUserPathsFromPath($file_path);
$filteredStreamUsers = self::filterUsersBySetting(array_keys($affectedUsers), 'stream', $activity_type);
foreach ($affectedUsers as $user => $path) {
if (empty($filteredStreamUsers[$user])) {
continue;
}
if ($user === \OCP\User::getUser()) {
$user_subject = $subject;
$user_params = array($path);
} else {
$user_subject = $subject_by;
$user_params = array($path, \OCP\User::getUser());
}
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($path)));
if (!empty($filteredStreamUsers[$user])) {
// Add activities to stream
Data::send('files', $user_subject, $user_params, '', array(), $path, $link, $user, $activity_type, Data::PRIORITY_HIGH);
}
}
}
/**
* Returns a "username => path" map for all affected users
*
* @param string $path
* @return array
*/
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']);
}
return array($path, $uidOwner);
}
/**
* @brief Manage sharing events
* @param array $params The hook params
*/
public static function share($params) {
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname($params['fileTarget'])));
$link2 = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => dirname('/Shared/'.$params['fileTarget'])));
$sharedFrom = \OCP\User::getUser();
$shareWith = $params['shareWith'];
if(!empty($shareWith)) {
$subject = 'You shared %s with %s';// Add to l10n: $l->t('You shared %s with %s');
Data::send('files', $subject, array(substr($params['fileTarget'], 1), $shareWith), '', array(), $params['fileTarget'], $link, \OCP\User::getUser(), 4, Data::PRIORITY_MEDIUM );
$subject = '%s shared %s with you';// Add to l10n: $l->t('%s shared %s with you');
Data::send('files', $subject, array($sharedFrom, substr('/Shared'.$params['fileTarget'], 1)), '', array(), '/Shared/'.$params['fileTarget'], $link2, $shareWith, 5, Data::PRIORITY_MEDIUM);
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 {
$subject = 'You shared %s';// Add to l10n: $l->t('You shared %s');
Data::send('files', $subject, array(substr($params['fileTarget'], 1)), '', array(), $params['fileTarget'], $link, \OCP\User::getUser(), 4, Data::PRIORITY_MEDIUM );
self::shareFileOrFolder($params);
}
}
}
/**
* @brief Sharing a file or folder with a user
* @param array $params The hook params
*/
public static function shareFileOrFolderWithUser($params) {
$file_path = \OC\Files\Filesystem::getPath($params['fileSource']);
list($path, $uidOwner) = self::getSourcePathAndOwner($file_path);
// Folder owner
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
// Add activity to stream
if (Data::getUserSetting($uidOwner, 'stream', Data::TYPE_SHARED)) {
Data::send('files', 'shared_user_self', array($file_path, $params['shareWith']), '', array(), $path, $link, $uidOwner, Data::TYPE_SHARED, Data::PRIORITY_MEDIUM );
}
// New shared user
$path = $params['fileTarget'];
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
// Add activity to stream
if (Data::getUserSetting($params['shareWith'], 'stream', Data::TYPE_SHARED)) {
Data::send('files', 'shared_with_by', array($path, \OCP\User::getUser()), '', array(), $path, $link, $params['shareWith'], Data::TYPE_SHARED, Data::PRIORITY_MEDIUM);
}
}
/**
* @brief Sharing a file or folder with a group
* @param array $params The hook params
*/
public static function shareFileOrFolderWithGroup($params) {
$file_path = \OC\Files\Filesystem::getPath($params['fileSource']);
list($path, $uidOwner) = self::getSourcePathAndOwner($file_path);
// Folder owner
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
// Add activity to stream
if (Data::getUserSetting($uidOwner, 'stream', Data::TYPE_SHARED)) {
Data::send('files', 'shared_group_self', array($file_path, $params['shareWith']), '', array(), $path, $link, $uidOwner, Data::TYPE_SHARED, Data::PRIORITY_MEDIUM );
}
// Members of the new group
$affectedUsers = array();
$usersInGroup = \OC_Group::usersInGroup($params['shareWith']);
foreach ($usersInGroup as $user) {
$affectedUsers[$user] = $params['fileTarget'];
}
if (!empty($affectedUsers)) {
$filteredStreamUsersInGroup = self::filterUsersBySetting($usersInGroup, 'stream', Data::TYPE_SHARED);
// Check when there was a naming conflict and the target is different
// for some of the users
$query = \OC_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', \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
} else {
while ($row = $result->fetchRow()) {
$affectedUsers[$row['share_with']] = $row['file_target'];
}
}
foreach ($affectedUsers as $user => $path) {
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
// Add activity to stream
if (!empty($filteredStreamUsersInGroup[$user])) {
Data::send('files', 'shared_with_by', array($path, \OCP\User::getUser()), '', array(), $path, $link, $user, Data::TYPE_SHARED, Data::PRIORITY_MEDIUM);
}
}
}
}
/**
* @brief Sharing a file or folder via link/public
* @param array $params The hook params
*/
public static function shareFileOrFolder($params) {
$path = \OC\Files\Filesystem::getPath($params['fileSource']);
$link = \OCP\Util::linkToAbsolute('files', 'index.php', array(
'dir' => ($params['itemType'] === 'file') ? dirname($path) : $path,
));
if (Data::getUserSetting(\OCP\User::getUser(), 'stream', Data::TYPE_SHARED)) {
Data::send('files', 'shared_link_self', array($path), '', array(), $path, $link, \OCP\User::getUser(), Data::TYPE_SHARED, Data::PRIORITY_MEDIUM);
}
}
/**
* Filters the given user array by their notification setting
*
* @param array $users
* @param string $method
* @param string $type
* @return array
*/
public static function filterUsersBySetting($users, $method, $type) {
if (empty($users) || !is_array($users)) return array();
$filteredUsers = array();
$chunked_users = array_chunk($users, 50, true);
$placeholders_50 = implode(',', array_fill(0, 50, '?'));
foreach ($chunked_users as $chunk) {
$placeholders = (sizeof($chunk) == 50) ? $placeholders_50 : implode(',', array_fill(0, sizeof($chunk), '?'));
$query = \OCP\DB::prepare(
'SELECT `userid`, `configvalue` '
. ' FROM `*PREFIX*preferences` '
. ' WHERE `appid` = ? AND `configkey` = ? AND `userid` IN (' . $placeholders . ')');
$result = $query->execute(array_merge(array(
'activity',
'notify_' . $method . '_' . $type,
), $chunk));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCA\Activity\Hooks::filterUsersBySetting', \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
} else {
while ($row = $result->fetchRow()) {
if ($row['configvalue']) {
$filteredUsers[$row['userid']] = true;
}
unset($users[array_search($row['userid'], $chunk)]);
}
}
}
if (!empty($users)) {
// If the setting is enabled by default,
// we add all users that didn't set the preference yet.
if (\OCA\Activity\Data::getUserDefaultSetting($method, $type)) {
foreach ($users as $user) {
$filteredUsers[$user] = true;
}
}
}
return $filteredUsers;
}
}

Просмотреть файл

@ -26,18 +26,15 @@ OCP\Util::addScript('activity', 'settings');
OCP\Util::addStyle('activity', 'settings');
$l=OC_L10N::get('activity');
$notify_email = unserialize(OCP\Config::getUserValue(OCP\User::getUser(), 'activity', 'notify_email', serialize(\OCA\Activity\Data::getUserDefaultSetting('email'))));
$notify_stream = unserialize(OCP\Config::getUserValue(OCP\User::getUser(), 'activity', 'notify_stream', serialize(\OCA\Activity\Data::getUserDefaultSetting('stream'))));
$types = \OCA\Activity\Data::getNotificationTypes($l);
$user = OCP\User::getUser();
$activities = array();
foreach ($types as $type => $data) {
$checked_email = array_intersect($data['types'], $notify_email);
$checked_stream = array_intersect($data['types'], $notify_stream);
foreach ($types as $type => $desc) {
$activities[$type] = array(
'desc' => $data['desc'],
'email' => !empty($checked_email),
'stream' => !empty($checked_stream),
'desc' => $desc,
'email' => \OCA\Activity\Data::getUserSetting($user, 'email', $type),
'stream' => \OCA\Activity\Data::getUserSetting($user, 'stream', $type),
);
}

Просмотреть файл

@ -1,10 +1,19 @@
<?php
/* Copyright (c) 2014, Joas Schilling nickvergessen@gmx.de
* This file is licensed under the Affero General Public License version 3
* or later. See the COPYING-README file. */
/** @var $l OC_L10N */
/** @var $_ array */
?>
<form id="activity_notifications" class="section">
<h2><?php p($l->t('Notifications')); ?></h2>
<table class="grid activitysettings">
<thead>
<tr>
<!--
<th class="small" activity_select_group" data-select-group="email">
<th class="small activity_select_group" data-select-group="email">
<?php p($l->t('Mail')); ?>
</th>
-->
@ -19,7 +28,7 @@
<tr>
<!-- Email is not available yet
<td class="small">
<label for="<?php p($activity) ?>_stream">
<label for="<?php p($activity) ?>_email">
<input type="checkbox" id="<?php p($activity) ?>_email" name="<?php p($activity) ?>_email"
value="1" class="<?php p($activity) ?> email" <?php if ($data['email']): ?> checked="checked"<?php endif; ?> />
</label>