From f461e456ff3daad54cf6604002a80233ebdcd473 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Mon, 24 Jul 2017 17:35:45 +0100 Subject: [PATCH] Retrieval functions --- .../clientapi/auth/authtypes/account_data.go | 22 ++++++ .../storage/accounts/account_data_table.go | 72 +++++++++++++++++-- .../clientapi/readers/account_data.go | 2 +- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account_data.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account_data.go b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account_data.go new file mode 100644 index 000000000..4579b562a --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/account_data.go @@ -0,0 +1,22 @@ +// Copyright 2017 Vector Creations Ltd +// +// 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 authtypes + +// AccountData represents data specific to a given account +type AccountData struct { + Localpart string + Type string + Content string +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/account_data_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/account_data_table.go index 734ffcd96..7a7dbe9c4 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/account_data_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/account_data_table.go @@ -16,6 +16,7 @@ package accounts import ( "database/sql" + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" ) const accountDataSchema = ` @@ -39,15 +40,19 @@ const insertAccountDataSQL = ` ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content ` -const selectAccountDataByLocalPartSQL = "" + - "SELECT room_id, type, content FROM account_data WHERE localpart = $1" +const selectGlobalAccountDataSQL = "" + + "SELECT type, content FROM account_data WHERE localpart = $1 AND room_id = ''" + +const selectRoomAccountDataSQL = "" + + "SELECT type, content FROM account_data WHERE localpart = $1 AND room_id = $2" const deleteAccountDataSQL = "" + "DELETE FROM account_data WHERE localpart = $1 AND room_id = $2 AND type = $3" type accountDataStatements struct { - insertAccountDataStmt *sql.Stmt - selectAccountDataByLocalPartStmt *sql.Stmt + insertAccountDataStmt *sql.Stmt + selectGlobalAccountDataStmt *sql.Stmt + selectRoomAccountDataStmt *sql.Stmt } func (s *accountDataStatements) prepare(db *sql.DB) (err error) { @@ -58,7 +63,10 @@ func (s *accountDataStatements) prepare(db *sql.DB) (err error) { if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil { return } - if s.selectAccountDataByLocalPartStmt, err = db.Prepare(selectAccountDataByLocalPartSQL); err != nil { + if s.selectGlobalAccountDataStmt, err = db.Prepare(selectGlobalAccountDataSQL); err != nil { + return + } + if s.selectRoomAccountDataStmt, err = db.Prepare(selectRoomAccountDataSQL); err != nil { return } return @@ -68,3 +76,57 @@ func (s *accountDataStatements) insertAccountData(localpart string, roomID strin _, err = s.insertAccountDataStmt.Exec(localpart, roomID, dataType, content) return } + +func (s *accountDataStatements) selectGlobalAccountData(localpart string) ([]authtypes.AccountData, error) { + var data []authtypes.AccountData + + rows, err := s.selectGlobalAccountDataStmt.Query(localpart) + if err != nil { + return data, err + } + + for rows.Next() { + var dataType string + var content string + + if err := rows.Scan(&dataType, &content); err != nil && err != sql.ErrNoRows { + return data, err + } + + ac := authtypes.AccountData{ + Localpart: localpart, + Type: dataType, + Content: content, + } + data = append(data, ac) + } + + return data, nil +} + +func (s *accountDataStatements) selectRoomAccountData(localpart string, roomID string) ([]authtypes.AccountData, error) { + var data []authtypes.AccountData + + rows, err := s.selectRoomAccountDataStmt.Query(localpart) + if err != nil { + return data, err + } + + for rows.Next() { + var dataType string + var content string + + if err := rows.Scan(&dataType, &content); err != nil && err != sql.ErrNoRows { + return data, err + } + + ac := authtypes.AccountData{ + Localpart: localpart, + Type: dataType, + Content: content, + } + data = append(data, ac) + } + + return data, nil +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/account_data.go b/src/github.com/matrix-org/dendrite/clientapi/readers/account_data.go index bed0852cd..ca2c2232e 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/account_data.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/account_data.go @@ -30,7 +30,7 @@ import ( // SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type} func SaveAccountData( req *http.Request, accountDB *accounts.Database, device *authtypes.Device, - userID string, dataType string, roomID string, + userID string, roomID string, dataType string, ) util.JSONResponse { if req.Method != "PUT" { return util.JSONResponse{