mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-27 00:31:55 -06:00
65 lines
2 KiB
Go
65 lines
2 KiB
Go
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// 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.
|
||
|
|
||
|
package api
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/matrix-org/gomatrixserverlib"
|
||
|
)
|
||
|
|
||
|
type CurrentStateInternalAPI interface {
|
||
|
QueryCurrentState(ctx context.Context, req *QueryCurrentStateRequest, res *QueryCurrentStateResponse) error
|
||
|
}
|
||
|
|
||
|
type QueryCurrentStateRequest struct {
|
||
|
RoomID string
|
||
|
StateTuples []gomatrixserverlib.StateKeyTuple
|
||
|
}
|
||
|
|
||
|
type QueryCurrentStateResponse struct {
|
||
|
StateEvents map[gomatrixserverlib.StateKeyTuple]gomatrixserverlib.HeaderedEvent
|
||
|
}
|
||
|
|
||
|
// MarshalJSON stringifies the StateKeyTuple keys so they can be sent over the wire in HTTP API mode.
|
||
|
func (r *QueryCurrentStateResponse) MarshalJSON() ([]byte, error) {
|
||
|
se := make(map[string]gomatrixserverlib.HeaderedEvent, len(r.StateEvents))
|
||
|
for k, v := range r.StateEvents {
|
||
|
// use 0x1F (unit separator) as the delimiter between type/state key,
|
||
|
se[fmt.Sprintf("%s\x1F%s", k.EventType, k.StateKey)] = v
|
||
|
}
|
||
|
return json.Marshal(se)
|
||
|
}
|
||
|
|
||
|
func (r *QueryCurrentStateResponse) UnmarshalJSON(data []byte) error {
|
||
|
res := make(map[string]gomatrixserverlib.HeaderedEvent)
|
||
|
err := json.Unmarshal(data, &res)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
r.StateEvents = make(map[gomatrixserverlib.StateKeyTuple]gomatrixserverlib.HeaderedEvent, len(res))
|
||
|
for k, v := range res {
|
||
|
fields := strings.Split(k, "\x1F")
|
||
|
r.StateEvents[gomatrixserverlib.StateKeyTuple{
|
||
|
EventType: fields[0],
|
||
|
StateKey: fields[1],
|
||
|
}] = v
|
||
|
}
|
||
|
return nil
|
||
|
}
|