Browse Source

redy to deploy on heroku

unknown 2 years ago
parent
commit
66806e4d5b
3 changed files with 136 additions and 17 deletions
  1. 15 17
      app.js
  2. 13 0
      model/chat.js
  3. 108 0
      model/schemas/chat.js

+ 15 - 17
app.js

@@ -24,23 +24,21 @@ const io = new Server(server, {
 io.on('connection', (socket) => {
 	console.log(socket.id);
 	socket.emit('me', socket.id);
-	socket.on(
-		'call',
-		async ({ socketId, signalData, from, userId, companionId }) => {
-			const { name, lastName } = await ChatModel.getByField(
-				userId,
-				companionId
-			);
-			io.to(socketId).emit('call', {
-				signal: signalData,
-				from,
-				name,
-				lastName,
-			});
-		}
-	);
-	socket.on('answer', (data) => {
-		io.to(data.to).emit('accepted', { signal: data.signal });
+	socket.on('callTo', async ({ to, signalData, from, userId, companionId }) => {
+		const { name, lastName, color, avatarUrl, number } =
+			await ChatModel.getByField(userId, companionId);
+		io.to(to).emit('incomeCall', {
+			signal: signalData,
+			from,
+			name,
+			lastName,
+			color,
+			avatarUrl,
+			number,
+		});
+	});
+	socket.on('answerCall', (data) => {
+		io.to(data.to).emit('acceptedCall', { signal: data.signal });
 	});
 });
 

+ 13 - 0
model/chat.js

@@ -0,0 +1,13 @@
+const Chat = require('./schemas/chat');
+
+const getByField = async (companionId, userId) => {
+	const chat = await Chat.findOne({
+		companionId,
+		owner: userId,
+	});
+	return chat;
+};
+
+module.exports = {
+	getByField,
+};

+ 108 - 0
model/schemas/chat.js

@@ -0,0 +1,108 @@
+const { boolean } = require('joi');
+const mongoose = require('mongoose');
+const { Schema, model, SchemaTypes } = mongoose;
+const mongoosePaginate = require('mongoose-paginate-v2');
+
+mongoose.Types.ObjectId.isValid();
+
+const chatSchema = new Schema(
+	{
+		companionId: {
+			type: String,
+		},
+		name: {
+			type: String,
+			default: null,
+		},
+		lastName: {
+			type: String,
+			default: null,
+		},
+		originalName: {
+			type: String,
+			default: null,
+		},
+		originalLastName: {
+			type: String,
+			default: null,
+		},
+		avatarUrl: {
+			type: String,
+			default: null,
+		},
+		avatarsArr: {
+			type: Array,
+			default: [],
+		},
+		color: {
+			type: String,
+			default: null,
+		},
+		online: {
+			type: String,
+			default: null,
+		},
+		lastMessage: {
+			type: String,
+			default: null,
+		},
+		lastMessageCreatedAt: {
+			type: String,
+			default: null,
+		},
+		mute: {
+			type: Boolean,
+			default: false,
+		},
+		seen: {
+			type: Number,
+			default: 0,
+		},
+		seenCompanion: {
+			type: Number,
+			default: 0,
+		},
+		sort: {
+			type: Boolean,
+			default: false,
+		},
+		total: {
+			type: Number,
+			default: 0,
+		},
+		watched: {
+			type: Boolean,
+			default: false,
+		},
+		typing: {
+			type: Boolean,
+			default: false,
+		},
+		number: {
+			type: String,
+			min: 8,
+			max: 14,
+		},
+		country: {
+			type: String,
+			default: false,
+		},
+		pinned: {
+			type: Boolean,
+			default: false,
+		},
+		socketId: {
+			type: String,
+			default: false,
+		},
+		owner: {
+			type: SchemaTypes.ObjectId,
+			ref: 'user',
+		},
+	},
+	{ timestamps: true }
+);
+chatSchema.plugin(mongoosePaginate);
+const Chat = model('chat', chatSchema);
+
+module.exports = Chat;