[FIX] Federation messages notifications (#15418)

pull/15420/head
Alan Sikora 6 years ago committed by Diego Sampaio
parent a3f559ea9f
commit 555c9f7555
  1. 4
      app/federation/server/constants.js
  2. 12
      app/federation/server/endpoints/dispatch.js
  3. 6
      app/federation/server/functions/helpers.js
  4. 15
      app/federation/server/startup/settings.js
  5. 2
      app/lib/server/lib/notifyUsersOnMessage.js
  6. 2
      app/lib/server/lib/sendNotificationsOnMessage.js

@ -0,0 +1,4 @@
export const STATUS_ENABLED = 'Enabled';
export const STATUS_REGISTERING = 'Registering with Hub...';
export const STATUS_ERROR_REGISTERING = 'Could not register with Hub';
export const STATUS_DISABLED = 'Disabled';

@ -19,6 +19,8 @@ import { getFederationDomain } from '../lib/getFederationDomain';
import { decryptIfNeeded } from '../lib/crypt';
import { isFederationEnabled } from '../lib/isFederationEnabled';
import { getUpload, requestEventsFromLatest } from '../handler';
import { notifyUsersOnMessage } from '../../../lib/server/lib/notifyUsersOnMessage';
import { sendAllNotifications } from '../../../lib/server/lib/sendNotificationsOnMessage';
API.v1.addRoute('federation.events.dispatch', { authRequired: false }, {
async post() {
@ -195,10 +197,10 @@ API.v1.addRoute('federation.events.dispatch', { authRequired: false }, {
// Update the federation
Messages.update({ _id: persistedMessage._id }, { $set: { federation: message.federation } });
} else {
// Update the subscription open status
Subscriptions.update({ rid: message.rid, name: message.u.username }, { $set: { open: true, alert: true } });
// Load the room
const room = Rooms.findOneById(message.rid);
// Denormalize user
// Denormalize message
const denormalizedMessage = normalizers.denormalizeMessage(message);
// Is there a file?
@ -234,6 +236,10 @@ API.v1.addRoute('federation.events.dispatch', { authRequired: false }, {
// Create the message
Messages.insert(denormalizedMessage);
// Notify users
notifyUsersOnMessage(denormalizedMessage, room);
sendAllNotifications(denormalizedMessage, room);
}
}
break;

@ -1,8 +1,14 @@
import { Settings, Subscriptions, Users } from '../../../models/server';
import { STATUS_ENABLED, STATUS_REGISTERING } from '../constants';
export const getNameAndDomain = (fullyQualifiedName) => fullyQualifiedName.split('@');
export const isFullyQualified = (name) => name.indexOf('@') !== -1;
export function isRegisteringOrEnabled() {
const status = Settings.findOneById('FEDERATION_Status');
return [STATUS_ENABLED, STATUS_REGISTERING].includes(status && status.value);
}
export function updateStatus(status) {
Settings.updateValueById('FEDERATION_Status', status);
}

@ -2,13 +2,14 @@ import { debounce } from 'underscore';
import { Meteor } from 'meteor/meteor';
import { settings } from '../../../settings/server';
import { updateStatus, updateEnabled } from '../functions/helpers';
import { updateStatus, updateEnabled, isRegisteringOrEnabled } from '../functions/helpers';
import { getFederationDomain } from '../lib/getFederationDomain';
import { getFederationDiscoveryMethod } from '../lib/getFederationDiscoveryMethod';
import { registerWithHub } from '../lib/dns';
import { enableCallbacks, disableCallbacks } from '../lib/callbacks';
import { logger } from '../lib/logger';
import { FederationKeys } from '../../../models/server';
import { STATUS_ENABLED, STATUS_REGISTERING, STATUS_ERROR_REGISTERING, STATUS_DISABLED } from '../constants';
Meteor.startup(function() {
const federationPublicKey = FederationKeys.getPublicKeyString();
@ -68,18 +69,22 @@ Meteor.startup(function() {
const updateSettings = debounce(Meteor.bindEnvironment(function() {
// Get the key pair
if (getFederationDiscoveryMethod() === 'hub') {
if (getFederationDiscoveryMethod() === 'hub' && !isRegisteringOrEnabled()) {
// Register with hub
try {
updateStatus(STATUS_REGISTERING);
registerWithHub(getFederationDomain(), settings.get('Site_Url'), FederationKeys.getPublicKeyString());
updateStatus(STATUS_ENABLED);
} catch (err) {
// Disable federation
updateEnabled(false);
updateStatus('Could not register with Hub');
updateStatus(STATUS_ERROR_REGISTERING);
}
} else {
updateStatus('Enabled');
updateStatus(STATUS_ENABLED);
}
}), 150);
@ -91,7 +96,7 @@ function enableOrDisable(key, value) {
enableCallbacks();
} else {
updateStatus('Disabled');
updateStatus(STATUS_DISABLED);
disableCallbacks();
}

@ -84,7 +84,7 @@ export function updateUsersSubscriptions(message, room, users) {
Subscriptions.setOpenForRoomIdExcludingUserId(message.rid, message.u._id);
}
function notifyUsersOnMessage(message, room) {
export function notifyUsersOnMessage(message, room) {
// skips this callback if the message was edited and increments it if the edit was way in the past (aka imported)
if (message.editedAt && Math.abs(moment(message.editedAt).diff()) > 60000) {
// TODO: Review as I am not sure how else to get around this as the incrementing of the msgs count shouldn't be in this callback

@ -286,7 +286,7 @@ export async function sendMessageNotifications(message, room, usersInThread = []
};
}
async function sendAllNotifications(message, room) {
export async function sendAllNotifications(message, room) {
// threads
if (message.tmid) {
return message;

Loading…
Cancel
Save