From 2f01f8ca232b7966421215b31787cbda015c1df8 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Wed, 16 Aug 2017 12:13:48 +0100 Subject: [PATCH] Fix attribute update statements --- .../publicroomsapi/directory/directory.go | 4 +- .../publicroomsapi/routing/routing.go | 2 +- .../storage/public_rooms_table.go | 43 ++++++++++++++++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/directory/directory.go b/src/github.com/matrix-org/dendrite/publicroomsapi/directory/directory.go index 04942da51..1718a18af 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/directory/directory.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/directory/directory.go @@ -29,7 +29,7 @@ type roomVisibility struct { // GetVisibility implements GET /directory/list/room/{roomID} func GetVisibility( - req *http.Request, publicRoomsDatabase storage.PublicRoomsServerDatabase, + req *http.Request, publicRoomsDatabase *storage.PublicRoomsServerDatabase, roomID string, ) util.JSONResponse { isPublic, err := publicRoomsDatabase.GetRoomVisibility(roomID) @@ -53,7 +53,7 @@ func GetVisibility( // SetVisibility implements PUT /directory/list/room/{roomID} // TODO: Check if user has the power level to edit the room visibility func SetVisibility( - req *http.Request, publicRoomsDatabase storage.PublicRoomsServerDatabase, + req *http.Request, publicRoomsDatabase *storage.PublicRoomsServerDatabase, roomID string, ) util.JSONResponse { var v roomVisibility diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/routing/routing.go b/src/github.com/matrix-org/dendrite/publicroomsapi/routing/routing.go index 936f5c51e..bcba06524 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/routing/routing.go @@ -27,7 +27,7 @@ import ( const pathPrefixR0 = "/_matrix/client/r0" // Setup configures the given mux with publicroomsapi server listeners -func Setup(apiMux *mux.Router, publicRoomsDB storage.PublicRoomsServerDatabase) { +func Setup(apiMux *mux.Router, publicRoomsDB *storage.PublicRoomsServerDatabase) { r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() r0mux.Handle("/directory/list/room/{roomID}", common.MakeAPI("directory_list", func(req *http.Request) util.JSONResponse { diff --git a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go index 26671b759..75bf4bf41 100644 --- a/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go +++ b/src/github.com/matrix-org/dendrite/publicroomsapi/storage/public_rooms_table.go @@ -16,11 +16,23 @@ package storage import ( "database/sql" + "errors" + "fmt" "github.com/lib/pq" "github.com/matrix-org/dendrite/publicroomsapi/types" ) +var editableAttributes = []string{ + "aliases", + "canonical_alias", + "name", + "topic", + "world_readable", + "guest_can_join", + "avatar_url", +} + const publicRoomsSchema = ` -- Stores all of the rooms with data needed to create the server's room directory CREATE TABLE IF NOT EXISTS publicroomsapi_public_rooms( @@ -29,7 +41,7 @@ CREATE TABLE IF NOT EXISTS publicroomsapi_public_rooms( -- Number of joined members in the room joined_members INTEGER NOT NULL DEFAULT 0, -- Aliases of the room (empty array if none) - aliases TEXT[] NOT NULL DEFAULT DEFAULT '{}'::TEXT[], + aliases TEXT[] NOT NULL DEFAULT '{}'::TEXT[], -- Canonical alias of the room (empty string if none) canonical_alias TEXT, -- Name of the room (empty string if none) @@ -84,8 +96,8 @@ const decrementJoinedMembersInRoomSQL = "" + const updateRoomAttributeSQL = "" + "UPDATE publicroomsapi_public_rooms" + - " SET $1 = $2" + - " WHERE room_id = $3" + " SET %s = $1" + + " WHERE room_id = $2" type publicRoomsStatements struct { countPublicRoomsStmt *sql.Stmt @@ -95,7 +107,7 @@ type publicRoomsStatements struct { insertNewRoomStmt *sql.Stmt incrementJoinedMembersInRoomStmt *sql.Stmt decrementJoinedMembersInRoomStmt *sql.Stmt - updateRoomAttributeStmt *sql.Stmt + updateRoomAttributeStmts map[string]*sql.Stmt } func (s *publicRoomsStatements) prepare(db *sql.DB) (err error) { @@ -124,9 +136,15 @@ func (s *publicRoomsStatements) prepare(db *sql.DB) (err error) { if s.decrementJoinedMembersInRoomStmt, err = db.Prepare(decrementJoinedMembersInRoomSQL); err != nil { return } - if s.updateRoomAttributeStmt, err = db.Prepare(updateRoomAttributeSQL); err != nil { - return + + s.updateRoomAttributeStmts = make(map[string]*sql.Stmt) + for _, editable := range editableAttributes { + stmt := fmt.Sprintf(updateRoomAttributeSQL, editable) + if s.updateRoomAttributeStmts[editable], err = db.Prepare(stmt); err != nil { + return + } } + return } @@ -194,6 +212,17 @@ func (s *publicRoomsStatements) decrementJoinedMembersInRoom(roomID string) erro } func (s *publicRoomsStatements) updateRoomAttribute(attrName string, attrValue attributeValue, roomID string) error { + isEditable := false + for _, editable := range editableAttributes { + if editable == attrName { + isEditable = true + } + } + + if !isEditable { + return errors.New("Cannot edit " + attrName) + } + var value interface{} if attrName == "aliases" { // Aliases need a special conversion @@ -202,6 +231,6 @@ func (s *publicRoomsStatements) updateRoomAttribute(attrName string, attrValue a value = attrValue } - _, err := s.updateRoomAttributeStmt.Exec(attrName, value, roomID) + _, err := s.updateRoomAttributeStmts[attrName].Exec(value, roomID) return err }