Przeglądaj źródła

add status online

unknown 3 lat temu
rodzic
commit
f048b12fd6

+ 16 - 6
controllers/chats.js

@@ -25,35 +25,44 @@ const startChat = async (req, res, next) => {
 		const companion = await UserModel.findById(id);
 		const isUser = await ChatModel.getByField(id, userId);
 		const isCompanion = await ChatModel.getByField(userId, id);
-		const { name, lastName, avatarUrl, color } = companion;
+		const { name, lastName, avatarUrl, color, online } = companion;
 		const {
 			name: Name,
 			lastName: LastName,
 			avatarUrl: AvatarUrl,
 			color: Color,
+			online: Online,
 		} = user;
 		if (companion && (isUser || isCompanion)) {
-			await ChatModel.update(isUser._id, { name, lastName, avatarUrl, color });
+			await ChatModel.update(isUser._id, {
+				name,
+				lastName,
+				avatarUrl,
+				color,
+				online,
+			});
 			await ChatModel.update(isCompanion._id, {
 				name: Name,
 				lastName: LastName,
 				avatarUrl: AvatarUrl,
 				color: Color,
+				online: Online,
 			});
-			const updatedUser = await ChatModel.getByField(id, userId);
+			const updatedChat = await ChatModel.getByField(id, userId);
 			return res.status(200).json({
 				status: 'success',
 				code: 200,
-				data: updatedUser,
+				data: updatedChat,
 			});
 		}
 
 		if (companion && !isUser && !isCompanion) {
-			const updatedUser = await ChatModel.add({
+			const newChat = await ChatModel.add({
 				name,
 				lastName,
 				avatarUrl,
 				color,
+				online,
 				companionId: id,
 				owner: userId,
 			});
@@ -62,13 +71,14 @@ const startChat = async (req, res, next) => {
 				lastName: LastName,
 				avatarUrl: AvatarUrl,
 				color: Color,
+				online: Online,
 				companionId: userId,
 				owner: id,
 			});
 			return res.status(201).json({
 				status: 'success',
 				code: 201,
-				data: updatedUser,
+				data: newChat,
 			});
 		}
 	} catch (e) {

+ 0 - 1
controllers/messages.js

@@ -23,7 +23,6 @@ const listMessagesById = async (req, res, next) => {
 		const userId = req.user.id;
 		const companionId = req.params.companionId;
 		const chats = await MessageModel.getListById(companionId, userId);
-
 		return res.json({
 			status: 'success',
 			code: 200,

+ 2 - 3
controllers/user.js

@@ -52,8 +52,7 @@ const logIn = async (req, res, next) => {
 		const token = jwt.sign(payload, SECRET_KEY, { expiresIn: '24h' });
 		let registered = true;
 		if (!user.name || !user.lastName || !user.avatarUrl) registered = false;
-		await UserModel.updateToken(id, token);
-		await UserModel.updateCode(id, '');
+		await UserModel.updateUser(id, { token, code: '', online: true });
 		return res.status(200).json({
 			status: 'success',
 			code: 200,
@@ -78,7 +77,7 @@ const logOut = async (req, res, next) => {
 				data: 'UNAUTHORIZED',
 				message: 'Invalid credentials',
 			});
-		await UserModel.updateToken(id, null);
+		await UserModel.updateUser(id, { token: null, online: new Date() });
 		return res.status(204).json({});
 	} catch (e) {
 		next(e);

BIN
images/62273c1ceff0054430e7fa67/monkey.png


BIN
images/622759a73cd51752d44043a7/monkey.png


BIN
images/62275a0b3cd51752d44043a8/telegram.png


BIN
images/62275dbc1f60894d30cce0c9/monkey.png


BIN
images/62275e151f60894d30cce0ca/telegram.png


+ 4 - 0
model/schemas/chat.js

@@ -25,6 +25,10 @@ const chatSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		online: {
+			type: String,
+			default: null,
+		},
 		owner: {
 			type: SchemaTypes.ObjectId,
 			ref: 'user',

+ 4 - 0
model/schemas/user.js

@@ -41,6 +41,10 @@ const userSchema = new Schema(
 			type: String,
 			default: null,
 		},
+		online: {
+			type: String,
+			default: null,
+		},
 	},
 	{ timestamps: true }
 );

+ 5 - 1
model/user.js

@@ -16,8 +16,11 @@ const updateCode = async (id, code) => {
 const updateToken = async (id, token) => {
 	return await User.updateOne({ _id: id }, { token });
 };
+const updateUser = async (id, obj) => {
+	return await User.updateOne({ _id: id }, { ...obj });
+};
 const updateCredentials = async (id, body) => {
-	return await User.findByIdAndUpdate({ _id: id }, {...body}, { new: true });
+	return await User.findByIdAndUpdate({ _id: id }, { ...body }, { new: true });
 };
 const updateAvatar = async (id, avatarUrl) => {
 	return await User.updateOne({ _id: id }, { avatarUrl });
@@ -28,6 +31,7 @@ module.exports = {
 	createUser,
 	updateCode,
 	updateToken,
+	updateUser,
 	updateCredentials,
 	updateAvatar,
 	findById,