|
|
|
@ -21,6 +21,8 @@ import { isFederationEnabled } from '../lib/isFederationEnabled'; |
|
|
|
|
import { getUpload, requestEventsFromLatest } from '../handler'; |
|
|
|
|
import { notifyUsersOnMessage } from '../../../lib/server/lib/notifyUsersOnMessage'; |
|
|
|
|
import { sendAllNotifications } from '../../../lib/server/lib/sendNotificationsOnMessage'; |
|
|
|
|
import { processThreads } from '../../../threads/server/hooks/aftersavemessage'; |
|
|
|
|
import { processDeleteInThread } from '../../../threads/server/hooks/afterdeletemessage'; |
|
|
|
|
|
|
|
|
|
const eventHandlers = { |
|
|
|
|
//
|
|
|
|
@ -90,6 +92,9 @@ const eventHandlers = { |
|
|
|
|
async [eventTypes.ROOM_ADD_USER](event) { |
|
|
|
|
const eventResult = await FederationRoomEvents.addEvent(event.context, event); |
|
|
|
|
|
|
|
|
|
// We only want to refresh the server list and update the room federation array if something changed
|
|
|
|
|
let federationAltered = false; |
|
|
|
|
|
|
|
|
|
// If the event was successfully added, handle the event locally
|
|
|
|
|
if (eventResult.success) { |
|
|
|
|
const { data: { roomId, user, subscription, domainsAfterAdd } } = event; |
|
|
|
@ -98,35 +103,49 @@ const eventHandlers = { |
|
|
|
|
const persistedUser = Users.findOne({ _id: user._id }); |
|
|
|
|
|
|
|
|
|
if (persistedUser) { |
|
|
|
|
// Update the federation
|
|
|
|
|
Users.update({ _id: persistedUser._id }, { $set: { federation: user.federation } }); |
|
|
|
|
// Update the federation, if its not already set (if it's set, this is likely an event being reprocessed)
|
|
|
|
|
if (!persistedUser.federation) { |
|
|
|
|
Users.update({ _id: persistedUser._id }, { $set: { federation: user.federation } }); |
|
|
|
|
federationAltered = true; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Denormalize user
|
|
|
|
|
const denormalizedUser = normalizers.denormalizeUser(user); |
|
|
|
|
|
|
|
|
|
// Create the user
|
|
|
|
|
Users.insert(denormalizedUser); |
|
|
|
|
federationAltered = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Check if subscription exists
|
|
|
|
|
const persistedSubscription = Subscriptions.findOne({ _id: subscription._id }); |
|
|
|
|
|
|
|
|
|
if (persistedSubscription) { |
|
|
|
|
// Update the federation
|
|
|
|
|
Subscriptions.update({ _id: persistedSubscription._id }, { $set: { federation: subscription.federation } }); |
|
|
|
|
} else { |
|
|
|
|
// Denormalize subscription
|
|
|
|
|
const denormalizedSubscription = normalizers.denormalizeSubscription(subscription); |
|
|
|
|
try { |
|
|
|
|
if (persistedSubscription) { |
|
|
|
|
// Update the federation, if its not already set (if it's set, this is likely an event being reprocessed
|
|
|
|
|
if (!persistedSubscription.federation) { |
|
|
|
|
Subscriptions.update({ _id: persistedSubscription._id }, { $set: { federation: subscription.federation } }); |
|
|
|
|
federationAltered = true; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Denormalize subscription
|
|
|
|
|
const denormalizedSubscription = normalizers.denormalizeSubscription(subscription); |
|
|
|
|
|
|
|
|
|
// Create the subscription
|
|
|
|
|
Subscriptions.insert(denormalizedSubscription); |
|
|
|
|
// Create the subscription
|
|
|
|
|
Subscriptions.insert(denormalizedSubscription); |
|
|
|
|
federationAltered = true; |
|
|
|
|
} |
|
|
|
|
} catch (ex) { |
|
|
|
|
logger.server.debug(`unable to create subscription for user ( ${ user._id } ) in room (${ roomId })`); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Refresh the servers list
|
|
|
|
|
FederationServers.refreshServers(); |
|
|
|
|
if (federationAltered) { |
|
|
|
|
FederationServers.refreshServers(); |
|
|
|
|
|
|
|
|
|
// Update the room's federation property
|
|
|
|
|
Rooms.update({ _id: roomId }, { $set: { 'federation.domains': domainsAfterAdd } }); |
|
|
|
|
// Update the room's federation property
|
|
|
|
|
Rooms.update({ _id: roomId }, { $set: { 'federation.domains': domainsAfterAdd } }); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return eventResult; |
|
|
|
@ -193,7 +212,9 @@ const eventHandlers = { |
|
|
|
|
|
|
|
|
|
if (persistedMessage) { |
|
|
|
|
// Update the federation
|
|
|
|
|
Messages.update({ _id: persistedMessage._id }, { $set: { federation: message.federation } }); |
|
|
|
|
if (!persistedMessage.federation) { |
|
|
|
|
Messages.update({ _id: persistedMessage._id }, { $set: { federation: message.federation } }); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Load the room
|
|
|
|
|
const room = Rooms.findOneById(message.rid); |
|
|
|
@ -239,11 +260,17 @@ const eventHandlers = { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create the message
|
|
|
|
|
Messages.insert(denormalizedMessage); |
|
|
|
|
try { |
|
|
|
|
Messages.insert(denormalizedMessage); |
|
|
|
|
|
|
|
|
|
processThreads(denormalizedMessage, room); |
|
|
|
|
|
|
|
|
|
// Notify users
|
|
|
|
|
notifyUsersOnMessage(denormalizedMessage, room); |
|
|
|
|
sendAllNotifications(denormalizedMessage, room); |
|
|
|
|
// Notify users
|
|
|
|
|
notifyUsersOnMessage(denormalizedMessage, room); |
|
|
|
|
sendAllNotifications(denormalizedMessage, room); |
|
|
|
|
} catch (err) { |
|
|
|
|
logger.server.debug(`Error on creating message: ${ message._id }`); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -285,6 +312,11 @@ const eventHandlers = { |
|
|
|
|
if (eventResult.success) { |
|
|
|
|
const { data: { roomId, messageId } } = event; |
|
|
|
|
|
|
|
|
|
const message = Messages.findOne({ _id: messageId }); |
|
|
|
|
if (message) { |
|
|
|
|
processDeleteInThread(message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Remove the message
|
|
|
|
|
Messages.removeById(messageId); |
|
|
|
|
|
|
|
|
|