コード例 #1
0
 /**
  * Bug list:
  * Chat wont automatically update if it is the first message
  *
  * Improvements:
  * Update the time for all messages repeatedly
  */
 public function create(Request $request)
 {
     $this->validate($request, ['body' => 'required|max:200']);
     do {
         $key = str_random(32);
         $chat_key = Chat::where('key', $key)->first();
     } while (!empty($chat_key));
     if (!Auth::check()) {
         return $response->json(json_encode(['auth' => false]));
     }
     Chat::create(['body' => $request->input('body'), 'user_id' => Auth::user()->id, 'key' => $key]);
     return;
 }
コード例 #2
0
 public function postRefresh(Request $request)
 {
     // check user auth and if has permission in chat room
     // If it is looking for new messages or old messages
     $new = $request->input('new');
     // Get the latest post of the user
     $current_post_id = Chat::where('key', $request->input('msg_key'))->first()->id;
     // Get ID of that latest post from server
     $latest_post_id = Chat::orderBy('id', 'desc')->first()->id;
     // Preparation of response messages
     $response_posts = [];
     // Loop to fill the response array with proper data, to not include it all
     if ($latest_post_id > $current_post_id) {
         $latest_posts = Chat::where('id', '>', $current_post_id)->get();
         foreach ($latest_posts as $post) {
             $response_posts[$post->id] = ['body' => $post->body, 'key' => $post->key, 'human_date' => $post->created_at->diffForHumans(), 'user' => ['name' => $post->user->username, 'avatar' => $post->user->getAvatar()]];
         }
     }
     $json = ['data' => $response_posts];
     return response()->json(json_encode($json));
     // posts in JSON, that is then coded by JS
 }