Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Operate
Environments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Matrix
Commits
406f7aa0
Commit
406f7aa0
authored
8 years ago
by
Richard van der Hoff
Browse files
Options
Downloads
Patches
Plain Diff
Implement GET /device/{deviceId}
parent
34f56b40
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
synapse/handlers/device.py
+37
-9
37 additions, 9 deletions
synapse/handlers/device.py
synapse/rest/client/v2_alpha/devices.py
+25
-0
25 additions, 0 deletions
synapse/rest/client/v2_alpha/devices.py
tests/handlers/test_device.py
+27
-10
27 additions, 10 deletions
tests/handlers/test_device.py
with
89 additions
and
19 deletions
synapse/handlers/device.py
+
37
−
9
View file @
406f7aa0
...
...
@@ -12,7 +12,8 @@
# 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.api.errors
import
StoreError
from
synapse.api
import
errors
from
synapse.util
import
stringutils
from
twisted.internet
import
defer
from
._base
import
BaseHandler
...
...
@@ -65,10 +66,10 @@ class DeviceHandler(BaseHandler):
ignore_if_known
=
False
,
)
defer
.
returnValue
(
device_id
)
except
StoreError
:
except
errors
.
StoreError
:
attempts
+=
1
raise
StoreError
(
500
,
"
Couldn
'
t generate a device ID.
"
)
raise
errors
.
StoreError
(
500
,
"
Couldn
'
t generate a device ID.
"
)
@defer.inlineCallbacks
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
())
)
for
device_id
in
devices
.
keys
():
ip
=
ips
.
get
((
user_id
,
device_id
),
{})
devices
[
device_id
].
update
({
"
last_seen_ts
"
:
ip
.
get
(
"
last_seen
"
),
"
last_seen_ip
"
:
ip
.
get
(
"
ip
"
),
})
for
device
in
devices
.
values
():
_update_device_from_client_ips
(
device
,
ips
)
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
"
),
})
This diff is collapsed.
Click to expand it.
synapse/rest/client/v2_alpha/devices.py
+
25
−
0
View file @
406f7aa0
...
...
@@ -47,5 +47,30 @@ class DevicesRestServlet(RestServlet):
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
):
DevicesRestServlet
(
hs
).
register
(
http_server
)
DeviceRestServlet
(
hs
).
register
(
http_server
)
This diff is collapsed.
Click to expand it.
tests/handlers/test_device.py
+
27
−
10
View file @
406f7aa0
...
...
@@ -19,6 +19,8 @@ import synapse.handlers.device
import
synapse.storage
from
tests
import
unittest
,
utils
user1
=
"
@boris:aaa
"
user2
=
"
@theresa:bbb
"
class
DeviceTestCase
(
unittest
.
TestCase
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
...
...
@@ -78,16 +80,7 @@ class DeviceTestCase(unittest.TestCase):
@defer.inlineCallbacks
def
test_get_devices_by_user
(
self
):
# check this works for both devices which have a recorded client_ip,
# 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
"
)
yield
self
.
_record_users
()
res
=
yield
self
.
handler
.
get_devices_by_user
(
user1
)
self
.
assertEqual
(
3
,
len
(
res
.
keys
()))
...
...
@@ -113,6 +106,30 @@ class DeviceTestCase(unittest.TestCase):
"
last_seen_ts
"
:
3000000
,
},
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
def
_record_user
(
self
,
user_id
,
device_id
,
display_name
,
access_token
=
None
,
ip
=
None
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment