Skip to content

Commit

Permalink
Merge branch 'develop' into fix/maybe-initial-stuck-client
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Dec 7, 2023
2 parents 9eaaa06 + a271587 commit a55c36f
Show file tree
Hide file tree
Showing 25 changed files with 138 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-islands-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/rest-typings': minor
'@rocket.chat/meteor': minor
---

fix Federation Regression, builds service correctly
11 changes: 11 additions & 0 deletions apps/meteor/app/autotranslate/server/autotranslate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { api } from '@rocket.chat/core-services';
import type {
IMessage,
IRoom,
Expand All @@ -15,6 +16,7 @@ import _ from 'underscore';

import { callbacks } from '../../../lib/callbacks';
import { isTruthy } from '../../../lib/isTruthy';
import { broadcastMessageSentEvent } from '../../../server/modules/watchers/lib/messages';
import { Markdown } from '../../markdown/server';
import { settings } from '../../settings/server';

Expand Down Expand Up @@ -305,6 +307,7 @@ export abstract class AutoTranslate {
const translations = await this._translateMessage(targetMessage, targetLanguages);
if (!_.isEmpty(translations)) {
await Messages.addTranslations(message._id, translations, TranslationProviderRegistry[Provider] || '');
this.notifyTranslatedMessage(message._id);
}
});
}
Expand All @@ -320,6 +323,7 @@ export abstract class AutoTranslate {

if (!_.isEmpty(translations)) {
await Messages.addAttachmentTranslations(message._id, String(index), translations);
this.notifyTranslatedMessage(message._id);
}
}
}
Expand All @@ -328,6 +332,13 @@ export abstract class AutoTranslate {
return Messages.findOneById(message._id);
}

private notifyTranslatedMessage(messageId: string): void {
void broadcastMessageSentEvent({
id: messageId,
broadcastCallback: (message) => api.broadcast('message.sent', message),
});
}

/**
* Returns metadata information about the service provider which is used by
* the generic implementation
Expand Down
42 changes: 42 additions & 0 deletions apps/meteor/app/federation/server/endpoints/dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { eventTypes } from '@rocket.chat/core-typings';
import { FederationServers, FederationRoomEvents, Rooms, Messages, Subscriptions, Users, ReadReceipts } from '@rocket.chat/models';
import EJSON from 'ejson';

import { broadcastMessageSentEvent } from '../../../../server/modules/watchers/lib/messages';
import { API } from '../../../api/server';
import { FileUpload } from '../../../file-upload/server';
import { deleteRoom } from '../../../lib/server/functions/deleteRoom';
Expand Down Expand Up @@ -214,11 +215,13 @@ const eventHandlers = {

// Check if message exists
const persistedMessage = await Messages.findOne({ _id: message._id });
let messageForNotification;

if (persistedMessage) {
// Update the federation
if (!persistedMessage.federation) {
await Messages.updateOne({ _id: persistedMessage._id }, { $set: { federation: message.federation } });
messageForNotification = { ...persistedMessage, federation: message.federation };
}
} else {
// Load the room
Expand Down Expand Up @@ -275,10 +278,18 @@ const eventHandlers = {
// Notify users
await notifyUsersOnMessage(denormalizedMessage, room);
sendAllNotifications(denormalizedMessage, room);
messageForNotification = denormalizedMessage;
} catch (err) {
serverLogger.debug(`Error on creating message: ${message._id}`);
}
}
if (messageForNotification) {
void broadcastMessageSentEvent({
id: messageForNotification._id,
data: messageForNotification,
broadcastCallback: (message) => api.broadcast('message.sent', message),
});
}
}

return eventResult;
Expand All @@ -305,6 +316,15 @@ const eventHandlers = {
} else {
// Update the message
await Messages.updateOne({ _id: persistedMessage._id }, { $set: { msg: message.msg, federation: message.federation } });
void broadcastMessageSentEvent({
id: persistedMessage._id,
data: {
...persistedMessage,
msg: message.msg,
federation: message.federation,
},
broadcastCallback: (message) => api.broadcast('message.sent', message),
});
}
}

Expand Down Expand Up @@ -367,6 +387,17 @@ const eventHandlers = {

// Update the property
await Messages.updateOne({ _id: messageId }, { $set: { [`reactions.${reaction}`]: reactionObj } });
void broadcastMessageSentEvent({
id: persistedMessage._id,
data: {
...persistedMessage,
reactions: {
...persistedMessage.reactions,
[reaction]: reactionObj,
},
},
broadcastCallback: (message) => api.broadcast('message.sent', message),
});
}

return eventResult;
Expand Down Expand Up @@ -415,6 +446,17 @@ const eventHandlers = {
// Otherwise, update the property
await Messages.updateOne({ _id: messageId }, { $set: { [`reactions.${reaction}`]: reactionObj } });
}
void broadcastMessageSentEvent({
id: persistedMessage._id,
data: {
...persistedMessage,
reactions: {
...persistedMessage.reactions,
[reaction]: reactionObj,
},
},
broadcastCallback: (message) => api.broadcast('message.sent', message),
});
}

return eventResult;
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/lib/server/functions/createRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function createUsersSubscriptions({
const extra: Partial<ISubscriptionExtraData> = options?.subscriptionExtra || {};
extra.open = true;
extra.ls = now;
extra.roles = ['owner'];

if (room.prid) {
extra.prid = room.prid;
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions apps/meteor/app/livechat/imports/server/rest/sms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OmnichannelIntegration } from '@rocket.chat/core-services';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { LivechatVisitors, LivechatRooms, LivechatDepartment } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
Expand All @@ -8,9 +9,10 @@ import { Meteor } from 'meteor/meteor';
import { API } from '../../../../api/server';
import { FileUpload } from '../../../../file-upload/server';
import { settings } from '../../../../settings/server';
import { Livechat } from '../../../server/lib/Livechat';
import { Livechat as LivechatTyped } from '../../../server/lib/LivechatTyped';

const logger = new Logger('SMS');

const getUploadFile = async (details, fileUrl) => {
const response = await fetch(fileUrl);

Expand Down Expand Up @@ -156,7 +158,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
attachment.title_link_download = true;
}
} catch (err) {
Livechat.logger.error({ msg: 'Attachment upload failed', err });
logger.error({ msg: 'Attachment upload failed', err });
attachment = {
fields: [
{
Expand Down
10 changes: 6 additions & 4 deletions apps/meteor/app/livechat/server/api/v1/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Logger } from '@rocket.chat/logger';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';

import { API } from '../../../../api/server';
import { settings } from '../../../../settings/server';
import { Livechat } from '../../lib/Livechat';

const logger = new Logger('WebhookTest');

API.v1.addRoute(
'livechat/webhook.test',
Expand Down Expand Up @@ -70,18 +72,18 @@ API.v1.addRoute(
}

try {
Livechat.logger.debug(`Testing webhook ${webhookUrl}`);
logger.debug(`Testing webhook ${webhookUrl}`);
const request = await fetch(webhookUrl, options);
const response = await request.text();

Livechat.logger.debug({ response });
logger.debug({ response });
if (request.status === 200) {
return API.v1.success();
}

throw new Error('Invalid status code');
} catch (error) {
Livechat.logger.error(`Error testing webhook: ${error}`);
logger.error(`Error testing webhook: ${error}`);
throw new Error('error-invalid-webhook-response');
}
},
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/app/livechat/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,3 @@ import './api';
import './api/rest';
import './externalFrame';
import './methods/saveBusinessHour';

export { Livechat } from './lib/Livechat';
3 changes: 1 addition & 2 deletions apps/meteor/app/livechat/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { hasRoleAsync } from '../../../authorization/server/functions/hasRole';
import { sendNotification } from '../../../lib/server';
import { sendMessage } from '../../../lib/server/functions/sendMessage';
import { settings } from '../../../settings/server';
import { Livechat } from './Livechat';
import { Livechat as LivechatTyped } from './LivechatTyped';
import { queueInquiry, saveQueueInquiry } from './QueueManager';
import { RoutingManager } from './RoutingManager';
Expand Down Expand Up @@ -296,7 +295,7 @@ export const parseAgentCustomFields = (customFields?: Record<string, any>) => {
const parseCustomFields = JSON.parse(accountCustomFields);
return Object.keys(parseCustomFields).filter((customFieldKey) => parseCustomFields[customFieldKey].sendToIntegrations === true);
} catch (error) {
Livechat.logger.error(error);
logger.error(error);
return [];
}
};
Expand Down
9 changes: 0 additions & 9 deletions apps/meteor/app/livechat/server/lib/Livechat.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Meteor } from 'meteor/meteor';

import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import { Livechat } from '../lib/Livechat';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -27,7 +26,6 @@ Meteor.methods<ServerMethods>({
}

if (!options.chartOptions?.name) {
Livechat.logger.warn('Incorrect analytics options');
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { Meteor } from 'meteor/meteor';

import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { Livechat } from '../lib/Livechat';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -24,14 +23,12 @@ Meteor.methods<ServerMethods>({
}

if (!options.chartOptions?.name) {
Livechat.logger.error('Incorrect chart options');
return;
}

const user = await Users.findOneById(userId, { projection: { _id: 1, utcOffset: 1 } });

if (!user) {
Livechat.logger.error('User not found');
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Meteor } from 'meteor/meteor';
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { methodDeprecationLogger } from '../../../lib/server/lib/deprecationWarningLogger';
import { settings } from '../../../settings/server';
import { Livechat } from '../lib/Livechat';

declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -27,7 +26,6 @@ Meteor.methods<ServerMethods>({
}

if (!options.analyticsOptions?.name) {
Livechat.logger.error('Incorrect analytics options');
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { LivechatRooms } from '@rocket.chat/models';
import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { Meteor } from 'meteor/meteor';
Expand All @@ -16,6 +17,7 @@ declare module '@rocket.chat/ui-contexts' {

Meteor.methods<ServerMethods>({
async 'livechat:removeAllClosedRooms'(departmentIds) {
const logger = new Logger('livechat:removeAllClosedRooms');
const user = Meteor.userId();

if (!user || !(await hasPermissionAsync(user, 'remove-closed-livechat-rooms'))) {
Expand All @@ -25,7 +27,7 @@ Meteor.methods<ServerMethods>({
}

// These are not debug logs since we want to know when the action is performed
Livechat.logger.info(`User ${Meteor.userId()} is removing all closed rooms`);
logger.info(`User ${Meteor.userId()} is removing all closed rooms`);

const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
const promises: Promise<void>[] = [];
Expand All @@ -34,7 +36,7 @@ Meteor.methods<ServerMethods>({
});
await Promise.all(promises);

Livechat.logger.info(`User ${Meteor.userId()} removed ${promises.length} closed rooms`);
logger.info(`User ${Meteor.userId()} removed ${promises.length} closed rooms`);
return promises.length;
},
});
6 changes: 4 additions & 2 deletions apps/meteor/app/livechat/server/startup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IUser } from '@rocket.chat/core-typings';
import { ILivechatAgentStatus, isOmnichannelRoom } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { LivechatRooms } from '@rocket.chat/models';
import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
Expand All @@ -12,12 +13,13 @@ import { hasPermissionAsync } from '../../authorization/server/functions/hasPerm
import { settings } from '../../settings/server';
import { businessHourManager } from './business-hour';
import { createDefaultBusinessHourIfNotExists } from './business-hour/Helper';
import { Livechat } from './lib/Livechat';
import { Livechat as LivechatTyped } from './lib/LivechatTyped';
import { RoutingManager } from './lib/RoutingManager';
import { LivechatAgentActivityMonitor } from './statistics/LivechatAgentActivityMonitor';
import './roomAccessValidator.internalService';

const logger = new Logger('LivechatStartup');

Meteor.startup(async () => {
roomCoordinator.setRoomFind('l', (_id) => LivechatRooms.findOneById(_id));

Expand Down Expand Up @@ -63,7 +65,7 @@ Meteor.startup(async () => {
await createDefaultBusinessHourIfNotExists();

settings.watch<boolean>('Livechat_enable_business_hours', async (value) => {
Livechat.logger.info(`Changing business hour type to ${value}`);
logger.info(`Changing business hour type to ${value}`);
if (value) {
await businessHourManager.startManager();
return;
Expand Down
14 changes: 14 additions & 0 deletions apps/meteor/client/startup/notifications/usersNameChanged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ Meteor.startup(() => {
},
);

Messages.update(
{
'editedBy._id': _id,
},
{
$set: {
'editedBy.username': username,
},
},
{
multi: true,
},
);

Messages.update(
{
mentions: {
Expand Down
Loading

0 comments on commit a55c36f

Please sign in to comment.