summary refs log tree commit diff
path: root/synapse/push/httppusher.py
blob: 9a3e0be15ebd15bf53c3d81f56f415031c91239f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
# Copyright 2014 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.push import Pusher, PusherConfigException
from synapse.http.client import SimpleHttpClient

from twisted.internet import defer

import logging

logger = logging.getLogger(__name__)


class HttpPusher(Pusher):
    def __init__(self, _hs, instance_handle, user_name, app_id,
                 app_display_name, device_display_name, pushkey, pushkey_ts,
                 data, last_token, last_success, failing_since):
        super(HttpPusher, self).__init__(
            _hs,
            instance_handle,
            user_name,
            app_id,
            app_display_name,
            device_display_name,
            pushkey,
            pushkey_ts,
            data,
            last_token,
            last_success,
            failing_since
        )
        if 'url' not in data:
            raise PusherConfigException(
                "'url' required in data for HTTP pusher"
            )
        self.url = data['url']
        self.httpCli = SimpleHttpClient(self.hs)
        self.data_minus_url = {}
        self.data_minus_url.update(self.data)
        del self.data_minus_url['url']

    @defer.inlineCallbacks
    def _build_notification_dict(self, event):
        # we probably do not want to push for every presence update
        # (we may want to be able to set up notifications when specific
        # people sign in, but we'd want to only deliver the pertinent ones)
        # Actually, presence events will not get this far now because we
        # need to filter them out in the main Pusher code.
        if 'event_id' not in event:
            defer.returnValue(None)

        ctx = yield self.get_context_for_event(event)

        d = {
            'notification': {
                'transition': 'new',
                # everything is new for now: we don't have read receipts
                'id': event['event_id'],
                'type': event['type'],
                'from': event['user_id'],
                # we may have to fetch this over federation and we
                # can't trust it anyway: is it worth it?
                #'fromDisplayName': 'Steve Stevington'
                #'counts': { -- we don't mark messages as read yet so
                # we have no way of knowing
                #    'unread': 1,
                #    'missedCalls': 2
                # },
                'devices': [
                    {
                        'app_id': self.app_id,
                        'pushkey': self.pushkey,
                        'pushkeyTs': long(self.pushkey_ts / 1000),
                        'data': self.data_minus_url
                    }
                ]
            }
        }

        if len(ctx['aliases']):
            d['notification']['roomAlias'] = ctx['aliases'][0]
        if 'name' in ctx:
            d['notification']['roomName'] = ctx['name']

        defer.returnValue(d)

    @defer.inlineCallbacks
    def dispatch_push(self, event):
        notification_dict = yield self._build_notification_dict(event)
        if not notification_dict:
            defer.returnValue([])
        try:
            resp = yield self.httpCli.post_json_get_json(self.url, notification_dict)
        except:
            logger.exception("Failed to push %s ", self.url)
            defer.returnValue(False)
        rejected = []
        if 'rejected' in resp:
            rejected = resp['rejected']
        defer.returnValue(rejected)