mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 01:13:10 -06:00
Retrieval functions
This commit is contained in:
parent
391787de40
commit
f461e456ff
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,7 @@ package accounts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
const accountDataSchema = `
|
const accountDataSchema = `
|
||||||
|
|
@ -39,15 +40,19 @@ const insertAccountDataSQL = `
|
||||||
ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content
|
ON CONFLICT (localpart, room_id, type) DO UPDATE SET content = EXCLUDED.content
|
||||||
`
|
`
|
||||||
|
|
||||||
const selectAccountDataByLocalPartSQL = "" +
|
const selectGlobalAccountDataSQL = "" +
|
||||||
"SELECT room_id, type, content FROM account_data WHERE localpart = $1"
|
"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 = "" +
|
const deleteAccountDataSQL = "" +
|
||||||
"DELETE FROM account_data WHERE localpart = $1 AND room_id = $2 AND type = $3"
|
"DELETE FROM account_data WHERE localpart = $1 AND room_id = $2 AND type = $3"
|
||||||
|
|
||||||
type accountDataStatements struct {
|
type accountDataStatements struct {
|
||||||
insertAccountDataStmt *sql.Stmt
|
insertAccountDataStmt *sql.Stmt
|
||||||
selectAccountDataByLocalPartStmt *sql.Stmt
|
selectGlobalAccountDataStmt *sql.Stmt
|
||||||
|
selectRoomAccountDataStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *accountDataStatements) prepare(db *sql.DB) (err error) {
|
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 {
|
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
|
||||||
return
|
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
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
@ -68,3 +76,57 @@ func (s *accountDataStatements) insertAccountData(localpart string, roomID strin
|
||||||
_, err = s.insertAccountDataStmt.Exec(localpart, roomID, dataType, content)
|
_, err = s.insertAccountDataStmt.Exec(localpart, roomID, dataType, content)
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ import (
|
||||||
// SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type}
|
// SaveAccountData implements PUT /user/{userId}/[rooms/{roomId}/]account_data/{type}
|
||||||
func SaveAccountData(
|
func SaveAccountData(
|
||||||
req *http.Request, accountDB *accounts.Database, device *authtypes.Device,
|
req *http.Request, accountDB *accounts.Database, device *authtypes.Device,
|
||||||
userID string, dataType string, roomID string,
|
userID string, roomID string, dataType string,
|
||||||
) util.JSONResponse {
|
) util.JSONResponse {
|
||||||
if req.Method != "PUT" {
|
if req.Method != "PUT" {
|
||||||
return util.JSONResponse{
|
return util.JSONResponse{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue