Messaging Logic issue

Hey everyone,

I guess I made a mistake in one of the methods and cannot figure this out where is it. The problem is that there is a list of users and the current user clicks on one of it and creates chat-room id instantly. Then the user can write messages to the target user. However, the problem starts and that point. The target user logs in and goes messages, and follows the same flow it creates new chat-rooms for every listed user as well. So they won’t be messaging just creating new rooms and leave messages into those rooms.


export const getDirectMessageRoom = new ValidatedMethod({
	name: 'ChatRooms.getDirectMessageRoom',

	validate: new SimpleSchema({
		friendUserId: {
			type: String
		}
	}).validator(),

	run({ friendUserId }) {
		console.log('M - ChatRooms.getDirectMessageRoom / run');
		
		let response = {
			success: false,
			message: 'There was some server error.',
			data: {
				chatRoomId: ''
			}
		};

		if (Meteor.userId()) {
			if (friendUserId != '') {
				// Check if both user have already started chatting (chat room exists) or chatting for first time (create new chat room)
				let commonChatRoom = false;

				// Chat room exists check
				const chatRooms = ChatRooms.find({ userId: Meteor.userId() }).fetch();
				
				if (chatRooms.length > 0) {
					chatRooms.forEach((chatRoom) => {
						console.log(chatRoom._id);
						
						const chatRoomMember = ChatRoomMembers.findOne({ chatRoomId: chatRoom._id, userId: friendUserId });
						if (chatRoomMember) {
							commonChatRoom = chatRoom;
						console.log(commonChatRoom);
						
						}
					});
				}

				let chatRoomId;

				if (commonChatRoom !== false) {
					// Chat room exists
					chatRoomId = commonChatRoom._id;
				} else {
					// Chat room does not exists, create a new chat room
					const friendUser = Meteor.users.findOne(friendUserId);
					chatRoomId = ChatRooms.insert({
						title: `${Meteor.user().username} and ${friendUser.username}`,
						description: 'Direct Message',
						userId: Meteor.userId(),
						isPublic: false
					});

					// Add chat room members
					if (chatRoomId) {
						ChatRoomMembers.insert({ chatRoomId: chatRoomId, userId: Meteor.userId() });
						ChatRoomMembers.insert({ chatRoomId: chatRoomId, userId: friendUserId });
					}
				}

				if (chatRoomId) {
					response.success = true;
					response.message = 'Chat room available.';
					response.data.chatRoomId = chatRoomId;
				}
			}
		} else {
			response.message = 'You are not logged in.';
		}

		return response;
	}
});

I think the code you check for existing chat room has problem.
It only find the room which is created by this user.
You should check for existing chat room by searching in members of the chat rooms. If it is one-one chatting room, you need to find the room which has 2 members: the current user and the target user.

1 Like

Do you have any suggestion? Literally I couldn’t make it for 2 days

The socialize:messaging might be of interest here.