Extension:Echo/Creating a new notification type

Notifications system allows logged in users to receive alerts and notices about their interactions with connected wikis. In Wikimedia projects, this means wikis in the cluster, but the Notifications system can also be used by third-parties - either in a single wiki or a series of connected wikis to utilize the cross-wiki notification system

The system allows extension developers the ability to add new notification types that users can receive under certain circumstances. This document will outline the way to create such notification, explain best practices and formulate the known pitfalls to avoid.

Introduction to how notifications work
Notifications are, very generally, made of two concepts - the Event and the PresentationModel. The event stores details of the triggered event and the presentation model defines the way it is presented in the user interface.

EchoEvent
EchoEvent class defines general events, collects information about the related page, user and wiki and inserts them into the database. Events are general, and one even may define multiple views. For example, mentioning 4 people will create a single event, that will then be referenced (and 'trigger' a notification) for the four mentioned users. All of their individual notifications, however, will reference the same event.

EchoPresentationModel
Every type of notifications requires a presentation model, a way to define what the notification will display, where it will link to, and what secondary actions is will present. The front-end of the notifications system (the notifications popup and the Special:Notifications page) read that information to produce a proper display of notification.

The presentation of a notification type is always based on the base definition in EchoEventPresentationModel. That class defines the base behavior of all notifications, and each child notification must extend it, and then adjust the details to its needs.

How events work
If you are creating a new notification type, you should first create an event. The logical process is: ''Note: This is a slight generalization of the process for the sake of clarity. Explaining how things work in practice with database table rows (per user, wiki, cross-wiki, etc) is outside the scope of this tutorial.''
 * 1) When relevant, your code creates the event using
 * 2) As part of the definition of the event you should detail which users receive a notification from this event. This is defined by the parameter   (For an example of this, see Flow's 'user-locators' method used to locate users who are following a title)
 * 3) The event is stored in the database, and as notifications for the relevant users
 * 4) When users request their notification list from the API, the system looks for the relevant presentation model and creates the data - title, primary link, secondary links, relevant user, etc.
 * 5) The front-end uses the structured data to present the notification.

Event definition
Events contain the following definitions that are stored in  in the hook The role of user-locators and user-filters is important to make sure we don't produce double-notification from a single event to users. For example, if a new topic is created in Flow, we want to notify all those who follow the board - but we want to ignore users who were mentioned, because 'mention' will produce its own event.

Note: When you create new events, try to make sure you filter out potential colliding events for your notified users, otherwise, they will receive two notifications for the same event.

Event creation details
When we create and trigger the event, we need to supply details as well:

Creating new notification type
Let's say we have an extension that keeps a list of users who are interested in new titles created with specific words in them. The extension allows users to add and remove themselves from a watch-list for a certain list of words in a title, and listens to the hook for creating new pages on the wiki to identify relevant pages. When a relevant page is created, the extension code will create an event with the relevant details, and notify the users that are on the list for that combination of words.

This section will demonstrate how to create this new notification type:
 * 1) Define a function to identify relevant users to notify
 * 2) Create an event
 * 3) Create a presentation model

Identify relevant users to notify
Notification system will try to notify relevant users, but it can't guess who those are. The code that creates the event needs to supply that information. To do that, we should create a function that is then inserted into the definition of the new event.

In this case, we will want to go over the list of users and add them all:

Defining the event
We need to make sure our event is defined in the system. This is done by listening to the hook  and defining the event definition: You can see another example of this in the Thank extension.

Adding code to trigger the event
Now that we have a way to identify who we want to notify, we need to actually trigger this event. Our hypothetical extension listens to page creation hook and then checks to see if the new title has words that fit the list. However way the extension code does this, once it identified a title that fits a list, it will create an event and notify the relevant users.

The code below skips the actual operation of listening to new page creation and checking the words, and jumps straight into defining the new event: You can see an example of filtering out mentioned users in Flow's event for flow-new-topic.