aboutsummaryrefslogtreecommitdiff
path: root/userapi/storage/accounts/sqlite3/threepid_table.go
blob: 230978feeff03865d1157d2cda36556a1c640446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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 sqlite3

import (
	"context"
	"database/sql"

	"github.com/matrix-org/dendrite/internal"
	"github.com/matrix-org/dendrite/internal/sqlutil"

	"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
)

const threepidSchema = `
-- Stores data about third party identifiers
CREATE TABLE IF NOT EXISTS account_threepid (
	-- The third party identifier
	threepid TEXT NOT NULL,
	-- The 3PID medium
	medium TEXT NOT NULL DEFAULT 'email',
	-- The localpart of the Matrix user ID associated to this 3PID
	localpart TEXT NOT NULL,

	PRIMARY KEY(threepid, medium)
);

CREATE INDEX IF NOT EXISTS account_threepid_localpart ON account_threepid(localpart);
`

const selectLocalpartForThreePIDSQL = "" +
	"SELECT localpart FROM account_threepid WHERE threepid = $1 AND medium = $2"

const selectThreePIDsForLocalpartSQL = "" +
	"SELECT threepid, medium FROM account_threepid WHERE localpart = $1"

const insertThreePIDSQL = "" +
	"INSERT INTO account_threepid (threepid, medium, localpart) VALUES ($1, $2, $3)"

const deleteThreePIDSQL = "" +
	"DELETE FROM account_threepid WHERE threepid = $1 AND medium = $2"

type threepidStatements struct {
	db                              *sql.DB
	writer                          sqlutil.Writer
	selectLocalpartForThreePIDStmt  *sql.Stmt
	selectThreePIDsForLocalpartStmt *sql.Stmt
	insertThreePIDStmt              *sql.Stmt
	deleteThreePIDStmt              *sql.Stmt
}

func (s *threepidStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) {
	s.db = db
	s.writer = writer
	_, err = db.Exec(threepidSchema)
	if err != nil {
		return
	}
	if s.selectLocalpartForThreePIDStmt, err = db.Prepare(selectLocalpartForThreePIDSQL); err != nil {
		return
	}
	if s.selectThreePIDsForLocalpartStmt, err = db.Prepare(selectThreePIDsForLocalpartSQL); err != nil {
		return
	}
	if s.insertThreePIDStmt, err = db.Prepare(insertThreePIDSQL); err != nil {
		return
	}
	if s.deleteThreePIDStmt, err = db.Prepare(deleteThreePIDSQL); err != nil {
		return
	}

	return
}

func (s *threepidStatements) selectLocalpartForThreePID(
	ctx context.Context, txn *sql.Tx, threepid string, medium string,
) (localpart string, err error) {
	stmt := sqlutil.TxStmt(txn, s.selectLocalpartForThreePIDStmt)
	err = stmt.QueryRowContext(ctx, threepid, medium).Scan(&localpart)
	if err == sql.ErrNoRows {
		return "", nil
	}
	return
}

func (s *threepidStatements) selectThreePIDsForLocalpart(
	ctx context.Context, localpart string,
) (threepids []authtypes.ThreePID, err error) {
	rows, err := s.selectThreePIDsForLocalpartStmt.QueryContext(ctx, localpart)
	if err != nil {
		return
	}
	defer internal.CloseAndLogIfError(ctx, rows, "selectThreePIDsForLocalpart: rows.close() failed")

	threepids = []authtypes.ThreePID{}
	for rows.Next() {
		var threepid string
		var medium string
		if err = rows.Scan(&threepid, &medium); err != nil {
			return
		}
		threepids = append(threepids, authtypes.ThreePID{
			Address: threepid,
			Medium:  medium,
		})
	}
	return threepids, rows.Err()
}

func (s *threepidStatements) insertThreePID(
	ctx context.Context, txn *sql.Tx, threepid, medium, localpart string,
) (err error) {
	return s.writer.Do(s.db, txn, func(txn *sql.Tx) error {
		stmt := sqlutil.TxStmt(txn, s.insertThreePIDStmt)
		_, err := stmt.ExecContext(ctx, threepid, medium, localpart)
		return err
	})
}

func (s *threepidStatements) deleteThreePID(
	ctx context.Context, threepid string, medium string) (err error) {
	return s.writer.Do(s.db, nil, func(txn *sql.Tx) error {
		stmt := sqlutil.TxStmt(txn, s.deleteThreePIDStmt)
		_, err := stmt.ExecContext(ctx, threepid, medium)
		return err
	})
}