use "[]byte" instead of "json.RawMessage" if posible

This commit is contained in:
6543 2021-06-10 02:55:22 +02:00
parent 2c9a390fa6
commit fdc3772b52
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
31 changed files with 102 additions and 119 deletions

View file

@ -62,7 +62,7 @@ func TestUserInteractivePasswordLogin(t *testing.T) {
UserID: fmt.Sprintf("@alice:%s", serverName), UserID: fmt.Sprintf("@alice:%s", serverName),
} }
// valid password requests // valid password requests
testCases := []json.RawMessage{ testCases := [][]byte{
// deprecated form // deprecated form
[]byte(`{ []byte(`{
"auth": { "auth": {
@ -101,7 +101,7 @@ func TestUserInteractivePasswordBadLogin(t *testing.T) {
} }
// invalid password requests // invalid password requests
testCases := []struct { testCases := []struct {
body json.RawMessage body []byte
wantRes util.JSONResponse wantRes util.JSONResponse
}{ }{
{ {

View file

@ -53,7 +53,7 @@ func GetAccountData(
return util.ErrorResponse(fmt.Errorf("userAPI.QueryAccountData: %w", err)) return util.ErrorResponse(fmt.Errorf("userAPI.QueryAccountData: %w", err))
} }
var data json.RawMessage var data []byte
var ok bool var ok bool
if roomID != "" { if roomID != "" {
data, ok = dataRes.RoomAccountData[roomID][dataType] data, ok = dataRes.RoomAccountData[roomID][dataType]
@ -118,7 +118,7 @@ func SaveAccountData(
UserID: userID, UserID: userID,
DataType: dataType, DataType: dataType,
RoomID: roomID, RoomID: roomID,
AccountData: json.RawMessage(body), AccountData: []byte(body),
} }
dataRes := api.InputAccountDataResponse{} dataRes := api.InputAccountDataResponse{}
if err := userAPI.InputAccountData(req.Context(), &dataReq, &dataRes); err != nil { if err := userAPI.InputAccountData(req.Context(), &dataReq, &dataRes); err != nil {

View file

@ -48,7 +48,7 @@ type createRoomRequest struct {
RoomAliasName string `json:"room_alias_name"` RoomAliasName string `json:"room_alias_name"`
GuestCanJoin bool `json:"guest_can_join"` GuestCanJoin bool `json:"guest_can_join"`
RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"` RoomVersion gomatrixserverlib.RoomVersion `json:"room_version"`
PowerLevelContentOverride json.RawMessage `json:"power_level_content_override"` PowerLevelContentOverride []byte `json:"power_level_content_override"`
} }
const ( const (

View file

@ -15,7 +15,6 @@
package routing package routing
import ( import (
"encoding/json"
"net/http" "net/http"
"time" "time"
@ -27,8 +26,8 @@ import (
) )
type uploadKeysRequest struct { type uploadKeysRequest struct {
DeviceKeys json.RawMessage `json:"device_keys"` DeviceKeys []byte `json:"device_keys"`
OneTimeKeys map[string]json.RawMessage `json:"one_time_keys"` OneTimeKeys map[string][]byte `json:"one_time_keys"`
} }
func UploadKeys(req *http.Request, keyAPI api.KeyInternalAPI, device *userapi.Device) util.JSONResponse { func UploadKeys(req *http.Request, keyAPI api.KeyInternalAPI, device *userapi.Device) util.JSONResponse {

View file

@ -205,7 +205,7 @@ func saveTagData(
UserID: userID, UserID: userID,
RoomID: roomID, RoomID: roomID,
DataType: "m.tag", DataType: "m.tag",
AccountData: json.RawMessage(newTagData), AccountData: []byte(newTagData),
} }
dataRes := api.InputAccountDataResponse{} dataRes := api.InputAccountDataResponse{}
return userAPI.InputAccountData(req.Context(), &dataReq, &dataRes) return userAPI.InputAccountData(req.Context(), &dataReq, &dataRes)

View file

@ -15,7 +15,6 @@
package routing package routing
import ( import (
"encoding/json"
"net/http" "net/http"
"strings" "strings"
@ -482,7 +481,7 @@ func Setup(
r0mux.Handle("/pushrules/", r0mux.Handle("/pushrules/",
httputil.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse { httputil.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {
// TODO: Implement push rules API // TODO: Implement push rules API
res := json.RawMessage(`{ res := []byte(`{
"global": { "global": {
"content": [], "content": [],
"override": [], "override": [],

View file

@ -13,7 +13,6 @@
package routing package routing
import ( import (
"encoding/json"
"net/http" "net/http"
"github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/httputil"
@ -39,7 +38,7 @@ func SendToDevice(
} }
var httpReq struct { var httpReq struct {
Messages map[string]map[string]json.RawMessage `json:"messages"` Messages map[string]map[string][]byte `json:"messages"`
} }
resErr := httputil.UnmarshalJSONRequest(req, &httpReq) resErr := httputil.UnmarshalJSONRequest(req, &httpReq)
if resErr != nil { if resErr != nil {

View file

@ -30,8 +30,8 @@ import (
type stateEventInStateResp struct { type stateEventInStateResp struct {
gomatrixserverlib.ClientEvent gomatrixserverlib.ClientEvent
PrevContent json.RawMessage `json:"prev_content,omitempty"` PrevContent []byte `json:"prev_content,omitempty"`
ReplacesState string `json:"replaces_state,omitempty"` ReplacesState string `json:"replaces_state,omitempty"`
} }
// OnIncomingStateRequest is called when a client makes a /rooms/{roomID}/state // OnIncomingStateRequest is called when a client makes a /rooms/{roomID}/state

View file

@ -184,9 +184,9 @@ func NotaryKeys(
} }
var response struct { var response struct {
ServerKeys []json.RawMessage `json:"server_keys"` ServerKeys [][]byte `json:"server_keys"`
} }
response.ServerKeys = []json.RawMessage{} response.ServerKeys = [][]byte{}
for serverName := range req.ServerKeys { for serverName := range req.ServerKeys {
var keys *gomatrixserverlib.ServerKeys var keys *gomatrixserverlib.ServerKeys

View file

@ -22,7 +22,7 @@ const (
var ( var (
testRoomVersion = gomatrixserverlib.RoomVersionV1 testRoomVersion = gomatrixserverlib.RoomVersionV1
testData = []json.RawMessage{ testData = [][]byte{
[]byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$0ok8ynDp7kjc95e3:kaer.morhen","hashes":{"sha256":"17kPoH+h0Dk4Omn7Sus0qMb6+oGcf+CZFEgDhv7UKWs"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"jP4a04f5/F10Pw95FPpdCyKAO44JOwUQ/MZOOeA/RTU1Dn+AHPMzGSaZnuGjRr/xQuADt+I3ctb5ZQfLKNzHDw"}},"state_key":"","type":"m.room.create"}`), []byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$0ok8ynDp7kjc95e3:kaer.morhen","hashes":{"sha256":"17kPoH+h0Dk4Omn7Sus0qMb6+oGcf+CZFEgDhv7UKWs"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"jP4a04f5/F10Pw95FPpdCyKAO44JOwUQ/MZOOeA/RTU1Dn+AHPMzGSaZnuGjRr/xQuADt+I3ctb5ZQfLKNzHDw"}},"state_key":"","type":"m.room.create"}`),
[]byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"content":{"membership":"join"},"depth":1,"event_id":"$LEwEu0kxrtu5fOiS:kaer.morhen","hashes":{"sha256":"B7M88PhXf3vd1LaFtjQutFu4x/w7fHD28XKZ4sAsJTo"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"p2vqmuJn7ZBRImctSaKbXCAxCcBlIjPH9JHte1ouIUGy84gpu4eLipOvSBCLL26hXfC0Zrm4WUto6Hr+ohdrCg"}},"state_key":"@userid:kaer.morhen","type":"m.room.member"}`), []byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"content":{"membership":"join"},"depth":1,"event_id":"$LEwEu0kxrtu5fOiS:kaer.morhen","hashes":{"sha256":"B7M88PhXf3vd1LaFtjQutFu4x/w7fHD28XKZ4sAsJTo"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"p2vqmuJn7ZBRImctSaKbXCAxCcBlIjPH9JHte1ouIUGy84gpu4eLipOvSBCLL26hXfC0Zrm4WUto6Hr+ohdrCg"}},"state_key":"@userid:kaer.morhen","type":"m.room.member"}`),
[]byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}],["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"content":{"join_rule":"public"},"depth":2,"event_id":"$SMHlqUrNhhBBRLeN:kaer.morhen","hashes":{"sha256":"vIuJQvmMjrGxshAkj1SXe0C4RqvMbv4ZADDw9pFCWqQ"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"hBMsb3Qppo3RaqqAl4JyTgaiWEbW5hlckATky6PrHun+F3YM203TzG7w9clwuQU5F5pZoB1a6nw+to0hN90FAw"}},"state_key":"","type":"m.room.join_rules"}`), []byte(`{"auth_events":[["$0ok8ynDp7kjc95e3:kaer.morhen",{"sha256":"sWCi6Ckp9rDimQON+MrUlNRkyfZ2tjbPbWfg2NMB18Q"}],["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"content":{"join_rule":"public"},"depth":2,"event_id":"$SMHlqUrNhhBBRLeN:kaer.morhen","hashes":{"sha256":"vIuJQvmMjrGxshAkj1SXe0C4RqvMbv4ZADDw9pFCWqQ"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[["$LEwEu0kxrtu5fOiS:kaer.morhen",{"sha256":"1aKajq6DWHru1R1HJjvdWMEavkJJHGaTmPvfuERUXaA"}]],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"hBMsb3Qppo3RaqqAl4JyTgaiWEbW5hlckATky6PrHun+F3YM203TzG7w9clwuQU5F5pZoB1a6nw+to0hN90FAw"}},"state_key":"","type":"m.room.join_rules"}`),
@ -363,7 +363,7 @@ func (c *txnFedClient) LookupMissingEvents(ctx context.Context, s gomatrixserver
return c.getMissingEvents(missing) return c.getMissingEvents(missing)
} }
func mustCreateTransaction(rsAPI api.RoomserverInternalAPI, fedClient txnFederationClient, pdus []json.RawMessage) *txnReq { func mustCreateTransaction(rsAPI api.RoomserverInternalAPI, fedClient txnFederationClient, pdus [][]byte) *txnReq {
t := &txnReq{ t := &txnReq{
rsAPI: rsAPI, rsAPI: rsAPI,
eduAPI: &testEDUProducer{}, eduAPI: &testEDUProducer{},
@ -447,7 +447,7 @@ func TestBasicTransaction(t *testing.T) {
} }
}, },
} }
pdus := []json.RawMessage{ pdus := [][]byte{
testData[len(testData)-1], // a message event testData[len(testData)-1], // a message event
} }
txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus) txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus)
@ -467,7 +467,7 @@ func TestTransactionFailAuthChecks(t *testing.T) {
} }
}, },
} }
pdus := []json.RawMessage{ pdus := [][]byte{
testData[len(testData)-1], // a message event testData[len(testData)-1], // a message event
} }
txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus) txn := mustCreateTransaction(rsAPI, &txnFedClient{}, pdus)
@ -552,7 +552,7 @@ func TestTransactionFetchMissingPrevEvents(t *testing.T) {
}, },
} }
pdus := []json.RawMessage{ pdus := [][]byte{
inputEvent.JSON(), inputEvent.JSON(),
} }
txn := mustCreateTransaction(rsAPI, cli, pdus) txn := mustCreateTransaction(rsAPI, cli, pdus)
@ -715,7 +715,7 @@ func TestTransactionFetchMissingStateByStateIDs(t *testing.T) {
// /event for event B returns it // /event for event B returns it
getEvent: map[string]gomatrixserverlib.Transaction{ getEvent: map[string]gomatrixserverlib.Transaction{
eventB.EventID(): { eventB.EventID(): {
PDUs: []json.RawMessage{ PDUs: [][]byte{
eventB.JSON(), eventB.JSON(),
}, },
}, },
@ -737,7 +737,7 @@ func TestTransactionFetchMissingStateByStateIDs(t *testing.T) {
}, },
} }
pdus := []json.RawMessage{ pdus := [][]byte{
eventD.JSON(), eventD.JSON(),
} }
txn := mustCreateTransaction(rsAPI, cli, pdus) txn := mustCreateTransaction(rsAPI, cli, pdus)

View file

@ -391,7 +391,7 @@ func (oq *destinationQueue) nextTransaction(
if pdu == nil || pdu.pdu == nil { if pdu == nil || pdu.pdu == nil {
continue continue
} }
// Append the JSON of the event, since this is a json.RawMessage type in the // Append the JSON of the event, since this is a []byte type in the
// gomatrixserverlib.Transaction struct // gomatrixserverlib.Transaction struct
t.PDUs = append(t.PDUs, pdu.pdu.JSON()) t.PDUs = append(t.PDUs, pdu.pdu.JSON())
pduReceipts = append(pduReceipts, pdu.receipt) pduReceipts = append(pduReceipts, pdu.receipt)

View file

@ -16,7 +16,6 @@ package api
import ( import (
"context" "context"
"encoding/json"
"strings" "strings"
"time" "time"
@ -83,7 +82,7 @@ type OneTimeKeys struct {
// The device ID of this device // The device ID of this device
DeviceID string DeviceID string
// A map of algorithm:key_id => key JSON // A map of algorithm:key_id => key JSON
KeyJSON map[string]json.RawMessage KeyJSON map[string][]byte
} }
// Split a key in KeyJSON into algorithm and key ID // Split a key in KeyJSON into algorithm and key ID
@ -144,7 +143,7 @@ type PerformClaimKeysRequest struct {
type PerformClaimKeysResponse struct { type PerformClaimKeysResponse struct {
// Map of user_id to device_id to algorithm:key_id to key JSON // Map of user_id to device_id to algorithm:key_id to key JSON
OneTimeKeys map[string]map[string]map[string]json.RawMessage OneTimeKeys map[string]map[string]map[string][]byte
// Map of remote server domain to error JSON // Map of remote server domain to error JSON
Failures map[string]interface{} Failures map[string]interface{}
// Set if there was a fatal error processing this action // Set if there was a fatal error processing this action
@ -161,7 +160,7 @@ type QueryKeysResponse struct {
// Map of remote server domain to error JSON // Map of remote server domain to error JSON
Failures map[string]interface{} Failures map[string]interface{}
// Map of user_id to device_id to device_key // Map of user_id to device_id to device_key
DeviceKeys map[string]map[string]json.RawMessage DeviceKeys map[string]map[string][]byte
// Set if there was a fatal error processing this query // Set if there was a fatal error processing this query
Error *KeyError Error *KeyError
} }

View file

@ -80,7 +80,7 @@ func (a *KeyInternalAPI) PerformUploadKeys(ctx context.Context, req *api.Perform
} }
func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformClaimKeysRequest, res *api.PerformClaimKeysResponse) { func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformClaimKeysRequest, res *api.PerformClaimKeysResponse) {
res.OneTimeKeys = make(map[string]map[string]map[string]json.RawMessage) res.OneTimeKeys = make(map[string]map[string]map[string][]byte)
res.Failures = make(map[string]interface{}) res.Failures = make(map[string]interface{})
// wrap request map in a top-level by-domain map // wrap request map in a top-level by-domain map
domainToDeviceKeys := make(map[string]map[string]map[string]string) domainToDeviceKeys := make(map[string]map[string]map[string]string)
@ -108,11 +108,11 @@ func (a *KeyInternalAPI) PerformClaimKeys(ctx context.Context, req *api.PerformC
for _, key := range keys { for _, key := range keys {
_, ok := res.OneTimeKeys[key.UserID] _, ok := res.OneTimeKeys[key.UserID]
if !ok { if !ok {
res.OneTimeKeys[key.UserID] = make(map[string]map[string]json.RawMessage) res.OneTimeKeys[key.UserID] = make(map[string]map[string][]byte)
} }
_, ok = res.OneTimeKeys[key.UserID][key.DeviceID] _, ok = res.OneTimeKeys[key.UserID][key.DeviceID]
if !ok { if !ok {
res.OneTimeKeys[key.UserID][key.DeviceID] = make(map[string]json.RawMessage) res.OneTimeKeys[key.UserID][key.DeviceID] = make(map[string][]byte)
} }
for keyID, keyJSON := range key.KeyJSON { for keyID, keyJSON := range key.KeyJSON {
res.OneTimeKeys[key.UserID][key.DeviceID][keyID] = keyJSON res.OneTimeKeys[key.UserID][key.DeviceID][keyID] = keyJSON
@ -165,9 +165,9 @@ func (a *KeyInternalAPI) claimRemoteKeys(
keysClaimed := 0 keysClaimed := 0
for result := range resultCh { for result := range resultCh {
for userID, nest := range result.OneTimeKeys { for userID, nest := range result.OneTimeKeys {
res.OneTimeKeys[userID] = make(map[string]map[string]json.RawMessage) res.OneTimeKeys[userID] = make(map[string]map[string][]byte)
for deviceID, nest2 := range nest { for deviceID, nest2 := range nest {
res.OneTimeKeys[userID][deviceID] = make(map[string]json.RawMessage) res.OneTimeKeys[userID][deviceID] = make(map[string][]byte)
for keyIDWithAlgo, otk := range nest2 { for keyIDWithAlgo, otk := range nest2 {
keyJSON, err := json.Marshal(otk) keyJSON, err := json.Marshal(otk)
if err != nil { if err != nil {
@ -220,7 +220,7 @@ func (a *KeyInternalAPI) QueryDeviceMessages(ctx context.Context, req *api.Query
} }
func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysRequest, res *api.QueryKeysResponse) { func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysRequest, res *api.QueryKeysResponse) {
res.DeviceKeys = make(map[string]map[string]json.RawMessage) res.DeviceKeys = make(map[string]map[string][]byte)
res.Failures = make(map[string]interface{}) res.Failures = make(map[string]interface{})
// make a map from domain to device keys // make a map from domain to device keys
domainToDeviceKeys := make(map[string]map[string][]string) domainToDeviceKeys := make(map[string]map[string][]string)
@ -254,7 +254,7 @@ func (a *KeyInternalAPI) QueryKeys(ctx context.Context, req *api.QueryKeysReques
} }
if res.DeviceKeys[userID] == nil { if res.DeviceKeys[userID] == nil {
res.DeviceKeys[userID] = make(map[string]json.RawMessage) res.DeviceKeys[userID] = make(map[string][]byte)
} }
for _, dk := range deviceKeys { for _, dk := range deviceKeys {
if len(dk.KeyJSON) == 0 { if len(dk.KeyJSON) == 0 {
@ -335,7 +335,7 @@ func (a *KeyInternalAPI) queryRemoteKeys(
for result := range resultCh { for result := range resultCh {
for userID, nest := range result.DeviceKeys { for userID, nest := range result.DeviceKeys {
res.DeviceKeys[userID] = make(map[string]json.RawMessage) res.DeviceKeys[userID] = make(map[string][]byte)
for deviceID, deviceKey := range nest { for deviceID, deviceKey := range nest {
keyJSON, err := json.Marshal(deviceKey) keyJSON, err := json.Marshal(deviceKey)
if err != nil { if err != nil {
@ -432,7 +432,7 @@ func (a *KeyInternalAPI) populateResponseWithDeviceKeysFromDatabase(
return fmt.Errorf("DeviceKeysForUser %s returned no keys but wanted all keys, falling back to remote", userID) return fmt.Errorf("DeviceKeysForUser %s returned no keys but wanted all keys, falling back to remote", userID)
} }
if res.DeviceKeys[userID] == nil { if res.DeviceKeys[userID] == nil {
res.DeviceKeys[userID] = make(map[string]json.RawMessage) res.DeviceKeys[userID] = make(map[string][]byte)
} }
for _, key := range keys { for _, key := range keys {

View file

@ -16,7 +16,6 @@ package storage
import ( import (
"context" "context"
"encoding/json"
"github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/dendrite/keyserver/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -25,7 +24,7 @@ import (
type Database interface { type Database interface {
// ExistingOneTimeKeys returns a map of keyIDWithAlgorithm to key JSON for the given parameters. If no keys exist with this combination // ExistingOneTimeKeys returns a map of keyIDWithAlgorithm to key JSON for the given parameters. If no keys exist with this combination
// of user/device/key/algorithm 4-uple then it is omitted from the map. Returns an error when failing to communicate with the database. // of user/device/key/algorithm 4-uple then it is omitted from the map. Returns an error when failing to communicate with the database.
ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error)
// StoreOneTimeKeys persists the given one-time keys. // StoreOneTimeKeys persists the given one-time keys.
StoreOneTimeKeys(ctx context.Context, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error) StoreOneTimeKeys(ctx context.Context, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error)

View file

@ -17,7 +17,6 @@ package postgres
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"time" "time"
"github.com/lib/pq" "github.com/lib/pq"
@ -94,14 +93,14 @@ func NewPostgresOneTimeKeysTable(db *sql.DB) (tables.OneTimeKeys, error) {
return s, nil return s, nil
} }
func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) {
rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID, pq.Array(keyIDsWithAlgorithms)) rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID, pq.Array(keyIDsWithAlgorithms))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer internal.CloseAndLogIfError(ctx, rows, "selectKeysStmt: rows.close() failed") defer internal.CloseAndLogIfError(ctx, rows, "selectKeysStmt: rows.close() failed")
result := make(map[string]json.RawMessage) result := make(map[string][]byte)
var ( var (
algorithmWithID string algorithmWithID string
keyJSONStr string keyJSONStr string
@ -110,7 +109,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d
if err := rows.Scan(&algorithmWithID, &keyJSONStr); err != nil { if err := rows.Scan(&algorithmWithID, &keyJSONStr); err != nil {
return nil, err return nil, err
} }
result[algorithmWithID] = json.RawMessage(keyJSONStr) result[algorithmWithID] = []byte(keyJSONStr)
} }
return result, rows.Err() return result, rows.Err()
} }
@ -172,7 +171,7 @@ func (s *oneTimeKeysStatements) InsertOneTimeKeys(ctx context.Context, txn *sql.
func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey(
ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string, ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string,
) (map[string]json.RawMessage, error) { ) (map[string][]byte, error) {
var keyID string var keyID string
var keyJSON string var keyJSON string
err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON) err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON)
@ -183,7 +182,7 @@ func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey(
return nil, err return nil, err
} }
_, err = sqlutil.TxStmtContext(ctx, txn, s.deleteOneTimeKeyStmt).ExecContext(ctx, userID, deviceID, algorithm, keyID) _, err = sqlutil.TxStmtContext(ctx, txn, s.deleteOneTimeKeyStmt).ExecContext(ctx, userID, deviceID, algorithm, keyID)
return map[string]json.RawMessage{ return map[string][]byte{
algorithm + ":" + keyID: json.RawMessage(keyJSON), algorithm + ":" + keyID: []byte(keyJSON),
}, err }, err
} }

View file

@ -17,7 +17,6 @@ package shared
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/dendrite/keyserver/api"
@ -34,7 +33,7 @@ type Database struct {
StaleDeviceListsTable tables.StaleDeviceLists StaleDeviceListsTable tables.StaleDeviceLists
} }
func (d *Database) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { func (d *Database) ExistingOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) {
return d.OneTimeKeysTable.SelectOneTimeKeys(ctx, userID, deviceID, keyIDsWithAlgorithms) return d.OneTimeKeysTable.SelectOneTimeKeys(ctx, userID, deviceID, keyIDsWithAlgorithms)
} }

View file

@ -17,7 +17,6 @@ package sqlite3
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"time" "time"
"github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal"
@ -93,7 +92,7 @@ func NewSqliteOneTimeKeysTable(db *sql.DB) (tables.OneTimeKeys, error) {
return s, nil return s, nil
} }
func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) { func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error) {
rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID) rows, err := s.selectKeysStmt.QueryContext(ctx, userID, deviceID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -105,7 +104,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d
wantSet[ka] = true wantSet[ka] = true
} }
result := make(map[string]json.RawMessage) result := make(map[string][]byte)
for rows.Next() { for rows.Next() {
var keyID string var keyID string
var algorithm string var algorithm string
@ -115,7 +114,7 @@ func (s *oneTimeKeysStatements) SelectOneTimeKeys(ctx context.Context, userID, d
} }
keyIDWithAlgo := algorithm + ":" + keyID keyIDWithAlgo := algorithm + ":" + keyID
if wantSet[keyIDWithAlgo] { if wantSet[keyIDWithAlgo] {
result[keyIDWithAlgo] = json.RawMessage(keyJSONStr) result[keyIDWithAlgo] = []byte(keyJSONStr)
} }
} }
return result, rows.Err() return result, rows.Err()
@ -180,7 +179,7 @@ func (s *oneTimeKeysStatements) InsertOneTimeKeys(
func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey( func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey(
ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string, ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string,
) (map[string]json.RawMessage, error) { ) (map[string][]byte, error) {
var keyID string var keyID string
var keyJSON string var keyJSON string
err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON) err := sqlutil.TxStmtContext(ctx, txn, s.selectKeyByAlgorithmStmt).QueryRowContext(ctx, userID, deviceID, algorithm).Scan(&keyID, &keyJSON)
@ -197,7 +196,7 @@ func (s *oneTimeKeysStatements) SelectAndDeleteOneTimeKey(
if keyJSON == "" { if keyJSON == "" {
return nil, nil return nil, nil
} }
return map[string]json.RawMessage{ return map[string][]byte{
algorithm + ":" + keyID: json.RawMessage(keyJSON), algorithm + ":" + keyID: []byte(keyJSON),
}, err }, err
} }

View file

@ -17,19 +17,18 @@ package tables
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"github.com/matrix-org/dendrite/keyserver/api" "github.com/matrix-org/dendrite/keyserver/api"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
type OneTimeKeys interface { type OneTimeKeys interface {
SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string]json.RawMessage, error) SelectOneTimeKeys(ctx context.Context, userID, deviceID string, keyIDsWithAlgorithms []string) (map[string][]byte, error)
CountOneTimeKeys(ctx context.Context, userID, deviceID string) (*api.OneTimeKeysCount, error) CountOneTimeKeys(ctx context.Context, userID, deviceID string) (*api.OneTimeKeysCount, error)
InsertOneTimeKeys(ctx context.Context, txn *sql.Tx, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error) InsertOneTimeKeys(ctx context.Context, txn *sql.Tx, keys api.OneTimeKeys) (*api.OneTimeKeysCount, error)
// SelectAndDeleteOneTimeKey selects a single one time key matching the user/device/algorithm specified and returns the algo:key_id => JSON. // SelectAndDeleteOneTimeKey selects a single one time key matching the user/device/algorithm specified and returns the algo:key_id => JSON.
// Returns an empty map if the key does not exist. // Returns an empty map if the key does not exist.
SelectAndDeleteOneTimeKey(ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string) (map[string]json.RawMessage, error) SelectAndDeleteOneTimeKey(ctx context.Context, txn *sql.Tx, userID, deviceID, algorithm string) (map[string][]byte, error)
} }
type DeviceKeys interface { type DeviceKeys interface {

View file

@ -1,7 +1,6 @@
package api package api
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -54,7 +53,7 @@ func (p *PerformError) JSONResponse() util.JSONResponse {
return util.JSONResponse{ return util.JSONResponse{
Code: p.RemoteCode, Code: p.RemoteCode,
// TODO: Should we assert this is in fact JSON? E.g gjson parse? // TODO: Should we assert this is in fact JSON? E.g gjson parse?
JSON: json.RawMessage(p.Msg), JSON: []byte(p.Msg),
} }
default: default:
return util.ErrorResponse(p) return util.ErrorResponse(p)

View file

@ -204,7 +204,7 @@ func (r *RoomserverInternalAPI) sendUpdatedAliasesEvent(
if err != nil { if err != nil {
return err return err
} }
err = builder.SetContent(json.RawMessage(rawContent)) err = builder.SetContent([]byte(rawContent))
if err != nil { if err != nil {
return err return err
} }

View file

@ -49,7 +49,7 @@ func (p *dummyProducer) SendMessage(msg *sarama.ProducerMessage) (partition int3
return 0, 0, nil return 0, 0, nil
} }
be := msg.Value.(sarama.ByteEncoder) be := msg.Value.(sarama.ByteEncoder)
b := json.RawMessage(be) b := []byte(be)
fmt.Println("SENDING >>>>>>>> ", string(b)) fmt.Println("SENDING >>>>>>>> ", string(b))
var out api.OutputEvent var out api.OutputEvent
err = json.Unmarshal(b, &out) err = json.Unmarshal(b, &out)
@ -143,7 +143,7 @@ func mustCreateEvents(t *testing.T, roomVer gomatrixserverlib.RoomVersion, event
return return
} }
func mustLoadRawEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []json.RawMessage) []*gomatrixserverlib.HeaderedEvent { func mustLoadRawEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events [][]byte) []*gomatrixserverlib.HeaderedEvent {
t.Helper() t.Helper()
hs := make([]*gomatrixserverlib.HeaderedEvent, len(events)) hs := make([]*gomatrixserverlib.HeaderedEvent, len(events))
for i := range events { for i := range events {
@ -186,7 +186,7 @@ func mustCreateRoomserverAPI(t *testing.T) (api.RoomserverInternalAPI, *dummyPro
), dp ), dp
} }
func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []json.RawMessage) (api.RoomserverInternalAPI, *dummyProducer, []*gomatrixserverlib.HeaderedEvent) { func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events [][]byte) (api.RoomserverInternalAPI, *dummyProducer, []*gomatrixserverlib.HeaderedEvent) {
t.Helper() t.Helper()
rsAPI, dp := mustCreateRoomserverAPI(t) rsAPI, dp := mustCreateRoomserverAPI(t)
hevents := mustLoadRawEvents(t, ver, events) hevents := mustLoadRawEvents(t, ver, events)
@ -197,7 +197,7 @@ func mustSendEvents(t *testing.T, ver gomatrixserverlib.RoomVersion, events []js
} }
func TestOutputRedactedEvent(t *testing.T) { func TestOutputRedactedEvent(t *testing.T) {
redactionEvents := []json.RawMessage{ redactionEvents := [][]byte{
// create event // create event
[]byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$N4us6vqqq3RjvpKd:kaer.morhen","hashes":{"sha256":"WTdrCn/YsiounXcJPsLP8xT0ZjHiO5Ov0NvXYmK2onE"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"9+5JcpaN5b5KlHYHGp6r+GoNDH98lbfzGYwjfxensa5C5D/bDACaYnMDLnhwsHOE5nxgI+jT/GV271pz6PMSBQ"}},"state_key":"","type":"m.room.create"}`), []byte(`{"auth_events":[],"content":{"creator":"@userid:kaer.morhen"},"depth":0,"event_id":"$N4us6vqqq3RjvpKd:kaer.morhen","hashes":{"sha256":"WTdrCn/YsiounXcJPsLP8xT0ZjHiO5Ov0NvXYmK2onE"},"origin":"kaer.morhen","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:kaer.morhen","sender":"@userid:kaer.morhen","signatures":{"kaer.morhen":{"ed25519:auto":"9+5JcpaN5b5KlHYHGp6r+GoNDH98lbfzGYwjfxensa5C5D/bDACaYnMDLnhwsHOE5nxgI+jT/GV271pz6PMSBQ"}},"state_key":"","type":"m.room.create"}`),
// join event // join event

View file

@ -123,7 +123,7 @@ func (s *stateBlockStatements) BulkSelectStateBlockEntries(
i := 0 i := 0
for ; rows.Next(); i++ { for ; rows.Next(); i++ {
var stateBlockNID types.StateBlockNID var stateBlockNID types.StateBlockNID
var result json.RawMessage var result []byte
if err = rows.Scan(&stateBlockNID, &result); err != nil { if err = rows.Scan(&stateBlockNID, &result); err != nil {
return nil, err return nil, err
} }

View file

@ -557,7 +557,7 @@ func TestSendToDeviceBehaviour(t *testing.T) {
streamPos, err := db.StoreNewSendForDeviceMessage(ctx, "alice", "one", gomatrixserverlib.SendToDeviceEvent{ streamPos, err := db.StoreNewSendForDeviceMessage(ctx, "alice", "one", gomatrixserverlib.SendToDeviceEvent{
Sender: "bob", Sender: "bob",
Type: "m.type", Type: "m.type",
Content: json.RawMessage("{}"), Content: []byte("{}"),
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -351,9 +351,9 @@ func NewStreamTokenFromString(tok string) (token StreamingToken, err error) {
// PrevEventRef represents a reference to a previous event in a state event upgrade // PrevEventRef represents a reference to a previous event in a state event upgrade
type PrevEventRef struct { type PrevEventRef struct {
PrevContent json.RawMessage `json:"prev_content"` PrevContent []byte `json:"prev_content"`
ReplacesState string `json:"replaces_state"` ReplacesState string `json:"replaces_state"`
PrevSender string `json:"prev_sender"` PrevSender string `json:"prev_sender"`
} }
// Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync // Response represents a /sync API response. See https://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
@ -445,14 +445,14 @@ func NewJoinResponse() *JoinResponse {
// InviteResponse represents a /sync response for a room which is under the 'invite' key. // InviteResponse represents a /sync response for a room which is under the 'invite' key.
type InviteResponse struct { type InviteResponse struct {
InviteState struct { InviteState struct {
Events []json.RawMessage `json:"events"` Events [][]byte `json:"events"`
} `json:"invite_state"` } `json:"invite_state"`
} }
// NewInviteResponse creates an empty response with initialised arrays. // NewInviteResponse creates an empty response with initialised arrays.
func NewInviteResponse(event *gomatrixserverlib.HeaderedEvent) *InviteResponse { func NewInviteResponse(event *gomatrixserverlib.HeaderedEvent) *InviteResponse {
res := InviteResponse{} res := InviteResponse{}
res.InviteState.Events = []json.RawMessage{} res.InviteState.Events = [][]byte{}
// First see if there's invite_room_state in the unsigned key of the invite. // First see if there's invite_room_state in the unsigned key of the invite.
// If there is then unmarshal it into the response. This will contain the // If there is then unmarshal it into the response. This will contain the

View file

@ -16,7 +16,6 @@ package api
import ( import (
"context" "context"
"encoding/json"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
@ -44,10 +43,10 @@ type UserInternalAPI interface {
// InputAccountDataRequest is the request for InputAccountData // InputAccountDataRequest is the request for InputAccountData
type InputAccountDataRequest struct { type InputAccountDataRequest struct {
UserID string // required: the user to set account data for UserID string // required: the user to set account data for
RoomID string // optional: the room to associate the account data with RoomID string // optional: the room to associate the account data with
DataType string // required: the data type of the data DataType string // required: the data type of the data
AccountData json.RawMessage // required: the message content AccountData []byte // required: the message content
} }
// InputAccountDataResponse is the response for InputAccountData // InputAccountDataResponse is the response for InputAccountData
@ -113,8 +112,8 @@ type QueryAccountDataRequest struct {
// QueryAccountDataResponse is the response for QueryAccountData // QueryAccountDataResponse is the response for QueryAccountData
type QueryAccountDataResponse struct { type QueryAccountDataResponse struct {
GlobalAccountData map[string]json.RawMessage // type -> data GlobalAccountData map[string][]byte // type -> data
RoomAccountData map[string]map[string]json.RawMessage // room -> type -> data RoomAccountData map[string]map[string][]byte // room -> type -> data
} }
// QueryDevicesRequest is the request for QueryDevices // QueryDevicesRequest is the request for QueryDevices

View file

@ -17,7 +17,6 @@ package internal
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -315,17 +314,17 @@ func (a *UserInternalAPI) QueryAccountData(ctx context.Context, req *api.QueryAc
return fmt.Errorf("cannot query account data of remote users: got %s want %s", domain, a.ServerName) return fmt.Errorf("cannot query account data of remote users: got %s want %s", domain, a.ServerName)
} }
if req.DataType != "" { if req.DataType != "" {
var data json.RawMessage var data []byte
data, err = a.AccountDB.GetAccountDataByType(ctx, local, req.RoomID, req.DataType) data, err = a.AccountDB.GetAccountDataByType(ctx, local, req.RoomID, req.DataType)
if err != nil { if err != nil {
return err return err
} }
res.RoomAccountData = make(map[string]map[string]json.RawMessage) res.RoomAccountData = make(map[string]map[string][]byte)
res.GlobalAccountData = make(map[string]json.RawMessage) res.GlobalAccountData = make(map[string][]byte)
if data != nil { if data != nil {
if req.RoomID != "" { if req.RoomID != "" {
if _, ok := res.RoomAccountData[req.RoomID]; !ok { if _, ok := res.RoomAccountData[req.RoomID]; !ok {
res.RoomAccountData[req.RoomID] = make(map[string]json.RawMessage) res.RoomAccountData[req.RoomID] = make(map[string][]byte)
} }
res.RoomAccountData[req.RoomID][req.DataType] = data res.RoomAccountData[req.RoomID][req.DataType] = data
} else { } else {

View file

@ -16,7 +16,6 @@ package accounts
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/authtypes"
@ -36,13 +35,13 @@ type Database interface {
// account already exists, it will return nil, ErrUserExists. // account already exists, it will return nil, ErrUserExists.
CreateAccount(ctx context.Context, localpart, plaintextPassword, appserviceID string) (*api.Account, error) CreateAccount(ctx context.Context, localpart, plaintextPassword, appserviceID string) (*api.Account, error)
CreateGuestAccount(ctx context.Context) (*api.Account, error) CreateGuestAccount(ctx context.Context) (*api.Account, error)
SaveAccountData(ctx context.Context, localpart, roomID, dataType string, content json.RawMessage) error SaveAccountData(ctx context.Context, localpart, roomID, dataType string, content []byte) error
GetAccountData(ctx context.Context, localpart string) (global map[string]json.RawMessage, rooms map[string]map[string]json.RawMessage, err error) GetAccountData(ctx context.Context, localpart string) (global map[string][]byte, rooms map[string]map[string][]byte, err error)
// GetAccountDataByType returns account data matching a given // GetAccountDataByType returns account data matching a given
// localpart, room ID and type. // localpart, room ID and type.
// If no account data could be found, returns nil // If no account data could be found, returns nil
// Returns an error if there was an issue with the retrieval // Returns an error if there was an issue with the retrieval
GetAccountDataByType(ctx context.Context, localpart, roomID, dataType string) (data json.RawMessage, err error) GetAccountDataByType(ctx context.Context, localpart, roomID, dataType string) (data []byte, err error)
GetNewNumericLocalpart(ctx context.Context) (int64, error) GetNewNumericLocalpart(ctx context.Context) (int64, error)
SaveThreePIDAssociation(ctx context.Context, threepid, localpart, medium string) (err error) SaveThreePIDAssociation(ctx context.Context, threepid, localpart, medium string) (err error)
RemoveThreePIDAssociation(ctx context.Context, threepid string, medium string) (err error) RemoveThreePIDAssociation(ctx context.Context, threepid string, medium string) (err error)

View file

@ -17,7 +17,6 @@ package postgres
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
@ -74,7 +73,7 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
} }
func (s *accountDataStatements) insertAccountData( func (s *accountDataStatements) insertAccountData(
ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content json.RawMessage, ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content []byte,
) (err error) { ) (err error) {
stmt := sqlutil.TxStmt(txn, s.insertAccountDataStmt) stmt := sqlutil.TxStmt(txn, s.insertAccountDataStmt)
_, err = stmt.ExecContext(ctx, localpart, roomID, dataType, content) _, err = stmt.ExecContext(ctx, localpart, roomID, dataType, content)
@ -84,8 +83,8 @@ func (s *accountDataStatements) insertAccountData(
func (s *accountDataStatements) selectAccountData( func (s *accountDataStatements) selectAccountData(
ctx context.Context, localpart string, ctx context.Context, localpart string,
) ( ) (
/* global */ map[string]json.RawMessage, /* global */ map[string][]byte,
/* rooms */ map[string]map[string]json.RawMessage, /* rooms */ map[string]map[string][]byte,
error, error,
) { ) {
rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart) rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart)
@ -94,8 +93,8 @@ func (s *accountDataStatements) selectAccountData(
} }
defer internal.CloseAndLogIfError(ctx, rows, "selectAccountData: rows.close() failed") defer internal.CloseAndLogIfError(ctx, rows, "selectAccountData: rows.close() failed")
global := map[string]json.RawMessage{} global := map[string][]byte{}
rooms := map[string]map[string]json.RawMessage{} rooms := map[string]map[string][]byte{}
for rows.Next() { for rows.Next() {
var roomID string var roomID string
@ -108,7 +107,7 @@ func (s *accountDataStatements) selectAccountData(
if roomID != "" { if roomID != "" {
if _, ok := rooms[roomID]; !ok { if _, ok := rooms[roomID]; !ok {
rooms[roomID] = map[string]json.RawMessage{} rooms[roomID] = map[string][]byte{}
} }
rooms[roomID][dataType] = content rooms[roomID][dataType] = content
} else { } else {
@ -121,7 +120,7 @@ func (s *accountDataStatements) selectAccountData(
func (s *accountDataStatements) selectAccountDataByType( func (s *accountDataStatements) selectAccountDataByType(
ctx context.Context, localpart, roomID, dataType string, ctx context.Context, localpart, roomID, dataType string,
) (data json.RawMessage, err error) { ) (data []byte, err error) {
var bytes []byte var bytes []byte
stmt := s.selectAccountDataByTypeStmt stmt := s.selectAccountDataByTypeStmt
if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil { if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil {
@ -130,6 +129,6 @@ func (s *accountDataStatements) selectAccountDataByType(
} }
return return
} }
data = json.RawMessage(bytes) data = []byte(bytes)
return return
} }

View file

@ -17,7 +17,6 @@ package postgres
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"strconv" "strconv"
"time" "time"
@ -198,7 +197,7 @@ func (d *Database) createAccount(
if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil { if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil {
return nil, err return nil, err
} }
if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{ if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", []byte(`{
"global": { "global": {
"content": [], "content": [],
"override": [], "override": [],
@ -218,7 +217,7 @@ func (d *Database) createAccount(
// update the corresponding row with the new content // update the corresponding row with the new content
// Returns a SQL error if there was an issue with the insertion/update // Returns a SQL error if there was an issue with the insertion/update
func (d *Database) SaveAccountData( func (d *Database) SaveAccountData(
ctx context.Context, localpart, roomID, dataType string, content json.RawMessage, ctx context.Context, localpart, roomID, dataType string, content []byte,
) error { ) error {
return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error { return sqlutil.WithTransaction(d.db, func(txn *sql.Tx) error {
return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content) return d.accountDatas.insertAccountData(ctx, txn, localpart, roomID, dataType, content)
@ -229,8 +228,8 @@ func (d *Database) SaveAccountData(
// If no account data could be found, returns an empty arrays // If no account data could be found, returns an empty arrays
// Returns an error if there was an issue with the retrieval // Returns an error if there was an issue with the retrieval
func (d *Database) GetAccountData(ctx context.Context, localpart string) ( func (d *Database) GetAccountData(ctx context.Context, localpart string) (
global map[string]json.RawMessage, global map[string][]byte,
rooms map[string]map[string]json.RawMessage, rooms map[string]map[string][]byte,
err error, err error,
) { ) {
return d.accountDatas.selectAccountData(ctx, localpart) return d.accountDatas.selectAccountData(ctx, localpart)
@ -242,7 +241,7 @@ func (d *Database) GetAccountData(ctx context.Context, localpart string) (
// Returns an error if there was an issue with the retrieval // Returns an error if there was an issue with the retrieval
func (d *Database) GetAccountDataByType( func (d *Database) GetAccountDataByType(
ctx context.Context, localpart, roomID, dataType string, ctx context.Context, localpart, roomID, dataType string,
) (data json.RawMessage, err error) { ) (data []byte, err error) {
return d.accountDatas.selectAccountDataByType( return d.accountDatas.selectAccountDataByType(
ctx, localpart, roomID, dataType, ctx, localpart, roomID, dataType,
) )

View file

@ -17,7 +17,6 @@ package sqlite3
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"github.com/matrix-org/dendrite/internal/sqlutil" "github.com/matrix-org/dendrite/internal/sqlutil"
) )
@ -75,7 +74,7 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
} }
func (s *accountDataStatements) insertAccountData( func (s *accountDataStatements) insertAccountData(
ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content json.RawMessage, ctx context.Context, txn *sql.Tx, localpart, roomID, dataType string, content []byte,
) error { ) error {
_, err := sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, localpart, roomID, dataType, content) _, err := sqlutil.TxStmt(txn, s.insertAccountDataStmt).ExecContext(ctx, localpart, roomID, dataType, content)
return err return err
@ -84,8 +83,8 @@ func (s *accountDataStatements) insertAccountData(
func (s *accountDataStatements) selectAccountData( func (s *accountDataStatements) selectAccountData(
ctx context.Context, localpart string, ctx context.Context, localpart string,
) ( ) (
/* global */ map[string]json.RawMessage, /* global */ map[string][]byte,
/* rooms */ map[string]map[string]json.RawMessage, /* rooms */ map[string]map[string][]byte,
error, error,
) { ) {
rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart) rows, err := s.selectAccountDataStmt.QueryContext(ctx, localpart)
@ -93,8 +92,8 @@ func (s *accountDataStatements) selectAccountData(
return nil, nil, err return nil, nil, err
} }
global := map[string]json.RawMessage{} global := map[string][]byte{}
rooms := map[string]map[string]json.RawMessage{} rooms := map[string]map[string][]byte{}
for rows.Next() { for rows.Next() {
var roomID string var roomID string
@ -107,7 +106,7 @@ func (s *accountDataStatements) selectAccountData(
if roomID != "" { if roomID != "" {
if _, ok := rooms[roomID]; !ok { if _, ok := rooms[roomID]; !ok {
rooms[roomID] = map[string]json.RawMessage{} rooms[roomID] = map[string][]byte{}
} }
rooms[roomID][dataType] = content rooms[roomID][dataType] = content
} else { } else {
@ -120,7 +119,7 @@ func (s *accountDataStatements) selectAccountData(
func (s *accountDataStatements) selectAccountDataByType( func (s *accountDataStatements) selectAccountDataByType(
ctx context.Context, localpart, roomID, dataType string, ctx context.Context, localpart, roomID, dataType string,
) (data json.RawMessage, err error) { ) (data []byte, err error) {
var bytes []byte var bytes []byte
stmt := s.selectAccountDataByTypeStmt stmt := s.selectAccountDataByTypeStmt
if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil { if err = stmt.QueryRowContext(ctx, localpart, roomID, dataType).Scan(&bytes); err != nil {
@ -129,6 +128,6 @@ func (s *accountDataStatements) selectAccountDataByType(
} }
return return
} }
data = json.RawMessage(bytes) data = []byte(bytes)
return return
} }

View file

@ -17,7 +17,6 @@ package sqlite3
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"strconv" "strconv"
"sync" "sync"
@ -228,7 +227,7 @@ func (d *Database) createAccount(
if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil { if err = d.profiles.insertProfile(ctx, txn, localpart); err != nil {
return nil, err return nil, err
} }
if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", json.RawMessage(`{ if err = d.accountDatas.insertAccountData(ctx, txn, localpart, "", "m.push_rules", []byte(`{
"global": { "global": {
"content": [], "content": [],
"override": [], "override": [],
@ -248,7 +247,7 @@ func (d *Database) createAccount(
// update the corresponding row with the new content // update the corresponding row with the new content
// Returns a SQL error if there was an issue with the insertion/update // Returns a SQL error if there was an issue with the insertion/update
func (d *Database) SaveAccountData( func (d *Database) SaveAccountData(
ctx context.Context, localpart, roomID, dataType string, content json.RawMessage, ctx context.Context, localpart, roomID, dataType string, content []byte,
) error { ) error {
d.accountDatasMu.Lock() d.accountDatasMu.Lock()
defer d.accountDatasMu.Unlock() defer d.accountDatasMu.Unlock()
@ -261,8 +260,8 @@ func (d *Database) SaveAccountData(
// If no account data could be found, returns an empty arrays // If no account data could be found, returns an empty arrays
// Returns an error if there was an issue with the retrieval // Returns an error if there was an issue with the retrieval
func (d *Database) GetAccountData(ctx context.Context, localpart string) ( func (d *Database) GetAccountData(ctx context.Context, localpart string) (
global map[string]json.RawMessage, global map[string][]byte,
rooms map[string]map[string]json.RawMessage, rooms map[string]map[string][]byte,
err error, err error,
) { ) {
return d.accountDatas.selectAccountData(ctx, localpart) return d.accountDatas.selectAccountData(ctx, localpart)
@ -274,7 +273,7 @@ func (d *Database) GetAccountData(ctx context.Context, localpart string) (
// Returns an error if there was an issue with the retrieval // Returns an error if there was an issue with the retrieval
func (d *Database) GetAccountDataByType( func (d *Database) GetAccountDataByType(
ctx context.Context, localpart, roomID, dataType string, ctx context.Context, localpart, roomID, dataType string,
) (data json.RawMessage, err error) { ) (data []byte, err error) {
return d.accountDatas.selectAccountDataByType( return d.accountDatas.selectAccountDataByType(
ctx, localpart, roomID, dataType, ctx, localpart, roomID, dataType,
) )