Make an email in talk space (/:roomId/message) [POST request]

Make an email in talk space (/:roomId/message) [POST request]

A factor to notice here’s userIds, means is coming from req.body while userId that will be are aliased as chatInitiatorId comes from req as a consequence of our very own decode middleware.

In the event that you recall, we affixed app.use(«/room», decode, chatRoomRouter); in our server/index.js document. This implies this path /room/initiate try authenticated. So const < userId:>= req; is the id of the existing user logged in.

We simply name the initiateChat approach from ChatRoomModel and move they allUserIds, type, chatInitiator . Whatever result comes we simply pass it to your individual.

However before we develop a note we have to write a design for our chatmessages . Thus let us do that first. Within versions folder generate an innovative new document known as ChatMessage.js and incorporate this amazing material to it:

  • We now have a MESSAGE_TYPES item that has only 1 means also known as book
  • Our company is identifying the schema for chatmessage and readByRecipient
  • Subsequently our company is creating our static method for createPostInChatRoom

I understand this is exactly most articles, but simply bear with me. Why don’t we simply create the control for the course that creates this content.

For any course defined inside our routes/chatRoom.js API (‘/:roomId/message’, chatRoom.postMessage) let’s check-out the operator in controllers/chatRoom.js and determine it:

  • $complement
  • $latest
  • $addToSet
  • $search
  • $unwind
  • $class

Discover conversation for a speak area because of it’s id [Get request]

For your path .get(‘/:roomId’, chatRoom.getConversationByRoomId) in routes/chatRoom.js available their controller in file controllers/chatRoom.js and add these information on speak space:

Immediately after which eventually, visit your ChatMessage design in models/ChatMessage.js and compose a fixed way called getConversationByRoomId :

Mark a complete dialogue as study (element comparable to WhatsApp)

Once the other person are signed in and see a conversation for an area id, we must mark that talk as review using their area.

All we are undertaking listed here is very first checking if place exists or otherwise not. If it does, we proceed further. We take-in the as currentLoggedUser and pass they toward preceding function:

A potential use case is the fact that the consumer may possibly not have see the latest 15 emails as soon as they open up a particular area conversation. They should all be noted as review. So we’re with the this.updateMany features by mongoose.

This says i do want to pick all content content when you look at the chatmessages collection where chatRoomId suits and readByRecipients selection does not. The userId that i will be driving for this features are currentUserOnlineId .

Next we would like to inform mongoose never to just update the very first record it discovers, but in addition to update every reports the spot where the disease fits. Very doing this:

Incentive Section

Get in touch with me on twitter along with your suggestions a€“ i’d love to listen to if you have any recommendations for advancements: twitter/adeelibr

Any time you liked to the article, be sure to perform allow the github repository a superstar and donate to my personal youtube station.

Learn to code for free. freeCodeCamp’s available origin curriculum possess assisted above 40,000 individuals bring employment as designers. Get started

The objective: to help individuals learn to code free of charge. We accomplish this by generating hundreds of videos, reports, and interactive coding lessons – all free towards people. We likewise have a large number of freeCodeCamp learn teams all over the world.

Inside root folder make a unique folder known as config . Inside that folder generate a file called list.js and create this amazing material:

Here we have been advising they that firstName are of means string. In the event that consumer forgets to provide this value while hitting the API, or if perhaps the kind is certainly not string, it will throw one.

We obtain the userId from our req.params . If you recall from the video clip previously, req.params is the /: explained within channels part.

We shall take that consumer id while the customer id (the consumer’s very own unique plug id that creates if they make a link with this feel).

We have been utilizing the make-validation collection here to verify the user’s demand. For any start API, we anticipate the consumer to deliver a range of users and define the type of the chat-room this is certainly being created.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *