diff --git a/js/admin.js b/js/admin.js index 893411167..6ad9fd99f 100644 --- a/js/admin.js +++ b/js/admin.js @@ -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); + }); + }); }); diff --git a/js/settings.js b/js/settings.js index bdfa1c1e5..fc6041fc6 100644 --- a/js/settings.js +++ b/js/settings.js @@ -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); + } + }); + } + } +}); diff --git a/l10n/de.js b/l10n/de.js index 724c477d7..802b46043 100644 --- a/l10n/de.js +++ b/l10n/de.js @@ -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:", diff --git a/l10n/de.json b/l10n/de.json index 39b02154b..83190d8ea 100644 --- a/l10n/de.json +++ b/l10n/de.json @@ -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);" -} \ No newline at end of file +} diff --git a/l10n/de_DE.js b/l10n/de_DE.js index 055473122..b6ab75f0c 100644 --- a/l10n/de_DE.js +++ b/l10n/de_DE.js @@ -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:", diff --git a/l10n/de_DE.json b/l10n/de_DE.json index d62ffbdf3..48de00b4d 100644 --- a/l10n/de_DE.json +++ b/l10n/de_DE.json @@ -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);" -} \ No newline at end of file +} diff --git a/lib/Data.php b/lib/Data.php index 687f1dcee..ed272b6e3 100755 --- a/lib/Data.php +++ b/lib/Data.php @@ -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 @@ -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; } /** @@ -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; } @@ -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 */ @@ -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; + } } diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 121a5d3b9..3b2b8b96b 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -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, @@ -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'); } diff --git a/templates/settings/admin.php b/templates/settings/admin.php index ef5d5737e..52200e01c 100644 --- a/templates/settings/admin.php +++ b/templates/settings/admin.php @@ -31,6 +31,15 @@ /> +
+
+
+ +

+ t('Define groups, such as system users groups, whose activities will not be displayed (except for themselves).')); ?> +
+ +