Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to filter activities by an object type and id #363

Merged
merged 6 commits into from
Aug 20, 2015
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
5 changes: 3 additions & 2 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function __construct (array $urlParams = array()) {

$container->registerService('Consumer', function(IContainer $c) {
return new Consumer(
$c->query('UserSettings'),
$c->query('CurrentUID')
$c->query('ActivityData'),
$c->query('UserSettings')
);
});

Expand Down Expand Up @@ -108,6 +108,7 @@ public function __construct (array $urlParams = array()) {
$server = $c->query('ServerContainer');

return new FilesHooks(
$server->getActivityManager(),
$c->query('ActivityData'),
$c->query('UserSettings'),
$server->getDatabaseConnection(),
Expand Down
13 changes: 13 additions & 0 deletions appinfo/database.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
<notnull>false</notnull>
<length>4000</length>
</field>
<field>
<name>object_type</name>
<type>text</type>
<notnull>false</notnull>
<length>255</length>
</field>
<field>
<name>object_id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be (future/third party) activities not linked to any object ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, they'd just have "0" as a value, fine by me.

<length>4</length>
</field>

<index>
<name>activity_user_time</name>
Expand Down
2 changes: 1 addition & 1 deletion appinfo/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.2
6 changes: 4 additions & 2 deletions controller/activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ public function showList($filter = 'all') {
*
* @param int $page
* @param string $filter
* @param string $objecttype
* @param int $objectid
* @return JSONResponse
*/
public function fetch($page, $filter = 'all') {
public function fetch($page, $filter = 'all', $objecttype = '', $objectid = 0) {
$pageOffset = $page - 1;
$filter = $this->data->validateFilter($filter);

$activities = $this->data->read($this->helper, $this->settings, $pageOffset * self::DEFAULT_PAGE_SIZE, self::DEFAULT_PAGE_SIZE, $filter);
$activities = $this->data->read($this->helper, $this->settings, $pageOffset * self::DEFAULT_PAGE_SIZE, self::DEFAULT_PAGE_SIZE, $filter, '', $objecttype, $objectid);

$preparedActivities = [];
foreach ($activities as $activity) {
Expand Down
46 changes: 19 additions & 27 deletions lib/consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\Activity;

use OCP\Activity\IConsumer;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\AppFramework\IAppContainer;

Expand All @@ -39,53 +40,44 @@ public static function register(IManager $am, IAppContainer $container) {
});
}

/** @var Data */
protected $data;

/** @var UserSettings */
protected $userSettings;

/** @var string */
protected $user;

/**
* Constructor
*
* @param Data $data
* @param UserSettings $userSettings
* @param string $user
*/
public function __construct(UserSettings $userSettings, $user) {
public function __construct(Data $data, UserSettings $userSettings) {
$this->data = $data;
$this->userSettings = $userSettings;
$this->user = $user;
}

/**
* Send an event into the activity stream of a user
* Send an event to the notifications of a user
*
* @param string $app The app where this event is associated with
* @param string $subject A short description of the event
* @param array $subjectParams Array with parameters that are filled in the subject
* @param string $message A longer description of the event
* @param array $messageParams Array with parameters that are filled in the message
* @param string $file The file including path where this event is associated with. (optional)
* @param string $link A link where this event is associated with (optional)
* @param string $affectedUser If empty the current user will be used
* @param string $type Type of the notification
* @param int $priority Priority of the notification
* @param IEvent $event
* @return null
*/
public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
$selfAction = $affectedUser === $this->user;
$streamSetting = $this->userSettings->getUserSetting($affectedUser, 'stream', $type);
$emailSetting = $this->userSettings->getUserSetting($affectedUser, 'email', $type);
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($affectedUser, 'setting', 'batchtime') : false;
public function receive(IEvent $event) {
$selfAction = $event->getAffectedUser() === $event->getAuthor();
$streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType());
$emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType());
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false;

// Add activity to stream
if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'self'))) {
Data::send($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority);
if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self'))) {
$this->data->send($event);
}

// Add activity to mail queue
if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'selfemail'))) {
$latestSend = time() + $emailSetting;
Data::storeMail($app, $subject, $subjectParams, $affectedUser, $type, $latestSend);
if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail'))) {
$latestSend = $event->getTimestamp() + $emailSetting;
$this->data->storeMail($event, $latestSend);
}
}
}
129 changes: 68 additions & 61 deletions lib/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* ownCloud - Activity App
*
* @author Frank Karlitschek
* @author Joas Schilling
* @copyright 2013 Frank Karlitschek [email protected]
*
* This library is free software; you can redistribute it and/or
Expand All @@ -23,6 +24,7 @@

namespace OCA\Activity;

use OCP\Activity\IEvent;
use OCP\Activity\IExtension;
use OCP\Activity\IManager;
use OCP\DB;
Expand Down Expand Up @@ -80,86 +82,80 @@ public function getNotificationTypes(\OCP\IL10N $l) {
/**
* Send an event into the activity stream
*
* @param string $app The app where this event is associated with
* @param string $subject A short description of the event
* @param array $subjectparams Array with parameters that are filled in the subject
* @param string $message A longer description of the event
* @param array $messageparams Array with parameters that are filled in the message
* @param string $file The file including path where this event is associated with. (optional)
* @param string $link A link where this event is associated with (optional)
* @param string $affecteduser If empty the current user will be used
* @param string $type Type of the notification
* @param int $prio Priority of the notification
* @param IEvent $event
* @return bool
*/
public static function send($app, $subject, $subjectparams = array(), $message = '', $messageparams = array(), $file = '', $link = '', $affecteduser = '', $type = '', $prio = IExtension::PRIORITY_MEDIUM) {
$timestamp = time();

$user = \OC::$server->getUserSession()->getUser();
if ($user instanceof IUser) {
$user = $user->getUID();
} else {
// Public page or incognito mode
$user = '';
}

if ($affecteduser === '' && $user === '') {
public function send(IEvent $event) {
if ($event->getAffectedUser() === '' || $event->getAffectedUser() === null) {
return false;
} elseif ($affecteduser === '') {
$auser = $user;
} else {
$auser = $affecteduser;
}

// store in DB
$query = DB::prepare('INSERT INTO `*PREFIX*activity`(`app`, `subject`, `subjectparams`, `message`, `messageparams`, `file`, `link`, `user`, `affecteduser`, `timestamp`, `priority`, `type`)' . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )');
$query->execute(array($app, $subject, json_encode($subjectparams), $message, json_encode($messageparams), $file, $link, $user, $auser, $timestamp, $prio, $type));

// fire a hook so that other apps like notification systems can connect
Util::emitHook('OC_Activity', 'post_event', array('app' => $app, 'subject' => $subject, 'user' => $user, 'affecteduser' => $affecteduser, 'message' => $message, 'file' => $file, 'link'=> $link, 'prio' => $prio, 'type' => $type));
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder->insert('activity')
->values([
'app' => $queryBuilder->createParameter('app'),
'subject' => $queryBuilder->createParameter('subject'),
'subjectparams' => $queryBuilder->createParameter('subjectparams'),
'message' => $queryBuilder->createParameter('message'),
'messageparams' => $queryBuilder->createParameter('messageparams'),
'file' => $queryBuilder->createParameter('object_name'),
'link' => $queryBuilder->createParameter('link'),
'user' => $queryBuilder->createParameter('user'),
'affecteduser' => $queryBuilder->createParameter('affecteduser'),
'timestamp' => $queryBuilder->createParameter('timestamp'),
'priority' => $queryBuilder->createParameter('priority'),
'type' => $queryBuilder->createParameter('type'),
'object_type' => $queryBuilder->createParameter('object_type'),
'object_id' => $queryBuilder->createParameter('object_id'),
])
->setParameters([
'app' => $event->getApp(),
'type' => $event->getType(),
'affecteduser' => $event->getAffectedUser(),
'user' => $event->getAuthor(),
'timestamp' => (int) $event->getTimestamp(),
'subject' => $event->getSubject(),
'subjectparams' => json_encode($event->getSubjectParameters()),
'message' => $event->getMessage(),
'messageparams' => json_encode($event->getMessageParameters()),
'priority' => IExtension::PRIORITY_MEDIUM,
'object_type' => $event->getObjectType(),
'object_id' => (int) $event->getObjectId(),
'object_name' => $event->getObjectName(),
'link' => $event->getLink(),
])
->execute();

return true;
}

/**
* @brief Send an event into the activity stream
* Send an event as email
*
* @param string $app The app where this event is associated with
* @param string $subject A short description of the event
* @param array $subjectParams Array of parameters that are filled in the placeholders
* @param string $affectedUser Name of the user we are sending the activity to
* @param string $type Type of notification
* @param int $latestSendTime Activity time() + batch setting of $affectedUser
* @param IEvent $event
* @param int $latestSendTime Activity $timestamp + batch setting of $affectedUser
* @return bool
*/
public static function storeMail($app, $subject, array $subjectParams, $affectedUser, $type, $latestSendTime) {
$timestamp = time();
public function storeMail(IEvent $event, $latestSendTime) {
if ($event->getAffectedUser() === '' || $event->getAffectedUser() === null) {
return false;
}

// store in DB
$query = DB::prepare('INSERT INTO `*PREFIX*activity_mq` '
. ' (`amq_appid`, `amq_subject`, `amq_subjectparams`, `amq_affecteduser`, `amq_timestamp`, `amq_type`, `amq_latest_send`) '
. ' (`amq_appid`, `amq_type`, `amq_affecteduser`, `amq_timestamp`, `amq_subject`, `amq_subjectparams`, `amq_latest_send`) '
. ' VALUES(?, ?, ?, ?, ?, ?, ?)');
$query->execute(array(
$app,
$subject,
json_encode($subjectParams),
$affectedUser,
$timestamp,
$type,
$event->getApp(),
$event->getType(),
$event->getAffectedUser(),
$event->getTimestamp(),
$event->getSubject(),
json_encode($event->getSubjectParameters()),
$latestSendTime,
));

// fire a hook so that other apps like notification systems can connect
Util::emitHook('OC_Activity', 'post_email', array(
'app' => $app,
'subject' => $subject,
'subjectparams' => $subjectParams,
'affecteduser' => $affectedUser,
'timestamp' => $timestamp,
'type' => $type,
'latest_send' => $latestSendTime,
));

return true;
}

Expand All @@ -171,9 +167,11 @@ public static function storeMail($app, $subject, array $subjectParams, $affected
* @param int $count The number of statements to read
* @param string $filter Filter the activities
* @param string $user User for whom we display the stream
* @param string $objectType
* @param int $objectId
* @return array
*/
public function read(GroupHelper $groupHelper, UserSettings $userSettings, $start, $count, $filter = 'all', $user = '') {
public function read(GroupHelper $groupHelper, UserSettings $userSettings, $start, $count, $filter = 'all', $user = '', $objectType = '', $objectId = 0) {
// get current user
if ($user === '') {
$user = $this->userSession->getUser();
Expand Down Expand Up @@ -202,10 +200,18 @@ public function read(GroupHelper $groupHelper, UserSettings $userSettings, $star
if ($filter === 'self') {
$limitActivities .= ' AND `user` = ?';
$parameters[] = $user;
}
else if ($filter === 'by' || $filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) {
} else if ($filter === 'by' || $filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) {
$limitActivities .= ' AND `user` <> ?';
$parameters[] = $user;
} else if ($filter === 'filter') {
if (!$userSettings->getUserSetting($user, 'setting', 'self')) {
$limitActivities .= ' AND `user` <> ?';
$parameters[] = $user;
}
$limitActivities .= ' AND `object_type` = ?';
$parameters[] = $objectType;
$limitActivities .= ' AND `object_id` = ?';
$parameters[] = $objectId;
}

list($condition, $params) = $this->activityManager->getQueryForFilter($filter);
Expand Down Expand Up @@ -262,6 +268,7 @@ public function validateFilter($filterValue) {
case 'by':
case 'self':
case 'all':
case 'filter':
return $filterValue;
default:
if ($this->activityManager->isFilterValid($filterValue)) {
Expand Down
Loading