forked from learnwithsumit/chat-application
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9e562c
commit c7a3316
Showing
20 changed files
with
6,894 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,205 @@ | ||
// external imports | ||
const createError = require("http-errors"); | ||
// internal imports | ||
const User = require("../models/People"); | ||
const Conversation = require("../models/Conversation"); | ||
const Message = require("../models/Message"); | ||
const escape = require("../utilities/escape"); | ||
|
||
// get inbox page | ||
function getInbox(req, res, next) { | ||
res.render("inbox"); | ||
async function getInbox(req, res, next) { | ||
try { | ||
const conversations = await Conversation.find({ | ||
$or: [ | ||
{ "creator.id": req.user.userid }, | ||
{ "participant.id": req.user.userid }, | ||
], | ||
}); | ||
res.locals.data = conversations; | ||
res.render("inbox"); | ||
} catch (err) { | ||
next(err); | ||
} | ||
} | ||
|
||
// search user | ||
async function searchUser(req, res, next) { | ||
const user = req.body.user; | ||
const searchQuery = user.replace("+88", ""); | ||
|
||
const name_search_regex = new RegExp(escape(searchQuery), "i"); | ||
const mobile_search_regex = new RegExp("^" + escape("+88" + searchQuery)); | ||
const email_search_regex = new RegExp("^" + escape(searchQuery) + "$", "i"); | ||
|
||
try { | ||
if (searchQuery !== "") { | ||
const users = await User.find( | ||
{ | ||
$or: [ | ||
{ | ||
name: name_search_regex, | ||
}, | ||
{ | ||
mobile: mobile_search_regex, | ||
}, | ||
{ | ||
email: email_search_regex, | ||
}, | ||
], | ||
}, | ||
"name avatar" | ||
); | ||
|
||
res.json(users); | ||
} else { | ||
throw createError("You must provide some text to search!"); | ||
} | ||
} catch (err) { | ||
res.status(500).json({ | ||
errors: { | ||
common: { | ||
msg: err.message, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
// add conversation | ||
async function addConversation(req, res, next) { | ||
try { | ||
const newConversation = new Conversation({ | ||
creator: { | ||
id: req.user.userid, | ||
name: req.user.username, | ||
avatar: req.user.avatar || null, | ||
}, | ||
participant: { | ||
name: req.body.participant, | ||
id: req.body.id, | ||
avatar: req.body.avatar || null, | ||
}, | ||
}); | ||
|
||
const result = await newConversation.save(); | ||
res.status(200).json({ | ||
message: "Conversation was added successfully!", | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
errors: { | ||
common: { | ||
msg: err.message, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
// get messages of a conversation | ||
async function getMessages(req, res, next) { | ||
try { | ||
const messages = await Message.find({ | ||
conversation_id: req.params.conversation_id, | ||
}).sort("-createdAt"); | ||
|
||
const { participant } = await Conversation.findById( | ||
req.params.conversation_id | ||
); | ||
|
||
res.status(200).json({ | ||
data: { | ||
messages: messages, | ||
participant, | ||
}, | ||
user: req.user.userid, | ||
conversation_id: req.params.conversation_id, | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
errors: { | ||
common: { | ||
msg: "Unknows error occured!", | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
// send new message | ||
async function sendMessage(req, res, next) { | ||
if (req.body.message || (req.files && req.files.length > 0)) { | ||
try { | ||
// save message text/attachment in database | ||
let attachments = null; | ||
|
||
if (req.files && req.files.length > 0) { | ||
attachments = []; | ||
|
||
req.files.forEach((file) => { | ||
attachments.push(file.filename); | ||
}); | ||
} | ||
|
||
const newMessage = new Message({ | ||
text: req.body.message, | ||
attachment: attachments, | ||
sender: { | ||
id: req.user.userid, | ||
name: req.user.username, | ||
avatar: req.user.avatar || null, | ||
}, | ||
receiver: { | ||
id: req.body.receiverId, | ||
name: req.body.receiverName, | ||
avatar: req.body.avatar || null, | ||
}, | ||
conversation_id: req.body.conversationId, | ||
}); | ||
|
||
const result = await newMessage.save(); | ||
|
||
// emit socket event | ||
global.io.emit("new_message", { | ||
message: { | ||
conversation_id: req.body.conversationId, | ||
sender: { | ||
id: req.user.userid, | ||
name: req.user.username, | ||
avatar: req.user.avatar || null, | ||
}, | ||
message: req.body.message, | ||
attachment: attachments, | ||
date_time: result.date_time, | ||
}, | ||
}); | ||
|
||
res.status(200).json({ | ||
message: "Successful!", | ||
data: result, | ||
}); | ||
} catch (err) { | ||
res.status(500).json({ | ||
errors: { | ||
common: { | ||
msg: err.message, | ||
}, | ||
}, | ||
}); | ||
} | ||
} else { | ||
res.status(500).json({ | ||
errors: { | ||
common: "message text or attachment is required!", | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getInbox, | ||
searchUser, | ||
addConversation, | ||
getMessages, | ||
sendMessage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const uploader = require("../../utilities/multipleUploader"); | ||
|
||
function attachmentUpload(req, res, next) { | ||
const upload = uploader( | ||
"attachments", | ||
["image/jpeg", "image/jpg", "image/png"], | ||
1000000, | ||
2, | ||
"Only .jpg, jpeg or .png format allowed!" | ||
); | ||
|
||
// call the middleware function | ||
upload.any()(req, res, (err) => { | ||
if (err) { | ||
res.status(500).json({ | ||
errors: { | ||
avatar: { | ||
msg: err.message, | ||
}, | ||
}, | ||
}); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = attachmentUpload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const conversationSchema = mongoose.Schema( | ||
{ | ||
creator: { | ||
id: mongoose.Types.ObjectId, | ||
name: String, | ||
avatar: String, | ||
}, | ||
|
||
participant: { | ||
id: mongoose.Types.ObjectId, | ||
name: String, | ||
avatar: String, | ||
}, | ||
last_updated: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
const Conversation = mongoose.model("Conversation", conversationSchema); | ||
|
||
module.exports = Conversation; |
Oops, something went wrong.