2014-11-19 11:20:59 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2014-11-19 11:20:59 -07:00
|
|
|
#
|
|
|
|
# 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
|
2014-11-21 05:21:00 -07:00
|
|
|
|
|
|
|
from twisted.internet import defer
|
2014-11-19 11:20:59 -07:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-12-03 06:37:02 -07:00
|
|
|
|
2014-11-19 11:20:59 -07:00
|
|
|
class HttpPusher(Pusher):
|
2016-02-18 09:05:13 -07:00
|
|
|
def __init__(self, _hs, user_id, app_id,
|
2015-01-13 12:48:37 -07:00
|
|
|
app_display_name, device_display_name, pushkey, pushkey_ts,
|
|
|
|
data, last_token, last_success, failing_since):
|
2014-12-03 06:37:02 -07:00
|
|
|
super(HttpPusher, self).__init__(
|
|
|
|
_hs,
|
2016-01-13 06:08:59 -07:00
|
|
|
user_id,
|
2014-12-03 06:37:02 -07:00
|
|
|
app_id,
|
|
|
|
app_display_name,
|
|
|
|
device_display_name,
|
|
|
|
pushkey,
|
2015-01-13 12:48:37 -07:00
|
|
|
pushkey_ts,
|
2014-12-03 06:37:02 -07:00
|
|
|
data,
|
|
|
|
last_token,
|
|
|
|
last_success,
|
|
|
|
failing_since
|
|
|
|
)
|
2014-11-19 11:20:59 -07:00
|
|
|
if 'url' not in data:
|
2014-12-03 06:37:02 -07:00
|
|
|
raise PusherConfigException(
|
|
|
|
"'url' required in data for HTTP pusher"
|
|
|
|
)
|
2014-11-19 11:20:59 -07:00
|
|
|
self.url = data['url']
|
2015-12-02 04:36:02 -07:00
|
|
|
self.http_client = _hs.get_simple_http_client()
|
2014-11-21 05:21:00 -07:00
|
|
|
self.data_minus_url = {}
|
|
|
|
self.data_minus_url.update(self.data)
|
|
|
|
del self.data_minus_url['url']
|
|
|
|
|
2015-01-15 09:56:18 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 11:55:57 -07:00
|
|
|
def _build_notification_dict(self, event, tweaks, badge):
|
2014-11-21 05:21:00 -07:00
|
|
|
# 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:
|
2015-01-15 09:56:18 -07:00
|
|
|
defer.returnValue(None)
|
|
|
|
|
|
|
|
ctx = yield self.get_context_for_event(event)
|
2014-11-21 05:21:00 -07:00
|
|
|
|
2015-01-15 09:56:18 -07:00
|
|
|
d = {
|
2014-12-03 06:37:02 -07:00
|
|
|
'notification': {
|
|
|
|
'id': event['event_id'],
|
2015-02-19 03:06:17 -07:00
|
|
|
'room_id': event['room_id'],
|
2014-12-03 06:37:02 -07:00
|
|
|
'type': event['type'],
|
2015-01-29 11:51:22 -07:00
|
|
|
'sender': event['user_id'],
|
2015-01-29 09:10:35 -07:00
|
|
|
'counts': { # -- we don't mark messages as read yet so
|
|
|
|
# we have no way of knowing
|
2015-01-28 04:55:49 -07:00
|
|
|
# Just set the badge to 1 until we have read receipts
|
2016-01-13 11:55:57 -07:00
|
|
|
'unread': badge,
|
2015-01-29 09:10:35 -07:00
|
|
|
# 'missed_calls': 2
|
2015-01-28 04:55:49 -07:00
|
|
|
},
|
2014-12-18 07:49:22 -07:00
|
|
|
'devices': [
|
|
|
|
{
|
|
|
|
'app_id': self.app_id,
|
|
|
|
'pushkey': self.pushkey,
|
2015-01-20 04:52:08 -07:00
|
|
|
'pushkey_ts': long(self.pushkey_ts / 1000),
|
2015-01-23 10:07:06 -07:00
|
|
|
'data': self.data_minus_url,
|
|
|
|
'tweaks': tweaks
|
2014-12-18 07:49:22 -07:00
|
|
|
}
|
|
|
|
]
|
2014-12-03 06:37:02 -07:00
|
|
|
}
|
2014-11-21 05:21:00 -07:00
|
|
|
}
|
2015-01-26 10:27:28 -07:00
|
|
|
if event['type'] == 'm.room.member':
|
|
|
|
d['notification']['membership'] = event['content']['membership']
|
2016-01-13 06:08:59 -07:00
|
|
|
d['notification']['user_is_target'] = event['state_key'] == self.user_id
|
2015-01-29 14:59:17 -07:00
|
|
|
if 'content' in event:
|
|
|
|
d['notification']['content'] = event['content']
|
2014-11-19 11:20:59 -07:00
|
|
|
|
2015-01-15 09:56:18 -07:00
|
|
|
if len(ctx['aliases']):
|
2015-01-26 10:27:28 -07:00
|
|
|
d['notification']['room_alias'] = ctx['aliases'][0]
|
2015-01-31 05:48:06 -07:00
|
|
|
if 'sender_display_name' in ctx and len(ctx['sender_display_name']) > 0:
|
2015-01-29 11:51:22 -07:00
|
|
|
d['notification']['sender_display_name'] = ctx['sender_display_name']
|
2015-01-31 05:48:06 -07:00
|
|
|
if 'name' in ctx and len(ctx['name']) > 0:
|
2015-01-26 10:27:28 -07:00
|
|
|
d['notification']['room_name'] = ctx['name']
|
2015-01-15 09:56:18 -07:00
|
|
|
|
|
|
|
defer.returnValue(d)
|
|
|
|
|
2014-11-21 05:21:00 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-01-13 11:55:57 -07:00
|
|
|
def dispatch_push(self, event, tweaks, badge):
|
|
|
|
notification_dict = yield self._build_notification_dict(event, tweaks, badge)
|
2014-12-03 06:37:02 -07:00
|
|
|
if not notification_dict:
|
2015-01-13 12:48:37 -07:00
|
|
|
defer.returnValue([])
|
2014-11-21 05:21:00 -07:00
|
|
|
try:
|
2015-12-02 04:36:02 -07:00
|
|
|
resp = yield self.http_client.post_json_get_json(self.url, notification_dict)
|
2014-11-21 05:21:00 -07:00
|
|
|
except:
|
2015-03-06 08:32:38 -07:00
|
|
|
logger.warn("Failed to push %s ", self.url)
|
2014-11-21 05:21:00 -07:00
|
|
|
defer.returnValue(False)
|
2015-01-13 12:48:37 -07:00
|
|
|
rejected = []
|
|
|
|
if 'rejected' in resp:
|
|
|
|
rejected = resp['rejected']
|
|
|
|
defer.returnValue(rejected)
|
2015-01-28 04:55:49 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-01-13 11:55:57 -07:00
|
|
|
def send_badge(self, badge):
|
2016-01-19 11:17:23 -07:00
|
|
|
logger.info("Sending updated badge count %d to %r", badge, self.user_id)
|
2015-01-28 04:55:49 -07:00
|
|
|
d = {
|
|
|
|
'notification': {
|
|
|
|
'id': '',
|
|
|
|
'type': None,
|
2015-01-29 11:51:22 -07:00
|
|
|
'sender': '',
|
2015-01-28 04:55:49 -07:00
|
|
|
'counts': {
|
2016-01-13 11:55:57 -07:00
|
|
|
'unread': badge
|
2015-01-28 04:55:49 -07:00
|
|
|
},
|
|
|
|
'devices': [
|
|
|
|
{
|
|
|
|
'app_id': self.app_id,
|
|
|
|
'pushkey': self.pushkey,
|
|
|
|
'pushkey_ts': long(self.pushkey_ts / 1000),
|
|
|
|
'data': self.data_minus_url,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try:
|
2015-12-02 04:36:02 -07:00
|
|
|
resp = yield self.http_client.post_json_get_json(self.url, d)
|
2015-01-28 04:55:49 -07:00
|
|
|
except:
|
|
|
|
logger.exception("Failed to push %s ", self.url)
|
|
|
|
defer.returnValue(False)
|
|
|
|
rejected = []
|
|
|
|
if 'rejected' in resp:
|
|
|
|
rejected = resp['rejected']
|
2015-01-29 09:10:35 -07:00
|
|
|
defer.returnValue(rejected)
|