Use gmsl structs rather than our own

This commit is contained in:
Kegan Dougal 2020-03-18 18:13:02 +00:00
parent dd97384c6a
commit 31bc5ce45d
8 changed files with 63 additions and 75 deletions

View file

@ -15,17 +15,18 @@
package directory
import (
"context"
"net/http"
"strconv"
"github.com/matrix-org/dendrite/clientapi/httputil"
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/publicroomsapi/storage"
"github.com/matrix-org/dendrite/publicroomsapi/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/util"
)
type publicRoomReq struct {
type PublicRoomReq struct {
Since string `json:"since,omitempty"`
Limit int16 `json:"limit,omitempty"`
Filter filter `json:"filter,omitempty"`
@ -35,65 +36,66 @@ type filter struct {
SearchTerms string `json:"generic_search_term,omitempty"`
}
type publicRoomRes struct {
Chunk []types.PublicRoom `json:"chunk"`
NextBatch string `json:"next_batch,omitempty"`
PrevBatch string `json:"prev_batch,omitempty"`
Estimate int64 `json:"total_room_count_estimate,omitempty"`
}
// GetPostPublicRooms implements GET and POST /publicRooms
func GetPostPublicRooms(
req *http.Request, publicRoomDatabase storage.Database,
) util.JSONResponse {
var limit int16
var offset int64
var request publicRoomReq
var response publicRoomRes
var request PublicRoomReq
if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil {
return *fillErr
}
limit = request.Limit
offset, err := strconv.ParseInt(request.Since, 10, 64)
// ParseInt returns 0 and an error when trying to parse an empty string
// In that case, we want to assign 0 so we ignore the error
if err != nil && len(request.Since) > 0 {
util.GetLogger(req.Context()).WithError(err).Error("strconv.ParseInt failed")
response, err := PublicRooms(req.Context(), request, publicRoomDatabase)
if err != nil {
return jsonerror.InternalServerError()
}
if response.Estimate, err = publicRoomDatabase.CountPublicRooms(req.Context()); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("publicRoomDatabase.CountPublicRooms failed")
return jsonerror.InternalServerError()
}
if offset > 0 {
response.PrevBatch = strconv.Itoa(int(offset) - 1)
}
nextIndex := int(offset) + int(limit)
if response.Estimate > int64(nextIndex) {
response.NextBatch = strconv.Itoa(nextIndex)
}
if response.Chunk, err = publicRoomDatabase.GetPublicRooms(
req.Context(), offset, limit, request.Filter.SearchTerms,
); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("publicRoomDatabase.GetPublicRooms failed")
return jsonerror.InternalServerError()
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: response,
}
}
func PublicRooms(ctx context.Context, request PublicRoomReq, publicRoomDatabase storage.Database) (*gomatrixserverlib.RespPublicRooms, error) {
var response gomatrixserverlib.RespPublicRooms
var limit int16
var offset int64
limit = request.Limit
offset, err := strconv.ParseInt(request.Since, 10, 64)
// ParseInt returns 0 and an error when trying to parse an empty string
// In that case, we want to assign 0 so we ignore the error
if err != nil && len(request.Since) > 0 {
util.GetLogger(ctx).WithError(err).Error("strconv.ParseInt failed")
return nil, err
}
est, err := publicRoomDatabase.CountPublicRooms(ctx)
if err != nil {
util.GetLogger(ctx).WithError(err).Error("publicRoomDatabase.CountPublicRooms failed")
return nil, err
}
response.TotalRoomCountEstimate = int(est)
if offset > 0 {
response.PrevBatch = strconv.Itoa(int(offset) - 1)
}
nextIndex := int(offset) + int(limit)
if response.TotalRoomCountEstimate > nextIndex {
response.NextBatch = strconv.Itoa(nextIndex)
}
if response.Chunk, err = publicRoomDatabase.GetPublicRooms(
ctx, offset, limit, request.Filter.SearchTerms,
); err != nil {
util.GetLogger(ctx).WithError(err).Error("publicRoomDatabase.GetPublicRooms failed")
return nil, err
}
return &response, nil
}
// fillPublicRoomsReq fills the Limit, Since and Filter attributes of a GET or POST request
// on /publicRooms by parsing the incoming HTTP request
// Filter is only filled for POST requests
func fillPublicRoomsReq(httpReq *http.Request, request *publicRoomReq) *util.JSONResponse {
func fillPublicRoomsReq(httpReq *http.Request, request *PublicRoomReq) *util.JSONResponse {
if httpReq.Method == http.MethodGet {
limit, err := strconv.Atoi(httpReq.FormValue("limit"))
// Atoi returns 0 and an error when trying to parse an empty string

View file

@ -67,11 +67,13 @@ func Setup(apiMux *mux.Router, deviceDB devices.Database, publicRoomsDB storage.
).Methods(http.MethodPut, http.MethodOptions)
r0mux.Handle("/publicRooms",
common.MakeExternalAPI("public_rooms", func(req *http.Request) util.JSONResponse {
if extRoomsProvider != nil {
}
return directory.GetPostPublicRooms(req, publicRoomsDB)
}),
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)
// Federation
// Federation - TODO: should this live here or in federation API? It's sure easier if it's here so here it is.
apiMux.Handle("/_matrix/federation/v1/publicRooms",
common.MakeExternalAPI("federation_public_rooms", func(req *http.Request) util.JSONResponse {
return directory.GetPostPublicRooms(req, publicRoomsDB)

View file

@ -18,7 +18,6 @@ import (
"context"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/publicroomsapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@ -27,7 +26,7 @@ type Database interface {
GetRoomVisibility(ctx context.Context, roomID string) (bool, error)
SetRoomVisibility(ctx context.Context, visible bool, roomID string) error
CountPublicRooms(ctx context.Context) (int64, error)
GetPublicRooms(ctx context.Context, offset int64, limit int16, filter string) ([]types.PublicRoom, error)
GetPublicRooms(ctx context.Context, offset int64, limit int16, filter string) ([]gomatrixserverlib.PublicRoom, error)
UpdateRoomFromEvents(ctx context.Context, eventsToAdd []gomatrixserverlib.Event, eventsToRemove []gomatrixserverlib.Event) error
UpdateRoomFromEvent(ctx context.Context, event gomatrixserverlib.Event) error
}

View file

@ -22,9 +22,9 @@ import (
"fmt"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/gomatrixserverlib"
"github.com/lib/pq"
"github.com/matrix-org/dendrite/publicroomsapi/types"
)
var editableAttributes = []string{
@ -177,7 +177,7 @@ func (s *publicRoomsStatements) countPublicRooms(ctx context.Context) (nb int64,
func (s *publicRoomsStatements) selectPublicRooms(
ctx context.Context, offset int64, limit int16, filter string,
) ([]types.PublicRoom, error) {
) ([]gomatrixserverlib.PublicRoom, error) {
var rows *sql.Rows
var err error
@ -203,17 +203,17 @@ func (s *publicRoomsStatements) selectPublicRooms(
}
if err != nil {
return []types.PublicRoom{}, nil
return []gomatrixserverlib.PublicRoom{}, nil
}
defer common.CloseAndLogIfError(ctx, rows, "selectPublicRooms: rows.close() failed")
rooms := []types.PublicRoom{}
rooms := []gomatrixserverlib.PublicRoom{}
for rows.Next() {
var r types.PublicRoom
var r gomatrixserverlib.PublicRoom
var aliases pq.StringArray
err = rows.Scan(
&r.RoomID, &r.NumJoinedMembers, &aliases, &r.CanonicalAlias,
&r.RoomID, &r.JoinedMembersCount, &aliases, &r.CanonicalAlias,
&r.Name, &r.Topic, &r.WorldReadable, &r.GuestCanJoin, &r.AvatarURL,
)
if err != nil {

View file

@ -21,7 +21,6 @@ import (
"encoding/json"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/publicroomsapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@ -85,7 +84,7 @@ func (d *PublicRoomsServerDatabase) CountPublicRooms(ctx context.Context) (int64
// Returns an error if the retrieval failed.
func (d *PublicRoomsServerDatabase) GetPublicRooms(
ctx context.Context, offset int64, limit int16, filter string,
) ([]types.PublicRoom, error) {
) ([]gomatrixserverlib.PublicRoom, error) {
return d.statements.selectPublicRooms(ctx, offset, limit, filter)
}

View file

@ -22,7 +22,7 @@ import (
"errors"
"fmt"
"github.com/matrix-org/dendrite/publicroomsapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
var editableAttributes = []string{
@ -164,7 +164,7 @@ func (s *publicRoomsStatements) countPublicRooms(ctx context.Context) (nb int64,
func (s *publicRoomsStatements) selectPublicRooms(
ctx context.Context, offset int64, limit int16, filter string,
) ([]types.PublicRoom, error) {
) ([]gomatrixserverlib.PublicRoom, error) {
var rows *sql.Rows
var err error
@ -190,16 +190,16 @@ func (s *publicRoomsStatements) selectPublicRooms(
}
if err != nil {
return []types.PublicRoom{}, nil
return []gomatrixserverlib.PublicRoom{}, nil
}
rooms := []types.PublicRoom{}
rooms := []gomatrixserverlib.PublicRoom{}
for rows.Next() {
var r types.PublicRoom
var r gomatrixserverlib.PublicRoom
var aliasesJSON string
err = rows.Scan(
&r.RoomID, &r.NumJoinedMembers, &aliasesJSON, &r.CanonicalAlias,
&r.RoomID, &r.JoinedMembersCount, &aliasesJSON, &r.CanonicalAlias,
&r.Name, &r.Topic, &r.WorldReadable, &r.GuestCanJoin, &r.AvatarURL,
)
if err != nil {

View file

@ -23,7 +23,6 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/publicroomsapi/types"
"github.com/matrix-org/gomatrixserverlib"
)
@ -87,7 +86,7 @@ func (d *PublicRoomsServerDatabase) CountPublicRooms(ctx context.Context) (int64
// Returns an error if the retrieval failed.
func (d *PublicRoomsServerDatabase) GetPublicRooms(
ctx context.Context, offset int64, limit int16, filter string,
) ([]types.PublicRoom, error) {
) ([]gomatrixserverlib.PublicRoom, error) {
return d.statements.selectPublicRooms(ctx, offset, limit, filter)
}

View file

@ -16,19 +16,6 @@ package types
import "time"
// PublicRoom represents a local public room
type PublicRoom struct {
RoomID string `json:"room_id"`
Aliases []string `json:"aliases,omitempty"`
CanonicalAlias string `json:"canonical_alias,omitempty"`
Name string `json:"name,omitempty"`
Topic string `json:"topic,omitempty"`
AvatarURL string `json:"avatar_url,omitempty"`
NumJoinedMembers int64 `json:"num_joined_members"`
WorldReadable bool `json:"world_readable"`
GuestCanJoin bool `json:"guest_can_join"`
}
// ExternalPublicRoomsProvider provides a list of homeservers who should be queried
// periodically for a list of public rooms on their server.
type ExternalPublicRoomsProvider interface {