unknown 2 rokov pred
rodič
commit
e27c78668f
7 zmenil súbory, kde vykonal 25 pridanie a 5 odobranie
  1. 2 2
      app.js
  2. 16 0
      controllers/chats.js
  3. 1 1
      controllers/user.js
  4. 4 0
      model/schemas/chat.js
  5. 1 1
      package-lock.json
  6. 0 1
      package.json
  7. 1 0
      routes/chats.js

+ 2 - 2
app.js

@@ -1,4 +1,5 @@
 const express = require('express');
+const app = express();
 const path = require('path');
 const logger = require('morgan');
 const cors = require('cors');
@@ -7,10 +8,9 @@ const chatsRouter = require('./routes/chats');
 const messagesRouter = require('./routes/messages');
 const contactsRouter = require('./routes/contacts');
 const userRoute = require('./routes/user');
-const app = express();
 const DIR_STATIC = process.env.DIR_STATIC;
-
 const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
+
 app.use(helmet());
 app.use(logger(formatsLogger));
 app.use(cors());

+ 16 - 0
controllers/chats.js

@@ -189,6 +189,21 @@ const sortChat = async (req, res, next) => {
 	}
 };
 
+const socketIdChat = async (req, res, next) => {
+	try {
+		const { socketId } = req.body;
+		const userId = req.user.id;
+		await ChatModel.updateCompanionsChat(userId, { socketId });
+		return res.status(200).json({
+			status: 'success',
+			code: 200,
+			data: {},
+		});
+	} catch (e) {
+		next(e);
+	}
+};
+
 const seenChat = async (req, res, next) => {
 	try {
 		const id = req.body.id;
@@ -278,6 +293,7 @@ module.exports = {
 	removeChatForBoth,
 	muteChat,
 	sortChat,
+	socketIdChat,
 	seenChat,
 	pinChat,
 	typingChat,

+ 1 - 1
controllers/user.js

@@ -4,7 +4,6 @@ const MessageModel = require('../model/message');
 const ContactModel = require('../model/contact');
 const fs = require('fs').promises;
 const Jimp = require('jimp');
-const path = require('path');
 const jwt = require('jsonwebtoken');
 const createFolderIsExist = require('../helpers/create-directory');
 require('dotenv').config();
@@ -189,6 +188,7 @@ const updateUser = async (req, res, next) => {
 	try {
 		const { id } = req.user;
 		await UserModel.updateUser(id, req.body);
+		const socketId = req.body.socketId;
 		return res.status(200).json({
 			data: {},
 		});

+ 4 - 0
model/schemas/chat.js

@@ -91,6 +91,10 @@ const chatSchema = new Schema(
 			type: Boolean,
 			default: false,
 		},
+		socketId: {
+			type: String,
+			default: false,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',

+ 1 - 1
package-lock.json

@@ -30,7 +30,7 @@
 				"passport": "0.4.1",
 				"passport-jwt": "4.0.0",
 				"shortid": "^2.2.16",
-				"socket.io": "4.0.0",
+				"socket.io": "^4.0.0",
 				"twilio": "^3.73.1"
 			},
 			"devDependencies": {

+ 0 - 1
package.json

@@ -35,7 +35,6 @@
 		"passport": "0.4.1",
 		"passport-jwt": "4.0.0",
 		"shortid": "^2.2.16",
-		"socket.io": "4.0.0",
 		"twilio": "^3.73.1"
 	},
 	"devDependencies": {

+ 1 - 0
routes/chats.js

@@ -9,6 +9,7 @@ router
 	.get('/:companionId', guard, controllers.getChatById)
 	.post('/', guard, validation.startChat, controllers.startChat)
 	.patch('/mute/', guard, controllers.muteChat)
+	.patch('/socketId/', guard, controllers.socketIdChat)
 	.patch('/sort/', guard, controllers.sortChat)
 	.patch('/seen/', guard, controllers.seenChat)
 	.patch('/typing/', guard, validation.typingChat, controllers.typingChat)