Tipask一个开源社区代码设计的学习记录(三)评论

image.png
DROP TABLE IF EXISTS `community_comments`;
CREATE TABLE `community_comments`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(10) UNSIGNED NOT NULL,
  `content` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `source_id` int(10) UNSIGNED NOT NULL,
  `source_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `to_user_id` int(10) UNSIGNED NULL DEFAULT NULL,
  `status` tinyint(4) NOT NULL DEFAULT 1,
  `supports` int(11) NOT NULL DEFAULT 0,
  `device` tinyint(4) NOT NULL DEFAULT 1,
  `created_at` timestamp(0) NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp(0) NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `comments_source_id_source_type_index`(`source_id`, `source_type`) USING BTREE,
  INDEX `comments_user_id_index`(`user_id`) USING BTREE,
  INDEX `comments_created_at_index`(`created_at`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 141 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

评论关联用户信息表

public function toUser(){
        return $this->belongsTo('App\Models\User','to_user_id');
    }

评论的监听和删除

public static function boot()
    {
        parent::boot();

        /*监听创建*/
        static::creating(function($comment){
            /*开启状态检查*/
            if(Setting()->get('verify_comment')==1){
                $comment->status = 0;
            }
        });

        /*监听删除事件*/
        static::deleting(function($comment){
            /*问题、回答、文章评论数 -1*/
            $comment->source()->where("comments",">",0)->decrement('comments');
        });
    }

评论的创建过程

 /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,$this->validateRules);

        $source_type = $request->input('source_type');
        $source_id = $request->input('source_id');
        if($source_type === 'question'){
            $source  = Question::find($source_id);
            $notify_subject = $source->title;
            $notify_type = 'comment_question';
            $notify_refer_type = 'question';
            $notify_refer_id = 0;
        }else if($source_type === 'answer'){
            $source = Answer::find($source_id);
            $notify_subject = $source->content;
            $notify_type = 'comment_answer';
            $notify_refer_type = 'answer';
            $notify_refer_id = $source->question_id;

        }else if($source_type === 'article'){
            $source = Article::find($source_id);
            $notify_subject = $source->title;
            $notify_type = 'comment_article';
            $notify_refer_type = 'article';
            $notify_refer_id = 0;
        }
        if(!$source){
            abort(404);
        }


        $data = [
            'user_id'     => $request->user()->id,
            'content'     => $request->input('content'),
            'source_id'   => $source_id,
            'source_type' => get_class($source),
            'to_user_id'  => $request->input('to_user_id'),
            'status'      => 1,
            'supports'    => 0
        ];


        $comment = Comment::create($data);
        /*问题、回答、文章评论数+1*/
        $comment->source()->increment('comments');

        if( $comment->to_user_id > 0 ){
            $this->notify($request->user()->id,$comment->to_user_id,'reply_comment',$notify_subject,$source_id,$comment->content,$notify_refer_type,$notify_refer_id);

        }else{
            $this->notify($request->user()->id,$source->user_id,$notify_type,$notify_subject,$source_id,$comment->content,$notify_refer_type,$notify_refer_id);
        }

        if (!empty($request->user())) {
            $ip = $request->getClientIp();
            $user_id = MongoUsers::get_id_by_csp_user($request->user());
            Log::info('comment_5_'.$user_id);
            Credits::updateUserCredit($user_id,'comment', $source_id, 5,'',$ip);
        }
        return view('theme::comment.item')->with('comment',$comment)
                                            ->with('source_type',$source_type)
                                            ->with('source_id',$source_id);
    }

你可能感兴趣的:(Tipask一个开源社区代码设计的学习记录(三)评论)