GetOnSiteNotifications provides other scripts with a ready-made solution to retrieve OnSiteNotifications (i.e. the notifications used for Feeds and Announcements). This script uses mw.hook to accept requests and return results. When submitting a request for notifications, scripts can specify the types of notifications, starting timestamp, and other parameters of the request. If a parameter is not specified, a default value will be used. The default parameter values as well as limits on parameter values can be customized by individual users. Therefore, scripts should not assume the results will always contain all of the requested information. This script is marked for personal use since most applications are likely to require this restriction.
This section is for users who want to restrict what other scripts can request. The other sections are for script developers.
The configuration options for this script allow individuals to restrict what can be requested by other scripts. The options are properties of this script's configuration object which can be created in a user's personal JS with the following lines of code above the other script's import. If importing more than one script that uses this script, place this configuration above the top-most import of those other scripts.
To set a particular option, place a line of code between this script's configuration object and the other script's import. It should follow the following format where property is replaced by the option name as listed in the table below and value is replaced by the desired value.
This example sets default_limit to 50 (the maximum allowed value). This will minimize the total number of requests sent to Fandom's notification service.
A user has used the @-Mentions feature in the opening post of a Discussions thread to get your attention.
mode_limit
string
"event"
This option limits the retrieval mode that scripts may use. Each mode retrieves a different level of detail. As the amount of detail increases, so does the number of requests made and the amount of data. This means it may take longer to get results back. Scripts will be allowed to use modes that ask for fewer details. Here is a list of the available modes in order from least to most detailed.
Value
Description
"notification"
Each notification is retrieved once and the returned data will contain the latest event and no more than 5 users. The users are those who most-recently triggered an event associated with the notification.
"user"
Each notification will be retrieved as many times as necessary to obtain a complete list of users. The returned data will contain the latest event as well as any other additional events obtained while retrieving the complete list of users.
"event"
Each notification will be retrieved as many times as necessary to obtain all associated events. The returned data will also contain a complete list of users.
default_mode
string
"notification"
This option sets the default retrieval mode. See the description for mode_limit for a table of recognized values.
limit_limit
number
50
This option limits the limit that scripts can set on requests for notifications. The limit of a request controls the maximum number of notifications that Fandom's notification service will return for each page (i.e. group) of notifications. The service returns one page for each request and thus a high limit means more data for each request. Additional requests for more pages will be made until either all notifications have been retrieved or the specified number of pages has been reached; whichever occurs first. In the event that all notifications are retrieved, a high limit means fewer requests and therefore, probably, a lower loading time. In the event that a specified number of pages is retrieved, a high limit means more notifications will be retrieved. Scripts will be allowed to set a lower limit. The value must be a positive integer and may not exceed the hard limit set by Fandom's notification service (currently 50).
default_limit
number
Fandom's default (currently 10)
This option sets the default request limit. See the description for limit_limit for details on acceptable values.
count_limit
number
Infinity
This option limits the number of pages this script will retrieve for each request from other scripts. See the description for limit_limit for details on how the number of pages, request limit, and number of notifications are related. Scripts will be allowed to set a lower limit. The value must be a positive integer, 0, or Infinity (default). If set to 0, scripts will simply receive an empty list of notifications.
default_count
number
count_limit
This option sets the default number of pages this script will attempt to retrieve for each request from other scripts. See the description for limit_limit for details on how the number of pages, request limit, and number of notifications are related. Scripts will be allowed to set any other valid value for their own requests. The value must be either 0 or a positive integer no greater than count_limit. If set to 0, scripts that do not set their own value will simply receive an empty list of notifications.
Usage
Client scripts submit requests and receive results using mw.hook. To keep things short, this documentation will use shortened names for the hooks. To get the full names, add andrewds1021.get_on_site_notifications. as a prefix. The hook ready is fired by this script once it has completed its set-up and is ready to start receiving requests. As part of the set-up, it performs an initial run using the default request parameter values. The results are returned so any client that does not have specific requirements and does not need to update its list of notifications can attempt to avoid making its own request by listening for the initial results. The initial results will have a counter value of 0 in the returned request information. The script returns results by firing the hook done with the list of notifications as the first argument and the request information as the second argument. Clients can submit requests by firing the hook run with the optional request configuration as the first (and only) argument. In order to avoid overwhelming Fandom's notification service, client requests are processed serially in the order they are received. Similarly, requests to the notification service are sent serially. In short, this script should have no more than one active request with the notification service at any given time.
Request Configuration
Because data passed through hooks can be accessed by other scripts, it is recommended that the client deep freeze the request configuration prior to firing the run hook. The configuration should be an object with any or all of the following properties. Remember that a client's options may be limited by the user as described in the configuration section.
Key
Data Type
Description
id
anything
This is a dummy value that the script simply passes right back in the results. A client can use this to help identify the results from a submitted request.
mode
string
This value controls the level of detail retrieved. Please see the table in the script configuration section for a list of recognized modes. Because of how the notification service works, retrieving additional details requires at least one separate request for each additional user/event. As such, requesting additional details could significantly increase the total number of requests made to the notification service. It is suggested that clients request as few details as possible.
types
string or array of strings
This value controls the types of notifications retrieved. A list of recognized types can be found in the table of the script configuration section. Fandom may add additional types in the future. Should this happen, scripts can request the new types whether or not the list on this page is updated. The script does not check the validity of the value submitted by a client other than ensuring that it is a string or array of strings.
This value is used to limit results to events that occurred prior to the specified datetime. Remember that events are cleared from Fandom's systems after a certain amount of time so providing a datetime too far in the past will simply result in an empty notification list.
limit
number
This value sets the maximum number of notifications the notification service will return for each page of notifications. The script will make additional requests as needed to retrieve all notifications or until the specified number of pages has been reached; whichever occurs first. Currently, the maximum value allowed by the notification service is 50.
page
number
This value sets the starting page (1-indexed) for notification retrieval. When there are more than limit notifications that meet the other criteria, each page (potentially except the last page) contains limit results. Notifications are returned most-recent first so setting page to some number, n, with limit set to some number, m, will omit the (n - 1) * m most-recent notifications from the results.
page_count
number
This value sets the maximum number of pages that will be retrieved. If set to 0, the result will be an empty notification list.
Request Information
Information about the request is returned as the second argument to the done hook. Aside from id, it is a deep frozen object. The returned information provides a means to match the results with a specific request as well as verify certain parameters of the request which may have been limited due to a user's individual configuration.
Key
Data Type
Description
counter
number
This is just an index (0-indexed) to keep track of how many client requests have been processed. Clients likely will have no use for this except to check for the automatically generated initial request as described earlier which is indicated by the value 0. The value should always be a positive integer or 0.
id
anything
This is whatever was passed in as the id of the corresponding request. The script should not alter this value. However, non-primitive values that have not been deep frozen are at risk of interference from other scripts.
mode
string
This is the retrieval mode that was used for the request. Please see the table in the script configuration section for a list of possible values.
types
array of strings
This is the list of notification types that was used for the request. The returned strings indicate what was searched for; not necessarily what was found.
time
string
This is the timestamp that was used for the request. Events that occurred at or after this timestamp are not included in the results. The string is generated from a JavaScript Date object using toISOString().
limit
number or undefined
This is the limit that was used for the notification service requests of the corresponding client request. If the value is undefined, then the notification service's default limit was used. Otherwise, this value should always be a positive integer.
page
number
This is the page (1-indexed) that retrieval of results started from. For more details on what this means, please see the description of the similarly named property in the request configuration section. This value should always be a positive integer.
page_count
number
This is the maximum number of pages that could have been retrieved for the results. The actual number of pages retrieved can be calculated using limit and the length of the notifications list.
error
object or omitted
In its current state, the notification service frequently returns 500 and 503 errors when attempting to retrieve a large number of notifications. When this happens, the script simply moves on to the next step. The presence of this object and its contents allow clients to determine what kind of errors occurred if any. If this object is omitted, then no errors occurred.
Key
Data Type
Description
page
number or omitted
This is the page (1-indexed) at which retrieval of initial notification data failed. The number is not relative to the starting page for returned results. Notifications contained on this and subsequent pages are not included in the results. If this string is omitted, then initial data was successfully retrieved for either all notifications or the specified page count; whichever came first.
notifications
array of objects or omitted
After retrieving initial notification data, additional details may be retrieved for notifications in order to provide a complete list of actors or events. This list contains notifications for which errors were encountered while attempting to retrieve additional details. The object references in this list refer to the exact same objects referred to by the returned notification data. As such, a client can use == and functions such as indexOf() to check if an error occurred while attempting to retrieve additional details for a particular notification. If this array is omitted, then all attempts to retrieve additional details were successful.
Notifications
The list of notifications is returned as the first argument to the done hook. It is a deep frozen array of notification objects. A notification object is returned as it was provided by the notification service except when additional details were retrieved due to the use of a mode other than "notification". The script alters the relevant notification object by appending additional users to latestActors and by adding otherEvents. At any level of the notification object structure, properties that are irrelevant or unused might be omitted. The following information regarding the structure is provided as guidance. Fandom may make changes which may or may not be reflected in this documentation.
Key
Data Type
Description
type
string
This is the type of the notification. The values are different from those previously mentioned in this documentation but are related by a 1-to-1 relationship.
Value
Notification Type
announcement-notification
announcement-target
article-comment-at-mention-notification
article-comment-at-mention
article-comment-reply-notification
article-comment-reply
article-comment-reply-at-mention-notification
article-comment-reply-at-mention
replies-notification
discussion-post
upvote-notification
discussion-upvote
message-wall-reply-notification
message-wall-post
message-wall-post-notification
message-wall-thread
post-at-mention-notification
post-at-mention
talk-page-notification
talk-page-message
thread-at-mention-notification
thread-at-mention
community
object
the wiki from which the notification originated
Key
Data Type
Description
id
string
numeric ID of the wiki
name
string
name of the wiki
refersTo
object
the content the events are in response to
Key
Data Type
Description
uri
string
URL of the content
createdBy
string
numeric ID of the user who created the content
createdByName
string
name of the user who created the content
type
string
content type
title
string
title of the content
snippet
string
content preview
events
object
information regarding the events
Key
Data Type
Description
total
number
number of events
totalUniqueActors
number
number of users
latestActors
array of objects
This is a list of the most-recent users who caused an event since the previous notification (if one exists) was marked as read. Using "notification" mode, this list is limited to the 5 most-recent users.
Key
Data Type
Description
id
string
numeric ID of the user
name
string
name of the user
avatarUrl
string
URL of the user's avatar
latestEvent
object
the most-recent event
Key
Data Type
Description
when
string
This is the timestamp of the event and is formatted as YYYY-MM-DDTHH:mm:ss.sssZ. This is, perhaps not by coincidence, the format returned by toISOString().
causedBy
object
This is the user who caused the event. See the description for latestActors for the structure of this object.
uri
string
URL of the event
type
string
This is the event type. See the script configuration section for a list of the expected values.
snippet
string
content preview for the event
otherEvents
array of objects
This is a list of other events associated with the notification. The list is built from available data while retrieving additional notification details. If "notification" mode was used, this property is omitted. An empty array indicates that the script may not have been able to retrieve additional details. See the description for latestEvent for the structure of the objects in this array.
metadata
string
JSON representing the following object
Key
Data Type
Description
wallOwnerName
string
name of the user who "owns" the message wall
read
boolean
whether or not the notification has been read
Used By
If you have developed a script that uses this script, feel free to add your script to this list.
It seems that using a large limit value for notification requests intermittently causes database errors and failures. As such, the use of a large request limit may result in early termination of notification retrieval.