2020-06-30 09:37:21 +00:00
|
|
|
// 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 {
|
2020-06-30 12:34:59 +00:00
|
|
|
// QueryCurrentState retrieves the requested state events. If state events are not found, they will be missing from
|
|
|
|
// the response.
|
2020-06-30 09:37:21 +00:00
|
|
|
QueryCurrentState(ctx context.Context, req *QueryCurrentStateRequest, res *QueryCurrentStateResponse) error
|
2020-06-30 12:34:59 +00:00
|
|
|
// QueryRoomsForUser retrieves a list of room IDs matching the given query.
|
|
|
|
QueryRoomsForUser(ctx context.Context, req *QueryRoomsForUserRequest, res *QueryRoomsForUserResponse) error
|
2020-07-02 14:41:18 +00:00
|
|
|
// QueryBulkStateContent does a bulk query for state event content in the given rooms.
|
|
|
|
QueryBulkStateContent(ctx context.Context, req *QueryBulkStateContentRequest, res *QueryBulkStateContentResponse) error
|
2020-06-30 12:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryRoomsForUserRequest struct {
|
|
|
|
UserID string
|
|
|
|
// The desired membership of the user. If this is the empty string then no rooms are returned.
|
|
|
|
WantMembership string
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryRoomsForUserResponse struct {
|
|
|
|
RoomIDs []string
|
2020-06-30 09:37:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 14:41:18 +00:00
|
|
|
type QueryBulkStateContentRequest struct {
|
|
|
|
// Returns state events in these rooms
|
|
|
|
RoomIDs []string
|
|
|
|
// If true, treats the '*' StateKey as "all state events of this type" rather than a literal value of '*'
|
|
|
|
AllowWildcards bool
|
|
|
|
// The state events to return. Only a small subset of tuples are allowed in this request as only certain events
|
|
|
|
// have their content fields extracted. Specifically, the tuple Type must be one of:
|
|
|
|
// m.room.avatar
|
|
|
|
// m.room.create
|
|
|
|
// m.room.canonical_alias
|
|
|
|
// m.room.guest_access
|
|
|
|
// m.room.history_visibility
|
|
|
|
// m.room.join_rules
|
|
|
|
// m.room.member
|
|
|
|
// m.room.name
|
|
|
|
// m.room.topic
|
|
|
|
// Any other tuple type will result in the query failing.
|
|
|
|
StateTuples []gomatrixserverlib.StateKeyTuple
|
|
|
|
}
|
|
|
|
type QueryBulkStateContentResponse struct {
|
|
|
|
// map of room ID -> tuple -> content_value
|
|
|
|
Rooms map[string]map[gomatrixserverlib.StateKeyTuple]string
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:37:21 +00:00
|
|
|
type QueryCurrentStateRequest struct {
|
|
|
|
RoomID string
|
|
|
|
StateTuples []gomatrixserverlib.StateKeyTuple
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryCurrentStateResponse struct {
|
2020-06-30 12:34:59 +00:00
|
|
|
StateEvents map[gomatrixserverlib.StateKeyTuple]*gomatrixserverlib.HeaderedEvent
|
2020-06-30 09:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON stringifies the StateKeyTuple keys so they can be sent over the wire in HTTP API mode.
|
|
|
|
func (r *QueryCurrentStateResponse) MarshalJSON() ([]byte, error) {
|
2020-06-30 12:34:59 +00:00
|
|
|
se := make(map[string]*gomatrixserverlib.HeaderedEvent, len(r.StateEvents))
|
2020-06-30 09:37:21 +00:00
|
|
|
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 {
|
2020-06-30 12:34:59 +00:00
|
|
|
res := make(map[string]*gomatrixserverlib.HeaderedEvent)
|
2020-06-30 09:37:21 +00:00
|
|
|
err := json.Unmarshal(data, &res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-30 12:34:59 +00:00
|
|
|
r.StateEvents = make(map[gomatrixserverlib.StateKeyTuple]*gomatrixserverlib.HeaderedEvent, len(res))
|
2020-06-30 09:37:21 +00:00
|
|
|
for k, v := range res {
|
|
|
|
fields := strings.Split(k, "\x1F")
|
|
|
|
r.StateEvents[gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: fields[0],
|
|
|
|
StateKey: fields[1],
|
|
|
|
}] = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|