Skip to content

Commit

Permalink
direct message rid
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Mar 24, 2020
1 parent 16f5190 commit 1bf9f2b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 34 deletions.
35 changes: 21 additions & 14 deletions app/integrations/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,28 +496,35 @@ integrations.triggerHandler = new class RocketChatIntegrationHandler {
if (room) {
switch (room.t) {
case 'd':
const id = room._id.replace(message.u._id, '');
const username = _.without(room.usernames, message.u.username)[0];

if (this.triggers[`@${ id }`]) {
for (const trigger of Object.values(this.triggers[`@${ id }`])) {
triggersToExecute.push(trigger);
}
}

if (this.triggers.all_direct_messages) {
for (const trigger of Object.values(this.triggers.all_direct_messages)) {
triggersToExecute.push(trigger);
}
}

if (id !== username && this.triggers[`@${ username }`]) {
for (const trigger of Object.values(this.triggers[`@${ username }`])) {
triggersToExecute.push(trigger);
}
if (!room.uids) {
return;
}
break;

room.uids.forEach((uid) => {
if (this.triggers[`@${ uid }`]) {
for (const trigger of Object.values(this.triggers[`@${ uid }`])) {
triggersToExecute.push(trigger);
}
}
});

room.usernames.forEach((username) => {
if (room.uids.includes(username) || username === message.u.username) {
return;
}
if (this.triggers[`@${ username }`]) {
for (const trigger of Object.values(this.triggers[`@${ username }`])) {
triggersToExecute.push(trigger);
}
}
});
break;
case 'c':
if (this.triggers.all_public_channels) {
for (const trigger of Object.values(this.triggers.all_public_channels)) {
Expand Down
3 changes: 2 additions & 1 deletion app/otr/client/rocketchat.otr.room.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import toastr from 'toastr';
import { OTR } from './rocketchat.otr';
import { Notifications } from '../../notifications';
import { modal } from '../../ui-utils';
import { getUidDirectMessage } from '../../ui-utils/client/lib/getUidDirectMessage';

OTR.Room = class {
constructor(userId, roomId) {
this.userId = userId;
this.roomId = roomId;
this.peerId = roomId.replace(userId, '');
this.peerId = getUidDirectMessage(roomId);
this.established = new ReactiveVar(false);
this.establishing = new ReactiveVar(false);

Expand Down
3 changes: 2 additions & 1 deletion app/otr/client/views/otrFlexTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Template } from 'meteor/templating';
import { OTR } from '../rocketchat.otr';
import { modal } from '../../../ui-utils';
import { t } from '../../../utils';
import { getUidDirectMessage } from '../../../ui-utils/client/lib/getUidDirectMessage';

Template.otrFlexTab.helpers({
otrAvailable() {
Expand All @@ -16,7 +17,7 @@ Template.otrFlexTab.helpers({
}

if (this.rid) {
const peerId = this.rid.replace(Meteor.userId(), '');
const peerId = getUidDirectMessage(this.rid);
if (peerId) {
const user = Meteor.users.findOne(peerId);
const online = user && user.status !== 'offline';
Expand Down
18 changes: 3 additions & 15 deletions app/ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { Template } from 'meteor/templating';

import { t, getUserPreference, roomTypes } from '../../utils';
import { popover, renderMessageBody, menu } from '../../ui-utils';
import { Users, ChatSubscription, Rooms } from '../../models/client';
import { Users, ChatSubscription } from '../../models/client';
import { settings } from '../../settings';
import { hasAtLeastOnePermission } from '../../authorization';
import { timeAgo } from '../../lib/client/lib/formatDate';
import { getUidDirectMessage } from '../../ui-utils/client/lib/getUidDirectMessage';

Template.sidebarItem.helpers({
streaming() {
Expand Down Expand Up @@ -200,25 +201,12 @@ Template.sidebarItem.events({
},
});

const getOtherUserId = (rid, userId) => {
const room = Rooms.findOne({ _id: rid }, { fields: { uids: 1 } });

if (!room || !room.uids || room.uids.length > 2) {
return false;
}

const other = room && room.uids.filter((uid) => uid !== userId);

return other && other[0];
};

Template.sidebarItemIcon.helpers({
uid() {
if (!this.rid) {
return this._id;
}

return getOtherUserId(this.rid, Meteor.userId());
return getUidDirectMessage(this.rid);
},
isRoom() {
return this.rid || this._id;
Expand Down
15 changes: 15 additions & 0 deletions app/ui-utils/client/lib/getUidDirectMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Meteor } from 'meteor/meteor';

import { Rooms } from '../../../models/client';

export const getUidDirectMessage = (rid, userId = Meteor.userId()) => {
const room = Rooms.findOne({ _id: rid }, { fields: { uids: 1 } });

if (!room || !room.uids || room.uids.length > 2) {
return false;
}

const other = room && room.uids.filter((uid) => uid !== userId);

return other && other[0];
};
5 changes: 3 additions & 2 deletions app/ui/client/components/header/headerRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { settings } from '../../../../settings';
import { emoji } from '../../../../emoji';
import { Markdown } from '../../../../markdown/client';
import { hasAllPermission } from '../../../../authorization';
import { getUidDirectMessage } from '../../../../ui-utils/client/lib/getUidDirectMessage';

import './headerRoom.html';

Expand All @@ -38,7 +39,7 @@ Template.headerRoom.helpers({
toggleFavoriteButtonIconLabel: () => (Template.instance().state.get('favorite') ? t('Unfavorite') : t('Favorite')),
toggleFavoriteButtonIcon: () => (Template.instance().state.get('favorite') ? 'star-filled' : 'star'),
uid() {
return this._id.replace(Meteor.userId(), '');
return getUidDirectMessage(this._id);
},
back() {
return Template.instance().data.back;
Expand Down Expand Up @@ -192,7 +193,7 @@ const loadUserStatusText = () => {
return;
}

const userId = id.replace(Meteor.userId(), '');
const userId = getUidDirectMessage(id);

// If the user is already on the local collection, the method call is not necessary
const found = Meteor.users.findOne(userId, { fields: { _id: 1 } });
Expand Down
2 changes: 1 addition & 1 deletion app/user-data-download/server/cronProcessDownloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const loadUserSubscriptions = function(exportOperation) {
const roomId = subscription.rid;
const roomData = Rooms.findOneById(roomId);
const roomName = roomData && roomData.name && subscription.t !== 'd' ? roomData.name : roomId;
const userId = subscription.t === 'd' ? roomId.replace(exportUserId, '') : null;
const [userId] = subscription.t === 'd' ? roomId.uids.filter((uid) => uid !== exportUserId) : [null];
const fileName = exportOperation.fullExport ? roomId : roomName;
const fileType = exportOperation.fullExport ? 'json' : 'html';
const targetFile = `${ fileName }.${ fileType }`;
Expand Down

0 comments on commit 1bf9f2b

Please sign in to comment.