Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
2.0 kB
2
Indexable
Never
exports.follower = function (req, res) {

  var page = req.query.page ? req.query.page : 1;
  var limit = (req.query.limit) ? req.query.limit : 20;
  var skip = (page - 1) * limit;
  var sort = {"_id": 1};
  if (req.query.sort) {
    var dataSort = req.query.sort.split(',');
    var temp = '{"' + dataSort[0] + '":' + dataSort[1] + ' }';
    sort = JSON.parse(temp);

  }

  var condition = {};

  var match = {
    $match: condition
  }
  var lookup = {
    $lookup: {
      from: "userFollow",
      localField: "_id",
      foreignField: "creator",
      as: "followers"
    }
  }
  var modelId = req.user._id;

  var addFields = {
    $addFields: {
      followers: {
        $filter: {input: "$followers", as: "follower", cond: {$eq: ["$$follower.creator", modelId]}}
      }
    }
  };
  var addFields2 = {
    $addFields: {
      avatarUrl: {'$cond': {if : {$ne: ["$avatar", null]}, then: {'$concat': [config.avatarBaseUrl]}, else: {'$concat': [config.CNDURL, '/', '$avatar.fileUrlBase']}}}
    }
  };
  var match = {
    $match: {
      "followers.0": {"$exists": true}
    }
  };

  var pipe = [lookup, addFields, match, addFields2];
  pipe.push({
    $lookup: {
      from: "userblacklists",
      localField: "_id",
      foreignField: "baneUserId",
      as: "usersBanned"
    }

  })
  pipe.push({
    $addFields: {
      usersBanned: {
        $filter: {input: "$usersBanned", as: "userBanned", cond: {$eq: ["$$userBanned.userId", modelId]}}
      }
    }
  }, {
    $addFields: {
      isBanned: {$gt: [{$size: "$usersBanned"}, 0]},

    }
  })
  pipe.push({"$limit": parseInt(limit)})
  pipe.push({$skip: skip})
  pipe.push({"$sort": sort})
  var query = UserModel.aggregate(pipe)

  query.exec(function (err, result) {

    return ReponseHelper.response(res, 200, HashDataHelper.make(result));
  }).catch(function (err) {

    return ReponseHelper.response(res, 500, HashDataHelper.makeError({error: err, 'msg': 'Sever error'}));
  });

}