Set notification filters for all users
![]() | Examples: exclude notifications from the XWiki space, add an inclusive filter for a fictive space for all users and the wiki |
Type | Snippet |
Category | Other |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
Add an exclusive filter for the XWiki space
As an example how to set notification filters for all users, this snippet adds an exclusive filter for the XWiki space, so users get no notifications just because another user changed their settings or admins configure something in the wiki administration.
{{velocity}}
#set ($mainWikiRef = $services.model.createWikiReference($services.wiki.getMainWikiId()))
#set($notificationFilterPreferenceService = $services.component.getInstance("org.xwiki.notifications.filters.NotificationFilterPreferenceManager"))
#if(!$notificationFilterPreferenceService)
{{error}}no notification service found; maybe missing programming rights?{{/error}}
#else
#set($check=("$!request.get('check')" != ''))
#set($action=("$!request.get('run')" != ''))
#if (!$check && !$action)
This page adds filters to exclude notifications from the "XWiki" space for all $services.query.xwql('from doc.object("XWiki.XWikiUsers") as user').count() users.
Either [[check>>$doc.fullName||queryString="check=1"]] for missing filters or [[add>>$doc.fullName||queryString="run=1"]] missing filters.
If you have a lot of users, the page might time out in the browser. Adding the filters should still happen.
#else
== wiki level filter ==
## FIXME: anyone wants to write a loop over all wikis?
## of course for subwikis one should only set filters for the users on their own wiki
* for the main wiki
#foreach ($wikiId in $services.wiki.getAllIds())
#set ($wikiRef = $services.model.createWikiReference($wikiId))
#set ($xwikiSpaceRef = $services.model.createSpaceReference('XWiki', $wikiRef))
#set ($filterFound = false)
#foreach($f in $services.notification.filters.getFilters())
#foreach ($filter in $services.notification.filters.getWikiFilterPreferences($f, $mainWikiRef))
#if ("$!filter.page" == "$wikiId:XWiki")
#set ($filterFound = true)
## #if ($check)
##** found filter for wiki "$wikiID" as $filter
## #end
## XXX only debugging: remove old tries
###set ($discard = $services.notification.filters.deleteWikiFilterPreference($filter.id, $mainWikiRef))
#end
#end
#end
#if ($filterFound)
##** filter for $wikiId already present
#else
#if ($check)
** no filter for wiki "$wikiId" found
#end
#if ($action)
#set ($discard = $services.notification.filters.createWikiScopeFilterPreference("exclusive", ['alert', 'email'], [], $xwikiSpaceRef, $mainWikiRef))
** adding XWiki space filter for wiki "$wikiId"
#end
#end
#end
== user specific filters ==
#foreach($userName in $services.query.xwql('from doc.object("XWiki.XWikiUsers") as user').execute())
#set($userDocReference = $services.model.resolveDocument($userName))
* User $userName
#foreach ($wikiId in $services.wiki.getAllIds())
#set ($wikiRef = $services.model.createWikiReference($wikiId))
#set ($xwikiSpaceRef = $services.model.createSpaceReference('XWiki', $wikiRef))
#set ($filterFound = false)
#foreach ($filter in $notificationFilterPreferenceService.getFilterPreferences($userDocReference))
#if ("$!filter.page" == "$wikiId:XWiki" )
#set ($filterFound = true)
## #if ($check)
##** found filter for wiki "$wikiID" as $filter
## #end
## XXX only debugging: remove old tries
##set($discard = $services.notification.filters.deleteFilterPreference($filter.id, $userDocReference))
#end
#end
#if ($filterFound)
## ** filter for $wikiId already present
#else
#if ($check)
** no filter for wiki "$wikiId" found
#end
#if ($action)
#set ($discard = $services.notification.filters.createScopeFilterPreference("exclusive", ['alert', 'email'], [], $xwikiSpaceRef, $userDocReference))
** added XWiki space filter for wiki "$wikiId"
#end
#end
#end ## for wikiId
#end ## for user
#end
#end ## if notifications service
{{/velocity}}
Tested with XWiki 13.8. The part with the "wiki level filter" will not work for older XWiki versions (before 13.4), so you will have to rerun the script to apply it to users created since the last run.
When https://jira.xwiki.org/browse/XWIKI-18509 is fixed, this script is no longer needed, as all you need to do is to add the wiki-level filter. This can be done easier manually via the Admin-UI.
Add an inclusive filter for a fictive space
Another example is adding an inclusive filter for a fictional space for all users and in the main wiki preferences. This is especially useful to workaround XWIKI-19070. It only needs to be ran once: it will set the preferences at wiki level so all new users will receive this filter upon creation (since XWIKI-17945) and it will migrate the existing users by adding them the filter, if it's not already present.
Note: the script below only works for users stored on the main wiki, as it manipulates the main wiki preferences. It should be copied in a page of the main wiki with a user having admin / programming rights and then ran.
{{velocity}}
#set ($mainWikiReference = $services.model.createWikiReference('xwiki'))
#set ($fictionalSpace = $services.model.createSpaceReference('34c6fc411b67974d6f1a16a9cfbafdec', $mainWikiReference))
## check if the filter preference needs to be added at wiki level
#set($shouldAddWikiFilterPreference = true)
#foreach($cF in $services.notification.filters.getWikiFilters($mainWikiReference))
#foreach($cFP in $services.notification.filters.getWikiFilterPreferences($cF, $mainWikiReference))
## TODO: checking all the paramaters of the filters is a pain (especially formats, email & alert) so we don't check it here to verify that the correct filter is in place. We assume that if it matches the page minimally it's enough
## in any case, once added it cannot be edited, only removed or disabled, and since the name is random, we assume that if page matches and it's enabled it's that it's the same one
#if ($cFP.isEnabled() && $cFP.getPage() == $services.model.serialize($fictionalSpace, 'default'))
## found, should not add it
#set($shouldAddWikiFilterPreference = false)
#end
#end
#end
= Wiki =
#if ($shouldAddWikiFilterPreference)
#if ("$!request.confirm" == 'true')
#set ($discard = $services.notification.filters.createWikiScopeFilterPreference("INCLUSIVE", ['alert', 'email'], [], $fictionalSpace, $mainWikiReference))
Inclusive filter for space $fictionalSpace was added on the main wiki.
#else
Actual run would add inclusive filter for space $fictionalSpace on the main wiki.
#end
#else
Filter already present at wiki level, nothing to do.
#end
= Users =
#set ($usersCountToChange = 0)
## check and collect users which don't have the fictionalSpace in their filters and no other filters, to add the filter to the fictional space to them
#foreach($u in $services.query.hql(", BaseObject obj where doc.fullName = obj.name and obj.className='XWiki.XWikiUsers'").execute())
#set($userReference = $services.model.resolveDocument($u, $mainWikiReference))
#set($shouldAddFilter = false)
## check the filters of this user
#set($uFilters = [])
#foreach($cF in $services.notification.filters.getFilters($userReference))
#foreach($cFP in $services.notification.filters.getFilterPreferences($cF, $userReference))
#if ($cFP.isEnabled())
#set($discard = $uFilters.add($cFP))
#end
#end
#end
## assume that the special filter is not added to this user, and then check: either there is no filter at all or there is none that matches the fictional page
#set($shouldAddFilter = true)
#foreach($uFilter in $uFilters)
#if ($uFilter.getPage() == $services.model.serialize($fictionalSpace, 'default'))
## when the page matches the fictional space, cancel the re-addition
#set($shouldAddFilter = false)
#end
#end
## and finally, add the filter, if needed
#if ($shouldAddFilter)
#if ("$!request.confirm" == 'true')
#set ($discard = $services.notification.filters.createScopeFilterPreference("INCLUSIVE", ['alert', 'email'], [], $fictionalSpace, $userReference))
* Inclusive filter for space $fictionalSpace was added on to the user $userReference
#else
* Actual run would add filter will be added to user $userReference
#set ($usersCountToChange = $usersCountToChange + 1)
#end
#end
#end
#if($usersCountToChange == 0)
Filter present for all users, nothing to do.
#end
##
#if ("$!request.confirm" != 'true' && ($shouldAddWikiFilterPreference || $usersCountToChange > 0))
{{html clean='false' wiki='false'}}
<form action='' method='post' class='xform'>
<div class='buttonwrapper'>
<input type='hidden' name='confirm' value='true' />
<input type='submit' class='btn button btn-primary' name='dosubmit' value='Initialize preferences' />
</div>
</form>
{{/html}}
#end
{{/velocity}}
This second snippet was tested on 13.4.7.