All files / controllers user.js

35.29% Statements 12/34
0% Branches 0/8
10% Functions 1/10
37.5% Lines 12/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 7015x     15x 15x 15x 15x 15x 15x   15x   15x                                             15x                                             15x                 15x    
module.exports = function(crowi, app) {
  'use strict'
 
  const User = crowi.model('User')
  const Bookmark = crowi.model('Bookmark')
  const Page = crowi.model('Page')
  const ApiResponse = require('../util/apiResponse')
  const actions = {}
  const api = {}
 
  actions.api = api
 
  api.checkUsername = function(req, res) {
    var username = req.query.username
 
    User.findUserByUsername(username)
      .then(function(userData) {
        if (userData) {
          return res.json({ valid: false })
        } else {
          return res.json({ valid: true })
        }
      })
      .catch(function(err) {
        return res.json({ valid: true })
      })
  }
 
  /**
   * @api {get} /users.list Get user list
   * @apiName GetUserList
   * @apiGroup User
   *
   * @apiParam {String} user_ids
   */
  api.list = function(req, res) {
    var userIds = req.query.user_ids || null // TODO: handling
 
    var userFetcher
    if (!userIds || userIds.split(',').length <= 0) {
      userFetcher = User.findAllUsers()
    } else {
      userFetcher = User.findUsersByIds(userIds.split(','))
    }
 
    userFetcher
      .then(function(userList) {
        var result = {
          users: userList,
        }
 
        return res.json(ApiResponse.success(result))
      })
      .catch(function(err) {
        return res.json(ApiResponse.error(err))
      })
  }
 
  api.getRecentlyViewedPages = async function(req, res) {
    const pageIds = await crowi.lru.get(req.user._id.toString(), 10)
    let pages = await Page.findPagesByIds(pageIds)
    pages = pages.sort((a, b) => pageIds.indexOf(a._id.toString()) - pageIds.indexOf(b._id.toString()))
    pages = pages.filter(({ path }) => path !== '/')
    pages = pages.slice(0, 5)
    return res.json(ApiResponse.success({ pages }))
  }
 
  return actions
}