<commit_before>const faker = require('faker')

const n = {
  communities: 50,
  networks: 5,
  posts: 3000,
  tags: 20,
  users: 1000
}

exports.seed = (knex, Promise) => delAll(knex)
  .then(() => seed('users', knex, Promise))
  .then(() => knex('tags').insert([
    {name: 'offer'},
    {name: 'request'},
    {name: 'intention'}
  ]))
  .then(() => seed('tags', knex, Promise))
  .then(() => seed('networks', knex, Promise))
  .then(() => knex('communities').insert({ name: 'starter-posts', slug: 'starter-posts' }))
  .then(() => seed('communities', knex, Promise))
  .then(() => seed('posts', knex, Promise))
  .catch(err => {
    let report = err.message
    if (err.message.includes('unique constraint')) {
      report =
`
  Error during seeding.

  This isn't uncommon: Faker generates a limited number of unique names,
  and sometimes they collide. You should be able to simply re-run the
  seed until it passes (sometimes four or five tries are required).

${err.message}
`
    }
    console.error(report)
  })

function delAll (knex) {
  return knex('activities').del()
    .then(() => knex('tag_follows').del())
    .then(() => knex('communities_tags').del())
    .then(() => knex('communities_posts').del())
    .then(() => knex('communities_users').del())
    .then(() => knex('community_invites').del())
    .then(() => knex('comments_tags').del())
    .then(() => knex('comments').del())
    .then(() => knex('contributions').del())
    .then(() => knex('devices').del())
    .then(() => knex('event_responses').del())
    .then(() => knex('follows').del())
    .then(() => knex('join_requests').del())
    .then(() => knex('link_previews').del())
    .then(() => knex('linked_account').del())
    .then(() => knex('media').del())
    .then(() => knex('nexudus_accounts').del())
    .then(() => knex('notifications').del())
    .then(() => knex('posts_about_users').del())
    .then(() => knex('posts_tags').del())
    .then(() => knex('posts_users').del())
    .then(() => knex('push_notifications').del())
    .then(() => knex('tags_users').del())
    .then(() => knex('thanks').del())
    .then(() => knex('user_external_data').del())
    .then(() => knex('user_post_relevance').del())
    .then(() => knex('votes').del())
    .then(() => knex('posts').del())
    .then(() => knex('communities').del())
    .then(() => knex('networks').del())
    .then(() => knex('tags').del())
    .then(() => knex('users').del())
}

const fake = {
  communities: fakeCommunity,
  networks: fakeNetwork,
  posts: fakePost,
  tags: fakeTag,
  users: fakeUser
}

function seed (entity, knex, Promise) {
  console.info(`  --> ${entity}`)
  return Promise.all(
    [...new Array(n[entity])]
      .map(() => fake[entity](knex, Promise).then(row => knex(entity).insert(row)))
  )
}

// Grab random row or rows from table
function sample (entity, knex, limit = 1) {
  return knex(entity)
    .select()
    .orderByRaw('random()')
    .limit(limit)
}

function fakeCommunity (knex) {
  const name = faker.random.words()

  return Promise.all([
    sample('users', knex),
    sample('networks', knex)
  ])
    .then(([ users, networks ]) => ({
      name,
      avatar_url: faker.internet.avatar(),
      background_url: faker.image.imageUrl(),
      beta_access_code: faker.random.uuid(),
      description: faker.lorem.paragraph(),
      slug: faker.helpers.slugify(name).toLowerCase(),
      daily_digest: faker.random.boolean(),<commit_msg>Add users to communities<commit_after>const faker = require('faker')

const n = {
  communities: 50,
  networks: 5,
  posts: 3000,
  tags: 20,
  users: 1000
}

exports.seed = (knex, Promise) => delAll(knex)
  .then(() => seed('users', knex, Promise))
  .then(() => knex('tags').insert([
    {name: 'offer'},
    {name: 'request'},
    {name: 'intention'}
  ]))
  .then(() => seed('tags', knex, Promise))
  .then(() => seed('networks', knex, Promise))
  .then(() => knex('communities').insert({ name: 'starter-posts', slug: 'starter-posts' }))
  .then(() => seed('communities', knex, Promise))
  .then(() => seed('posts', knex, Promise))
  .then(() => addUsersToCommunities(knex, Promise))
  .catch(err => {
    let report = err.message
    if (err.message.includes('unique constraint')) {
      report =
`
  Error during seeding.

  This isn't uncommon: Faker generates a limited number of unique names,
  and sometimes they collide. You should be able to simply re-run the
  seed until it passes (sometimes four or five tries are required).

${err.message}
`
    }
    console.error(report)
  })

function delAll (knex) {
  return knex('activities').del()
    .then(() => knex('tag_follows').del())
    .then(() => knex('communities_tags').del())
    .then(() => knex('communities_posts').del())
    .then(() => knex('communities_users').del())
    .then(() => knex('community_invites').del())
    .then(() => knex('comments_tags').del())
    .then(() => knex('comments').del())
    .then(() => knex('contributions').del())
    .then(() => knex('devices').del())
    .then(() => knex('event_responses').del())
    .then(() => knex('follows').del())
    .then(() => knex('join_requests').del())
    .then(() => knex('link_previews').del())
    .then(() => knex('linked_account').del())
    .then(() => knex('media').del())
    .then(() => knex('nexudus_accounts').del())
    .then(() => knex('notifications').del())
    .then(() => knex('posts_about_users').del())
    .then(() => knex('posts_tags').del())
    .then(() => knex('posts_users').del())
    .then(() => knex('push_notifications').del())
    .then(() => knex('tags_users').del())
    .then(() => knex('thanks').del())
    .then(() => knex('user_external_data').del())
    .then(() => knex('user_post_relevance').del())
    .then(() => knex('votes').del())
    .then(() => knex('posts').del())
    .then(() => knex('communities').del())
    .then(() => knex('networks').del())
    .then(() => knex('tags').del())
    .then(() => knex('users').del())
}

const fake = {
  communities: fakeCommunity,
  networks: fakeNetwork,
  posts: fakePost,
  tags: fakeTag,
  users: fakeUser
}

function randomIndex (length) {
  return Math.floor(Math.random() * length)
}

function moderatorOrMember () {
  // Role 1 is moderator
  return Math.random() > 0.9 ? 1 : 0
}

function addUsersToCommunities (knex, Promise) {
  return knex('users').select('id')
    .then(users => knex('communities').select('id').then(communities => ({ users, communities })))
    .then(({ users, communities }) => Promise.all(
      users.map(user => knex('communities_users').insert({
        user_id: user.id,
        community_id: communities[randomIndex(communities.length)].id,
        role: moderatorOrMember(),
        settings: '{ "send_email": true, "send_push_notifications": true }' 
      }))
    ))
}

function seed (entity, knex, Promise) {
  console.info(`  --> ${entity}`)
  return Promise.all(
    [...new Array(n[entity])]
      .map(() => fake[entity](knex, Promise).then(row => knex(entity).insert(row)))
  )
}

// Grab random row or rows from table
function sample (entity, knex, limit = 1) {
  return knex(entity)
    .select()
    .orderByRaw('random()')
    .limit(limit)
}

function fakeCommunity (knex) {
  const name = faker.random.words()

  return Promise.all([
    sample('users', knex),
    sample('networks', knex)
  ])
    .then(([ users, networks ]) => ({
      name,
      avatar_url: faker.internet.avatar(),
      background_url: faker.image.imageUrl(),
      beta_access_code: faker.random.uuid(),
      description: faker.lorem.paragraph(),
      slug: faker.helpers.slugify(name).toLowerCase(),
      daily_digest: faker.random.boolean(),
{'fraction_non_alphanumeric': 0.2250130821559393, 'fraction_numerical': 0.0043171114599686025, 'mean_word_length': 3.6474164133738602, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 97, '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': '5587221', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2554, 'n_words': 529}