Add last_seen_ip and last_seen_ts to /devices response (#1592)

This commit is contained in:
alexkursell 2020-11-20 04:26:50 -05:00 committed by GitHub
parent 6353b0b7e4
commit 13cbd50dc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 ""
}