Streaming: use pgPool.query instead of manually acquiring & releasing a connection (#30964)
This commit is contained in:
		
							parent
							
								
									b87c41115e
								
							
						
					
					
						commit
						c953dca1de
					
				@ -524,25 +524,11 @@ 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
 | 
					 | 
				
			||||||
      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) => {
 | 
					 | 
				
			||||||
        done();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (err) {
 | 
					 | 
				
			||||||
          reject(err);
 | 
					 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (result.rows.length === 0) {
 | 
					    if (result.rows.length === 0) {
 | 
				
			||||||
          reject(new AuthenticationError('Invalid access token'));
 | 
					      throw new AuthenticationError('Invalid access token');
 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    req.accessTokenId = result.rows[0].id;
 | 
					    req.accessTokenId = result.rows[0].id;
 | 
				
			||||||
@ -551,16 +537,14 @@ const startServer = async () => {
 | 
				
			|||||||
    req.chosenLanguages = result.rows[0].chosen_languages;
 | 
					    req.chosenLanguages = result.rows[0].chosen_languages;
 | 
				
			||||||
    req.deviceId = result.rows[0].device_id;
 | 
					    req.deviceId = result.rows[0].device_id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        resolve({
 | 
					    return {
 | 
				
			||||||
      accessTokenId: result.rows[0].id,
 | 
					      accessTokenId: result.rows[0].id,
 | 
				
			||||||
      scopes: result.rows[0].scopes.split(' '),
 | 
					      scopes: result.rows[0].scopes.split(' '),
 | 
				
			||||||
      accountId: result.rows[0].account_id,
 | 
					      accountId: result.rows[0].account_id,
 | 
				
			||||||
      chosenLanguages: result.rows[0].chosen_languages,
 | 
					      chosenLanguages: result.rows[0].chosen_languages,
 | 
				
			||||||
      deviceId: result.rows[0].device_id
 | 
					      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();
 | 
					    if (result.rows.length === 0) {
 | 
				
			||||||
        return;
 | 
					      throw new AuthenticationError('List not found');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
      // @ts-ignore
 | 
					 | 
				
			||||||
      client.query('SELECT id, account_id FROM lists WHERE id = $1 LIMIT 1', [listId], (err, result) => {
 | 
					 | 
				
			||||||
        done();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (err || result.rows.length === 0 || result.rows[0].account_id !== accountId) {
 | 
					 | 
				
			||||||
          reject();
 | 
					 | 
				
			||||||
          return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        resolve();
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * @param {string[]} channelIds
 | 
					   * @param {string[]} channelIds
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user