예제 #1
0
 public function createConversation(Request $request)
 {
     $target = User::name(request('username'))->firstOrFail();
     if ($target->getKey() == auth()->id()) {
         return redirect()->action('ConversationController@showCreateForm')->withInput()->with('danger_msg', 'Ekhm... wysyłanie wiadomości do samego siebie chyba nie ma sensu ;)');
     }
     if ($target->isBlockingUser(user())) {
         return redirect()->action('ConversationController@showCreateForm')->withInput()->with('danger_msg', 'Zostałeś zablokowany przez wybranego użytkownika.');
     }
     $this->validate($request, ['text' => 'required|max:10000']);
     $conversation = Conversation::withUser(auth()->id())->withUser($target->getKey())->first();
     if (!$conversation) {
         $conversation = Conversation::create([]);
         $conversation->users()->attach([auth()->id(), $target->getKey()]);
     } else {
         $conversation->notifications()->where('user_id', $target->getKey())->delete();
     }
     $conversation->messages()->create(['user_id' => Auth::id(), 'text' => $request->input('text')]);
     return redirect()->to('/conversations');
 }