<commit_before>"use strict";

var Promise = require('bluebird')
  , models = require('../../../models')
  , exceptions = require('../../../support/exceptions')
  , PostSerializer = models.PostSerializer
  , FeedFactory = models.FeedFactory
  , ForbiddenException = exceptions.ForbiddenException

exports.addController = function(app) {
  /**
   * @constructor
   */
  var PostsController = function() {
  }

  PostsController.create = function(req, res) {
    if (!req.user)
      return res.status(401).jsonp({ err: 'Not found' })

    var feeds = []
    req.body.meta = req.body.meta || {}
    if (Array.isArray(req.body.meta.feeds)) {
      feeds = req.body.meta.feeds
    } else if (req.body.meta.feeds) {
      feeds = [req.body.meta.feeds]
    } else {
      feeds = [req.user.username]
    }

    Promise.map(feeds, function(username) {
      return FeedFactory.findByUsername(username)
        .then(function(feed) {
          return feed.validateCanPost(req.user)
        })
        .then(function(feed) {
          return feed.getPostsTimelineId()
        })
      })
      .then(function(timelineIds) {
        return req.user.newPost({
          body: req.body.post.body,
          attachments: req.body.post.attachments,
          timelineIds: timelineIds
        })
      })
      .then(function(newPost) { return newPost.create() })
      .then(function(newPost) {
        new PostSerializer(newPost).toJSON(function(err, json) {
          res.jsonp(json)
        })
      })
      .catch(exceptions.reportError(res))
  }

  PostsController.update = function(req, res) {
    if (!req.user)
      return res.status(401).jsonp({ err: 'Not found' })

    models.Post.findById(req.params.postId)
      .then(function(post) {
        if (post.userId != req.user.id) {
          return Promise.reject(new ForbiddenException(
              "You can't update another user's post"))
        }
        return post.update({
          body: req.body.post.body
        })
      })
      .then(function(post) {
        new PostSerializer(post).toJSON(function(err, json) {
          res.jsonp(json)
        })
      })
      .catch(exceptions.reportError(res))
  }

  PostsController.show = async function(req, res) {
    try {
      var post = await models.Post.getById(req.params.postId, {
        maxComments: req.query.maxComments,
        maxLikes: req.query.maxLikes,
        currentUser: req.user ? req.user.id : null
      })

      var author = await models.User.findById(post.userId)
      var banIds = await author.getBanIds()

      if (banIds.indexOf(post.currentUser) >= 0)
        throw new ForbiddenException("This user has prevented you from seeing their posts")

      var json = new PostSerializer(post).promiseToJSON()

      res.jsonp(await json)<commit_msg>Hide single-post pages for blocker user.<commit_after>"use strict";

var Promise = require('bluebird')
  , models = require('../../../models')
  , exceptions = require('../../../support/exceptions')
  , PostSerializer = models.PostSerializer
  , FeedFactory = models.FeedFactory
  , ForbiddenException = exceptions.ForbiddenException

exports.addController = function(app) {
  /**
   * @constructor
   */
  var PostsController = function() {
  }

  PostsController.create = function(req, res) {
    if (!req.user)
      return res.status(401).jsonp({ err: 'Not found' })

    var feeds = []
    req.body.meta = req.body.meta || {}
    if (Array.isArray(req.body.meta.feeds)) {
      feeds = req.body.meta.feeds
    } else if (req.body.meta.feeds) {
      feeds = [req.body.meta.feeds]
    } else {
      feeds = [req.user.username]
    }

    Promise.map(feeds, function(username) {
      return FeedFactory.findByUsername(username)
        .then(function(feed) {
          return feed.validateCanPost(req.user)
        })
        .then(function(feed) {
          return feed.getPostsTimelineId()
        })
      })
      .then(function(timelineIds) {
        return req.user.newPost({
          body: req.body.post.body,
          attachments: req.body.post.attachments,
          timelineIds: timelineIds
        })
      })
      .then(function(newPost) { return newPost.create() })
      .then(function(newPost) {
        new PostSerializer(newPost).toJSON(function(err, json) {
          res.jsonp(json)
        })
      })
      .catch(exceptions.reportError(res))
  }

  PostsController.update = function(req, res) {
    if (!req.user)
      return res.status(401).jsonp({ err: 'Not found' })

    models.Post.findById(req.params.postId)
      .then(function(post) {
        if (post.userId != req.user.id) {
          return Promise.reject(new ForbiddenException(
              "You can't update another user's post"))
        }
        return post.update({
          body: req.body.post.body
        })
      })
      .then(function(post) {
        new PostSerializer(post).toJSON(function(err, json) {
          res.jsonp(json)
        })
      })
      .catch(exceptions.reportError(res))
  }

  PostsController.show = async function(req, res) {
    try {
      var post = await models.Post.getById(req.params.postId, {
        maxComments: req.query.maxComments,
        maxLikes: req.query.maxLikes,
        currentUser: req.user ? req.user.id : null
      })

      var author = await models.User.findById(post.userId)
      var banIds = await author.getBanIds()
      if (banIds.indexOf(post.currentUser) >= 0)
        throw new ForbiddenException("This user has prevented you from seeing their posts")

      var you = await models.User.findById(post.currentUser)
      if (you) {
        var yourBanIds = await you.getBanIds()
        if (yourBanIds.indexOf(author.id) >= 0)
          throw new ForbiddenException("You have blocked this user and do not want to see their posts")
      }

      var json = new PostSerializer(post).promiseToJSON()

      res.jsonp(await json)
{'fraction_non_alphanumeric': 0.1335149863760218, 'fraction_numerical': 0.0025544959128065395, 'mean_word_length': 2.3540833809251858, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '4613921', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1794, 'n_words': 340}