summary refs log tree commit diff
path: root/synapse/rest/pusher.py
blob: 85d0d1c8cd6cf6be9da2600080bb04dcc375e25a (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
# -*- 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 twisted.internet import defer

from synapse.api.errors import SynapseError, Codes
from synapse.push import PusherConfigException
from base import RestServlet, client_path_pattern

import json


class PusherRestServlet(RestServlet):
    PATTERN = client_path_pattern("/pushers/(?P<pushkey>[\w]*)$")

    @defer.inlineCallbacks
    def on_PUT(self, request, pushkey):
        user = yield self.auth.get_user_by_req(request)

        content = _parse_json(request)

        reqd = ['kind', 'app', 'app_display_name', 'device_display_name', 'data']
        missing = []
        for i in reqd:
            if i not in content:
                missing.append(i)
        if len(missing):
            raise SynapseError(400, "Missing parameters: "+','.join(missing), errcode=Codes.MISSING_PARAM)

        pusher_pool = self.hs.get_pusherpool()
        try:
            pusher_pool.add_pusher(user_name=user.to_string(),
                                     kind=content['kind'],
                                     app=content['app'],
                                     app_display_name=content['app_display_name'],
                                     device_display_name=content['device_display_name'],
                                     pushkey=pushkey,
                                     data=content['data'])
        except PusherConfigException as pce:
            raise SynapseError(400, "Config Error: "+pce.message, errcode=Codes.MISSING_PARAM)

        defer.returnValue((200, {}))

    def on_OPTIONS(self, request):
        return (200, {})

# 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)

def register_servlets(hs, http_server):
    PusherRestServlet(hs).register(http_server)