Use a structure instead of a map to represent a 3PID

This commit is contained in:
Brendan Abolivier 2017-08-31 19:16:50 +01:00
parent e35b7fc869
commit c0e8185d04
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
4 changed files with 30 additions and 19 deletions

View file

@ -0,0 +1,21 @@
// 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
// ThreePID represents a third-party identifier
type ThreePID struct {
Address string `json:"address"`
Medium string `json:"medium"`
}

View file

@ -284,6 +284,6 @@ func (d *Database) GetLocalpartForThreePID(threepid string, medium string) (loca
// a given local user. // a given local user.
// If no association is known for this user, returns an empty slice. // If no association is known for this user, returns an empty slice.
// Returns an error if there was an issue talking to the database. // Returns an error if there was an issue talking to the database.
func (d *Database) GetThreePIDsForLocalpart(localpart string) (threepids map[string]string, err error) { func (d *Database) GetThreePIDsForLocalpart(localpart string) (threepids []authtypes.ThreePID, err error) {
return d.threepids.selectThreePIDsForLocalpart(localpart) return d.threepids.selectThreePIDsForLocalpart(localpart)
} }

View file

@ -16,6 +16,8 @@ package accounts
import ( import (
"database/sql" "database/sql"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
) )
const threepidSchema = ` const threepidSchema = `
@ -88,20 +90,20 @@ func (s *threepidStatements) selectLocalpartForThreePID(txn *sql.Tx, threepid st
return return
} }
func (s *threepidStatements) selectThreePIDsForLocalpart(localpart string) (threepids map[string]string, err error) { func (s *threepidStatements) selectThreePIDsForLocalpart(localpart string) (threepids []authtypes.ThreePID, err error) {
rows, err := s.selectThreePIDsForLocalpartStmt.Query(localpart) rows, err := s.selectThreePIDsForLocalpartStmt.Query(localpart)
if err != nil { if err != nil {
return return
} }
threepids = make(map[string]string) threepids = []authtypes.ThreePID{}
for rows.Next() { for rows.Next() {
var threepid string var threepid string
var medium string var medium string
if err = rows.Scan(&threepid, &medium); err != nil { if err = rows.Scan(&threepid, &medium); err != nil {
return return
} }
threepids[threepid] = medium threepids = append(threepids, authtypes.ThreePID{threepid, medium})
} }
return return

View file

@ -32,12 +32,7 @@ type reqTokenResponse struct {
} }
type threePIDsResponse struct { type threePIDsResponse struct {
ThreePIDs []threePID `json:"threepids"` ThreePIDs []authtypes.ThreePID `json:"threepids"`
}
type threePID struct {
Medium string `json:"medium"`
Address string `json:"address"`
} }
// RequestEmailToken implements: // RequestEmailToken implements:
@ -141,22 +136,15 @@ func GetAssociated3PIDs(
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
var resp threePIDsResponse
resp.ThreePIDs = []threePID{}
for address, medium := range threepids {
tpid := threePID{Medium: medium, Address: address}
resp.ThreePIDs = append(resp.ThreePIDs, tpid)
}
return util.JSONResponse{ return util.JSONResponse{
Code: 200, Code: 200,
JSON: resp, JSON: threePIDsResponse{threepids},
} }
} }
// Forget3PID implements POST /account/3pid/delete // Forget3PID implements POST /account/3pid/delete
func Forget3PID(req *http.Request, accountDB *accounts.Database) util.JSONResponse { func Forget3PID(req *http.Request, accountDB *accounts.Database) util.JSONResponse {
var body threePID var body authtypes.ThreePID
if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil { if reqErr := httputil.UnmarshalJSONRequest(req, &body); reqErr != nil {
return *reqErr return *reqErr
} }