refactor: Remove meteor from OTR (startup) (#35022)
parent
c7b21f1c1e
commit
85f8a6abfe
@ -1,93 +0,0 @@ |
||||
import { isOTRMessage } from '@rocket.chat/core-typings'; |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { Tracker } from 'meteor/tracker'; |
||||
|
||||
import OTR from '../../app/otr/client/OTR'; |
||||
import { OtrRoomState } from '../../app/otr/lib/OtrRoomState'; |
||||
import { sdk } from '../../app/utils/client/lib/SDKClient'; |
||||
import { t } from '../../app/utils/lib/i18n'; |
||||
import { onClientBeforeSendMessage } from '../lib/onClientBeforeSendMessage'; |
||||
import { onClientMessageReceived } from '../lib/onClientMessageReceived'; |
||||
|
||||
Meteor.startup(() => { |
||||
Tracker.autorun(() => { |
||||
const uid = Meteor.userId(); |
||||
|
||||
if (!uid) { |
||||
return; |
||||
} |
||||
|
||||
sdk.stream('notify-user', [`${uid}/otr`], (type, data) => { |
||||
if (!data.roomId || !data.userId || data.userId === uid) { |
||||
return; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, data.roomId); |
||||
otrRoom?.onUserStream(type, data); |
||||
}); |
||||
}); |
||||
|
||||
onClientBeforeSendMessage.use(async (message) => { |
||||
const uid = Meteor.userId(); |
||||
|
||||
if (!uid) { |
||||
return message; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, message.rid); |
||||
|
||||
if (otrRoom && otrRoom.getState() === OtrRoomState.ESTABLISHED) { |
||||
const msg = await otrRoom.encrypt(message); |
||||
return { ...message, msg, t: 'otr' }; |
||||
} |
||||
return message; |
||||
}); |
||||
|
||||
onClientMessageReceived.use(async (message) => { |
||||
const uid = Meteor.userId(); |
||||
|
||||
if (!uid) { |
||||
return message; |
||||
} |
||||
|
||||
if (!isOTRMessage(message)) { |
||||
return message; |
||||
} |
||||
|
||||
if ('notification' in message) { |
||||
return { ...message, msg: t('Encrypted_message') }; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, message.rid); |
||||
|
||||
if (otrRoom && otrRoom.getState() === OtrRoomState.ESTABLISHED) { |
||||
const decrypted = await otrRoom.decrypt(message.msg); |
||||
if (typeof decrypted === 'string') { |
||||
return { ...message, msg: decrypted }; |
||||
} |
||||
const { _id, text: msg, ack, ts, userId } = decrypted; |
||||
|
||||
if (ts) message.ts = ts; |
||||
|
||||
if (message.otrAck) { |
||||
const otrAck = await otrRoom.decrypt(message.otrAck); |
||||
if (typeof otrAck === 'string') { |
||||
return { ...message, msg: otrAck }; |
||||
} |
||||
|
||||
if (ack === otrAck.text) { |
||||
return { ...message, _id, t: 'otr-ack', msg }; |
||||
} |
||||
} else if (userId !== Meteor.userId()) { |
||||
const encryptedAck = await otrRoom.encryptText(ack); |
||||
|
||||
void sdk.call('updateOTRAck', { message, ack: encryptedAck }); |
||||
} |
||||
|
||||
return { ...message, _id, msg }; |
||||
} |
||||
if (message.t === 'otr') message.msg = ''; |
||||
|
||||
return message; |
||||
}); |
||||
}); |
||||
@ -0,0 +1,104 @@ |
||||
import type { AtLeast, IMessage } from '@rocket.chat/core-typings'; |
||||
import { isOTRMessage } from '@rocket.chat/core-typings'; |
||||
import { useMethod, useStream, useUserId } from '@rocket.chat/ui-contexts'; |
||||
import { useEffect } from 'react'; |
||||
|
||||
import OTR from '../../../../app/otr/client/OTR'; |
||||
import { OtrRoomState } from '../../../../app/otr/lib/OtrRoomState'; |
||||
import { t } from '../../../../app/utils/lib/i18n'; |
||||
import { onClientBeforeSendMessage } from '../../../lib/onClientBeforeSendMessage'; |
||||
import { onClientMessageReceived } from '../../../lib/onClientMessageReceived'; |
||||
|
||||
export const useOTRMessaging = () => { |
||||
const uid = useUserId(); |
||||
const updateOTRAck = useMethod('updateOTRAck'); |
||||
const notifyUser = useStream('notify-user'); |
||||
|
||||
useEffect(() => { |
||||
if (!uid) { |
||||
return; |
||||
} |
||||
|
||||
const handleNotifyUser = (type: 'handshake' | 'acknowledge' | 'deny' | 'end', data: { roomId: string; userId: string }) => { |
||||
if (!data.roomId || !data.userId || data.userId === uid) { |
||||
return; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, data.roomId); |
||||
otrRoom?.onUserStream(type, data); |
||||
}; |
||||
|
||||
const handleBeforeSendMessage = async ( |
||||
message: AtLeast<IMessage, '_id' | 'rid' | 'msg'>, |
||||
): Promise<AtLeast<IMessage, '_id' | 'rid' | 'msg'>> => { |
||||
if (!uid) { |
||||
return message; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, message.rid); |
||||
|
||||
if (otrRoom && otrRoom.getState() === OtrRoomState.ESTABLISHED) { |
||||
const msg = await otrRoom.encrypt(message); |
||||
return { ...message, msg, t: 'otr' }; |
||||
} |
||||
return message; |
||||
}; |
||||
|
||||
const handleMessageReceived = async (message: IMessage): Promise<IMessage> => { |
||||
if (!uid) { |
||||
return message; |
||||
} |
||||
|
||||
if (!isOTRMessage(message)) { |
||||
return message; |
||||
} |
||||
|
||||
if ('notification' in message) { |
||||
return { ...message, msg: t('Encrypted_message') }; |
||||
} |
||||
|
||||
const otrRoom = OTR.getInstanceByRoomId(uid, message.rid); |
||||
if (otrRoom && otrRoom.getState() === OtrRoomState.ESTABLISHED) { |
||||
const decrypted = await otrRoom.decrypt(message.msg); |
||||
if (typeof decrypted === 'string') { |
||||
return { ...message, msg: decrypted }; |
||||
} |
||||
|
||||
const { _id, text: msg, ack, ts, userId } = decrypted; |
||||
|
||||
if (ts) message.ts = ts; |
||||
|
||||
if (message.otrAck) { |
||||
const otrAck = await otrRoom.decrypt(message.otrAck); |
||||
if (typeof otrAck === 'string') { |
||||
return { ...message, msg: otrAck }; |
||||
} |
||||
|
||||
if (ack === otrAck.text) { |
||||
return { ...message, _id, t: 'otr-ack', msg }; |
||||
} |
||||
} else if (userId !== uid) { |
||||
const encryptedAck = await otrRoom.encryptText(ack); |
||||
|
||||
void updateOTRAck({ message, ack: encryptedAck }); |
||||
} |
||||
|
||||
return { ...message, _id, msg }; |
||||
} |
||||
|
||||
if (message.t === 'otr') message.msg = ''; |
||||
|
||||
return message; |
||||
}; |
||||
|
||||
const handleStopNotifyUser = notifyUser(`${uid}/otr`, handleNotifyUser); |
||||
const unregisterBeforeSendMessage = onClientBeforeSendMessage.use(handleBeforeSendMessage); |
||||
const unregisterMessageReceived = onClientMessageReceived.use(handleMessageReceived); |
||||
|
||||
return () => { |
||||
handleStopNotifyUser(); |
||||
unregisterBeforeSendMessage(); |
||||
unregisterMessageReceived(); |
||||
}; |
||||
}, [uid, notifyUser, updateOTRAck]); |
||||
}; |
||||
Loading…
Reference in new issue