Edgewall Software

Changes between Initial Version and Version 1 of TracDev/Proposals/AdvancedNotification/INotificationDistributor


Ignore:
Timestamp:
Nov 17, 2012, 10:38:38 PM (11 years ago)
Author:
Peter Suter
Comment:

Document the proposed INotificationDistributor extension point.

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/Proposals/AdvancedNotification/INotificationDistributor

    v1 v1  
     1== Extension Point : ''INotificationDistributor'' ==
     2
     3||'''Interface'''||''INotificationDistributor''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.1#INotificationDistributor 1.1.1]||
     4||'''Module'''||''trac.notification''||'''Source'''||[source:trunk/trac/notification/api.py#/INotificationDistributor api.py]||
     5
     6
     7The ''INotificationDistributor'' sends [TracNotification notification] events over some distribution channel, like email.
     8
     9== Purpose ==
     10
     11Trac provides an extendible and flexible notification system, that historically has sent notifications by email. Notifications could however also be distributed by XMPP, SMS or any other messaging protocol.
     12
     13== Usage ==
     14
     15Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment].
     16
     17A simple `transports()` method lists the names of the supported transports (protocols / distribution channels). The `distribute()` method distributes a message to one or more recipients. It receives the following parameters:
     18* `transport`: The name of the transports that should be used. One of those returned by `transports()`.
     19* `recipients`: A list of `(sid, authenticated, address, format)` tuples.
     20  * `sid`: The Trac session ID of the recipient (or `None`).
     21  * `authenticated`: `1` if the Trac session ID is authenticated, `0` otherwise.
     22  * `address`: A protocol specific address to send the notification to. If it is `None` the implementation might still be able to send the message to the recipient using the `sid` / `authenticated` information somehow. (E.g. the `EmailDistributor` falls back to [wiki:IEmailAddressResolver IEmailAddressResolver] implementations.)
     23  * `format`: MIME type of the message format to be used.
     24* `event`: A `trac.notification.api.NotificationEvent` instance describing the event about which the recipients should be notified.
     25
     26The `event` is supposed to be formatted into a message in the given `format` using a [wiki:INotificationFormatter INotificationFormatter].
     27
     28== Examples ==
     29
     30The following example distributes notifications by SMS. (The `sms` library does not exist. Several commercial SMS services provide real APIs.)
     31
     32{{{#!python
     33from itertools import groupby
     34from operator import itemgetter
     35
     36from trac.core import *
     37from trac.notification.api import INotificationDistributor, \
     38                                  INotificationFormatter
     39
     40import sms
     41
     42class SmsNotificationDistributor(Component):
     43
     44    implements(INotificationDistributor)
     45
     46    formatters = ExtensionPoint(INotificationFormatter)
     47
     48    # INotificationDistributor methods
     49
     50    def transports(self):
     51        return ['sms']
     52
     53    def distribute(self, transport, recipients, event):
     54        if transport != 'sms':
     55            return
     56
     57        # Group by message format:
     58        for format, group in groupby(recipients, itemgetter(3)):
     59            formatter = None
     60            for f in self.formatters:
     61                for style in f.styles(transport, event.realm):
     62                    if style == format:
     63                        formatter = f
     64            if formatter:
     65                message = formatter.format(transport, format, event)
     66                for sid, authenticated, address, format in group:
     67                    if address:                       
     68                        sms.send(message, address)
     69}}}
     70
     71== Available Implementations ==
     72
     73Only `trac.notification.mail.EmailDistributor` is part of core Trac.
     74
     75`XmppDistributor` will be part of th:AnnouncerPlugin.
     76
     77== Additional Information and References ==
     78
     79 * [http://www.edgewall.org/docs/trac-trunk/epydoc/trac.notification.api.INotificationDistributor-class.html epydoc]
     80 * [http://www.edgewall.org/docs/trac-trunk/html/api/trac_notification.html#trac.notification.api.INotificationDistributor API Reference]
     81 * Related to the [wiki:INotificationFormatter INotificationFormatter]
     82 * This interface originated in th:AnnouncerPlugin as `IAnnouncementDistributor`.