2014-11-19 19:20:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-11-19 19:20:59 +01: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 twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError, Codes
|
|
|
|
from synapse.push import PusherConfigException
|
2015-12-01 18:34:32 +01:00
|
|
|
from .base import ClientV1RestServlet, client_path_patterns
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-02-11 15:23:10 +01:00
|
|
|
import simplejson as json
|
2015-12-07 12:52:20 +01:00
|
|
|
import logging
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 12:52:20 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 12:57:48 +01:00
|
|
|
|
2015-01-28 15:10:46 +01:00
|
|
|
class PusherRestServlet(ClientV1RestServlet):
|
2015-12-01 18:34:32 +01:00
|
|
|
PATTERNS = client_path_patterns("/pushers/set$")
|
2014-11-19 19:20:59 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-12-18 15:49:22 +01:00
|
|
|
def on_POST(self, request):
|
2016-01-11 16:29:57 +01:00
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
|
|
|
user = requester.user
|
2014-11-19 19:20:59 +01:00
|
|
|
|
|
|
|
content = _parse_json(request)
|
|
|
|
|
2015-01-29 18:04:31 +01:00
|
|
|
pusher_pool = self.hs.get_pusherpool()
|
|
|
|
|
|
|
|
if ('pushkey' in content and 'app_id' in content
|
2015-02-10 17:30:48 +01:00
|
|
|
and 'kind' in content and
|
|
|
|
content['kind'] is None):
|
2015-01-29 18:04:31 +01:00
|
|
|
yield pusher_pool.remove_pusher(
|
2016-01-13 14:08:59 +01:00
|
|
|
content['app_id'], content['pushkey'], user_id=user.to_string()
|
2015-01-29 18:04:31 +01:00
|
|
|
)
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2015-02-03 17:51:07 +01:00
|
|
|
reqd = ['profile_tag', 'kind', 'app_id', 'app_display_name',
|
2015-01-16 12:24:10 +01:00
|
|
|
'device_display_name', 'pushkey', 'lang', 'data']
|
2014-11-19 19:20:59 +01:00
|
|
|
missing = []
|
|
|
|
for i in reqd:
|
|
|
|
if i not in content:
|
|
|
|
missing.append(i)
|
|
|
|
if len(missing):
|
2016-02-02 18:18:50 +01:00
|
|
|
raise SynapseError(400, "Missing parameters: " + ','.join(missing),
|
2014-12-03 14:37:02 +01:00
|
|
|
errcode=Codes.MISSING_PARAM)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
2015-12-07 13:01:00 +01:00
|
|
|
logger.debug("set pushkey %s to kind %s", content['pushkey'], content['kind'])
|
2015-12-07 12:52:20 +01:00
|
|
|
logger.debug("Got pushers request with body: %r", content)
|
|
|
|
|
2015-03-25 20:06:22 +01:00
|
|
|
append = False
|
|
|
|
if 'append' in content:
|
|
|
|
append = content['append']
|
|
|
|
|
|
|
|
if not append:
|
|
|
|
yield pusher_pool.remove_pushers_by_app_id_and_pushkey_not_user(
|
|
|
|
app_id=content['app_id'],
|
|
|
|
pushkey=content['pushkey'],
|
|
|
|
not_user_id=user.to_string()
|
|
|
|
)
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
try:
|
2014-12-18 15:49:22 +01:00
|
|
|
yield pusher_pool.add_pusher(
|
2016-01-13 14:08:59 +01:00
|
|
|
user_id=user.to_string(),
|
2016-01-11 16:29:57 +01:00
|
|
|
access_token=requester.access_token_id,
|
2015-02-03 17:51:07 +01:00
|
|
|
profile_tag=content['profile_tag'],
|
2014-12-03 14:37:02 +01:00
|
|
|
kind=content['kind'],
|
|
|
|
app_id=content['app_id'],
|
|
|
|
app_display_name=content['app_display_name'],
|
|
|
|
device_display_name=content['device_display_name'],
|
2014-12-18 15:49:22 +01:00
|
|
|
pushkey=content['pushkey'],
|
2015-01-16 12:24:10 +01:00
|
|
|
lang=content['lang'],
|
2014-12-03 14:37:02 +01:00
|
|
|
data=content['data']
|
|
|
|
)
|
2014-11-19 19:20:59 +01:00
|
|
|
except PusherConfigException as pce:
|
2016-02-02 18:18:50 +01:00
|
|
|
raise SynapseError(400, "Config Error: " + pce.message,
|
2014-12-03 14:37:02 +01:00
|
|
|
errcode=Codes.MISSING_PARAM)
|
2014-11-19 19:20:59 +01:00
|
|
|
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2014-12-03 14:37:02 +01:00
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
|
|
|
|
# XXX: C+ped from rest/room.py - surely this should be common?
|
|
|
|
def _parse_json(request):
|
|
|
|
try:
|
|
|
|
content = json.loads(request.content.read())
|
|
|
|
if type(content) != dict:
|
|
|
|
raise SynapseError(400, "Content must be a JSON object.",
|
|
|
|
errcode=Codes.NOT_JSON)
|
|
|
|
return content
|
|
|
|
except ValueError:
|
|
|
|
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
|
|
|
|
|
2014-12-03 14:37:02 +01:00
|
|
|
|
2014-11-19 19:20:59 +01:00
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
PusherRestServlet(hs).register(http_server)
|