Skip to content

Commit 9e87833

Browse files
authored
Use matrix-appservice-bridge's PostgreSQL datastore for Draupnir appservice (#46)
* use matrix-appservice-bridge support for postgres * upgrade matrix-appservice-bridge to include postgres fixes. These are unreleased and a specific develop commit, but we're the only ones who have made changes so far to the develop branch, so should be fine. * Upgrade matrix-appservice-bridge to 8.1.1. 8.0.1 was never added to npm for some weird reason.
1 parent d8ef8a9 commit 9e87833

File tree

7 files changed

+88
-63
lines changed

7 files changed

+88
-63
lines changed

mx-tester.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ up:
55
- docker run --rm --network $MX_TEST_NETWORK_NAME --name mjolnir-test-postgres --domainname mjolnir-test-postgres -e POSTGRES_PASSWORD=mjolnir-test -e POSTGRES_USER=mjolnir-tester -e POSTGRES_DB=mjolnir-test-db -d -p 127.0.0.1:8083:5432 postgres
66
# Wait until postgresql is ready
77
- until psql postgres://mjolnir-tester:mjolnir-test@localhost:8083/mjolnir-test-db -c ""; do echo "Waiting for psql..."; sleep 1s; done
8-
# Make table in postgres
9-
- psql postgres://mjolnir-tester:mjolnir-test@localhost:8083/mjolnir-test-db -c "CREATE TABLE mjolnir (local_part VARCHAR(255), owner VARCHAR(255), management_room TEXT)"
108
# Launch the reverse proxy, listening for connections *only* on the local host.
119
- docker run --rm --network host --name mjolnir-test-reverse-proxy -p 127.0.0.1:8081:80 -v $MX_TEST_CWD/test/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
1210
- yarn install

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"humanize-duration-ts": "^2.1.1",
5858
"js-yaml": "^4.1.0",
5959
"jsdom": "^16.6.0",
60-
"matrix-appservice-bridge": "8.0.0",
60+
"matrix-appservice-bridge": "8.1.1",
6161
"parse-duration": "^1.0.2",
6262
"pg": "^8.8.0",
6363
"shell-quote": "^1.7.3",

src/appservice/AppService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ limitations under the License.
2727

2828
import { AppServiceRegistration, Bridge, Request, WeakEvent, BridgeContext, MatrixUser, Logger } from "matrix-appservice-bridge";
2929
import { MjolnirManager } from ".//MjolnirManager";
30-
import { DataStore, PgDataStore } from ".//datastore";
30+
import { DataStore } from ".//datastore";
31+
import { PgDataStore } from "./postgres/PgDataStore";
3132
import { Api } from "./Api";
3233
import { IConfig } from "./config/config";
3334
import { AccessControl } from "./AccessControl";

src/appservice/datastore.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ limitations under the License.
2424
* However, this file is modified and the modifications in this file
2525
* are NOT distributed, contributed, committed, or licensed under the Apache License.
2626
*/
27-
import { Client } from "pg";
2827

2928
export interface MjolnirRecord {
3029
local_part: string,
@@ -67,53 +66,4 @@ export interface DataStore {
6766
lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]>;
6867
}
6968

70-
export class PgDataStore implements DataStore {
71-
private pgClient: Client;
7269

73-
constructor(connectionString: string) {
74-
this.pgClient = new Client({ connectionString: connectionString });
75-
}
76-
77-
public async init(): Promise<void> {
78-
await this.pgClient.connect();
79-
}
80-
81-
public async close(): Promise<void> {
82-
await this.pgClient.end()
83-
}
84-
85-
public async list(): Promise<MjolnirRecord[]> {
86-
const result = await this.pgClient.query<MjolnirRecord>("SELECT local_part, owner, management_room FROM mjolnir");
87-
88-
if (!result.rowCount) {
89-
return [];
90-
}
91-
92-
return result.rows;
93-
}
94-
95-
public async store(mjolnirRecord: MjolnirRecord): Promise<void> {
96-
await this.pgClient.query(
97-
"INSERT INTO mjolnir (local_part, owner, management_room) VALUES ($1, $2, $3)",
98-
[mjolnirRecord.local_part, mjolnirRecord.owner, mjolnirRecord.management_room],
99-
);
100-
}
101-
102-
public async lookupByOwner(owner: string): Promise<MjolnirRecord[]> {
103-
const result = await this.pgClient.query<MjolnirRecord>(
104-
"SELECT local_part, owner, management_room FROM mjolnir WHERE owner = $1",
105-
[owner],
106-
);
107-
108-
return result.rows;
109-
}
110-
111-
public async lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]> {
112-
const result = await this.pgClient.query<MjolnirRecord>(
113-
"SELECT local_part, owner, management_room FROM mjolnir WHERE local_part = $1",
114-
[localPart],
115-
);
116-
117-
return result.rows;
118-
}
119-
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { PostgresStore, SchemaUpdateFunction } from "matrix-appservice-bridge";
18+
import { DataStore, MjolnirRecord } from "../datastore";
19+
20+
function getSchema(): SchemaUpdateFunction[] {
21+
const nSchema = 1;
22+
const schema = [];
23+
for (let schemaID = 1; schemaID < nSchema + 1; schemaID++) {
24+
schema.push(require(`./schema/v${schemaID}`).runSchema);
25+
}
26+
return schema;
27+
}
28+
29+
export class PgDataStore extends PostgresStore implements DataStore {
30+
31+
constructor(connectionString: string) {
32+
super(getSchema(), { url: connectionString })
33+
}
34+
35+
public async init(): Promise<void> {
36+
await this.ensureSchema();
37+
}
38+
39+
public async close(): Promise<void> {
40+
await this.destroy();
41+
}
42+
43+
public async list(): Promise<MjolnirRecord[]> {
44+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir`;
45+
if (!result.count) {
46+
return [];
47+
}
48+
49+
return result.flat() as MjolnirRecord[];
50+
}
51+
52+
public async store(mjolnirRecord: MjolnirRecord): Promise<void> {
53+
await this.sql`INSERT INTO mjolnir (local_part, owner, management_room)
54+
VALUES (${mjolnirRecord.local_part}, ${mjolnirRecord.owner}, ${mjolnirRecord.management_room})`;
55+
}
56+
57+
public async lookupByOwner(owner: string): Promise<MjolnirRecord[]> {
58+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir
59+
WHERE owner = ${owner}`;
60+
return result.flat() as MjolnirRecord[];
61+
}
62+
63+
public async lookupByLocalPart(localPart: string): Promise<MjolnirRecord[]> {
64+
const result = await this.sql`SELECT local_part, owner, management_room FROM mjolnir
65+
WHERE local_part = ${localPart}`;
66+
return result.flat() as MjolnirRecord[];
67+
}
68+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import postgres from 'postgres';
3+
4+
export async function runSchema(sql: postgres.Sql) {
5+
await sql.begin(s => [
6+
s`CREATE TABLE mjolnir (local_part VARCHAR(255), owner VARCHAR(255), management_room TEXT);`
7+
]);
8+
}

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,10 +1265,10 @@ expect@^27.0.6:
12651265
jest-message-util "^27.2.4"
12661266
jest-regex-util "^27.0.6"
12671267

1268-
express-rate-limit@^6.2.0:
1269-
version "6.5.1"
1270-
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-6.5.1.tgz#2b4c329f03265f94f19613519b169afbd018e783"
1271-
integrity sha512-pxO6ioBLd3i8IHL+RmJtL4noYzte5fugoMdaDabtU4hcg53+x0QkTwfPtM7vWD0YUaXQgNj9NRdzmps+CHEHlA==
1268+
express-rate-limit@^6.7.0:
1269+
version "6.7.0"
1270+
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-6.7.0.tgz#6aa8a1bd63dfe79702267b3af1161a93afc1d3c2"
1271+
integrity sha512-vhwIdRoqcYB/72TK3tRZI+0ttS8Ytrk24GfmsxDXK9o9IhHNO5bXRiXQSExPQ4GbaE5tvIS7j1SGrxsuWs+sGA==
12721272

12731273
express@^4.17:
12741274
version "4.17.3"
@@ -2235,17 +2235,17 @@ make-error@^1.1.1:
22352235
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
22362236
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
22372237

2238-
matrix-appservice-bridge@8.0.0:
2239-
version "8.0.0"
2240-
resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-8.0.0.tgz#6849ac05c281399b2c2b35daba784f8291d3b35d"
2241-
integrity sha512-XFo3avVfKb34d7kalXcsi0vThlnqmrwvewcfhjintmpbFlwu54/lvdbykFSyu2kT8BY1zUtDz7iQ3Q3RAyaN1g==
2238+
matrix-appservice-bridge@8.1.1:
2239+
version "8.1.1"
2240+
resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-8.1.1.tgz#9bf37d39742aed276e01d01e5c28c75d7ba1fcf7"
2241+
integrity sha512-6QYvTxe8kLXa2u+npeFUJph0PVqaOK6BPyC2J4bM0iklS8wNyprwzpToxGUr0WZS6D8vRc8qbyxhN1JTUbu9kw==
22422242
dependencies:
22432243
"@alloc/quick-lru" "^5.2.0"
22442244
"@types/pkginfo" "^0.4.0"
22452245
axios "^0.27.2"
22462246
chalk "^4.1.0"
22472247
express "^4.18.1"
2248-
express-rate-limit "^6.2.0"
2248+
express-rate-limit "^6.7.0"
22492249
extend "^3.0.2"
22502250
ip-cidr "^3.0.4"
22512251
is-my-json-valid "^2.20.5"

0 commit comments

Comments
 (0)