src/crypto/algorithms/megolm.js
// returns a promise which resolves once the keyshare is successful.
returns a promise which resolves once the keyshare is successful.
returns a promise which resolves once the keyshare is successful .
async function prepareSession(oldSession) {
        session = oldSession;
        if (session && session.needsRotation(self._sessionRotationPeriodMsgs,
                                             self._sessionRotationPeriodMs)
           ) {
            logger.log("Starting new megolm session because we need to rotate.");
            session = null;
        }
        if (session && session.sharedWithTooManyDevices(devicesInRoom)) {
            session = null;
        }
        if (!session) {
            logger.log(`Starting new megolm session for room ${self._roomId}`);
            session = await self._prepareNewSession();
            self._outboundSessions[session.sessionId] = session;
        }
        const shareMap = {};
        for (const userId in devicesInRoom) {
            if (!devicesInRoom.hasOwnProperty(userId)) {
                continue;
            }
            const userDevices = devicesInRoom[userId];
            for (const deviceId in userDevices) {
                if (!userDevices.hasOwnProperty(deviceId)) {
                    continue;
                }
                const deviceInfo = userDevices[deviceId];
                const key = deviceInfo.getIdentityKey();
                if (key == self._olmDevice.deviceCurve25519Key) {
                    continue;
                }
                if (
                    !session.sharedWithDevices[userId] ||
                        session.sharedWithDevices[userId][deviceId] === undefined
                ) {
                    shareMap[userId] = shareMap[userId] || [];
                    shareMap[userId].push(deviceInfo);
                }
            }
        }
        return self._shareKeyWithDevices(
            session, shareMap,
        );
    }
async function prepareSession ( oldSession ) { session = oldSession ; if ( session && session . needsRotation ( self . _sessionRotationPeriodMsgs , self . _sessionRotationPeriodMs ) ) { logger . log ( "Starting new megolm session because we need to rotate." ) ; session = null ; } if ( session && session . sharedWithTooManyDevices ( devicesInRoom ) ) { session = null ; } if ( ! session ) { logger . log ( ` ${ self . _roomId } ` ) ; session = await self . _prepareNewSession ( ) ; self . _outboundSessions [ session . sessionId ] = session ; } const shareMap = { } ; for ( const userId in devicesInRoom ) { if ( ! devicesInRoom . hasOwnProperty ( userId ) ) { continue ; } const userDevices = devicesInRoom [ userId ] ; for ( const deviceId in userDevices ) { if ( ! userDevices . hasOwnProperty ( deviceId ) ) { continue ; } const deviceInfo = userDevices [ deviceId ] ; const key = deviceInfo . getIdentityKey ( ) ; if ( key == self . _olmDevice . deviceCurve25519Key ) { continue ; } if ( ! session . sharedWithDevices [ userId ] || session . sharedWithDevices [ userId ] [ deviceId ] === undefined ) { shareMap [ userId ] = shareMap [ userId ] || [ ] ; shareMap [ userId ] . push ( deviceInfo ) ; } } } return self . _shareKeyWithDevices ( session , shareMap , ) ; }
returns a promise which resolves once the keyshare is successful.
returns a promise which resolves once the keyshare is successful .
// need to make a brand new session? // determine if we have shared with anyone we shouldn't have // now check if we need to share with any devices // don't bother sending to ourself