<commit_before>import Crowi from 'server/crowi'
import { EventEmitter } from 'events'
import Debug from 'debug'
const debug = Debug('crowi:events:user')

export default class UserEvent extends EventEmitter {
  public crowi: Crowi

  constructor(crowi: Crowi) {
    super()
    this.crowi = crowi
  }

  async onActivated(user) {
    const Page = this.crowi.model('Page')

    const userPagePath = Page.getUserPagePath(user)
    Page.findPage(userPagePath, user, {}, false)
      .then(function (page) {
        // do nothing because user page is already exists.
      })
      .catch(function (err) {
        const body = `# ${user.username}\nThis is ${user.username}'s page`
        // create user page
        Page.createPage(userPagePath, body, user, {})
          .then(function (page) {
            // page created
            debug('User page created', page)
          })
          .catch(function (err) {
            debug('Failed to create user page', err)
          })
      })
  }
}<commit_msg>feat: move manually created user page automatically<commit_after>import Crowi from 'server/crowi'
import { EventEmitter } from 'events'
import Debug from 'debug'
import { UserDocument } from 'server/models/user'

const debug = Debug('crowi:events:user')

export default class UserEvent extends EventEmitter {
  public crowi: Crowi

  constructor(crowi: Crowi) {
    super()
    this.crowi = crowi
  }

  async onActivated(user: UserDocument) {
    const Page = this.crowi.model('Page')
    const userPagePath = Page.getUserPagePath(user)

    try {
      const page = await Page.findPage(userPagePath, user, {}, false)

      if (page === null) {
        throw new Error()
      }

      // User page is already exists.
      const renamedUserPagePath = `/tmp/user-${user.username}-${Date.now()}`
      await Page.rename(page, renamedUserPagePath, user, {})
      await this.createUserPage(userPagePath, user)
    } catch {
      await this.createUserPage(userPagePath, user)
    }
  }

  private async createUserPage(userPagePath: string, user: UserDocument) {
    const Page = this.crowi.model('Page')
    const body = `# ${user.username}\nThis is ${user.username}'s page`

    try {
      const page = await Page.createPage(userPagePath, body, user, {})
      debug('User page created', page)
    } catch (err) {
      debug('Failed to create user page', err)
    }
  }
}
{'fraction_non_alphanumeric': 0.12026913372582002, 'fraction_numerical': 0.0, 'mean_word_length': 2.868292682926829, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1412018', 'n_tokens_mistral': 725, 'n_tokens_neox': 714, 'n_words': 194}