Example #1
0
 /**
  * Creates a new notification
  *
  * @param      Message       $message
  * @param      Conversation  $conversation
  */
 public static function make(Message $message, Conversation $conversation)
 {
     $notification = [];
     foreach ($conversation->users as $user) {
         $is_sender = $message->user_id == $user->id ? 1 : 0;
         $notification[] = ['user_id' => $user->id, 'message_id' => $message->id, 'conversation_id' => $conversation->id, 'is_seen' => $is_sender, 'is_sender' => $is_sender, 'created_at' => $message->created_at];
     }
     MessageNotification::insert($notification);
 }
Example #2
0
 /**
  * Creates an entry in the message_notification table for each participant
  * This will be used to determine if a message is read or deleted
  */
 public function createNotifications()
 {
     MessageNotification::make($this->message, $this->message->conversation);
 }
Example #3
0
 /**
  * Get recent user messages for each conversation
  *
  * @param int $userId
  *
  * @return Message
  */
 public function conversations($userId)
 {
     $c = ConversationUser::join('messages', 'messages.conversation_id', '=', 'conversation_user.conversation_id')->where('conversation_user.user_id', $userId)->groupBy('messages.conversation_id')->orderBy('messages.id', 'DESC')->get(['messages.*', 'messages.id as message_id', 'conversation_user.*']);
     $messages = [];
     foreach ($c as $user) {
         $recent_message = $user->conversation->messages()->orderBy('id', 'desc')->first()->toArray();
         $notification = MessageNotification::where('user_id', $userId)->where('message_id', $user->id)->get(['message_notification.id', 'message_notification.is_seen', 'message_notification.is_sender']);
         $messages[] = array_merge($recent_message, ['notification' => $notification]);
     }
     return $messages;
 }
Example #4
0
 public function conversationRead($conversationId, $userId)
 {
     return MessageNotification::where('user_id', $userId)->where('conversation_id', $conversationId)->update(['is_seen' => 1]);
 }
Example #5
0
 /**
  * marks message as read
  *
  * @param      integer  $messageId
  * @param      integer  $userId
  *
  * @return
  */
 public function messageRead($messageId, $userId)
 {
     return MessageNotification::where('user_id', $userId)->where('message_id', $messageId)->update(['is_seen' => 1]);
 }