mirror of
https://github.com/mastodon/mastodon.git
synced 2024-12-19 16:07:08 +00:00
Streaming: use pgPool.query instead of manually acquiring & releasing a connection (#30964)
This commit is contained in:
parent
b87c41115e
commit
c953dca1de
|
@ -524,43 +524,27 @@ const startServer = async () => {
|
||||||
* @param {any} req
|
* @param {any} req
|
||||||
* @returns {Promise<ResolvedAccount>}
|
* @returns {Promise<ResolvedAccount>}
|
||||||
*/
|
*/
|
||||||
const accountFromToken = (token, req) => new Promise((resolve, reject) => {
|
const accountFromToken = async (token, req) => {
|
||||||
pgPool.connect((err, client, done) => {
|
const result = await pgPool.query('SELECT oauth_access_tokens.id, oauth_access_tokens.resource_owner_id, users.account_id, users.chosen_languages, oauth_access_tokens.scopes, devices.device_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id LEFT OUTER JOIN devices ON oauth_access_tokens.id = devices.access_token_id WHERE oauth_access_tokens.token = $1 AND oauth_access_tokens.revoked_at IS NULL LIMIT 1', [token]);
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
if (result.rows.length === 0) {
|
||||||
client.query('SELECT oauth_access_tokens.id, oauth_access_tokens.resource_owner_id, users.account_id, users.chosen_languages, oauth_access_tokens.scopes, devices.device_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id LEFT OUTER JOIN devices ON oauth_access_tokens.id = devices.access_token_id WHERE oauth_access_tokens.token = $1 AND oauth_access_tokens.revoked_at IS NULL LIMIT 1', [token], (err, result) => {
|
throw new AuthenticationError('Invalid access token');
|
||||||
done();
|
}
|
||||||
|
|
||||||
if (err) {
|
req.accessTokenId = result.rows[0].id;
|
||||||
reject(err);
|
req.scopes = result.rows[0].scopes.split(' ');
|
||||||
return;
|
req.accountId = result.rows[0].account_id;
|
||||||
}
|
req.chosenLanguages = result.rows[0].chosen_languages;
|
||||||
|
req.deviceId = result.rows[0].device_id;
|
||||||
|
|
||||||
if (result.rows.length === 0) {
|
return {
|
||||||
reject(new AuthenticationError('Invalid access token'));
|
accessTokenId: result.rows[0].id,
|
||||||
return;
|
scopes: result.rows[0].scopes.split(' '),
|
||||||
}
|
accountId: result.rows[0].account_id,
|
||||||
|
chosenLanguages: result.rows[0].chosen_languages,
|
||||||
req.accessTokenId = result.rows[0].id;
|
deviceId: result.rows[0].device_id
|
||||||
req.scopes = result.rows[0].scopes.split(' ');
|
};
|
||||||
req.accountId = result.rows[0].account_id;
|
};
|
||||||
req.chosenLanguages = result.rows[0].chosen_languages;
|
|
||||||
req.deviceId = result.rows[0].device_id;
|
|
||||||
|
|
||||||
resolve({
|
|
||||||
accessTokenId: result.rows[0].id,
|
|
||||||
scopes: result.rows[0].scopes.split(' '),
|
|
||||||
accountId: result.rows[0].account_id,
|
|
||||||
chosenLanguages: result.rows[0].chosen_languages,
|
|
||||||
deviceId: result.rows[0].device_id
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {any} req
|
* @param {any} req
|
||||||
|
@ -771,28 +755,15 @@ const startServer = async () => {
|
||||||
* @param {any} req
|
* @param {any} req
|
||||||
* @returns {Promise.<void>}
|
* @returns {Promise.<void>}
|
||||||
*/
|
*/
|
||||||
const authorizeListAccess = (listId, req) => new Promise((resolve, reject) => {
|
const authorizeListAccess = async (listId, req) => {
|
||||||
const { accountId } = req;
|
const { accountId } = req;
|
||||||
|
|
||||||
pgPool.connect((err, client, done) => {
|
const result = await pgPool.query('SELECT id, account_id FROM lists WHERE id = $1 AND account_id = $2 LIMIT 1', [listId, accountId]);
|
||||||
if (err) {
|
|
||||||
reject();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
if (result.rows.length === 0) {
|
||||||
client.query('SELECT id, account_id FROM lists WHERE id = $1 LIMIT 1', [listId], (err, result) => {
|
throw new AuthenticationError('List not found');
|
||||||
done();
|
}
|
||||||
|
};
|
||||||
if (err || result.rows.length === 0 || result.rows[0].account_id !== accountId) {
|
|
||||||
reject();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string[]} channelIds
|
* @param {string[]} channelIds
|
||||||
|
|
Loading…
Reference in a new issue