允许用户每小时在Laravel应用程序中发布10个帖子



我有一个允许用户发布类似Facebook状态的网站,我不想只允许用户每小时发布多达10个帖子,如果用户试图在同一小时内发布超过该数量,我想用消息重定向用户一个小时只允许10个帖子

我的代码发布状态看起来像这样:

public function postStatus(Request $request, LaravelFacebookSdk $fb, $checked) 
{
    $checked = $checked;
    $twitterToken = Session::get('access_token');
    $token = Session::get('fb_user_access_token');
    if ($request->has('image_id')) {
        $preloaded = new CloudinaryPreloadedFile($_POST['image_id']);
        if (!$preloaded->is_valid()) {
          echo "Invalid upload signature";  //Add a route to page with sorry there was a problem uploading your image.      
        } else {
        //Save Photo URL and User ID to Photo model
        $photorequest = $preloaded->pmsFileName();
        $user = Auth::user();
        $photo = new Photo;
        $photo->image_identifier = $photorequest;
        $photo->user()->associate($user);
        $photo->save();
        $regPhoto = Photo::select('id')->where('image_identifier', $preloaded->pmsFileName())->first();
        $statusPhotoID = $regPhoto->id;
    }

    }else{
        $statusPhotoID = NULL;
    }

    $this->validate($request, [
        'player_url' => 'url|max:255',
        'body' => 'max:1000',
    ]);
    if ($request->has('player_url') || $request->has('body') || $request->has('image_id')) {
        Auth::user()->statuses()->create([
        'body' => $request->input('body'),
        'player_url' => $request->input('player_url'),
        'status_photo_id' => $statusPhotoID,
    ]);
    $userPage = ' http://example.com'; //Do something like {{ route('profile.index', [username = Auth::user()->username])}}
    $tweetLength = 140 - strlen($userPage);
        //Post to Facebook with Image
        if ($checked == 'checked' && isset($photorequest)) {
            try
            {
                $fb->setDefaultAccessToken($token);
                $fb->post('/me/photos', ['url' => cloudinary_url("$photorequest"),'caption' => $request->input('body') . $userPage]);
            }
            catch (FacebookExceptionsFacebookResponseException $e)
            {
                // continue to next request;
            }
            //Post to Fb without Image
        } elseif ($checked == 'checked' && !isset($photorequest)) {
            try
            {
                $fb->setDefaultAccessToken($token);
                $fb->post('/me/feed', ['message' => $request->input('body') . $userPage]);
            }
            catch (FacebookExceptionsFacebookResponseException $e)
            {
                // continue to next request;
            }
        }
        //Post to Twitter with Image
        if($twitterToken && isset($photorequest)) {
            try
            {
                // Twitter::postTweet(['status' => str_limit($request->input('body') . ' SITE',137), 'format' => 'json']);
                $uploaded_media = Twitter::uploadMedia(['media' => file_get_contents(cloudinary_url("$photorequest"))]);
                Twitter::postTweet(['status' => str_limit($request->input('body') . $userPage,$tweetLength), 'media_ids' => $uploaded_media->media_id_string]);
            }
            catch (Exception $e)
            {
                // continue to next request;
            }
            //Post to twitter without image
        } elseif ($twitterToken && !isset($photorequest)) {
            Twitter::postTweet(['status' => str_limit($request->input('body') . $userPage,$tweetLength), 'format' => 'json']);
        }
        return redirect()->back()->with('info', 'Status Posted.');
    }
        return redirect()->back()->with('info', 'Error Occured, Nothing Posted.');
}

我的表看起来像:

| id | user_id | parent_id | body | created_at | updated_at |

在我的created_at中我使用timestamp()

实现我想要做的事情的最佳选择是什么?

仅取当前时间戳并扣除一个小时的秒值。然后,您可以通过添加where子句(当然是伪代码)来获取行数

SELECT COUNT(id) FROM posts WHERE created_at >= $timestamp

您所要做的就是检查最近一个小时内创建的帖子数量是否少于10。

相关内容

  • 没有找到相关文章

最新更新