<commit_before><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 <NAME>ang Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\API2;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Zhiyi\Plus\Services\Push;
use Zhiyi\Plus\Http\Controllers\Controller;
use Zhiyi\Plus\Models\Comment as CommentModel;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed as FeedModel;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\FormRequest\API2\StoreFeedComment as CommentFormRequest;

class FeedCommentController extends Controller
{
    /**
     * List comments of the feed.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function index(Request $request, ResponseFactoryContract $response, FeedModel $feed)
    {
        $limit = $request->query('limit', 15);
        $after = $request->query('after', false);

        $comments = $feed->comments()
            ->when($after, function ($query) use ($after) {
                return $query->where('id', '<', $after);
            })
            ->with([
                'user' => function ($query) {
                    return $query->withTrashed();
                },
                'reply',
            ])
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        return $response->json([
            'pinneds' => ! $after ? app()->call([$this, 'pinneds'], ['feed' => $feed]) : [],
            'comments' => $comments,
        ])->setStatusCode(200);
    }

    public function pinneds(Request $request, Carbon $dateTime, FeedModel $feed)
    {
        if ($request->query('after')) {
            return [];
        }

        return $feed->pinnedComments()
            ->with(['user' => function ($query) {
                return $query->withTrashed();
            }, 'reply'])
            ->where('expires_at', '>', $dateTime)
            ->get();
    }

    /**
     * Get a comment.
     *
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param mixed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function show(ResponseFactoryContract $response, $feed, CommentModel $comment)
    {
        unset($feed);

        return $response->json($comment, 200);
    }

    /**
     * destroy the comment.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function destroy(Request $request, ResponseFactoryContract $response, FeedModel $feed, CommentModel $comment)
    {
        $user = $request->user();
        if ($comment->user_id !== $user->id) {
            return $response->json(['message' => ['没有权限']], 403);
        }

        $feed->getConnection()->transaction(function () use ($user, $feed, $comment) {
            $feed->decrement('feed_comment_count', 1);
            $user->extra()->decrement('comments_count', 1);
            $comment->delete();
        });

        return $response->make('', 204);
    }

    /**
     * Send comment of the feed.
     *
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\FormRequest\API2\StoreFeedComment $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function store(CommentFormRequest $request,
                          ResponseFactoryContract $response,
                          FeedModel $feed,
                          CommentModel $comment)
    {
        $replyUser = intval($request->input('reply_user', 0));
        $body = $request->input('body');<commit_msg>chore: 增加动态评论黑名单筛选<commit_after><?php

declare(strict_types=1);

/*
 * +----------------------------------------------------------------------+
 * |                          ThinkSNS Plus                               |
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2017 Chengdu ZhiYiChuangXiang Technology Co., Ltd.     |
 * +----------------------------------------------------------------------+
 * | This source file is subject to version 2.0 of the Apache license,    |
 * | that is bundled with this package in the file LICENSE, and is        |
 * | available through the world-wide-web at the following url:           |
 * | http://www.apache.org/licenses/LICENSE-2.0.html                      |
 * +----------------------------------------------------------------------+
 * | Author: <NAME> <<EMAIL>>                          |
 * | Homepage: www.thinksns.com                                           |
 * +----------------------------------------------------------------------+
 */

namespace Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\API2;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Zhiyi\Plus\Services\Push;
use Zhiyi\Plus\Http\Controllers\Controller;
use Zhiyi\Plus\Models\Comment as CommentModel;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed as FeedModel;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\FormRequest\API2\StoreFeedComment as CommentFormRequest;

class FeedCommentController extends Controller
{
    /**
     * List comments of the feed.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function index(Request $request, ResponseFactoryContract $response, FeedModel $feed)
    {
        $user = $request->user('api')->id ?? 0;
        $limit = $request->query('limit', 15);
        $after = $request->query('after', false);

        $comments = $feed->comments()
            ->whereDoesntHave('blacks', function ($query) use ($user) {
                $query->where('user_id', $user);
            })
            ->when($after, function ($query) use ($after) {
                return $query->where('id', '<', $after);
            })
            ->with([
                'user' => function ($query) {
                    return $query->withTrashed();
                },
                'reply',
            ])
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        return $response->json([
            'pinneds' => ! $after ? app()->call([$this, 'pinneds'], ['feed' => $feed]) : [],
            'comments' => $comments,
        ])->setStatusCode(200);
    }

    public function pinneds(Request $request, Carbon $dateTime, FeedModel $feed)
    {
        if ($request->query('after')) {
            return [];
        }

        return $feed->pinnedComments()
            ->with(['user' => function ($query) {
                return $query->withTrashed();
            }, 'reply'])
            ->where('expires_at', '>', $dateTime)
            ->get();
    }

    /**
     * Get a comment.
     *
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param mixed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function show(ResponseFactoryContract $response, $feed, CommentModel $comment)
    {
        unset($feed);

        return $response->json($comment, 200);
    }

    /**
     * destroy the comment.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function destroy(Request $request, ResponseFactoryContract $response, FeedModel $feed, CommentModel $comment)
    {
        $user = $request->user();
        if ($comment->user_id !== $user->id) {
            return $response->json(['message' => ['没有权限']], 403);
        }

        $feed->getConnection()->transaction(function () use ($user, $feed, $comment) {
            $feed->decrement('feed_comment_count', 1);
            $user->extra()->decrement('comments_count', 1);
            $comment->delete();
        });

        return $response->make('', 204);
    }

    /**
     * Send comment of the feed.
     *
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\FormRequest\API2\StoreFeedComment $request
     * @param \Illuminate\Contracts\Routing\ResponseFactory $response
     * @param \Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed $feed
     * @param \Zhiyi\Plus\Models\Comment $comment
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function store(
        CommentFormRequest $request,
        ResponseFactoryContract $response,
        FeedModel $feed,
        CommentModel $comment
    ) {
        $replyUser = intval($request->input('reply_user', 0));
        $body = $request->input('body');
{'fraction_non_alphanumeric': 0.20144844673146559, 'fraction_numerical': 0.005622260339241471, 'mean_word_length': 1.7742532381707639, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018265', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2823, 'n_words': 578}