Fix attribute update statements

This commit is contained in:
Brendan Abolivier 2017-08-16 12:13:48 +01:00
parent bd7e43bcab
commit 2f01f8ca23
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
3 changed files with 39 additions and 10 deletions

View file

@ -29,7 +29,7 @@ type roomVisibility struct {
// GetVisibility implements GET /directory/list/room/{roomID} // GetVisibility implements GET /directory/list/room/{roomID}
func GetVisibility( func GetVisibility(
req *http.Request, publicRoomsDatabase storage.PublicRoomsServerDatabase, req *http.Request, publicRoomsDatabase *storage.PublicRoomsServerDatabase,
roomID string, roomID string,
) util.JSONResponse { ) util.JSONResponse {
isPublic, err := publicRoomsDatabase.GetRoomVisibility(roomID) isPublic, err := publicRoomsDatabase.GetRoomVisibility(roomID)
@ -53,7 +53,7 @@ func GetVisibility(
// SetVisibility implements PUT /directory/list/room/{roomID} // SetVisibility implements PUT /directory/list/room/{roomID}
// TODO: Check if user has the power level to edit the room visibility // TODO: Check if user has the power level to edit the room visibility
func SetVisibility( func SetVisibility(
req *http.Request, publicRoomsDatabase storage.PublicRoomsServerDatabase, req *http.Request, publicRoomsDatabase *storage.PublicRoomsServerDatabase,
roomID string, roomID string,
) util.JSONResponse { ) util.JSONResponse {
var v roomVisibility var v roomVisibility

View file

@ -27,7 +27,7 @@ import (
const pathPrefixR0 = "/_matrix/client/r0" const pathPrefixR0 = "/_matrix/client/r0"
// Setup configures the given mux with publicroomsapi server listeners // 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 := apiMux.PathPrefix(pathPrefixR0).Subrouter()
r0mux.Handle("/directory/list/room/{roomID}", r0mux.Handle("/directory/list/room/{roomID}",
common.MakeAPI("directory_list", func(req *http.Request) util.JSONResponse { common.MakeAPI("directory_list", func(req *http.Request) util.JSONResponse {

View file

@ -16,11 +16,23 @@ package storage
import ( import (
"database/sql" "database/sql"
"errors"
"fmt"
"github.com/lib/pq" "github.com/lib/pq"
"github.com/matrix-org/dendrite/publicroomsapi/types" "github.com/matrix-org/dendrite/publicroomsapi/types"
) )
var editableAttributes = []string{
"aliases",
"canonical_alias",
"name",
"topic",
"world_readable",
"guest_can_join",
"avatar_url",
}
const publicRoomsSchema = ` const publicRoomsSchema = `
-- Stores all of the rooms with data needed to create the server's room directory -- Stores all of the rooms with data needed to create the server's room directory
CREATE TABLE IF NOT EXISTS publicroomsapi_public_rooms( 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 -- Number of joined members in the room
joined_members INTEGER NOT NULL DEFAULT 0, joined_members INTEGER NOT NULL DEFAULT 0,
-- Aliases of the room (empty array if none) -- 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 of the room (empty string if none)
canonical_alias TEXT, canonical_alias TEXT,
-- Name of the room (empty string if none) -- Name of the room (empty string if none)
@ -84,8 +96,8 @@ const decrementJoinedMembersInRoomSQL = "" +
const updateRoomAttributeSQL = "" + const updateRoomAttributeSQL = "" +
"UPDATE publicroomsapi_public_rooms" + "UPDATE publicroomsapi_public_rooms" +
" SET $1 = $2" + " SET %s = $1" +
" WHERE room_id = $3" " WHERE room_id = $2"
type publicRoomsStatements struct { type publicRoomsStatements struct {
countPublicRoomsStmt *sql.Stmt countPublicRoomsStmt *sql.Stmt
@ -95,7 +107,7 @@ type publicRoomsStatements struct {
insertNewRoomStmt *sql.Stmt insertNewRoomStmt *sql.Stmt
incrementJoinedMembersInRoomStmt *sql.Stmt incrementJoinedMembersInRoomStmt *sql.Stmt
decrementJoinedMembersInRoomStmt *sql.Stmt decrementJoinedMembersInRoomStmt *sql.Stmt
updateRoomAttributeStmt *sql.Stmt updateRoomAttributeStmts map[string]*sql.Stmt
} }
func (s *publicRoomsStatements) prepare(db *sql.DB) (err error) { 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 { if s.decrementJoinedMembersInRoomStmt, err = db.Prepare(decrementJoinedMembersInRoomSQL); err != nil {
return 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 return
} }
@ -194,6 +212,17 @@ func (s *publicRoomsStatements) decrementJoinedMembersInRoom(roomID string) erro
} }
func (s *publicRoomsStatements) updateRoomAttribute(attrName string, attrValue attributeValue, roomID string) error { 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{} var value interface{}
if attrName == "aliases" { if attrName == "aliases" {
// Aliases need a special conversion // Aliases need a special conversion
@ -202,6 +231,6 @@ func (s *publicRoomsStatements) updateRoomAttribute(attrName string, attrValue a
value = attrValue value = attrValue
} }
_, err := s.updateRoomAttributeStmt.Exec(attrName, value, roomID) _, err := s.updateRoomAttributeStmts[attrName].Exec(value, roomID)
return err return err
} }