aboutsummaryrefslogtreecommitdiff
path: root/clientapi
diff options
context:
space:
mode:
authoralexkursell <alexkursell@gmail.com>2020-11-20 04:26:50 -0500
committerGitHub <noreply@github.com>2020-11-20 09:26:50 +0000
commit13cbd50dc26721792a36ab47c17e62b7bb965a85 (patch)
tree8ad7eba523759a0176138748ae80d455e7164fea /clientapi
parent6353b0b7e42d65d92368b93c021b3a744c03214b (diff)
Add last_seen_ip and last_seen_ts to /devices response (#1592)
Diffstat (limited to 'clientapi')
-rw-r--r--clientapi/routing/device.go24
1 files changed, 23 insertions, 1 deletions
diff --git a/clientapi/routing/device.go b/clientapi/routing/device.go
index d50c73b3..6adaa769 100644
--- a/clientapi/routing/device.go
+++ b/clientapi/routing/device.go
@@ -16,6 +16,7 @@ package routing
import (
"io/ioutil"
+ "net"
"net/http"
"github.com/matrix-org/dendrite/clientapi/auth"
@@ -32,7 +33,7 @@ type deviceJSON struct {
DeviceID string `json:"device_id"`
DisplayName string `json:"display_name"`
LastSeenIP string `json:"last_seen_ip"`
- LastSeenTS uint64 `json:"last_seen_ts"`
+ LastSeenTS int64 `json:"last_seen_ts"`
}
type devicesJSON struct {
@@ -79,6 +80,8 @@ func GetDeviceByID(
JSON: deviceJSON{
DeviceID: targetDevice.ID,
DisplayName: targetDevice.DisplayName,
+ LastSeenIP: stripIPPort(targetDevice.LastSeenIP),
+ LastSeenTS: targetDevice.LastSeenTS,
},
}
}
@@ -102,6 +105,8 @@ func GetDevicesByLocalpart(
res.Devices = append(res.Devices, deviceJSON{
DeviceID: dev.ID,
DisplayName: dev.DisplayName,
+ LastSeenIP: stripIPPort(dev.LastSeenIP),
+ LastSeenTS: dev.LastSeenTS,
})
}
@@ -230,3 +235,20 @@ func DeleteDevices(
JSON: struct{}{},
}
}
+
+// stripIPPort converts strings like "[::1]:12345" to "::1"
+func stripIPPort(addr string) string {
+ ip := net.ParseIP(addr)
+ if ip != nil {
+ return addr
+ }
+ host, _, err := net.SplitHostPort(addr)
+ if err != nil {
+ return ""
+ }
+ ip = net.ParseIP(host)
+ if ip != nil {
+ return host
+ }
+ return ""
+}