I am developing an application using Laravel and Vue JS which includes a chat feature also. When user sends a new message admin gets a notification. Now i want get that notification deleted when user deleting that particular message.
Can anyone tell me how can i do it? Thanks.
Here is my sendMessage function.
public function sendMessageMobile(int $id, Request $request): JsonResponse
{
$chat = Chat::find($id);
$inquiryId = $chat->inquiry_id;
$type = Message::GENERAL_MESSAGE;
$attachmentId = null;
$user = Auth::user();
if (empty($request->message)) {
if (!(count($request->get('files')) > 0)) {
return response()->json(['error' => 'cannot send empty messages'], 400);
}
}
if (count($request->get('files')) > 0) {
$response = $this->fileUploadController->uploadFilesMobile($request, $inquiryId);
$attachmentId = $response->getData()->attachment_id;
$type = Message::ATTACHMENT;
$mobileNotificationText = $user->full_name . ' Sent a File in Chat: ' . $chat->name;
} else {
if (strlen($request->message) > 100) {
$shortMessage = substr($request->message, 0, 100) . '...';
} else {
$shortMessage = $request->message;
}
$mobileNotificationText = $user->full_name . ' said "' . $shortMessage . '"in Chat: ' . $chat->name;
}
$data = [
'inquiry_id' => $inquiryId,
'chat_id' => $id,
'message' => $request->message,
'reply_id' => $request->reply_id,
'type' => $type,
'attachment_id' => $attachmentId,
'created_by' => $user->id,
];
$create = Message::create($data);
$chatEntity = new chatEntity();
$chatEntity->updateReadAtOnSend($id, $create->id);
$create->brand_name = $user->brand_name;
broadcast(new PrivateMessageSent($id, $create))->toOthers();
$inquiry = Inquiry::find($chat->inquiry_id);
$membersToNotify = $chat->members->where('id', '!=', $user->id);
foreach ($membersToNotify as $member) {
$datares = $this->getUnreadCountForChatBroadcast($id, $member->id);
broadcast(new UnreadCountChanged($id, $datares, $member->id))->toOthers();
}
if (strlen($request->message) > 100) {
$shortMessage = substr($request->message, 0, 100) . '...';
} else {
$shortMessage = $request->message;
}
Notification::send($membersToNotify, new UserNotification($user->full_name . ' said "' . $shortMessage . '" in Inquiry: ' . $inquiry->style_number . ', Chat: ' . $chat->name, 'inquiry/' . $inquiry->id, true, false, 'View Chat'));
$expo = \ExponentPhpSDK\Expo::normalSetup();
$channelName = $chat->id;
$interest = false;
foreach ($membersToNotify as $member) {
$notificationData = [
'message' => $mobileNotificationText,
'navigation' => 'Chat',
'inquiry' => $inquiry,
'chat' => $chat,
'user_id' => $member->id,
];
MobileNotification::create($notificationData);
if ($member->push_token != null) {
$expo->subscribe($channelName, $member->push_token);
$interest = true;
}
}
$notification = ['title' => $inquiry->style_number, 'body' => $mobileNotificationText, 'data' => json_encode(array('inquiry' => $inquiry))];
// Notify an interest with a notification
if ($interest == true) {
$expo->notify([$channelName], $notification);
}
Notification::send($membersToNotify, new UserNotification($user->full_name . ' said "' . $shortMessage . '" in Inquiry: ' . $inquiry->style_number . ', Chat: ' . $chat->name, 'inquiry/' . $inquiry->id, false));
if ($create) {
return response()->json([
'status' => true,
'message' => 'Success Input Message',
'data' => $create
]);
}
return response()->json([
'status' => false,
'message' => 'Failed, Something went wrong',
]);
}
Here is my removeMessage function.
public function removeMessage($id): JsonResponse
{
$user = Auth::user();
$roleId = $user->role_id;
$roleName = Roles::where('id', $roleId)->value('name');
$createdBy = Message::where('id', $id)->value('created_by');
$chatId = Message::where('id', $id)->value('chat_id');
if ($roleName === Roles::ACCOUNT_MANAGER || (($roleName === Roles::SUPPLIER || $roleName === Roles::CUSTOMER) && $createdBy === $user->id)) {
$message = Message::findOrFail($id);
if ($message->delete()) {
$pins = PinMessage::where('message_id', $message->id)->get();
foreach ($pins as $pin) {
$pin->delete();
}
$data = [
'message_id' => $message->id,
'created_by' => $user->id,
'chat_id' => $chatId
];
broadcast(new PrivateMessageDelete($chatId, $data))->toOthers();
return response()->json([
"success" => true,
"message" => "message deleted successfully.",
"data" => $message->id
]);
}
}
return response()->json([
"success" => false,
"message" => "you cant delete this message."
]);
}