Commit Graph

37 Commits

Author SHA1 Message Date
Simon Ser 2216dd91a0 database: move schema into separate file 2023-12-06 11:39:46 +01:00
sentriz 5ae86d69cc database: use postgresQueryTimeout for PostgresDB StoreMessages
using the sqliteQueryTimeout means a `undefined: sqliteQueryTimeout`
when building with `-tags=nosqlite`
2023-11-06 15:20:42 +01:00
Simon Ser 5bd38100c6 database: drop unnecessary comment
The error message is clear enough already.
2023-11-01 00:05:18 +01:00
Simon Ser 4f7b577734 database: add NewUser 2023-11-01 00:04:57 +01:00
Simon Ser 38e32ddd6c database: fix default value for Network.AutoAway 2023-10-31 23:51:32 +01:00
Simon Ser e19dd8f0da database: add NewNetwork 2023-10-31 23:51:04 +01:00
Calvin Lee d137c69131 database: batch msg inserts
This commit takes insert query compilation and transaction creation out
of the critical loop for migrating message logs. I have tested with
the sqlite backend, and a speedup of approximately 40x has been achieved
for log migration.
2023-10-25 16:03:00 +02:00
delthas 0ee94759f7 Fix SEARCH queries with special characters on SQLite
Previously, we would pass SQLite FTS5 MATCH queries as is from the
SEARCH parameter, when searching for messages.

By default FTS5 queries follow a specific syntax, e.g. AND, OR, ...
can be used to filter searches logically.

Passing special characters in these unescaped queries, like `.`,
would be rejected by SQLite, failing the SEARCH request.

Here, in order to replicate PostgreSQL and FS behavior, we simply
want to search for the string as is, and not give it glob or logic
operators semantics.

To do that, we quote each token of the string, skipping over
separators.
2023-08-02 15:19:38 +02:00
Simon Ser 0548fdfa09 database/sqlite: fix missing argument in DeleteUser 2023-04-14 17:37:53 +02:00
Simon Ser d2067f74d8 database/sqlite: delete push subscriptions and msgstore in DeleteUser 2023-03-17 10:23:37 +01:00
Simon Ser 5a1cbc425f Remove legacy Go build comments 2023-02-23 10:33:48 +01:00
sentriz 5a26674614 database/postgres: fix StoreNetwork positional arguments 2023-02-18 15:42:00 +01:00
Simon Ser 8f1f67f1f0 Rate limit Web Push checks
No need to re-check that a Web Push subscription is valid every
time a downstream connects. Mobile devices may reconnect pretty
frequently.

Check at most once a day.
2023-02-18 13:27:13 +01:00
delthas e510f7a461 Add support for explicit PostgreSQL schema prefixes for tests
PostgreSQL tests use pg_temp only. pg_temp is never searched for FTS
objects, so creating then altering an FTS configuration will not work
because PostgreSQL will not be able to find the FTS configuration it
just created.

Instead, we explicitly refer to the FTS objects with their full name
including their prefix, which makes PostgreSQL able to find the object.

This is only needed for tests.

See: https://stackoverflow.com/a/31095452/2347617
See: https://www.postgresql.org/message-id/15191.1208975632@sss.pgh.pa.us
2023-02-17 14:13:43 +01:00
delthas 1ccc7ce6d2 Add a database store for messages
This adds a new config option, `logs db`, which enables storing chat
logs in the soju database.

Regular store options, CHATHISTORY options, and SEARCH operations are
supported, like the fs logs backend.

Messages are stored in a new table, Message. In order to track the list
of targets we have messages for in an optimized manner, another database
is used: MessageTarget.

All new requests are backend by indexes so should be fast even with
hundreds of thousands of messages.

A contrib script is provided for migrating existing logs fs chat logs to
the database. It can be run with eg:

  go run ./contrib/migrate-logs/ logs/ sqlite3:soju.db

Co-authored-by: Simon Ser <contact@emersion.fr>
2023-02-17 14:13:43 +01:00
Simon Ser 251af0d9d3 database/postgres: fix nil time errors
We need to use sql.NullTime, otherwise we get errors like these:

    sql: Scan error on column index 7, name "downstream_interacted_at": unsupported Scan, storing driver.Value type <nil> into type *time.Time
2023-02-13 19:22:15 +01:00
Simon Ser 9df9880301 Add disable-inactive-user config option
This can be used to automatically disable users if they don't
actively use the bouncer for a while.
2023-01-26 19:42:29 +01:00
Simon Ser 57f5ee8d6f Add user downstream interaction time 2023-01-26 19:42:29 +01:00
Simon Ser d7d9d45b45 Add a flag to disable users
Add a new flag to disable users. This can be useful to temporarily
deactivate an account without erasing data.

The user goroutine is kept alive for simplicity's sake. Most of the
infrastructure assumes that each user always has a running goroutine.
A disabled user's goroutine is responsible for sending back an error
to downstream connections, and listening for potential events to
re-enable the account.
2023-01-26 18:33:55 +01:00
Simon Ser 5a9dced249 database: store user creation date
This is useful to figure out which users were created recently.
2023-01-26 17:12:58 +01:00
Simon Ser 2abe231eef database/sqlite: introduce sqliteTime type
This implements sql.Scanner and sql/driver.Valuer, so that we can
load/store time values into SQLite with the format we want, and
properly handle NULL (which the go-sqlite3 package doesn't do
correctly).
2023-01-26 14:38:28 +01:00
rj1 be185fba33 Implement TLS fingerprint pinning
Closes: https://todo.sr.ht/~emersion/soju/56
2022-12-16 17:44:14 +01:00
zsrv 926dcb37ac Make the auto-away functionality configurable 2022-09-26 19:52:07 +02:00
Łukasz Margiela d605d64d1d Add build tag for modernc/sqlite driver 2022-09-26 19:31:43 +02:00
Simon Ser a9949c2e95 database/sqlite: make optional
SQLite requires cgo, for some use cases this is undesirable.
2022-09-11 13:57:00 +02:00
Simon Ser ce19f76673 database/postgres: fix inverted networkID check in ListWebPushSubscriptions
Same as 02844ca9df ("database/postgres: fix inverted networkID
check in StoreWebPushSubscription").
2022-07-27 09:48:52 +02:00
Simon Ser 02844ca9df database/postgres: fix inverted networkID check in StoreWebPushSubscription
Presented without comment.
2022-07-25 19:32:09 +02:00
Simon Ser 8849669d61 database/postgres: set max open conns
Open at most 25 connections. Should fix errors such as
"pq: sorry, too many clients already".
2022-07-25 19:00:14 +02:00
Simon Ser d0868722cd database/postgres: make WebPushSubscription.user NOT NULL
Also see 6734464b72 ("database/postgres: fix inverted network and
user in StoreWebPushSubscription").
2022-07-25 18:55:22 +02:00
Simon Ser 6734464b72 database/postgres: fix inverted network and user in StoreWebPushSubscription 2022-07-25 18:49:21 +02:00
gildarts 48960a2992 database: upgrade bcrypt cost as needed
Closes: https://todo.sr.ht/~emersion/soju/136
2022-07-12 18:52:49 +02:00
Simon Ser dc0a847240 Add per-user default nickname
The soju username is immutable. Add a separate nickname setting so
that users can change their nickname for all networks.

References: https://todo.sr.ht/~emersion/soju/110
2022-07-08 18:01:05 +02:00
Simon Ser 27b8547572 database/sqlite: add migration for WebPushSubscription.user
This should be enough to fixup the user column for subscriptions
with a network specified.
2022-06-16 19:46:59 +02:00
Simon Ser f0db261fc0 database: add missing user column to WebPushSubscription table
Some WebPushSubscription entries aren't tried to a network, in
which case the "network" column is NULL. But then all users share
the same row. Oops.

Fortunately network-less subscriptions aren't used for anything
yet, they're just stored. So the impact should be minimal.
2022-06-16 19:33:39 +02:00
Simon Ser 3863b8cb6b Add webpush extension
References: https://github.com/ircv3/ircv3-specifications/pull/471
Co-authored-by: delthas <delthas@dille.cc>
2022-06-14 16:16:12 +02:00
Simon Ser fe40c51ff0 database: add User.{Check,Set}Password 2022-06-08 13:27:33 +02:00
Simon Ser 3a7dee8128 Introduce a database package 2022-05-09 15:08:04 +02:00