2022-09-11 11:57:00 +00:00
|
|
|
//go:build !nosqlite
|
2022-12-10 23:01:16 +00:00
|
|
|
// +build !nosqlite
|
2022-09-11 11:57:00 +00:00
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
package database
|
2021-05-24 19:13:31 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-14 14:13:24 +00:00
|
|
|
"context"
|
2021-05-24 19:13:31 +00:00
|
|
|
"database/sql"
|
2023-01-26 13:11:07 +00:00
|
|
|
sqldriver "database/sql/driver"
|
2021-05-24 19:13:31 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-12-10 23:01:16 +00:00
|
|
|
"git.sr.ht/~emersion/soju/xirc"
|
2021-11-17 14:40:02 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
promcollectors "github.com/prometheus/client_golang/prometheus/collectors"
|
2022-12-10 23:01:16 +00:00
|
|
|
"gopkg.in/irc.v4"
|
2021-05-24 19:13:31 +00:00
|
|
|
)
|
|
|
|
|
2022-09-11 11:57:00 +00:00
|
|
|
const SqliteEnabled = true
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
const sqliteQueryTimeout = 5 * time.Second
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
const sqliteTimeLayout = "2006-01-02T15:04:05.000Z"
|
2023-01-26 11:34:53 +00:00
|
|
|
const sqliteTimeFormat = "%Y-%m-%dT%H:%M:%fZ"
|
2022-05-09 10:34:43 +00:00
|
|
|
|
2023-01-26 13:11:07 +00:00
|
|
|
type sqliteTime struct {
|
|
|
|
time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ sql.Scanner = (*sqliteTime)(nil)
|
|
|
|
_ sqldriver.Valuer = sqliteTime{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *sqliteTime) Scan(value interface{}) error {
|
|
|
|
if value == nil {
|
|
|
|
t.Time = time.Time{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if s, ok := value.(string); ok {
|
|
|
|
tt, err := time.Parse(sqliteTimeLayout, s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.Time = tt
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("cannot scan time from type %T", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t sqliteTime) Value() (sqldriver.Value, error) {
|
|
|
|
if t.Time.IsZero() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return t.UTC().Format(sqliteTimeLayout), nil
|
2022-05-09 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
const sqliteSchema = `
|
|
|
|
CREATE TABLE User (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2021-11-02 21:58:50 +00:00
|
|
|
username TEXT NOT NULL UNIQUE,
|
|
|
|
password TEXT,
|
2021-06-25 18:33:13 +00:00
|
|
|
admin INTEGER NOT NULL DEFAULT 0,
|
2022-07-08 16:01:05 +00:00
|
|
|
realname TEXT,
|
2023-01-26 11:34:53 +00:00
|
|
|
nick TEXT,
|
2023-01-26 17:33:55 +00:00
|
|
|
created_at TEXT NOT NULL,
|
2023-01-26 13:02:11 +00:00
|
|
|
enabled INTEGER NOT NULL DEFAULT 1,
|
|
|
|
downstream_interacted_at TEXT
|
2021-05-24 19:13:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Network (
|
|
|
|
id INTEGER PRIMARY KEY,
|
2021-11-02 21:58:50 +00:00
|
|
|
name TEXT,
|
2021-05-24 19:13:31 +00:00
|
|
|
user INTEGER NOT NULL,
|
2021-11-02 21:58:50 +00:00
|
|
|
addr TEXT NOT NULL,
|
2021-11-02 22:33:17 +00:00
|
|
|
nick TEXT,
|
2021-11-02 21:58:50 +00:00
|
|
|
username TEXT,
|
|
|
|
realname TEXT,
|
2022-12-10 08:12:46 +00:00
|
|
|
certfp TEXT,
|
2021-11-02 21:58:50 +00:00
|
|
|
pass TEXT,
|
|
|
|
connect_commands TEXT,
|
|
|
|
sasl_mechanism TEXT,
|
|
|
|
sasl_plain_username TEXT,
|
|
|
|
sasl_plain_password TEXT,
|
|
|
|
sasl_external_cert BLOB,
|
|
|
|
sasl_external_key BLOB,
|
2022-09-26 17:49:26 +00:00
|
|
|
auto_away INTEGER NOT NULL DEFAULT 1,
|
2021-05-26 08:49:52 +00:00
|
|
|
enabled INTEGER NOT NULL DEFAULT 1,
|
2021-05-24 19:13:31 +00:00
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
|
|
|
UNIQUE(user, addr, nick),
|
|
|
|
UNIQUE(user, name)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE Channel (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
2021-11-02 21:58:50 +00:00
|
|
|
name TEXT NOT NULL,
|
|
|
|
key TEXT,
|
2021-05-24 19:13:31 +00:00
|
|
|
detached INTEGER NOT NULL DEFAULT 0,
|
2021-11-02 21:58:50 +00:00
|
|
|
detached_internal_msgid TEXT,
|
2021-05-24 19:13:31 +00:00
|
|
|
relay_detached INTEGER NOT NULL DEFAULT 0,
|
|
|
|
reattach_on INTEGER NOT NULL DEFAULT 0,
|
|
|
|
detach_after INTEGER NOT NULL DEFAULT 0,
|
|
|
|
detach_on INTEGER NOT NULL DEFAULT 0,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, name)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE DeliveryReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
2021-11-02 21:58:50 +00:00
|
|
|
target TEXT NOT NULL,
|
|
|
|
client TEXT,
|
|
|
|
internal_msgid TEXT NOT NULL,
|
2021-05-24 19:13:31 +00:00
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target, client)
|
|
|
|
);
|
2021-01-29 15:57:38 +00:00
|
|
|
|
|
|
|
CREATE TABLE ReadReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target TEXT NOT NULL,
|
|
|
|
timestamp TEXT NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target)
|
|
|
|
);
|
2021-11-27 10:48:10 +00:00
|
|
|
|
|
|
|
CREATE TABLE WebPushConfig (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
created_at TEXT NOT NULL,
|
|
|
|
vapid_key_public TEXT NOT NULL,
|
|
|
|
vapid_key_private TEXT NOT NULL,
|
|
|
|
UNIQUE(vapid_key_public)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE WebPushSubscription (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
created_at TEXT NOT NULL,
|
|
|
|
updated_at TEXT NOT NULL,
|
2022-06-16 17:33:39 +00:00
|
|
|
user INTEGER NOT NULL,
|
2021-11-27 10:48:10 +00:00
|
|
|
network INTEGER,
|
|
|
|
endpoint TEXT NOT NULL,
|
|
|
|
key_vapid TEXT,
|
|
|
|
key_auth TEXT,
|
|
|
|
key_p256dh TEXT,
|
2022-06-16 17:33:39 +00:00
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
2021-11-27 10:48:10 +00:00
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, endpoint)
|
|
|
|
);
|
2022-12-10 23:01:16 +00:00
|
|
|
|
|
|
|
CREATE TABLE Message (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
target INTEGER NOT NULL,
|
|
|
|
raw TEXT NOT NULL,
|
|
|
|
time TEXT NOT NULL,
|
|
|
|
sender TEXT NOT NULL,
|
|
|
|
text TEXT,
|
|
|
|
FOREIGN KEY(target) REFERENCES MessageTarget(id)
|
|
|
|
);
|
|
|
|
CREATE INDEX MessageIndex ON Message(target, time);
|
|
|
|
|
|
|
|
CREATE TABLE MessageTarget (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target TEXT NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE VIRTUAL TABLE MessageFTS USING fts5 (
|
|
|
|
text,
|
|
|
|
content=Message,
|
|
|
|
content_rowid=id
|
|
|
|
);
|
|
|
|
CREATE TRIGGER MessageFTSInsert AFTER INSERT ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(rowid, text) VALUES (new.id, new.text);
|
|
|
|
END;
|
|
|
|
CREATE TRIGGER MessageFTSDelete AFTER DELETE ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(MessageFTS, rowid, text) VALUES ('delete', old.id, old.text);
|
|
|
|
END;
|
|
|
|
CREATE TRIGGER MessageFTSUpdate AFTER UPDATE ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(MessageFTS, rowid, text) VALUES ('delete', old.id, old.text);
|
|
|
|
INSERT INTO MessageFTS(rowid, text) VALUES (new.id, new.text);
|
|
|
|
END;
|
2021-05-24 19:13:31 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var sqliteMigrations = []string{
|
|
|
|
"", // migration #0 is reserved for schema initialization
|
|
|
|
"ALTER TABLE Network ADD COLUMN connect_commands VARCHAR(1023)",
|
|
|
|
"ALTER TABLE Channel ADD COLUMN detached INTEGER NOT NULL DEFAULT 0",
|
|
|
|
"ALTER TABLE Network ADD COLUMN sasl_external_cert BLOB DEFAULT NULL",
|
|
|
|
"ALTER TABLE Network ADD COLUMN sasl_external_key BLOB DEFAULT NULL",
|
|
|
|
"ALTER TABLE User ADD COLUMN admin INTEGER NOT NULL DEFAULT 0",
|
|
|
|
`
|
|
|
|
CREATE TABLE UserNew (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
username VARCHAR(255) NOT NULL UNIQUE,
|
|
|
|
password VARCHAR(255),
|
|
|
|
admin INTEGER NOT NULL DEFAULT 0
|
|
|
|
);
|
|
|
|
INSERT INTO UserNew SELECT rowid, username, password, admin FROM User;
|
|
|
|
DROP TABLE User;
|
|
|
|
ALTER TABLE UserNew RENAME TO User;
|
|
|
|
`,
|
|
|
|
`
|
|
|
|
CREATE TABLE NetworkNew (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name VARCHAR(255),
|
|
|
|
user INTEGER NOT NULL,
|
|
|
|
addr VARCHAR(255) NOT NULL,
|
|
|
|
nick VARCHAR(255) NOT NULL,
|
|
|
|
username VARCHAR(255),
|
|
|
|
realname VARCHAR(255),
|
|
|
|
pass VARCHAR(255),
|
|
|
|
connect_commands VARCHAR(1023),
|
|
|
|
sasl_mechanism VARCHAR(255),
|
|
|
|
sasl_plain_username VARCHAR(255),
|
|
|
|
sasl_plain_password VARCHAR(255),
|
|
|
|
sasl_external_cert BLOB DEFAULT NULL,
|
|
|
|
sasl_external_key BLOB DEFAULT NULL,
|
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
|
|
|
UNIQUE(user, addr, nick),
|
|
|
|
UNIQUE(user, name)
|
|
|
|
);
|
|
|
|
INSERT INTO NetworkNew
|
|
|
|
SELECT Network.id, name, User.id as user, addr, nick,
|
|
|
|
Network.username, realname, pass, connect_commands,
|
|
|
|
sasl_mechanism, sasl_plain_username, sasl_plain_password,
|
|
|
|
sasl_external_cert, sasl_external_key
|
|
|
|
FROM Network
|
|
|
|
JOIN User ON Network.user = User.username;
|
|
|
|
DROP TABLE Network;
|
|
|
|
ALTER TABLE NetworkNew RENAME TO Network;
|
|
|
|
`,
|
|
|
|
`
|
|
|
|
ALTER TABLE Channel ADD COLUMN relay_detached INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN reattach_on INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN detach_after INTEGER NOT NULL DEFAULT 0;
|
|
|
|
ALTER TABLE Channel ADD COLUMN detach_on INTEGER NOT NULL DEFAULT 0;
|
|
|
|
`,
|
|
|
|
`
|
|
|
|
CREATE TABLE DeliveryReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target VARCHAR(255) NOT NULL,
|
|
|
|
client VARCHAR(255),
|
|
|
|
internal_msgid VARCHAR(255) NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target, client)
|
|
|
|
);
|
|
|
|
`,
|
|
|
|
"ALTER TABLE Channel ADD COLUMN detached_internal_msgid VARCHAR(255)",
|
2021-05-26 08:49:52 +00:00
|
|
|
"ALTER TABLE Network ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1",
|
2021-06-25 18:33:13 +00:00
|
|
|
"ALTER TABLE User ADD COLUMN realname VARCHAR(255)",
|
2021-11-02 22:33:17 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE NetworkNew (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name TEXT,
|
|
|
|
user INTEGER NOT NULL,
|
|
|
|
addr TEXT NOT NULL,
|
|
|
|
nick TEXT,
|
|
|
|
username TEXT,
|
|
|
|
realname TEXT,
|
|
|
|
pass TEXT,
|
|
|
|
connect_commands TEXT,
|
|
|
|
sasl_mechanism TEXT,
|
|
|
|
sasl_plain_username TEXT,
|
|
|
|
sasl_plain_password TEXT,
|
|
|
|
sasl_external_cert BLOB,
|
|
|
|
sasl_external_key BLOB,
|
|
|
|
enabled INTEGER NOT NULL DEFAULT 1,
|
|
|
|
FOREIGN KEY(user) REFERENCES User(id),
|
|
|
|
UNIQUE(user, addr, nick),
|
|
|
|
UNIQUE(user, name)
|
|
|
|
);
|
|
|
|
INSERT INTO NetworkNew
|
|
|
|
SELECT id, name, user, addr, nick, username, realname, pass,
|
|
|
|
connect_commands, sasl_mechanism, sasl_plain_username,
|
|
|
|
sasl_plain_password, sasl_external_cert, sasl_external_key,
|
|
|
|
enabled
|
|
|
|
FROM Network;
|
|
|
|
DROP TABLE Network;
|
|
|
|
ALTER TABLE NetworkNew RENAME TO Network;
|
|
|
|
`,
|
2021-01-29 15:57:38 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE ReadReceipt (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target TEXT NOT NULL,
|
|
|
|
timestamp TEXT NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target)
|
|
|
|
);
|
|
|
|
`,
|
2021-11-27 10:48:10 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE WebPushConfig (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
created_at TEXT NOT NULL,
|
|
|
|
vapid_key_public TEXT NOT NULL,
|
|
|
|
vapid_key_private TEXT NOT NULL,
|
|
|
|
UNIQUE(vapid_key_public)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE WebPushSubscription (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
created_at TEXT NOT NULL,
|
|
|
|
updated_at TEXT NOT NULL,
|
|
|
|
network INTEGER,
|
|
|
|
endpoint TEXT NOT NULL,
|
|
|
|
key_vapid TEXT,
|
|
|
|
key_auth TEXT,
|
|
|
|
key_p256dh TEXT,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, endpoint)
|
|
|
|
);
|
|
|
|
`,
|
2022-06-16 17:33:39 +00:00
|
|
|
`
|
|
|
|
ALTER TABLE WebPushSubscription ADD COLUMN user INTEGER REFERENCES User(id);
|
2022-06-16 17:46:59 +00:00
|
|
|
UPDATE WebPushSubscription AS wps SET user = (SELECT n.user FROM Network AS n WHERE n.id = wps.network);
|
2022-06-16 17:33:39 +00:00
|
|
|
`,
|
2022-07-08 16:01:05 +00:00
|
|
|
"ALTER TABLE User ADD COLUMN nick TEXT;",
|
2022-09-26 17:49:26 +00:00
|
|
|
"ALTER TABLE Network ADD COLUMN auto_away INTEGER NOT NULL DEFAULT 1;",
|
2022-12-10 08:12:46 +00:00
|
|
|
"ALTER TABLE Network ADD COLUMN certfp TEXT;",
|
2023-01-26 11:34:53 +00:00
|
|
|
// SQLite doesn't support non-constant default values, so use an empty
|
|
|
|
// string as default and update all columns in a separate statement
|
|
|
|
`
|
|
|
|
ALTER TABLE User ADD COLUMN created_at TEXT NOT NULL DEFAULT '';
|
|
|
|
UPDATE User SET created_at = strftime('` + sqliteTimeFormat + `', 'now');
|
|
|
|
`,
|
2023-01-26 17:33:55 +00:00
|
|
|
"ALTER TABLE User ADD COLUMN enabled INTEGER NOT NULL DEFAULT 1",
|
2023-01-26 13:02:11 +00:00
|
|
|
"ALTER TABLE User ADD COLUMN downstream_interacted_at TEXT;",
|
2022-12-10 23:01:16 +00:00
|
|
|
`
|
|
|
|
CREATE TABLE Message (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
target INTEGER NOT NULL,
|
|
|
|
raw TEXT NOT NULL,
|
|
|
|
time TEXT NOT NULL,
|
|
|
|
sender TEXT NOT NULL,
|
|
|
|
text TEXT,
|
|
|
|
FOREIGN KEY(target) REFERENCES MessageTarget(id)
|
|
|
|
);
|
|
|
|
CREATE INDEX MessageIndex ON Message(target, time);
|
|
|
|
|
|
|
|
CREATE TABLE MessageTarget (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
network INTEGER NOT NULL,
|
|
|
|
target TEXT NOT NULL,
|
|
|
|
FOREIGN KEY(network) REFERENCES Network(id),
|
|
|
|
UNIQUE(network, target)
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE VIRTUAL TABLE MessageFTS USING fts5 (
|
|
|
|
text,
|
|
|
|
content=Message,
|
|
|
|
content_rowid=id
|
|
|
|
);
|
|
|
|
CREATE TRIGGER MessageFTSInsert AFTER INSERT ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(rowid, text) VALUES (new.id, new.text);
|
|
|
|
END;
|
|
|
|
CREATE TRIGGER MessageFTSDelete AFTER DELETE ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(MessageFTS, rowid, text) VALUES ('delete', old.id, old.text);
|
|
|
|
END;
|
|
|
|
CREATE TRIGGER MessageFTSUpdate AFTER UPDATE ON Message BEGIN
|
|
|
|
INSERT INTO MessageFTS(MessageFTS, rowid, text) VALUES ('delete', old.id, old.text);
|
|
|
|
INSERT INTO MessageFTS(rowid, text) VALUES (new.id, new.text);
|
|
|
|
END;
|
|
|
|
`,
|
2021-05-24 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SqliteDB struct {
|
2022-05-03 21:17:56 +00:00
|
|
|
db *sql.DB
|
2021-05-24 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 17:15:56 +00:00
|
|
|
func OpenSqliteDB(source string) (Database, error) {
|
2022-05-03 21:17:56 +00:00
|
|
|
// Open the DB with cache=shared and SetMaxOpenConns(1) to allow usage from
|
|
|
|
// multiple goroutines
|
2022-09-26 17:21:06 +00:00
|
|
|
sqlSqliteDB, err := sql.Open(sqliteDriver, source+"?cache=shared")
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-03 21:17:56 +00:00
|
|
|
sqlSqliteDB.SetMaxOpenConns(1)
|
2021-05-24 19:13:31 +00:00
|
|
|
|
|
|
|
db := &SqliteDB{db: sqlSqliteDB}
|
|
|
|
if err := db.upgrade(); err != nil {
|
2021-09-13 14:59:41 +00:00
|
|
|
sqlSqliteDB.Close()
|
2021-05-24 19:13:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2022-05-09 10:34:43 +00:00
|
|
|
func OpenTempSqliteDB() (Database, error) {
|
|
|
|
// :memory: will open a separate database for each new connection. Make
|
|
|
|
// sure the sql package only uses a single connection via SetMaxOpenConns.
|
|
|
|
// An alternative solution is to use "file::memory:?cache=shared".
|
|
|
|
return OpenSqliteDB(":memory:")
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
func (db *SqliteDB) Close() error {
|
|
|
|
return db.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) upgrade() error {
|
|
|
|
var version int
|
|
|
|
if err := db.db.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
|
|
|
|
return fmt.Errorf("failed to query schema version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == len(sqliteMigrations) {
|
|
|
|
return nil
|
|
|
|
} else if version > len(sqliteMigrations) {
|
|
|
|
return fmt.Errorf("soju (version %d) older than schema (version %d)", len(sqliteMigrations), version)
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if version == 0 {
|
|
|
|
if _, err := tx.Exec(sqliteSchema); err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize schema: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := version; i < len(sqliteMigrations); i++ {
|
|
|
|
if _, err := tx.Exec(sqliteMigrations[i]); err != nil {
|
|
|
|
return fmt.Errorf("failed to execute migration #%v: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason prepared statements don't work here
|
|
|
|
_, err = tx.Exec(fmt.Sprintf("PRAGMA user_version = %d", len(sqliteMigrations)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to bump schema version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:36:59 +00:00
|
|
|
func (db *SqliteDB) RegisterMetrics(r prometheus.Registerer) error {
|
|
|
|
return r.Register(promcollectors.NewDBStatsCollector(db.db, "main"))
|
2021-11-17 14:40:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) Stats(ctx context.Context) (*DatabaseStats, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-10-05 17:31:06 +00:00
|
|
|
var stats DatabaseStats
|
2021-10-14 14:13:24 +00:00
|
|
|
row := db.db.QueryRowContext(ctx, `SELECT
|
2021-10-05 17:31:06 +00:00
|
|
|
(SELECT COUNT(*) FROM User) AS users,
|
|
|
|
(SELECT COUNT(*) FROM Network) AS networks,
|
|
|
|
(SELECT COUNT(*) FROM Channel) AS channels`)
|
|
|
|
if err := row.Scan(&stats.Users, &stats.Networks, &stats.Channels); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stats, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) ListUsers(ctx context.Context) ([]User, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx,
|
2023-01-26 13:02:11 +00:00
|
|
|
`SELECT id, username, password, admin, nick, realname, enabled,
|
|
|
|
downstream_interacted_at
|
2023-01-26 17:33:55 +00:00
|
|
|
FROM User`)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var users []User
|
|
|
|
for rows.Next() {
|
|
|
|
var user User
|
2022-07-08 16:01:05 +00:00
|
|
|
var password, nick, realname sql.NullString
|
2023-01-26 13:02:11 +00:00
|
|
|
var downstreamInteractedAt sqliteTime
|
|
|
|
if err := rows.Scan(&user.ID, &user.Username, &password, &user.Admin, &nick, &realname, &user.Enabled, &downstreamInteractedAt); err != nil {
|
2021-05-24 19:13:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user.Password = password.String
|
2022-07-08 16:01:05 +00:00
|
|
|
user.Nick = nick.String
|
2021-10-05 09:10:48 +00:00
|
|
|
user.Realname = realname.String
|
2023-01-26 13:02:11 +00:00
|
|
|
user.DownstreamInteractedAt = downstreamInteractedAt.Time
|
2021-05-24 19:13:31 +00:00
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) GetUser(ctx context.Context, username string) (*User, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
user := &User{Username: username}
|
|
|
|
|
2022-07-08 16:01:05 +00:00
|
|
|
var password, nick, realname sql.NullString
|
2023-01-26 13:02:11 +00:00
|
|
|
var downstreamInteractedAt sqliteTime
|
2021-10-14 14:13:24 +00:00
|
|
|
row := db.db.QueryRowContext(ctx,
|
2023-01-26 13:02:11 +00:00
|
|
|
`SELECT id, password, admin, nick, realname, enabled,
|
|
|
|
downstream_interacted_at
|
2023-01-26 17:33:55 +00:00
|
|
|
FROM User
|
|
|
|
WHERE username = ?`,
|
2021-10-14 14:13:24 +00:00
|
|
|
username)
|
2023-01-26 13:02:11 +00:00
|
|
|
if err := row.Scan(&user.ID, &password, &user.Admin, &nick, &realname, &user.Enabled, &downstreamInteractedAt); err != nil {
|
2021-05-24 19:13:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user.Password = password.String
|
2022-07-08 16:01:05 +00:00
|
|
|
user.Nick = nick.String
|
2021-06-25 18:33:13 +00:00
|
|
|
user.Realname = realname.String
|
2023-01-26 13:02:11 +00:00
|
|
|
user.DownstreamInteractedAt = downstreamInteractedAt.Time
|
2021-05-24 19:13:31 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2023-01-26 15:57:07 +00:00
|
|
|
func (db *SqliteDB) ListInactiveUsernames(ctx context.Context, limit time.Time) ([]string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx,
|
|
|
|
"SELECT username FROM User WHERE coalesce(downstream_interacted_at, created_at) < ?",
|
|
|
|
sqliteTime{limit})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var usernames []string
|
|
|
|
for rows.Next() {
|
|
|
|
var username string
|
|
|
|
if err := rows.Scan(&username); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
usernames = append(usernames, username)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return usernames, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) StoreUser(ctx context.Context, user *User) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-09-27 14:54:06 +00:00
|
|
|
args := []interface{}{
|
|
|
|
sql.Named("username", user.Username),
|
|
|
|
sql.Named("password", toNullString(user.Password)),
|
|
|
|
sql.Named("admin", user.Admin),
|
2022-07-08 16:01:05 +00:00
|
|
|
sql.Named("nick", toNullString(user.Nick)),
|
2021-09-27 14:54:06 +00:00
|
|
|
sql.Named("realname", toNullString(user.Realname)),
|
2023-01-26 17:33:55 +00:00
|
|
|
sql.Named("enabled", user.Enabled),
|
2023-01-26 11:34:53 +00:00
|
|
|
sql.Named("now", sqliteTime{time.Now()}),
|
2023-01-26 13:02:11 +00:00
|
|
|
sql.Named("downstream_interacted_at", sqliteTime{user.DownstreamInteractedAt}),
|
2021-09-27 14:54:06 +00:00
|
|
|
}
|
2021-05-24 19:13:31 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if user.ID != 0 {
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = db.db.ExecContext(ctx, `
|
2023-01-26 17:33:55 +00:00
|
|
|
UPDATE User
|
|
|
|
SET password = :password, admin = :admin, nick = :nick,
|
2023-01-26 13:02:11 +00:00
|
|
|
realname = :realname, enabled = :enabled,
|
|
|
|
downstream_interacted_at = :downstream_interacted_at
|
2023-01-26 17:33:55 +00:00
|
|
|
WHERE username = :username`,
|
2021-10-14 14:13:24 +00:00
|
|
|
args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
} else {
|
|
|
|
var res sql.Result
|
2021-10-14 14:13:24 +00:00
|
|
|
res, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO
|
2023-01-26 17:33:55 +00:00
|
|
|
User(username, password, admin, nick, realname, created_at,
|
2023-01-26 13:02:11 +00:00
|
|
|
enabled, downstream_interacted_at)
|
2023-01-26 17:33:55 +00:00
|
|
|
VALUES (:username, :password, :admin, :nick, :realname, :now,
|
2023-01-26 13:02:11 +00:00
|
|
|
:enabled, :downstream_interacted_at)`,
|
2021-10-14 14:13:24 +00:00
|
|
|
args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) DeleteUser(ctx context.Context, id int64) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, `DELETE FROM DeliveryReceipt
|
2021-09-13 16:24:01 +00:00
|
|
|
WHERE id IN (
|
|
|
|
SELECT DeliveryReceipt.id
|
|
|
|
FROM DeliveryReceipt
|
|
|
|
JOIN Network ON DeliveryReceipt.network = Network.id
|
|
|
|
WHERE Network.user = ?
|
|
|
|
)`, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:57:38 +00:00
|
|
|
_, err = tx.ExecContext(ctx, `DELETE FROM ReadReceipt
|
|
|
|
WHERE id IN (
|
|
|
|
SELECT ReadReceipt.id
|
|
|
|
FROM ReadReceipt
|
|
|
|
JOIN Network ON ReadReceipt.network = Network.id
|
|
|
|
WHERE Network.user = ?
|
|
|
|
)`, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, `DELETE FROM Channel
|
2021-05-24 19:13:31 +00:00
|
|
|
WHERE id IN (
|
|
|
|
SELECT Channel.id
|
|
|
|
FROM Channel
|
|
|
|
JOIN Network ON Channel.network = Network.id
|
|
|
|
WHERE Network.user = ?
|
|
|
|
)`, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM Network WHERE user = ?", id)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM User WHERE id = ?", id)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) ListNetworks(ctx context.Context, userID int64) ([]Network, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, `
|
2022-12-10 08:12:46 +00:00
|
|
|
SELECT id, name, addr, nick, username, realname, certfp, pass,
|
2021-05-24 19:13:31 +00:00
|
|
|
connect_commands, sasl_mechanism, sasl_plain_username, sasl_plain_password,
|
2022-09-26 17:49:26 +00:00
|
|
|
sasl_external_cert, sasl_external_key, auto_away, enabled
|
2021-05-24 19:13:31 +00:00
|
|
|
FROM Network
|
|
|
|
WHERE user = ?`,
|
|
|
|
userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var networks []Network
|
|
|
|
for rows.Next() {
|
|
|
|
var net Network
|
2022-12-10 08:12:46 +00:00
|
|
|
var name, nick, username, realname, certfp, pass, connectCommands sql.NullString
|
2021-05-24 19:13:31 +00:00
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
|
2022-12-10 08:12:46 +00:00
|
|
|
err := rows.Scan(&net.ID, &name, &net.Addr, &nick, &username, &realname, &certfp,
|
2021-05-24 19:13:31 +00:00
|
|
|
&pass, &connectCommands, &saslMechanism, &saslPlainUsername, &saslPlainPassword,
|
2022-09-26 17:49:26 +00:00
|
|
|
&net.SASL.External.CertBlob, &net.SASL.External.PrivKeyBlob, &net.AutoAway, &net.Enabled)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
net.Name = name.String
|
2021-11-02 22:33:17 +00:00
|
|
|
net.Nick = nick.String
|
2021-05-24 19:13:31 +00:00
|
|
|
net.Username = username.String
|
|
|
|
net.Realname = realname.String
|
2022-12-10 08:12:46 +00:00
|
|
|
net.CertFP = certfp.String
|
2021-05-24 19:13:31 +00:00
|
|
|
net.Pass = pass.String
|
|
|
|
if connectCommands.Valid {
|
|
|
|
net.ConnectCommands = strings.Split(connectCommands.String, "\r\n")
|
|
|
|
}
|
|
|
|
net.SASL.Mechanism = saslMechanism.String
|
|
|
|
net.SASL.Plain.Username = saslPlainUsername.String
|
|
|
|
net.SASL.Plain.Password = saslPlainPassword.String
|
|
|
|
networks = append(networks, net)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) StoreNetwork(ctx context.Context, userID int64, network *Network) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
var saslMechanism, saslPlainUsername, saslPlainPassword sql.NullString
|
|
|
|
if network.SASL.Mechanism != "" {
|
|
|
|
saslMechanism = toNullString(network.SASL.Mechanism)
|
|
|
|
switch network.SASL.Mechanism {
|
|
|
|
case "PLAIN":
|
|
|
|
saslPlainUsername = toNullString(network.SASL.Plain.Username)
|
|
|
|
saslPlainPassword = toNullString(network.SASL.Plain.Password)
|
|
|
|
network.SASL.External.CertBlob = nil
|
|
|
|
network.SASL.External.PrivKeyBlob = nil
|
|
|
|
case "EXTERNAL":
|
|
|
|
// keep saslPlain* nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("soju: cannot store network: unsupported SASL mechanism %q", network.SASL.Mechanism)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:54:06 +00:00
|
|
|
args := []interface{}{
|
|
|
|
sql.Named("name", toNullString(network.Name)),
|
|
|
|
sql.Named("addr", network.Addr),
|
2021-11-02 22:33:17 +00:00
|
|
|
sql.Named("nick", toNullString(network.Nick)),
|
2021-09-27 14:54:06 +00:00
|
|
|
sql.Named("username", toNullString(network.Username)),
|
|
|
|
sql.Named("realname", toNullString(network.Realname)),
|
2022-12-10 08:12:46 +00:00
|
|
|
sql.Named("certfp", toNullString(network.CertFP)),
|
2021-09-27 14:54:06 +00:00
|
|
|
sql.Named("pass", toNullString(network.Pass)),
|
|
|
|
sql.Named("connect_commands", toNullString(strings.Join(network.ConnectCommands, "\r\n"))),
|
|
|
|
sql.Named("sasl_mechanism", saslMechanism),
|
|
|
|
sql.Named("sasl_plain_username", saslPlainUsername),
|
|
|
|
sql.Named("sasl_plain_password", saslPlainPassword),
|
|
|
|
sql.Named("sasl_external_cert", network.SASL.External.CertBlob),
|
|
|
|
sql.Named("sasl_external_key", network.SASL.External.PrivKeyBlob),
|
2022-09-26 17:49:26 +00:00
|
|
|
sql.Named("auto_away", network.AutoAway),
|
2021-09-27 14:54:06 +00:00
|
|
|
sql.Named("enabled", network.Enabled),
|
|
|
|
|
|
|
|
sql.Named("id", network.ID), // only for UPDATE
|
|
|
|
sql.Named("user", userID), // only for INSERT
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
var err error
|
|
|
|
if network.ID != 0 {
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = db.db.ExecContext(ctx, `
|
2021-09-27 14:54:06 +00:00
|
|
|
UPDATE Network
|
|
|
|
SET name = :name, addr = :addr, nick = :nick, username = :username,
|
2022-12-10 08:12:46 +00:00
|
|
|
realname = :realname, certfp = :certfp, pass = :pass, connect_commands = :connect_commands,
|
2021-09-27 14:54:06 +00:00
|
|
|
sasl_mechanism = :sasl_mechanism, sasl_plain_username = :sasl_plain_username, sasl_plain_password = :sasl_plain_password,
|
|
|
|
sasl_external_cert = :sasl_external_cert, sasl_external_key = :sasl_external_key,
|
2022-09-26 17:49:26 +00:00
|
|
|
auto_away = :auto_away, enabled = :enabled
|
2021-09-27 14:54:06 +00:00
|
|
|
WHERE id = :id`, args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
} else {
|
|
|
|
var res sql.Result
|
2021-10-14 14:13:24 +00:00
|
|
|
res, err = db.db.ExecContext(ctx, `
|
2022-12-10 08:12:46 +00:00
|
|
|
INSERT INTO Network(user, name, addr, nick, username, realname, certfp, pass,
|
2021-09-27 14:54:06 +00:00
|
|
|
connect_commands, sasl_mechanism, sasl_plain_username,
|
2022-09-26 17:49:26 +00:00
|
|
|
sasl_plain_password, sasl_external_cert, sasl_external_key, auto_away, enabled)
|
2022-12-10 08:12:46 +00:00
|
|
|
VALUES (:user, :name, :addr, :nick, :username, :realname, :certfp, :pass,
|
2021-09-27 14:54:06 +00:00
|
|
|
:connect_commands, :sasl_mechanism, :sasl_plain_username,
|
2022-09-26 17:49:26 +00:00
|
|
|
:sasl_plain_password, :sasl_external_cert, :sasl_external_key, :auto_away, :enabled)`,
|
2021-09-27 14:54:06 +00:00
|
|
|
args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
network.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) DeleteNetwork(ctx context.Context, id int64) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2022-12-10 23:01:16 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM Message WHERE target IN (SELECT id FROM MessageTarget WHERE network = ?)", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM MessageTarget WHERE network = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-27 10:48:10 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM WebPushSubscription WHERE network = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM DeliveryReceipt WHERE network = ?", id)
|
2021-09-13 16:24:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-29 15:57:38 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM ReadReceipt WHERE network = ?", id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM Channel WHERE network = ?", id)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM Network WHERE id = ?", id)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) ListChannels(ctx context.Context, networkID int64) ([]Channel, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, `SELECT
|
2021-05-24 19:13:31 +00:00
|
|
|
id, name, key, detached, detached_internal_msgid,
|
|
|
|
relay_detached, reattach_on, detach_after, detach_on
|
|
|
|
FROM Channel
|
|
|
|
WHERE network = ?`, networkID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var channels []Channel
|
|
|
|
for rows.Next() {
|
|
|
|
var ch Channel
|
|
|
|
var key, detachedInternalMsgID sql.NullString
|
|
|
|
var detachAfter int64
|
|
|
|
if err := rows.Scan(&ch.ID, &ch.Name, &key, &ch.Detached, &detachedInternalMsgID, &ch.RelayDetached, &ch.ReattachOn, &detachAfter, &ch.DetachOn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ch.Key = key.String
|
|
|
|
ch.DetachedInternalMsgID = detachedInternalMsgID.String
|
|
|
|
ch.DetachAfter = time.Duration(detachAfter) * time.Second
|
|
|
|
channels = append(channels, ch)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return channels, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) StoreChannel(ctx context.Context, networkID int64, ch *Channel) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-09-27 14:54:06 +00:00
|
|
|
args := []interface{}{
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("name", ch.Name),
|
|
|
|
sql.Named("key", toNullString(ch.Key)),
|
|
|
|
sql.Named("detached", ch.Detached),
|
|
|
|
sql.Named("detached_internal_msgid", toNullString(ch.DetachedInternalMsgID)),
|
|
|
|
sql.Named("relay_detached", ch.RelayDetached),
|
|
|
|
sql.Named("reattach_on", ch.ReattachOn),
|
|
|
|
sql.Named("detach_after", int64(math.Ceil(ch.DetachAfter.Seconds()))),
|
|
|
|
sql.Named("detach_on", ch.DetachOn),
|
|
|
|
|
|
|
|
sql.Named("id", ch.ID), // only for UPDATE
|
|
|
|
}
|
2021-05-24 19:13:31 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if ch.ID != 0 {
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = db.db.ExecContext(ctx, `UPDATE Channel
|
2021-09-27 14:54:06 +00:00
|
|
|
SET network = :network, name = :name, key = :key, detached = :detached,
|
|
|
|
detached_internal_msgid = :detached_internal_msgid, relay_detached = :relay_detached,
|
|
|
|
reattach_on = :reattach_on, detach_after = :detach_after, detach_on = :detach_on
|
|
|
|
WHERE id = :id`, args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
} else {
|
|
|
|
var res sql.Result
|
2021-10-14 14:13:24 +00:00
|
|
|
res, err = db.db.ExecContext(ctx, `INSERT INTO Channel(network, name, key, detached, detached_internal_msgid, relay_detached, reattach_on, detach_after, detach_on)
|
2021-09-27 14:54:06 +00:00
|
|
|
VALUES (:network, :name, :key, :detached, :detached_internal_msgid, :relay_detached, :reattach_on, :detach_after, :detach_on)`, args...)
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ch.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) DeleteChannel(ctx context.Context, id int64) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := db.db.ExecContext(ctx, "DELETE FROM Channel WHERE id = ?", id)
|
2021-05-24 19:13:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) ListDeliveryReceipts(ctx context.Context, networkID int64) ([]DeliveryReceipt, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, `
|
|
|
|
SELECT id, target, client, internal_msgid
|
2021-05-24 19:13:31 +00:00
|
|
|
FROM DeliveryReceipt
|
|
|
|
WHERE network = ?`, networkID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var receipts []DeliveryReceipt
|
|
|
|
for rows.Next() {
|
|
|
|
var rcpt DeliveryReceipt
|
|
|
|
var client sql.NullString
|
|
|
|
if err := rows.Scan(&rcpt.ID, &rcpt.Target, &client, &rcpt.InternalMsgID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rcpt.Client = client.String
|
|
|
|
receipts = append(receipts, rcpt)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return receipts, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 17:15:15 +00:00
|
|
|
func (db *SqliteDB) StoreClientDeliveryReceipts(ctx context.Context, networkID int64, client string, receipts []DeliveryReceipt) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
2021-10-14 14:13:24 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2021-05-24 19:13:31 +00:00
|
|
|
tx, err := db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
_, err = tx.ExecContext(ctx, "DELETE FROM DeliveryReceipt WHERE network = ? AND client IS ?",
|
2021-05-24 19:13:31 +00:00
|
|
|
networkID, toNullString(client))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range receipts {
|
|
|
|
rcpt := &receipts[i]
|
|
|
|
|
2021-10-14 14:13:24 +00:00
|
|
|
res, err := tx.ExecContext(ctx, `
|
|
|
|
INSERT INTO DeliveryReceipt(network, target, client, internal_msgid)
|
2021-09-27 14:54:06 +00:00
|
|
|
VALUES (:network, :target, :client, :internal_msgid)`,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", rcpt.Target),
|
|
|
|
sql.Named("client", toNullString(client)),
|
|
|
|
sql.Named("internal_msgid", rcpt.InternalMsgID))
|
2021-05-24 19:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcpt.ID, err = res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
2021-01-29 15:57:38 +00:00
|
|
|
|
|
|
|
func (db *SqliteDB) GetReadReceipt(ctx context.Context, networkID int64, name string) (*ReadReceipt, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
receipt := &ReadReceipt{
|
|
|
|
Target: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
row := db.db.QueryRowContext(ctx, `
|
|
|
|
SELECT id, timestamp FROM ReadReceipt WHERE network = :network AND target = :target`,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", name),
|
|
|
|
)
|
2023-01-26 13:11:07 +00:00
|
|
|
var timestamp sqliteTime
|
2021-01-29 15:57:38 +00:00
|
|
|
if err := row.Scan(&receipt.ID, ×tamp); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-26 13:11:07 +00:00
|
|
|
receipt.Timestamp = timestamp.Time
|
2021-01-29 15:57:38 +00:00
|
|
|
return receipt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) StoreReadReceipt(ctx context.Context, networkID int64, receipt *ReadReceipt) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
args := []interface{}{
|
|
|
|
sql.Named("id", receipt.ID),
|
2023-01-26 13:11:07 +00:00
|
|
|
sql.Named("timestamp", sqliteTime{receipt.Timestamp}),
|
2021-01-29 15:57:38 +00:00
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", receipt.Target),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if receipt.ID != 0 {
|
|
|
|
_, err = db.db.ExecContext(ctx, `
|
|
|
|
UPDATE ReadReceipt SET timestamp = :timestamp WHERE id = :id`,
|
|
|
|
args...)
|
|
|
|
} else {
|
|
|
|
var res sql.Result
|
|
|
|
res, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO
|
|
|
|
ReadReceipt(network, target, timestamp)
|
|
|
|
VALUES (:network, :target, :timestamp)`,
|
|
|
|
args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
receipt.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-11-27 10:48:10 +00:00
|
|
|
|
|
|
|
func (db *SqliteDB) ListWebPushConfigs(ctx context.Context) ([]WebPushConfig, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, `
|
|
|
|
SELECT id, vapid_key_public, vapid_key_private
|
|
|
|
FROM WebPushConfig`)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var configs []WebPushConfig
|
|
|
|
for rows.Next() {
|
|
|
|
var config WebPushConfig
|
|
|
|
if err := rows.Scan(&config.ID, &config.VAPIDKeys.Public, &config.VAPIDKeys.Private); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
configs = append(configs, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configs, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) StoreWebPushConfig(ctx context.Context, config *WebPushConfig) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if config.ID != 0 {
|
|
|
|
return fmt.Errorf("cannot update a WebPushConfig")
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO WebPushConfig(created_at, vapid_key_public, vapid_key_private)
|
|
|
|
VALUES (:now, :vapid_key_public, :vapid_key_private)`,
|
|
|
|
sql.Named("vapid_key_public", config.VAPIDKeys.Public),
|
|
|
|
sql.Named("vapid_key_private", config.VAPIDKeys.Private),
|
2023-01-26 13:11:07 +00:00
|
|
|
sql.Named("now", sqliteTime{time.Now()}))
|
2021-11-27 10:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.ID, err = res.LastInsertId()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:33:39 +00:00
|
|
|
func (db *SqliteDB) ListWebPushSubscriptions(ctx context.Context, userID, networkID int64) ([]WebPushSubscription, error) {
|
2021-11-27 10:48:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
nullNetworkID := sql.NullInt64{
|
|
|
|
Int64: networkID,
|
|
|
|
Valid: networkID != 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, `
|
2023-02-18 12:23:02 +00:00
|
|
|
SELECT id, endpoint, created_at, updated_at, key_auth, key_p256dh, key_vapid
|
2021-11-27 10:48:10 +00:00
|
|
|
FROM WebPushSubscription
|
2022-06-16 17:33:39 +00:00
|
|
|
WHERE user = ? AND network IS ?`, userID, nullNetworkID)
|
2021-11-27 10:48:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var subs []WebPushSubscription
|
|
|
|
for rows.Next() {
|
|
|
|
var sub WebPushSubscription
|
2023-02-18 12:23:02 +00:00
|
|
|
var createdAt, updatedAt sqliteTime
|
|
|
|
if err := rows.Scan(&sub.ID, &sub.Endpoint, &createdAt, &updatedAt, &sub.Keys.Auth, &sub.Keys.P256DH, &sub.Keys.VAPID); err != nil {
|
2021-11-27 10:48:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-18 12:23:02 +00:00
|
|
|
sub.CreatedAt = createdAt.Time
|
|
|
|
sub.UpdatedAt = updatedAt.Time
|
2021-11-27 10:48:10 +00:00
|
|
|
subs = append(subs, sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subs, rows.Err()
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:33:39 +00:00
|
|
|
func (db *SqliteDB) StoreWebPushSubscription(ctx context.Context, userID, networkID int64, sub *WebPushSubscription) error {
|
2021-11-27 10:48:10 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
args := []interface{}{
|
|
|
|
sql.Named("id", sub.ID),
|
2022-06-16 17:33:39 +00:00
|
|
|
sql.Named("user", userID),
|
2021-11-27 10:48:10 +00:00
|
|
|
sql.Named("network", sql.NullInt64{
|
|
|
|
Int64: networkID,
|
|
|
|
Valid: networkID != 0,
|
|
|
|
}),
|
2023-01-26 13:11:07 +00:00
|
|
|
sql.Named("now", sqliteTime{time.Now()}),
|
2021-11-27 10:48:10 +00:00
|
|
|
sql.Named("endpoint", sub.Endpoint),
|
|
|
|
sql.Named("key_auth", sub.Keys.Auth),
|
|
|
|
sql.Named("key_p256dh", sub.Keys.P256DH),
|
|
|
|
sql.Named("key_vapid", sub.Keys.VAPID),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if sub.ID != 0 {
|
|
|
|
_, err = db.db.ExecContext(ctx, `
|
|
|
|
UPDATE WebPushSubscription
|
|
|
|
SET updated_at = :now, key_auth = :key_auth, key_p256dh = :key_p256dh,
|
|
|
|
key_vapid = :key_vapid
|
|
|
|
WHERE id = :id`,
|
|
|
|
args...)
|
|
|
|
} else {
|
|
|
|
var res sql.Result
|
|
|
|
res, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO
|
2022-06-16 17:33:39 +00:00
|
|
|
WebPushSubscription(created_at, updated_at, user, network, endpoint,
|
2021-11-27 10:48:10 +00:00
|
|
|
key_auth, key_p256dh, key_vapid)
|
2022-06-16 17:33:39 +00:00
|
|
|
VALUES (:now, :now, :user, :network, :endpoint, :key_auth,
|
|
|
|
:key_p256dh, :key_vapid)`,
|
2021-11-27 10:48:10 +00:00
|
|
|
args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sub.ID, err = res.LastInsertId()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) DeleteWebPushSubscription(ctx context.Context, id int64) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := db.db.ExecContext(ctx, "DELETE FROM WebPushSubscription WHERE id = ?", id)
|
|
|
|
return err
|
|
|
|
}
|
2022-12-10 23:01:16 +00:00
|
|
|
|
|
|
|
func (db *SqliteDB) GetMessageLastID(ctx context.Context, networkID int64, name string) (int64, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var msgID int64
|
|
|
|
row := db.db.QueryRowContext(ctx, `
|
|
|
|
SELECT m.id FROM Message AS m, MessageTarget AS t
|
|
|
|
WHERE t.network = :network AND t.target = :target AND m.target = t.id
|
|
|
|
ORDER BY m.time DESC LIMIT 1`,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", name),
|
|
|
|
)
|
|
|
|
if err := row.Scan(&msgID); err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return msgID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) StoreMessage(ctx context.Context, networkID int64, name string, msg *irc.Message) (int64, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var t time.Time
|
|
|
|
if tag, ok := msg.Tags["time"]; ok {
|
|
|
|
var err error
|
|
|
|
t, err = time.Parse(xirc.ServerTimeLayout, tag)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to parse message time tag: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
var text sql.NullString
|
|
|
|
switch msg.Command {
|
|
|
|
case "PRIVMSG", "NOTICE":
|
|
|
|
if len(msg.Params) > 1 {
|
|
|
|
text.Valid = true
|
|
|
|
text.String = msg.Params[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO MessageTarget(network, target)
|
|
|
|
VALUES (:network, :target)
|
|
|
|
ON CONFLICT DO NOTHING`,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", name),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO Message(target, raw, time, sender, text)
|
|
|
|
SELECT id, :raw, :time, :sender, :text
|
|
|
|
FROM MessageTarget as t
|
|
|
|
WHERE network = :network AND target = :target`,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", name),
|
|
|
|
sql.Named("raw", msg.String()),
|
|
|
|
sql.Named("time", sqliteTime{t}),
|
|
|
|
sql.Named("sender", msg.Name),
|
|
|
|
sql.Named("text", text),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) ListMessageLastPerTarget(ctx context.Context, networkID int64, options *MessageOptions) ([]MessageTarget, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
innerQuery := `
|
|
|
|
SELECT time
|
|
|
|
FROM Message
|
|
|
|
WHERE target = MessageTarget.id `
|
|
|
|
if !options.Events {
|
|
|
|
innerQuery += `AND text IS NOT NULL `
|
|
|
|
}
|
|
|
|
innerQuery += `
|
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT target, (` + innerQuery + `) latest
|
|
|
|
FROM MessageTarget
|
|
|
|
WHERE network = :network `
|
|
|
|
if !options.AfterTime.IsZero() {
|
|
|
|
// compares time strings by lexicographical order
|
|
|
|
query += `AND latest > :after `
|
|
|
|
}
|
|
|
|
if !options.BeforeTime.IsZero() {
|
|
|
|
// compares time strings by lexicographical order
|
|
|
|
query += `AND latest < :before `
|
|
|
|
}
|
|
|
|
if options.TakeLast {
|
|
|
|
query += `ORDER BY latest DESC `
|
|
|
|
} else {
|
|
|
|
query += `ORDER BY latest ASC `
|
|
|
|
}
|
|
|
|
query += `LIMIT :limit`
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, query,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("after", sqliteTime{options.AfterTime}),
|
|
|
|
sql.Named("before", sqliteTime{options.BeforeTime}),
|
|
|
|
sql.Named("limit", options.Limit),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var l []MessageTarget
|
|
|
|
for rows.Next() {
|
|
|
|
var mt MessageTarget
|
|
|
|
var ts sqliteTime
|
|
|
|
if err := rows.Scan(&mt.Name, &ts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mt.LatestMessage = ts.Time
|
|
|
|
l = append(l, mt)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.TakeLast {
|
|
|
|
// We ordered by DESC to limit to the last lines.
|
|
|
|
// Reverse the list to order by ASC these last lines.
|
|
|
|
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
l[i], l[j] = l[j], l[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *SqliteDB) ListMessages(ctx context.Context, networkID int64, name string, options *MessageOptions) ([]*irc.Message, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, sqliteQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT m.raw
|
|
|
|
FROM Message AS m, MessageTarget AS t
|
|
|
|
WHERE m.target = t.id AND t.network = :network AND t.target = :target `
|
|
|
|
if options.AfterID > 0 {
|
|
|
|
query += `AND m.id > :afterID `
|
|
|
|
}
|
|
|
|
if !options.AfterTime.IsZero() {
|
|
|
|
// compares time strings by lexicographical order
|
|
|
|
query += `AND m.time > :after `
|
|
|
|
}
|
|
|
|
if !options.BeforeTime.IsZero() {
|
|
|
|
// compares time strings by lexicographical order
|
|
|
|
query += `AND m.time < :before `
|
|
|
|
}
|
|
|
|
if options.Sender != "" {
|
|
|
|
query += `AND m.sender = :sender `
|
|
|
|
}
|
|
|
|
if options.Text != "" {
|
|
|
|
query += `AND m.id IN (SELECT ROWID FROM MessageFTS WHERE MessageFTS MATCH :text) `
|
|
|
|
}
|
|
|
|
if !options.Events {
|
|
|
|
query += `AND m.text IS NOT NULL `
|
|
|
|
}
|
|
|
|
if options.TakeLast {
|
|
|
|
query += `ORDER BY m.time DESC `
|
|
|
|
} else {
|
|
|
|
query += `ORDER BY m.time ASC `
|
|
|
|
}
|
|
|
|
query += `LIMIT :limit`
|
|
|
|
|
|
|
|
rows, err := db.db.QueryContext(ctx, query,
|
|
|
|
sql.Named("network", networkID),
|
|
|
|
sql.Named("target", name),
|
|
|
|
sql.Named("afterID", options.AfterID),
|
|
|
|
sql.Named("after", sqliteTime{options.AfterTime}),
|
|
|
|
sql.Named("before", sqliteTime{options.BeforeTime}),
|
|
|
|
sql.Named("sender", options.Sender),
|
|
|
|
sql.Named("text", options.Text),
|
|
|
|
sql.Named("limit", options.Limit),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var l []*irc.Message
|
|
|
|
for rows.Next() {
|
|
|
|
var raw string
|
|
|
|
if err := rows.Scan(&raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := irc.ParseMessage(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l = append(l, msg)
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.TakeLast {
|
|
|
|
// We ordered by DESC to limit to the last lines.
|
|
|
|
// Reverse the list to order by ASC these last lines.
|
|
|
|
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
l[i], l[j] = l[j], l[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|