2019-08-03 02:18:33 +08:00
|
|
|
import { GroupChatMsg, UsersTyping } from '/imports/api/group-chat-msg';
|
2021-03-10 04:52:20 +08:00
|
|
|
import Users from '/imports/api/users';
|
2018-03-10 02:42:14 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
2021-12-02 01:47:08 +08:00
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import GroupChat from '/imports/api/group-chat';
|
2018-03-10 02:42:14 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2020-09-02 00:31:11 +08:00
|
|
|
import AuthTokenValidation, { ValidationStates } from '/imports/api/auth-token-validation';
|
2018-03-10 02:42:14 +08:00
|
|
|
|
2023-05-04 04:14:05 +08:00
|
|
|
async function groupChatMsg() {
|
2023-04-01 04:40:41 +08:00
|
|
|
const tokenValidation = await AuthTokenValidation
|
|
|
|
.findOneAsync({ connectionId: this.connection.id });
|
2020-09-02 00:31:11 +08:00
|
|
|
|
|
|
|
if (!tokenValidation || tokenValidation.validationStatus !== ValidationStates.VALIDATED) {
|
|
|
|
Logger.warn(`Publishing GroupChatMsg was requested by unauth connection ${this.connection.id}`);
|
2020-02-07 04:47:28 +08:00
|
|
|
return GroupChatMsg.find({ meetingId: '' });
|
|
|
|
}
|
2020-09-02 00:31:11 +08:00
|
|
|
|
|
|
|
const { meetingId, userId } = tokenValidation;
|
2018-03-10 02:42:14 +08:00
|
|
|
|
2018-08-06 20:05:07 +08:00
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
|
|
|
|
|
2020-11-24 23:13:09 +08:00
|
|
|
Logger.debug('Publishing group-chat-msg', { meetingId, userId });
|
2018-03-10 02:42:14 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const chats = await GroupChat.find({
|
2021-12-02 01:47:08 +08:00
|
|
|
$or: [
|
|
|
|
{ meetingId, users: { $all: [userId] } },
|
|
|
|
],
|
2023-04-01 04:40:41 +08:00
|
|
|
}).fetchAsync();
|
2021-12-02 01:47:08 +08:00
|
|
|
|
|
|
|
const chatsIds = chats.map((ct) => ct.chatId);
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const User = await Users.findOneAsync({ userId, meetingId });
|
2021-02-04 04:49:58 +08:00
|
|
|
const selector = {
|
2021-03-10 04:52:20 +08:00
|
|
|
timestamp: { $gte: User.authTokenValidatedTime },
|
2018-08-06 20:05:07 +08:00
|
|
|
$or: [
|
|
|
|
{ meetingId, chatId: { $eq: PUBLIC_GROUP_CHAT_ID } },
|
2023-05-04 04:14:05 +08:00
|
|
|
{ meetingId, participants: { $in: [userId] } },
|
2018-08-06 20:05:07 +08:00
|
|
|
],
|
2021-02-04 04:49:58 +08:00
|
|
|
};
|
2023-05-04 04:14:05 +08:00
|
|
|
return GroupChatMsg.find(selector, { fields: { participants: 0 } });
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function publish(...args) {
|
|
|
|
const boundGroupChat = groupChatMsg.bind(this);
|
2018-11-06 03:30:37 +08:00
|
|
|
return boundGroupChat(...args);
|
2018-03-10 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Meteor.publish('group-chat-msg', publish);
|
2019-08-03 02:18:33 +08:00
|
|
|
|
2020-02-07 04:47:28 +08:00
|
|
|
function usersTyping() {
|
2020-09-02 00:31:11 +08:00
|
|
|
const tokenValidation = AuthTokenValidation.findOne({ connectionId: this.connection.id });
|
|
|
|
|
|
|
|
if (!tokenValidation || tokenValidation.validationStatus !== ValidationStates.VALIDATED) {
|
|
|
|
Logger.warn(`Publishing users-typing was requested by unauth connection ${this.connection.id}`);
|
2020-02-07 04:47:28 +08:00
|
|
|
return UsersTyping.find({ meetingId: '' });
|
|
|
|
}
|
2019-08-03 02:18:33 +08:00
|
|
|
|
2020-09-02 00:31:11 +08:00
|
|
|
const { meetingId, userId } = tokenValidation;
|
2020-03-06 07:14:22 +08:00
|
|
|
|
2020-11-24 23:13:09 +08:00
|
|
|
Logger.debug('Publishing users-typing', { meetingId, userId });
|
2019-08-03 02:18:33 +08:00
|
|
|
|
|
|
|
return UsersTyping.find({ meetingId });
|
|
|
|
}
|
|
|
|
|
|
|
|
function pubishUsersTyping(...args) {
|
|
|
|
const boundUsersTyping = usersTyping.bind(this);
|
|
|
|
return boundUsersTyping(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Meteor.publish('users-typing', pubishUsersTyping);
|