mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-17 20:03:10 -06:00
Implement API for querying room version
This commit is contained in:
parent
25cdf733e2
commit
f3dee39c9d
|
|
@ -47,6 +47,17 @@ func BuildEvent(
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("BUILDING EVENT FOR ROOM", builder.RoomID)
|
||||||
|
roomVersion := 2
|
||||||
|
|
||||||
|
vQueryReq := api.QueryRoomVersionForRoomIDRequest{RoomID: builder.RoomID}
|
||||||
|
vQueryRes := api.QueryRoomVersionForRoomIDResponse{}
|
||||||
|
if e := queryAPI.QueryRoomVersionForRoomID(ctx, &vQueryReq, &vQueryRes); e != nil {
|
||||||
|
fmt.Println("Eaux neaux, an error occured:", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("ROOM VERSION IS", roomVersion)
|
||||||
|
|
||||||
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
|
eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), cfg.Matrix.ServerName)
|
||||||
event, err := builder.Build(eventID, evTime, cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey)
|
event, err := builder.Build(eventID, evTime, cfg.Matrix.ServerName, cfg.Matrix.KeyID, cfg.Matrix.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -251,8 +251,18 @@ type QueryRoomVersionCapabilitiesRequest struct{}
|
||||||
|
|
||||||
// QueryRoomVersionCapabilitiesResponse is a response to QueryServersInRoomAtEventResponse
|
// QueryRoomVersionCapabilitiesResponse is a response to QueryServersInRoomAtEventResponse
|
||||||
type QueryRoomVersionCapabilitiesResponse struct {
|
type QueryRoomVersionCapabilitiesResponse struct {
|
||||||
DefaultRoomVersion string `json:"default"`
|
DefaultRoomVersion int `json:"default"`
|
||||||
AvailableRoomVersions map[string]string `json:"available"`
|
AvailableRoomVersions map[int]string `json:"available"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRoomVersionCapabilities asks for the default room version
|
||||||
|
type QueryRoomVersionForRoomIDRequest struct {
|
||||||
|
RoomID string `json:"room_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryRoomVersionCapabilitiesResponse is a response to QueryServersInRoomAtEventResponse
|
||||||
|
type QueryRoomVersionForRoomIDResponse struct {
|
||||||
|
RoomVersion int `json:"version"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomserverQueryAPI is used to query information from the room server.
|
// RoomserverQueryAPI is used to query information from the room server.
|
||||||
|
|
@ -341,6 +351,13 @@ type RoomserverQueryAPI interface {
|
||||||
request *QueryRoomVersionCapabilitiesRequest,
|
request *QueryRoomVersionCapabilitiesRequest,
|
||||||
response *QueryRoomVersionCapabilitiesResponse,
|
response *QueryRoomVersionCapabilitiesResponse,
|
||||||
) error
|
) error
|
||||||
|
|
||||||
|
// Asks for the default room version as preferred by the server.
|
||||||
|
QueryRoomVersionForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
request *QueryRoomVersionForRoomIDRequest,
|
||||||
|
response *QueryRoomVersionForRoomIDResponse,
|
||||||
|
) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.
|
// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.
|
||||||
|
|
@ -379,6 +396,9 @@ const RoomserverQueryServersInRoomAtEventPath = "/api/roomserver/queryServersInR
|
||||||
// RoomserverQueryRoomVersionCapabilitiesPath is the HTTP path for the QueryRoomVersionCapabilities API
|
// RoomserverQueryRoomVersionCapabilitiesPath is the HTTP path for the QueryRoomVersionCapabilities API
|
||||||
const RoomserverQueryRoomVersionCapabilitiesPath = "/api/roomserver/queryRoomVersionCapabilities"
|
const RoomserverQueryRoomVersionCapabilitiesPath = "/api/roomserver/queryRoomVersionCapabilities"
|
||||||
|
|
||||||
|
// RoomserverQueryRoomVersionCapabilitiesPath is the HTTP path for the QueryRoomVersionCapabilities API
|
||||||
|
const RoomserverQueryRoomVersionForRoomIDPath = "/api/roomserver/queryRoomVersionForRoomID"
|
||||||
|
|
||||||
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
|
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
|
||||||
// If httpClient is nil then it uses the http.DefaultClient
|
// If httpClient is nil then it uses the http.DefaultClient
|
||||||
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
|
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
|
||||||
|
|
@ -548,3 +568,16 @@ func (h *httpRoomserverQueryAPI) QueryRoomVersionCapabilities(
|
||||||
apiURL := h.roomserverURL + RoomserverQueryRoomVersionCapabilitiesPath
|
apiURL := h.roomserverURL + RoomserverQueryRoomVersionCapabilitiesPath
|
||||||
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryServersInRoomAtEvent implements RoomServerQueryAPI
|
||||||
|
func (h *httpRoomserverQueryAPI) QueryRoomVersionForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
request *QueryRoomVersionForRoomIDRequest,
|
||||||
|
response *QueryRoomVersionForRoomIDResponse,
|
||||||
|
) error {
|
||||||
|
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryRoomVersionForRoomID")
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
apiURL := h.roomserverURL + RoomserverQueryRoomVersionForRoomIDPath
|
||||||
|
return commonHTTP.PostJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
|
@ -765,19 +764,41 @@ func (r *RoomserverQueryAPI) QueryRoomVersionCapabilities(
|
||||||
request *api.QueryRoomVersionCapabilitiesRequest,
|
request *api.QueryRoomVersionCapabilitiesRequest,
|
||||||
response *api.QueryRoomVersionCapabilitiesResponse,
|
response *api.QueryRoomVersionCapabilitiesResponse,
|
||||||
) error {
|
) error {
|
||||||
response.DefaultRoomVersion = strconv.Itoa(int(version.GetDefaultRoomVersion()))
|
response.DefaultRoomVersion = int(version.GetDefaultRoomVersion())
|
||||||
response.AvailableRoomVersions = make(map[string]string)
|
response.AvailableRoomVersions = make(map[int]string)
|
||||||
for v, desc := range version.GetSupportedRoomVersions() {
|
for v, desc := range version.GetSupportedRoomVersions() {
|
||||||
sv := strconv.Itoa(int(v))
|
|
||||||
if desc.Stable {
|
if desc.Stable {
|
||||||
response.AvailableRoomVersions[sv] = "stable"
|
response.AvailableRoomVersions[int(v)] = "stable"
|
||||||
} else {
|
} else {
|
||||||
response.AvailableRoomVersions[sv] = "unstable"
|
response.AvailableRoomVersions[int(v)] = "unstable"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QueryRoomVersionCapabilities implements api.RoomserverQueryAPI
|
||||||
|
func (r *RoomserverQueryAPI) QueryRoomVersionForRoomID(
|
||||||
|
ctx context.Context,
|
||||||
|
request *api.QueryRoomVersionForRoomIDRequest,
|
||||||
|
response *api.QueryRoomVersionForRoomIDResponse,
|
||||||
|
) error {
|
||||||
|
// Get the room NID for the given room ID
|
||||||
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then look up the room version for that room NID
|
||||||
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, roomNID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate the response
|
||||||
|
response.RoomVersion = int(roomVersion)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux.
|
// SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux.
|
||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue