From c0e8185d0484722670ede0580662dee19da37d16 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 31 Aug 2017 19:16:50 +0100 Subject: [PATCH] Use a structure instead of a map to represent a 3PID --- .../clientapi/auth/authtypes/threepid.go | 21 +++++++++++++++++++ .../auth/storage/accounts/storage.go | 2 +- .../auth/storage/accounts/threepid_table.go | 8 ++++--- .../dendrite/clientapi/readers/threepid.go | 18 +++------------- 4 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/threepid.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/threepid.go b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/threepid.go new file mode 100644 index 000000000..60d77dc6f --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/authtypes/threepid.go @@ -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"` +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go index 44d643288..c025853e0 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/storage.go @@ -284,6 +284,6 @@ func (d *Database) GetLocalpartForThreePID(threepid string, medium string) (loca // a given local user. // If no association is known for this user, returns an empty slice. // 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) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/threepid_table.go b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/threepid_table.go index 648102337..55bcee6bc 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/threepid_table.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/storage/accounts/threepid_table.go @@ -16,6 +16,8 @@ package accounts import ( "database/sql" + + "github.com/matrix-org/dendrite/clientapi/auth/authtypes" ) const threepidSchema = ` @@ -88,20 +90,20 @@ func (s *threepidStatements) selectLocalpartForThreePID(txn *sql.Tx, threepid st 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) if err != nil { return } - threepids = make(map[string]string) + threepids = []authtypes.ThreePID{} for rows.Next() { var threepid string var medium string if err = rows.Scan(&threepid, &medium); err != nil { return } - threepids[threepid] = medium + threepids = append(threepids, authtypes.ThreePID{threepid, medium}) } return diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go b/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go index 2eb5eb919..4b86108e2 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/threepid.go @@ -32,12 +32,7 @@ type reqTokenResponse struct { } type threePIDsResponse struct { - ThreePIDs []threePID `json:"threepids"` -} - -type threePID struct { - Medium string `json:"medium"` - Address string `json:"address"` + ThreePIDs []authtypes.ThreePID `json:"threepids"` } // RequestEmailToken implements: @@ -141,22 +136,15 @@ func GetAssociated3PIDs( 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{ Code: 200, - JSON: resp, + JSON: threePIDsResponse{threepids}, } } // Forget3PID implements POST /account/3pid/delete 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 { return *reqErr }