'Create a \'modmail\' event for event-collector.
message: An r2.models.Message object
request: pylons.request of the request that created the message
context: pylons.tmpl_context of the request that created the message'

@squelch_exceptions
@sampled('events_collector_modmail_sample_rate')
def modmail_event(self, message, request=None, context=None):

    from r2.models import Account, Message
    sender = message.author_slow
    sr = message.subreddit_slow
    sender_is_moderator = sr.is_moderator_with_perms(sender, 'mail')
    if message.first_message:
        first_message = Message._byID(message.first_message, data=True)
    else:
        first_message = message
    event = Event(topic='message_events', event_type='ss.send_message', time=message._date, request=request, context=context, data={'user_id': sender._id, 'user_name': sender.name})
    if (sender == Account.system_user()):
        sender_type = 'automated'
    elif sender_is_moderator:
        sender_type = 'moderator'
    else:
        sender_type = 'user'
    event.add('sender_type', sender_type)
    event.add('sr_id', sr._id)
    event.add('sr_name', sr.name)
    event.add('message_id', message._id)
    event.add('message_kind', 'modmail')
    event.add('message_fullname', message._fullname)
    event.add_text('message_body', message.body)
    event.add_text('message_subject', message.subject)
    event.add('first_message_id', first_message._id)
    event.add('first_message_fullname', first_message._fullname)
    if (request and request.POST.get('source', None)):
        source = request.POST['source']
        if (source in {'compose', 'permalink', 'modmail', 'usermail'}):
            event.add('page', source)
    if message.sent_via_email:
        event.add('is_third_party', True)
        event.add('third_party_metadata', 'mailgun')
    if (not message.to_id):
        target = sr
    else:
        target = Account._byID(message.to_id, data=True)
    event.add_target_fields(target)
    self.save_event(event)
