Merge pull request #78 from globekeeper/DEV-955/LatestKeysUploadTs

🚸 Update `LatestKeysUploadTs` on Cross Signing Keys Uploads
This commit is contained in:
Daniel Aloni 2023-09-27 12:13:36 +03:00 committed by GitHub
commit c19093d211
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 0 deletions

View file

@ -1667,6 +1667,24 @@ func TestKeys(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
dataReq := uapi.QueryAccountDataRequest{
UserID: alice.ID,
DataType: "account_data",
RoomID: "",
}
res := uapi.QueryAccountDataResponse{}
if err = userAPI.QueryAccountData(processCtx.Context(), &dataReq, &res); err != nil {
t.Fatal(err)
}
var accoundData uapi.AccountData
err = json.Unmarshal(res.GlobalAccountData["account_data"], &accoundData)
if err != nil {
t.Fatal(err)
}
if accoundData.LatestKeysUploadTs == 0 ||
time.Now().UnixMilli()-accoundData.LatestKeysUploadTs > 5*time.Second.Milliseconds() {
t.Fatal(err)
}
// tests `/keys/query` // tests `/keys/query`
dev, err := oc.GetOrFetchDevice(ctx, id.UserID(alice.ID), id.DeviceID(accessTokens[alice].deviceID)) dev, err := oc.GetOrFetchDevice(ctx, id.UserID(alice.ID), id.DeviceID(accessTokens[alice].deviceID))

View file

@ -15,7 +15,10 @@
package routing package routing
import ( import (
"encoding/json"
"fmt"
"net/http" "net/http"
"time"
"github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
@ -99,6 +102,52 @@ func UploadCrossSigningDeviceKeys(
} }
} }
// Following additional logic is implemented to follow the [Notion PRD](https://globekeeper.notion.site/Account-Data-State-Event-c64c8df8025a494d86d3137d4e080ece)
if device.UserID != "" {
prevAccountDataReq := api.QueryAccountDataRequest{
UserID: device.UserID,
DataType: "account_data",
RoomID: "",
}
accountDataRes := api.QueryAccountDataResponse{}
if err := accountAPI.QueryAccountData(req.Context(), &prevAccountDataReq, &accountDataRes); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryAccountData failed")
return util.ErrorResponse(fmt.Errorf("userAPI.QueryAccountData: %w", err))
}
var accoundData api.AccountData
if len(accountDataRes.GlobalAccountData) != 0 {
err := json.Unmarshal(accountDataRes.GlobalAccountData["account_data"], &accoundData)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: spec.InternalServerError{Err: err.Error()},
}
}
}
accoundData.LatestKeysUploadTs = time.Now().UnixMilli()
newAccountData, err := json.Marshal(accoundData)
if err != nil {
return util.JSONResponse{
Code: http.StatusInternalServerError,
JSON: spec.InternalServerError{Err: err.Error()},
}
}
dataReq := api.InputAccountDataRequest{
UserID: device.UserID,
DataType: "account_data",
RoomID: "",
AccountData: json.RawMessage(newAccountData),
}
dataRes := api.InputAccountDataResponse{}
if err := accountAPI.InputAccountData(req.Context(), &dataReq, &dataRes); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("userAPI.InputAccountData on LatestKeysUploadTs update failed")
return util.ErrorResponse(err)
}
logger := util.GetLogger(req.Context()).WithField("user_id", device.UserID)
logger.Info("updated latestKeysUploadTs field in account data")
}
return util.JSONResponse{ return util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,
JSON: struct{}{}, JSON: struct{}{},

View file

@ -296,6 +296,12 @@ type QueryAccountDataResponse struct {
RoomAccountData map[string]map[string]json.RawMessage // room -> type -> data RoomAccountData map[string]map[string]json.RawMessage // room -> type -> data
} }
// Custom Connnect AccountData information
type AccountData struct {
IsProfileFilled bool `json:"isProfileFilled"`
LatestKeysUploadTs int64 `json:"latestKeysUploadTs"`
}
// QueryDevicesRequest is the request for QueryDevices // QueryDevicesRequest is the request for QueryDevices
type QueryDevicesRequest struct { type QueryDevicesRequest struct {
UserID string UserID string