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

Hide stream / disable notifications for specific groups #504

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 10 additions & 1 deletion js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,14 @@ $(document).ready(function() {
'activity', 'enable_email',
$(this).attr('checked') === 'checked' ? 'yes' : 'no'
);
})
});

$('#activity_system_users_group_list').each(function (index, element) {
OC.Settings.setupGroupsSelect($(element));
$(element).change(function(ev) {
var groups = ev.val || [];
groups = JSON.stringify(groups);
OCP.AppConfig.setValue('activity', $(this).attr('name'), groups);
});
});
});
96 changes: 96 additions & 0 deletions js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,99 @@ $(document).ready(function() {
saveSettings();
});
});

OC.Settings = OC.Settings || {};
OC.Settings = _.extend(OC.Settings, {

_cachedGroups: null,

/**
* Setup selection box for group selection.
*
* Values need to be separated by a pipe "|" character.
* (mostly because a comma is more likely to be used
* for groups)
*
* @param $elements jQuery element (hidden input) to setup select2 on
* @param {Array} [extraOptions] extra options hash to pass to select2
* @param {Array} [options] extra options
* @param {Array} [options.excludeAdmins=false] flag whether to exclude admin groups
*/
setupGroupsSelect: function($elements, extraOptions, options) {
var self = this;
options = options || {};
if ($elements.length > 0) {
// Let's load the data and THEN init our select
$.ajax({
url: OC.linkToOCS('cloud/groups', 2) + 'details',
dataType: 'json',
success: function(data) {
var results = [];

if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {

data.ocs.data.groups.forEach(function(group) {
if (!options.excludeAdmins || group.id !== 'admin') {
results.push({ id: group.id, displayname: group.displayname });
}
})

// note: settings are saved through a "change" event registered
// on all input fields
$elements.select2(_.extend({
placeholder: t('core', 'Groups'),
allowClear: true,
multiple: true,
toggleSelect: true,
separator: '|',
data: { results: results, text: 'displayname' },
initSelection: function(element, callback) {
var groups = $(element).val();
var selection;
if (groups && results.length > 0) {
selection = _.map(_.filter((groups || []).split('|').sort(), function(groupId) {
return results.find(function(group) {
return group.id === groupId
}) !== undefined
}), function(groupId) {
return {
id: groupId,
displayname: results.find(function(group) {
return group.id === groupId
}).displayname
}
})
} else if (groups) {
selection = _.map((groups || []).split('|').sort(), function(groupId) {
return {
id: groupId,
displayname: groupId
};
});
}
callback(selection);
},
formatResult: function(element) {
return escapeHTML(element.displayname);
},
formatSelection: function(element) {
return escapeHTML(element.displayname);
},
escapeMarkup: function(m) {
// prevent double markup escape
return m;
}
}, extraOptions || {}));
} else {
OC.Notification.show(t('settings', 'Group list is empty'), { type: 'error' });
console.log(data);
}
},
error: function(data) {
OC.Notification.show(t('settings', 'Unable to retrieve the group list'), { type: 'error' });
console.log(data);
}
});
}
}
});
1 change: 1 addition & 0 deletions l10n/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ OC.L10N.register(
"Stream" : "Stream",
"Send activity emails" : "E-Mails zu Aktivitäten senden",
"Configure the default activity settings for new users." : "Nehme die Voreinstellungen der Aktivitäten für neue Benutzer vor.",
"Define groups, such as system users groups, whose activities will not be displayed (except for themselves)." : "Definiere Gruppen, z. B. Systembenutzergruppen, deren Aktivitäten nicht angezeigt werden (außer für sich selbst).",
"List your own actions in the stream" : "Deine eigenen Aktivitäten im Stream auflisten",
"Notify about your own actions via email" : "Über Deine eigenen Aktivitäten via E-Mail benachrichtigen",
"Send emails:" : "Sende E-Mails:",
Expand Down
3 changes: 2 additions & 1 deletion l10n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@
"Stream" : "Stream",
"Send activity emails" : "E-Mails zu Aktivitäten senden",
"Configure the default activity settings for new users." : "Nehme die Voreinstellungen der Aktivitäten für neue Benutzer vor.",
"Define groups, such as system users groups, whose activities will not be displayed (except for themselves)." : "Definiere Gruppen, z. B. Systembenutzergruppen, deren Aktivitäten nicht angezeigt werden (außer für sich selbst).",
"List your own actions in the stream" : "Deine eigenen Aktivitäten im Stream auflisten",
"Notify about your own actions via email" : "Über Deine eigenen Aktivitäten via E-Mail benachrichtigen",
"Send emails:" : "Sende E-Mails:",
"Choose for which activities you want to get an email notification, and which should show up in the activity app stream." : "Wähle für welche Aktivitäten E-Mails empfangen und welche Aktivitäten in Deinem Feed angezeigt werden.",
"Choose which activities you want to see in your stream." : "Wähle aus, welche Aktivitäten in Deinem Feed angezeigt werden."
},"pluralForm" :"nplurals=2; plural=(n != 1);"
}
}
1 change: 1 addition & 0 deletions l10n/de_DE.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ OC.L10N.register(
"Stream" : "Stream",
"Send activity emails" : "E-Mails zu Aktivitäten senden",
"Configure the default activity settings for new users." : "Nehmen Sie die Voreinstellungen der Aktivitäten für neue Benutzer vor.",
"Define groups, such as system users groups, whose activities will not be displayed (except for themselves)." : "Definiere Gruppen, z. B. Systembenutzergruppen, deren Aktivitäten nicht angezeigt werden (außer für sich selbst).",
"List your own actions in the stream" : "Ihre eigenen Aktivitäten im Stream auflisten",
"Notify about your own actions via email" : "Über Ihre eigenen Aktivitäten via E-Mail benachrichtigen",
"Send emails:" : "E-Mails senden:",
Expand Down
3 changes: 2 additions & 1 deletion l10n/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@
"Stream" : "Stream",
"Send activity emails" : "E-Mails zu Aktivitäten senden",
"Configure the default activity settings for new users." : "Nehmen Sie die Voreinstellungen der Aktivitäten für neue Benutzer vor.",
"Define groups, such as system users groups, whose activities will not be displayed (except for themselves)." : "Definiere Gruppen, z. B. Systembenutzergruppen, deren Aktivitäten nicht angezeigt werden (außer für sich selbst).",
"List your own actions in the stream" : "Ihre eigenen Aktivitäten im Stream auflisten",
"Notify about your own actions via email" : "Über Ihre eigenen Aktivitäten via E-Mail benachrichtigen",
"Send emails:" : "E-Mails senden:",
"Choose for which activities you want to get an email notification, and which should show up in the activity app stream." : "Wählen Sie für welche Aktivitäten E-Mails empfangen und welche Aktivitäten in Ihrem Feed angezeigt werden.",
"Choose which activities you want to see in your stream." : "Wählen Sie aus, welche Aktivitäten in Ihrem Feed angezeigt werden."
},"pluralForm" :"nplurals=2; plural=(n != 1);"
}
}
92 changes: 89 additions & 3 deletions lib/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;

/**
* @brief Class for managing the data in the activities
Expand All @@ -44,13 +48,34 @@ class Data {
/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/** @var IGroupManager */
protected $groupManager;

/** @var IUserManager */
protected $userManager;

/**
* @param IManager $activityManager
* @param IDBConnection $connection
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserManager $userManager
*/
public function __construct(IManager $activityManager, IDBConnection $connection) {
public function __construct(
IManager $activityManager,
IDBConnection $connection,
IConfig $config,
IGroupManager $groupManager,
IUserManager $userManager
) {
$this->activityManager = $activityManager;
$this->connection = $connection;
$this->config = $config;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
}

/**
Expand Down Expand Up @@ -113,7 +138,7 @@ public function send(IEvent $event): int {
*/
public function storeMail(IEvent $event, int $latestSendTime): bool {
$affectedUser = $event->getAffectedUser();
if ($affectedUser === '' || $affectedUser === null) {
if ($affectedUser === '' || $affectedUser === null || $this->isSystemUser($event->getAuthor())) {
return false;
}

Expand Down Expand Up @@ -223,6 +248,16 @@ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user,
}
}

// if the user is not a system user, hide system users activities
if (!$this->isSystemUser($user)){
$systemUsers = $this->getSystemUsers();
if(!empty($systemUsers)){
$query->andWhere($query->expr()->notIn('user', $query->createNamedParameter(
$systemUsers, IQueryBuilder::PARAM_STR_ARRAY
)));
}
}

/**
* Order and specify the offset
*/
Expand Down Expand Up @@ -457,4 +492,55 @@ public function getActivitySince(string $user, int $since, bool $byOthers) {

return $query->execute()->fetch();
}

/**
* @param mixed $user
* @return bool
*/
protected function isSystemUser($user)
{
if(!($user instanceof IUser)){
$user = $this->userManager->get($user);
}
foreach ($this->getSystemUserGroups() as $group){
$group = $this->groupManager->get($group);
if (!($group instanceof IGroup)) {
continue;
}
if($group->inGroup($user)){
return true;
}
}

return false;
}

/**
* @return mixed
*/
protected function getSystemUserGroups()
{
$systemUsersGroupList = $this->config->getAppValue('activity', 'activity_system_users_group_list', '');
return json_decode($systemUsersGroupList);
}

/**
* @return array
*/
protected function getSystemUsers()
{
$users = [];
foreach ($this->getSystemUserGroups() as $group) {
$group = $this->groupManager->get($group);
if (!($group instanceof IGroup)) {
continue;
}
$groupUsers = $group->getUsers();
foreach ($groupUsers as $user){
array_push($users, $user->getUID());
}
}

return $users;
}
}
6 changes: 6 additions & 0 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public function getForm() {
$settingBatchTime = UserSettings::EMAIL_SEND_ASAP;
}

$systemUsersGroups = $this->config->getAppValue('activity', 'activity_system_users_group_list', '');
$systemUsersGroupList = !is_null(json_decode($systemUsersGroups))
? implode('|', json_decode($systemUsersGroups, true)) : '';

return new TemplateResponse('activity', 'settings/admin', [
'setting' => 'admin',
'activityGroups' => $activityGroups,
Expand All @@ -135,6 +139,8 @@ public function getForm() {
IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
IExtension::METHOD_NOTIFICATION => $this->l10n->t('Push'),
],

'system_users_group_list' => $systemUsersGroupList,
], 'blank');
}

Expand Down
9 changes: 9 additions & 0 deletions templates/settings/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
<input id="activity_email_enabled" name="activity_email_enabled" type="checkbox" class="checkbox"
value="1" <?php if ($_['email_enabled']) { print_unescaped('checked="checked"'); } ?> />
<label for="activity_email_enabled"><?php p($l->t('Enable notification emails')); ?></label>
<br />
<br />
<br />

<p class="settings-hint indent">
<?php p($l->t('Define groups, such as system users groups, whose activities will not be displayed (except for themselves).')); ?>
<br />
<input name="activity_system_users_group_list" id="activity_system_users_group_list" value="<?php p($_['system_users_group_list']) ?>" style="width: 400px" class="noJSAutoUpdate"/>
</p>

</div>

Expand Down