Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Split OMEMO plugin into view and headless components #3117

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions src/headless/plugins/omemo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* @copyright The Converse.js contributors
* @license Mozilla Public License (MPLv2)
*/
import ConverseMixins from './mixins/converse.js';
import omemo_api from './api.js';
import Device from './device.js';
import DeviceList from './devicelist.js';
import DeviceLists from './devicelists.js';
import Devices from './devices.js';
import OMEMOStore from './store.js';
import { _converse, api, converse } from '@converse/headless/core';

import {
createOMEMOMessageStanza,
encryptFile,
getOutgoingMessageAttributes,
handleEncryptedFiles,
handleMessageSendError,
initOMEMO,
omemo,
onChatBoxesInitialized,
parseEncryptedMessage,
registerPEPPushHandler
setEncryptedFileURL,
} from './utils.js'

const { Strophe } = converse.env;

converse.env.omemo = omemo;

Strophe.addNamespace('OMEMO_DEVICELIST', Strophe.NS.OMEMO + '.devicelist');
Strophe.addNamespace('OMEMO_VERIFICATION', Strophe.NS.OMEMO + '.verification');
Strophe.addNamespace('OMEMO_WHITELISTED', Strophe.NS.OMEMO + '.whitelisted');
Strophe.addNamespace('OMEMO_BUNDLES', Strophe.NS.OMEMO + '.bundles');


converse.plugins.add('converse-omemo', {
enabled (_converse) {
return (
window.libsignal &&
_converse.config.get('trusted') &&
!api.settings.get('clear_cache_on_logout') &&
!_converse.api.settings.get('blacklisted-plugins').includes('converse-omemo-views')
);
},

dependencies: ['converse-chat', 'converse-pubsub'],

initialize() {
api.settings.extend({ 'omemo-default': false });
api.promises.add(['OMEMOInitialized']);

_converse.NUM_PREKEYS = 100; // Set here so that tests can override

Object.assign(_converse, ConverseMixins);
Object.assign(_converse.api, omemo_api);

_converse.OMEMOStore = OMEMOStore;
_converse.Device = Device;
_converse.Devices = Devices;
_converse.DeviceList = DeviceList;
_converse.DeviceLists = DeviceLists;

/******************** Event Handlers ********************/
api.waitUntil('chatBoxesInitialized').then(onChatBoxesInitialized);

api.listen.on('getOutgoingMessageAttributes', getOutgoingMessageAttributes);

api.listen.on('createMessageStanza', async (chat, data) => {
try {
data = await createOMEMOMessageStanza(chat, data);
} catch (e) {
handleMessageSendError(e, chat);
}
return data;
});

api.listen.on('afterFileUploaded', (msg, attrs) => msg.file.xep454_ivkey ? setEncryptedFileURL(msg, attrs) : attrs);
api.listen.on('beforeFileUpload', (chat, file) => chat.get('omemo_active') ? encryptFile(file) : file);

api.listen.on('parseMessage', parseEncryptedMessage);
api.listen.on('parseMUCMessage', parseEncryptedMessage);

api.listen.on('connected', registerPEPPushHandler);

api.listen.on('statusInitialized', initOMEMO);
api.listen.on('addClientFeatures', () => api.disco.own.features.add(`${Strophe.NS.OMEMO_DEVICELIST}+notify`));

api.listen.on('afterMessageBodyTransformed', handleEncryptedFiles);

api.listen.on('clearSession', () => {
delete _converse.omemo_store
if (_converse.shouldClearCache() && _converse.devicelists) {
_converse.devicelists.clearStore();
delete _converse.devicelists;
}
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import log from '@converse/headless/log';
import range from 'lodash-es/range';
import omit from 'lodash-es/omit';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this file can also be moved to the headless plugin. Generally speaking, all models should go into headless, and OMEMOStore is a model.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, okay! All the tests passed but when I tried running in the browser it's not actually running. So let me debug that and then I'll get to this 🙂

import { Model } from '@converse/skeletor/src/model.js';
import { generateDeviceID } from './utils.js';
import { generateDeviceID } from '@converse/headless/plugins/omemo/utils.js';
import { _converse, api, converse } from '@converse/headless/core';

const { Strophe, $build, u } = converse.env;
Expand Down
85 changes: 1 addition & 84 deletions src/plugins/omemo-views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,13 @@
import './fingerprints.js';
import './profile.js';
import 'shared/modals/user-details.js';
import ConverseMixins from './mixins/converse.js';
import Device from './device.js';
import DeviceList from './devicelist.js';
import DeviceLists from './devicelists.js';
import Devices from './devices.js';
import OMEMOStore from './store.js';
import log from '@converse/headless/log';
import omemo_api from './api.js';
import { _converse, api, converse } from '@converse/headless/core';
import {
createOMEMOMessageStanza,
getOMEMOToolbarButton,
onChatInitialized,
} from './utils.js';

// Temporary: to be removed to headless if possible!
import {
// createOMEMOMessageStanza,
encryptFile,
// getOMEMOToolbarButton,
getOutgoingMessageAttributes,
handleEncryptedFiles,
handleMessageSendError,
initOMEMO,
omemo,
/onChatBoxesInitialized,
// onChatInitialized,
parseEncryptedMessage,
registerPEPPushHandler,
setEncryptedFileURL,
} from '@converse/headless/plugins/omemo/utils.js'

const { Strophe } = converse.env;

converse.env.omemo = omemo;

Strophe.addNamespace('OMEMO_DEVICELIST', Strophe.NS.OMEMO + '.devicelist');
Strophe.addNamespace('OMEMO_VERIFICATION', Strophe.NS.OMEMO + '.verification');
Strophe.addNamespace('OMEMO_WHITELISTED', Strophe.NS.OMEMO + '.whitelisted');
Strophe.addNamespace('OMEMO_BUNDLES', Strophe.NS.OMEMO + '.bundles');


converse.plugins.add('converse-omemo-views', {
enabled (_converse) {
return (
Expand All @@ -57,54 +22,14 @@ converse.plugins.add('converse-omemo-views', {
);
},

dependencies: ['converse-chatview', 'converse-pubsub', 'converse-profile'],
dependencies: ['converse-chatview', 'converse-pubsub', 'converse-profile', 'converse-omemo'],

initialize () {
api.settings.extend({ 'omemo_default': false });
api.promises.add(['OMEMOInitialized']);

_converse.NUM_PREKEYS = 100; // Set here so that tests can override

Object.assign(_converse, ConverseMixins);
Object.assign(_converse.api, omemo_api);

_converse.OMEMOStore = OMEMOStore;
_converse.Device = Device;
_converse.Devices = Devices;
_converse.DeviceList = DeviceList;
_converse.DeviceLists = DeviceLists;

/******************** Event Handlers ********************/
api.waitUntil('chatBoxesInitialized').then(onChatBoxesInitialized);

api.listen.on('getOutgoingMessageAttributes', getOutgoingMessageAttributes);

api.listen.on('createMessageStanza', async (chat, data) => {
try {
data = await createOMEMOMessageStanza(chat, data);
} catch (e) {
handleMessageSendError(e, chat);
}
return data;
});

api.listen.on('afterFileUploaded', (msg, attrs) => msg.file.xep454_ivkey ? setEncryptedFileURL(msg, attrs) : attrs);
api.listen.on('beforeFileUpload', (chat, file) => chat.get('omemo_active') ? encryptFile(file) : file);

api.listen.on('parseMessage', parseEncryptedMessage);
api.listen.on('parseMUCMessage', parseEncryptedMessage);

api.listen.on('chatBoxViewInitialized', onChatInitialized);
api.listen.on('chatRoomViewInitialized', onChatInitialized);

api.listen.on('connected', registerPEPPushHandler);
api.listen.on('getToolbarButtons', getOMEMOToolbarButton);

api.listen.on('statusInitialized', initOMEMO);
api.listen.on('addClientFeatures', () => api.disco.own.features.add(`${Strophe.NS.OMEMO_DEVICELIST}+notify`));

api.listen.on('afterMessageBodyTransformed', handleEncryptedFiles);

api.listen.on('userDetailsModalInitialized', contact => {
const jid = contact.get('jid');
_converse.generateFingerprints(jid).catch(e => log.error(e));
Expand All @@ -113,13 +38,5 @@ converse.plugins.add('converse-omemo-views', {
api.listen.on('profileModalInitialized', () => {
_converse.generateFingerprints(_converse.bare_jid).catch(e => log.error(e));
});

api.listen.on('clearSession', () => {
delete _converse.omemo_store
if (_converse.shouldClearCache() && _converse.devicelists) {
_converse.devicelists.clearStore();
delete _converse.devicelists;
}
});
}
});