<commit_before>require! {
	'../../../config': config
	'../../../models/notice': Notice
	'../../../models/post': Post
	'../../../models/post-favorite': PostFavorite
	'../../../utils/streaming': Streamer
}

authorize = require '../../auth'

favorite-step = (req, res, app, user, target-post) ->
	PostFavorite.is-favorited target-post.id, user.id (is-favorited) ->
		if is-favorited
			res.api-error 400 'This post is already favorited :('
			return

		PostFavorite.create target-post.id, user.id (favorite) ->
			target-post.favorites-count++;
			target-post.update ->
			Post.build-response-object target-post, (obj) ->
				res.api-render obj
			content = {}
			content.post = target-post
			content.user = user.filt!
			Notice.create config.web-client-id, 'favorite', JSON.stringify content, target-post.user-id, (notice) ->
				Streamer.publish 'userStream:' + target-post.user-id, JSON.stringify do
					type: 'notice'
					value: notice

module.exports = (req, res) ->
	authorize req, res, (user, app) ->
		if req.body.post_id == null
			res.api-error 400 'post_id parameter is required :('
			return
		post-id = req.body.post_id

		Post.find post-id, (target-post) ->
			if target-post == null
				res.api-error 404 'Post not found...'
				return

			if target-post.repost-from-post-id == null
				favorite-step req, res, app, user, target-post
			else
				Post.find target-post.repost-from-post-id, (true-target-post) ->
					favorite-step req, res, app, user, true-target-post
<commit_msg>Improve readability<commit_after>require! {
	'../../../config': config
	'../../../models/notice': Notice
	'../../../models/post': Post
	'../../../models/post-favorite': PostFavorite
	'../../../utils/streaming': Streamer
	'../../auth': authorize
}
module.exports = (req, res) -> authorize req, res, (user, app) ->
	| (post-id = req.body.post_id) == null => res.api-error 400 'post_id parameter is required :('
	| _ => Post.find post-id, (target-post) ->
			| target-post == null => res.api-error 404 'Post not found...'
			| target-post.repost-from-post-id == null => favorite-step req, res, app, user, target-post
			| _ => Post.find target-post.repost-from-post-id, (true-target-post) -> favorite-step req, res, app, user, true-target-post

favorite-step = (req, res, app, user, target-post) ->
	PostFavorite.is-favorited target-post.id, user.id, (is-favorited) ->
		| is-favorited => res.api-error 400 'This post is already favorited :('
		| _ => PostFavorite.create target-post.id, user.id, (favorite) ->
			target-post
				..favorites-count++
				..update ->
			Post.build-response-object target-post, res.api-render
			content =
				post: target-post
				user: user.filt!
			Notice.create config.web-client-id, 'favorite', JSON.stringify content, target-post.user-id, (notice) ->
				Streamer.publish 'userStream:' + target-post.user-id, JSON.stringify do
					type: 'notice'
					value: notice
{'fraction_non_alphanumeric': 0.1848652384243262, 'fraction_numerical': 0.006219765031098825, 'mean_word_length': 2.9068825910931175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '3371163', 'n_tokens_mistral': 1110, 'n_tokens_neox': 982, 'n_words': 224}