<commit_before>// @flow

import type {
  UserInfos,
  CurrentUserInfo,
  LoggedInUserInfo,
} from 'lib/types/user-types';
import type { Viewer } from '../session/viewer';

import { ServerError } from 'lib/utils/errors';

import { dbQuery, SQL } from '../database';

async function fetchUserInfos(userIDs: string[]): Promise<UserInfos> {
  if (userIDs.length <= 0) {
    return {};
  }

  const query = SQL`
    SELECT id, username FROM users WHERE id IN (${userIDs})
  `;
  const [result] = await dbQuery(query);

  const userInfos = {};
  for (let row of result) {
    const id = row.id.toString();
    userInfos[id] = {
      id,
      username: row.username,
    };
  }
  for (let userID of userIDs) {
    if (!userInfos[userID]) {
      userInfos[userID] = {
        id: userID,
        username: null,
      };
    }
  }

  return userInfos;
}

async function fetchKnownUserInfos(
  viewer: Viewer,
  userIDs?: $ReadOnlyArray<string>,
) {
  if (!viewer.loggedIn) {
    throw new ServerError('not_logged_in');
  }

  const query = SQL`
    SELECT DISTINCT u.id, u.username FROM relationships_undirected r 
    LEFT JOIN users u ON r.user1 = u.id OR r.user2 = u.id 
  `;
  if (userIDs) {
    query.append(SQL`
      WHERE (r.user1 = ${viewer.userID} AND r.user2 IN (${userIDs})) OR
        (r.user1 IN (${userIDs}) AND r.user2 = ${viewer.userID})
    `);
  } else {
    query.append(SQL`
      WHERE r.user1 = ${viewer.userID} OR r.user2 = ${viewer.userID}
    `);
  }
  const [result] = await dbQuery(query);

  const userInfos = {};
  for (let row of result) {
    const id = row.id.toString();
    userInfos[id] = {
      id,
      username: row.username,
    };
  }<commit_msg>[server] Fix case where empty array passed to fetchKnownUserInfos throws exception<commit_after>// @flow

import type {
  UserInfos,
  CurrentUserInfo,
  LoggedInUserInfo,
} from 'lib/types/user-types';
import type { Viewer } from '../session/viewer';

import { ServerError } from 'lib/utils/errors';

import { dbQuery, SQL } from '../database';

async function fetchUserInfos(userIDs: string[]): Promise<UserInfos> {
  if (userIDs.length <= 0) {
    return {};
  }

  const query = SQL`
    SELECT id, username FROM users WHERE id IN (${userIDs})
  `;
  const [result] = await dbQuery(query);

  const userInfos = {};
  for (let row of result) {
    const id = row.id.toString();
    userInfos[id] = {
      id,
      username: row.username,
    };
  }
  for (let userID of userIDs) {
    if (!userInfos[userID]) {
      userInfos[userID] = {
        id: userID,
        username: null,
      };
    }
  }

  return userInfos;
}

async function fetchKnownUserInfos(
  viewer: Viewer,
  userIDs?: $ReadOnlyArray<string>,
) {
  if (!viewer.loggedIn) {
    throw new ServerError('not_logged_in');
  }
  if (userIDs && userIDs.length === 0) {
    return {};
  }

  const query = SQL`
    SELECT DISTINCT u.id, u.username FROM relationships_undirected r 
    LEFT JOIN users u ON r.user1 = u.id OR r.user2 = u.id 
  `;
  if (userIDs) {
    query.append(SQL`
      WHERE (r.user1 = ${viewer.userID} AND r.user2 IN (${userIDs})) OR
        (r.user1 IN (${userIDs}) AND r.user2 = ${viewer.userID})
    `);
  } else {
    query.append(SQL`
      WHERE r.user1 = ${viewer.userID} OR r.user2 = ${viewer.userID}
    `);
  }
  const [result] = await dbQuery(query);

  const userInfos = {};
  for (let row of result) {
    const id = row.id.toString();
    userInfos[id] = {
      id,
      username: row.username,
    };
  }
{'fraction_non_alphanumeric': 0.14555333142693738, 'fraction_numerical': 0.005433228481555619, 'mean_word_length': 2.7212765957446807, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2747636', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1240, 'n_words': 317}