Skip to content

Commit 21eaf53

Browse files
committed
lint
1 parent 106f197 commit 21eaf53

File tree

6 files changed

+57
-43
lines changed

6 files changed

+57
-43
lines changed

src/activity-pub-db.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ async function firstTimeSetup(actorName) {
201201
try {
202202
const actorInfo = await getActorInfo();
203203
const actorRecord = actorJson(actorInfo, publicKey);
204-
const webfingerRecord = webfingerJson(account);
204+
const webfingerRecord = webfingerJson(actorInfo.username);
205205

206206
await db.run(
207207
'INSERT OR REPLACE INTO accounts (name, actor, pubkey, privkey, webfinger) VALUES (?, ?, ?, ?, ?)',
@@ -221,11 +221,6 @@ async function firstTimeSetup(actorName) {
221221
}
222222

223223
function setup() {
224-
// activitypub not set up yet, skip until we have the data we need
225-
if (false) {
226-
return;
227-
}
228-
229224
// Initialize the database
230225
const exists = fs.existsSync(dbFile);
231226

src/activitypub.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ export async function lookupActorInfo(actorUsername) {
184184
}
185185

186186
export async function broadcastMessage(bookmark, action, db, account, domain) {
187-
if (false /* TODO actorInfo.disabled */) {
188-
return; // no fediverse setup, so no purpose trying to send messages
189-
}
187+
// TODO bail if activitypub not set up
190188

191189
const result = await db.getFollowers();
192190
const followers = JSON.parse(result);

src/database.js

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const connect = new Promise((resolve, reject) => {
77
if (error) {
88
reject(error);
99
} else {
10-
console.log('i have hereby connected :)')
10+
console.log('i have hereby connected :)');
1111
result.exec(schema, (execError) => {
1212
if (execError) {
1313
reject(execError);
@@ -19,18 +19,20 @@ const connect = new Promise((resolve, reject) => {
1919
});
2020
});
2121

22-
const query = (method) => async (...args) => {
23-
const db = await connect;
24-
return new Promise((resolve, reject) => {
25-
db[method](...args, (error, result) => {
26-
if (error) {
27-
reject(error);
28-
} else {
29-
resolve(result);
30-
}
22+
const query =
23+
(method) =>
24+
async (...args) => {
25+
const db = await connect;
26+
return new Promise((resolve, reject) => {
27+
db[method](...args, (error, result) => {
28+
if (error) {
29+
reject(error);
30+
} else {
31+
resolve(result);
32+
}
33+
});
3134
});
32-
});
33-
}
35+
};
3436

3537
const run = query('run');
3638
const get = query('get');
@@ -50,7 +52,7 @@ export const getSetting = async (name) => {
5052
}
5153

5254
return null;
53-
}
55+
};
5456

5557
export const getSettings = async (names) => {
5658
// TODO: There must be a way to get node-sqlite3 to accept parameters for an
@@ -59,30 +61,47 @@ export const getSettings = async (names) => {
5961
// enforcing elsewhere in business logic, and this exact bit of code will
6062
// likely like to weird bugs in the future, but for now it's maybe better to
6163
// be safe!
62-
if (!names.every(name => name.match(/^[a-z0-9-_ .\/]+$/i))) {
64+
if (!names.every((name) => name.match(/^[a-z0-9-_ ./]+$/i))) {
6365
throw new Error('Names contain unexpected characters');
6466
}
6567

6668
const rows = await all(`
6769
select name, value
6870
from settings
69-
where name in (${names.map(n => `'${n}'`).join(',')})
71+
where name in (${names.map((n) => `'${n}'`).join(',')})
7072
`);
7173

7274
return Object.fromEntries(rows.map(({ name, value }) => [name, JSON.parse(value)]));
73-
}
75+
};
7476

75-
export const setSetting = async (name, value) => {
76-
const serializedValue = JSON.stringify(value);
77+
export const setSettings = async (obj) => {
78+
// TODO: See caveat in getSettings
79+
if (!Object.keys(obj).every((name) => name.match(/^[a-z0-9-_ ./]+$/i))) {
80+
throw new Error('Names contain unexpected characters');
81+
}
82+
83+
const values = Object.entries(obj).map(
84+
([name, value]) =>
85+
// TODO: Escape this properly
86+
`('${name}', '${JSON.stringify(value).replace("'", "\\'")}')`,
87+
);
7788

89+
await run(`
90+
insert into settings
91+
(name, value)
92+
values ${values.join(',')}
93+
on conflict (name) do update set value = excluded.value
94+
`);
95+
};
96+
97+
export const setSetting = async (name, value) => {
7898
return run(
7999
`
80100
insert into settings (name, value)
81101
values (?, ?)
82-
on conflict (name) do update set value = ?
102+
on conflict (name) do update set value = excluded.value
83103
`,
84104
name,
85-
serializedValue,
86-
serializedValue
105+
JSON.stringify(value),
87106
);
88107
};

src/routes/admin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ router.get('/followers', isAuthenticated, async (req, res) => {
4242

4343
const apDb = req.app.get('apDb');
4444

45-
if (false /* TODO actorinfo.disabled */) {
45+
const { actorInfo } = await getActorInfo();
46+
// TODO
47+
if (actorInfo.disabled) {
4648
return res.render('nonfederated', params);
4749
}
4850

@@ -75,7 +77,9 @@ router.get('/following', isAuthenticated, async (req, res) => {
7577

7678
const apDb = req.app.get('apDb');
7779

78-
if (false /* TODO actorinfo.disabled */) {
80+
const { actorInfo } = await getActorInfo();
81+
// TODO
82+
if (actorInfo.disabled) {
7983
return res.render('nonfederated', params);
8084
}
8185

src/routes/core.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ router.get('/', async (req, res) => {
1010
const params = {};
1111

1212
const bookmarksDb = req.app.get('bookmarksDb');
13-
const db = req.app.get('db');
1413

1514
const limit = Math.max(req.query?.limit || 10, 1);
1615
const offset = Math.max(req.query?.offset || 0, 0);

src/util.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as db from './database.js';
66

77
dotenv.config();
88

9-
const ACTOR_SETTING_NAMES = ['username', 'avatar', 'displayName', 'description']
9+
const ACTOR_SETTING_NAMES = ['username', 'avatar', 'displayName', 'description'];
1010
const IS_ACCOUNT_FILE_IMPORTED = 'isAccountFileImported';
1111

1212
export const data = {
@@ -19,18 +19,17 @@ try {
1919
const accountFileData = JSON.parse(accountFile);
2020
const isAccountFileImported = await db.getSetting(IS_ACCOUNT_FILE_IMPORTED);
2121
if (isAccountFileImported) {
22-
console.log('Postmarks detected an account.json file that will no longer be read. You should remove this file.')
22+
console.log('Postmarks detected an account.json file that will no longer be read. You should remove this file.');
2323
} else {
24-
for (const name of ACTOR_SETTING_NAMES) {
25-
if (accountFileData.hasOwnProperty(name)) {
26-
await db.setSetting(name, accountFileData[name]);
27-
}
28-
}
29-
await db.setSetting(IS_ACCOUNT_FILE_IMPORTED, true);
30-
console.log('Your account.json file has been imported to the database. You should now remove this file.')
24+
await db.setSettings({
25+
...Object.fromEntries(Object.entries(accountFileData).filter(([name]) => ACTOR_SETTING_NAMES.includes(name))),
26+
[IS_ACCOUNT_FILE_IMPORTED]: true,
27+
});
28+
console.log('Your account.json file has been imported to the database. You should now remove this file.');
3129
}
3230
} catch (e) {
33-
console.log('uhhh', e)
31+
// TODO: Check for existence of account.json instead of catching error
32+
console.log('Failed to read account.json', e);
3433
}
3534

3635
export const getActorInfo = () => db.getSettings(ACTOR_SETTING_NAMES);

0 commit comments

Comments
 (0)