forked from MirrorHub/synapse
Implement GET /device/{deviceId}
This commit is contained in:
parent
34f56b40fd
commit
406f7aa0f6
3 changed files with 89 additions and 19 deletions
|
@ -12,7 +12,8 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
from synapse.api.errors import StoreError
|
|
||||||
|
from synapse.api import errors
|
||||||
from synapse.util import stringutils
|
from synapse.util import stringutils
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
from ._base import BaseHandler
|
from ._base import BaseHandler
|
||||||
|
@ -65,10 +66,10 @@ class DeviceHandler(BaseHandler):
|
||||||
ignore_if_known=False,
|
ignore_if_known=False,
|
||||||
)
|
)
|
||||||
defer.returnValue(device_id)
|
defer.returnValue(device_id)
|
||||||
except StoreError:
|
except errors.StoreError:
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
raise StoreError(500, "Couldn't generate a device ID.")
|
raise errors.StoreError(500, "Couldn't generate a device ID.")
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_devices_by_user(self, user_id):
|
def get_devices_by_user(self, user_id):
|
||||||
|
@ -88,11 +89,38 @@ class DeviceHandler(BaseHandler):
|
||||||
devices=((user_id, device_id) for device_id in devices.keys())
|
devices=((user_id, device_id) for device_id in devices.keys())
|
||||||
)
|
)
|
||||||
|
|
||||||
for device_id in devices.keys():
|
for device in devices.values():
|
||||||
ip = ips.get((user_id, device_id), {})
|
_update_device_from_client_ips(device, ips)
|
||||||
devices[device_id].update({
|
|
||||||
"last_seen_ts": ip.get("last_seen"),
|
|
||||||
"last_seen_ip": ip.get("ip"),
|
|
||||||
})
|
|
||||||
|
|
||||||
defer.returnValue(devices)
|
defer.returnValue(devices)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def get_device(self, user_id, device_id):
|
||||||
|
""" Retrieve the given device
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_id (str):
|
||||||
|
device_id (str)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
defer.Deferred: dict[str, X]: info on the device
|
||||||
|
Raises:
|
||||||
|
errors.NotFoundError: if the device was not found
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
device = yield self.store.get_device(user_id, device_id)
|
||||||
|
except errors.StoreError, e:
|
||||||
|
raise errors.NotFoundError
|
||||||
|
ips = yield self.store.get_last_client_ip_by_device(
|
||||||
|
devices=((user_id, device_id),)
|
||||||
|
)
|
||||||
|
_update_device_from_client_ips(device, ips)
|
||||||
|
defer.returnValue(device)
|
||||||
|
|
||||||
|
|
||||||
|
def _update_device_from_client_ips(device, client_ips):
|
||||||
|
ip = client_ips.get((device["user_id"], device["device_id"]), {})
|
||||||
|
device.update({
|
||||||
|
"last_seen_ts": ip.get("last_seen"),
|
||||||
|
"last_seen_ip": ip.get("ip"),
|
||||||
|
})
|
||||||
|
|
|
@ -47,5 +47,30 @@ class DevicesRestServlet(RestServlet):
|
||||||
defer.returnValue((200, {"devices": devices}))
|
defer.returnValue((200, {"devices": devices}))
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceRestServlet(RestServlet):
|
||||||
|
PATTERNS = client_v2_patterns("/devices/(?P<device_id>[^/]*)$",
|
||||||
|
releases=[], v2_alpha=False)
|
||||||
|
|
||||||
|
def __init__(self, hs):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
hs (synapse.server.HomeServer): server
|
||||||
|
"""
|
||||||
|
super(DeviceRestServlet, self).__init__()
|
||||||
|
self.hs = hs
|
||||||
|
self.auth = hs.get_auth()
|
||||||
|
self.device_handler = hs.get_device_handler()
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def on_GET(self, request, device_id):
|
||||||
|
requester = yield self.auth.get_user_by_req(request)
|
||||||
|
device = yield self.device_handler.get_device(
|
||||||
|
requester.user.to_string(),
|
||||||
|
device_id,
|
||||||
|
)
|
||||||
|
defer.returnValue((200, device))
|
||||||
|
|
||||||
|
|
||||||
def register_servlets(hs, http_server):
|
def register_servlets(hs, http_server):
|
||||||
DevicesRestServlet(hs).register(http_server)
|
DevicesRestServlet(hs).register(http_server)
|
||||||
|
DeviceRestServlet(hs).register(http_server)
|
||||||
|
|
|
@ -19,6 +19,8 @@ import synapse.handlers.device
|
||||||
import synapse.storage
|
import synapse.storage
|
||||||
from tests import unittest, utils
|
from tests import unittest, utils
|
||||||
|
|
||||||
|
user1 = "@boris:aaa"
|
||||||
|
user2 = "@theresa:bbb"
|
||||||
|
|
||||||
class DeviceTestCase(unittest.TestCase):
|
class DeviceTestCase(unittest.TestCase):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -78,16 +80,7 @@ class DeviceTestCase(unittest.TestCase):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def test_get_devices_by_user(self):
|
def test_get_devices_by_user(self):
|
||||||
# check this works for both devices which have a recorded client_ip,
|
yield self._record_users()
|
||||||
# and those which don't.
|
|
||||||
user1 = "@boris:aaa"
|
|
||||||
user2 = "@theresa:bbb"
|
|
||||||
yield self._record_user(user1, "xyz", "display 0")
|
|
||||||
yield self._record_user(user1, "fco", "display 1", "token1", "ip1")
|
|
||||||
yield self._record_user(user1, "abc", "display 2", "token2", "ip2")
|
|
||||||
yield self._record_user(user1, "abc", "display 2", "token3", "ip3")
|
|
||||||
|
|
||||||
yield self._record_user(user2, "def", "dispkay", "token4", "ip4")
|
|
||||||
|
|
||||||
res = yield self.handler.get_devices_by_user(user1)
|
res = yield self.handler.get_devices_by_user(user1)
|
||||||
self.assertEqual(3, len(res.keys()))
|
self.assertEqual(3, len(res.keys()))
|
||||||
|
@ -113,6 +106,30 @@ class DeviceTestCase(unittest.TestCase):
|
||||||
"last_seen_ts": 3000000,
|
"last_seen_ts": 3000000,
|
||||||
}, res["abc"])
|
}, res["abc"])
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def test_get_device(self):
|
||||||
|
yield self._record_users()
|
||||||
|
|
||||||
|
res = yield self.handler.get_device(user1, "abc")
|
||||||
|
self.assertDictContainsSubset({
|
||||||
|
"user_id": user1,
|
||||||
|
"device_id": "abc",
|
||||||
|
"display_name": "display 2",
|
||||||
|
"last_seen_ip": "ip3",
|
||||||
|
"last_seen_ts": 3000000,
|
||||||
|
}, res)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def _record_users(self):
|
||||||
|
# check this works for both devices which have a recorded client_ip,
|
||||||
|
# and those which don't.
|
||||||
|
yield self._record_user(user1, "xyz", "display 0")
|
||||||
|
yield self._record_user(user1, "fco", "display 1", "token1", "ip1")
|
||||||
|
yield self._record_user(user1, "abc", "display 2", "token2", "ip2")
|
||||||
|
yield self._record_user(user1, "abc", "display 2", "token3", "ip3")
|
||||||
|
|
||||||
|
yield self._record_user(user2, "def", "dispkay", "token4", "ip4")
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _record_user(self, user_id, device_id, display_name,
|
def _record_user(self, user_id, device_id, display_name,
|
||||||
access_token=None, ip=None):
|
access_token=None, ip=None):
|
||||||
|
|
Loading…
Reference in a new issue