Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/ChatifyMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getMaxUploadSize()
return config('chatify.attachments.max_upload_size') * 1048576;
}


public function __construct()
{
$this->pusher = new Pusher(
Expand Down Expand Up @@ -150,7 +151,22 @@ public function parseMessage($prefetchedMessage = null, $id = null)
$attachment = $attachmentOBJ->new_name;
$attachment_title = htmlentities(trim($attachmentOBJ->old_name), ENT_QUOTES, 'UTF-8');
$ext = pathinfo($attachment, PATHINFO_EXTENSION);
$attachment_type = in_array($ext, $this->getAllowedImages()) ? 'image' : 'file';
if (isset($msg->attachment)) {
$attachmentOBJ = json_decode($msg->attachment);
$attachment = $attachmentOBJ->new_name;
$attachment_title = htmlentities(trim($attachmentOBJ->old_name), ENT_QUOTES, 'UTF-8');
$ext = pathinfo($attachment, PATHINFO_EXTENSION);
//
if( in_array($ext, $this->getAllowedImages())) {
$attachment_type = 'image';
}else if(in_array($ext, $this->getAllowedVoiceMessages())){
$attachment_type = 'audio';
}else{

$attachment_type = 'file';
}

}
}
return [
'id' => $msg->id,
Expand Down Expand Up @@ -442,4 +458,16 @@ public function getAttachmentUrl($attachment_name)
{
return self::storage()->url(config('chatify.attachments.folder') . '/' . $attachment_name);
}


/**
* This method returns the allowed voice messages extensions
*
* @return array
*/
public function getAllowedVoiceMessages()
{
return config('chatify.attachments.allowed_voice_messages');
}

}
204 changes: 143 additions & 61 deletions src/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,77 +88,159 @@ public function download($fileName)
return abort(404, "Sorry, File does not exist in our server or may have been deleted!");
}

/**
* Send a message to database
*
* @param Request $request
* @return JsonResponse
*/
public function send(Request $request)
{
// default variables
$error = (object)[
'status' => 0,
'message' => null
];
$attachment = null;
$attachment_title = null;
/**
* Send a message to database
*
*
* @param Request $request
* @return JsonResponse
*/
public function send(Request $request)
{
// Initialize error object and attachment variables
$error = (object) [
'status' => 0,
'message' => null
];
$attachment = null;
$attachment_title = null;
$isVoiceMessage = $request->hasFile('audio');

// Process the message based on its type
if ($isVoiceMessage) {
$this->handleVoiceMessage($request, $error, $attachment, $attachment_title);
} else {
$this->handleTextOrFileMessage($request, $error, $attachment, $attachment_title);
}

// if there is attachment [file]
if ($request->hasFile('file')) {
// allowed extensions
$allowed_images = Chatify::getAllowedImages();
$allowed_files = Chatify::getAllowedFiles();
$allowed = array_merge($allowed_images, $allowed_files);
// If no error occurred, create the message and push notification
if (!$error->status) {
$message = $this->createMessage($request, $isVoiceMessage, $attachment, $attachment_title);
$messageData = Chatify::parseMessage($message);
$this->pushNotification($request, $messageData);
}

$file = $request->file('file');
// check file size
if ($file->getSize() < Chatify::getMaxUploadSize()) {
if (in_array(strtolower($file->extension()), $allowed)) {
// get attachment name
$attachment_title = $file->getClientOriginalName();
// upload attachment and store the new name
$attachment = Str::uuid() . "." . $file->extension();
$file->storeAs(config('chatify.attachments.folder'), $attachment, config('chatify.storage_disk_name'));
} else {
$error->status = 1;
$error->message = "File extension not allowed!";
}
// Return JSON response
return Response::json([
'status' => '200',
'error' => $error,
'message' => Chatify::messageCard(@$messageData),
'tempID' => $request['temporaryMsgId'],
]);
}

/**
* Handle Voice Message

*
* @param Request $request
* @param object $error
* @param string $attachment
* @param string $attachment_title
* @return void
*/
private function handleVoiceMessage(Request $request, &$error, &$attachment, &$attachment_title)
{
$allowed_voice_messages = Chatify::getAllowedVoiceMessages();

if ($request->hasFile('audio')) {
$file = $request->file('audio');
if ($file->getSize() < Chatify::getMaxUploadSize()) {
if (in_array(strtolower($file->extension()), $allowed_voice_messages)) {
$this->storeAttachment($file, $attachment, $attachment_title);
} else {
$error->status = 1;
$error->message = "File size you are trying to upload is too large!";
$this->setError($error, "File extension not allowed!");
}
} else {
$this->setError($error, "File size you are trying to upload is too large!");
}
}
}

if (!$error->status) {
$message = Chatify::newMessage([
'from_id' => Auth::user()->id,
'to_id' => $request['id'],
'body' => htmlentities(trim($request['message']), ENT_QUOTES, 'UTF-8'),
'attachment' => ($attachment) ? json_encode((object)[
'new_name' => $attachment,
'old_name' => htmlentities(trim($attachment_title), ENT_QUOTES, 'UTF-8'),
]) : null,
]);
$messageData = Chatify::parseMessage($message);
if (Auth::user()->id != $request['id']) {
Chatify::push("private-chatify.".$request['id'], 'messaging', [
'from_id' => Auth::user()->id,
'to_id' => $request['id'],
'message' => Chatify::messageCard($messageData, true)
]);
/**
* Handle Text or File Message
* @param Request $request
* @param object $error
* @param string $attachment
* @param string $attachment_title
* @return void
*/
private function handleTextOrFileMessage(Request $request, &$error, &$attachment, &$attachment_title)
{
if ($request->hasFile('file')) {
$allowed = array_merge(Chatify::getAllowedImages(), Chatify::getAllowedFiles());
$file = $request->file('file');

if ($file->getSize() < Chatify::getMaxUploadSize()) {
if (in_array(strtolower($file->extension()), $allowed)) {
$this->storeAttachment($file, $attachment, $attachment_title);
} else {
$this->setError($error, "File extension not allowed!");
}
} else {
$this->setError($error, "File size you are trying to upload is too large!");
}

// send the response
return Response::json([
'status' => '200',
'error' => $error,
'message' => Chatify::messageCard(@$messageData),
'tempID' => $request['temporaryMsgId'],
]);
}
}

/**
* Store Attachment
*
* @param UploadedFile $file
* @param string $attachment
* @param string $attachment_title
* @return void
*/
private function storeAttachment($file, &$attachment, &$attachment_title)
{
$attachment = Str::uuid() . "." . $file->getClientOriginalExtension();
$attachment_title = $file->getClientOriginalName();
$file->storeAs(config('chatify.attachments.folder'), $attachment, config('chatify.storage_disk_name'));
}

/**
* Set Error
* @param object $error
* @param string $message
* @return void
*/
private function setError(&$error, $message)
{
$error->status = 1;
$error->message = $message;
}

/**
* Create Message
*
* @param Request $request
* @param bool $isVoiceMessage
* @param string $attachment
* @param string $attachment_title
* @return mixed
*/
private function createMessage(Request $request, $isVoiceMessage, $attachment, $attachment_title)
{
return Chatify::newMessage([
'from_id' => Auth::user()->id,
'to_id' => $request->input('id'),
'body' => $isVoiceMessage ? '' : htmlentities(trim($request['message']), ENT_QUOTES, 'UTF-8'),
'attachment' => ($attachment) ? json_encode((object) [
'new_name' => $attachment,
'old_name' => htmlentities(trim($attachment_title), ENT_QUOTES, 'UTF-8'),
]) : null,
]);
}
private function pushNotification(Request $request, $messageData)
{
if (Auth::user()->id != $request->input('id')) {
Chatify::push("private-chatify." . $request->input('id'), 'messaging', [
'from_id' => Auth::user()->id,
'to_id' => $request->input('id'),
'message' => Chatify::messageCard($messageData, true)
]);
}
}
/**
* fetch [user/group] messages from database
*
Expand Down
Loading