convert Session

This commit is contained in:
Bruno Windels 2022-02-15 18:20:49 +01:00
parent 7aeda70ff6
commit 74c640f937
3 changed files with 31 additions and 24 deletions

View file

@ -16,7 +16,7 @@ limitations under the License.
import {groupByWithCreator} from "../../../utils/groupBy"; import {groupByWithCreator} from "../../../utils/groupBy";
import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js"; import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js";
import {createSessionEntry} from "./Session.js"; import {createSessionEntry} from "./Session";
function findFirstSessionId(sessionIds) { function findFirstSessionId(sessionIds) {
return sessionIds.reduce((first, sessionId) => { return sessionIds.reduce((first, sessionId) => {

View file

@ -14,7 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
export function createSessionEntry(olmSession, senderKey, timestamp, pickleKey) { import type {OlmSessionEntry} from "../../storage/idb/stores/OlmSessionStore";
import type * as OlmNamespace from "@matrix-org/olm";
type Olm = typeof OlmNamespace;
export function createSessionEntry(olmSession: Olm.Session, senderKey: string, timestamp: number, pickleKey: string): OlmSessionEntry {
return { return {
session: olmSession.pickle(pickleKey), session: olmSession.pickle(pickleKey),
sessionId: olmSession.session_id(), sessionId: olmSession.session_id(),
@ -24,35 +28,38 @@ export function createSessionEntry(olmSession, senderKey, timestamp, pickleKey)
} }
export class Session { export class Session {
constructor(data, pickleKey, olm, isNew = false) { public isModified: boolean;
this.data = data;
this._olm = olm; constructor(
this._pickleKey = pickleKey; public readonly data: OlmSessionEntry,
this.isNew = isNew; private readonly pickleKey: string,
private readonly olm: Olm,
public isNew: boolean = false
) {
this.isModified = isNew; this.isModified = isNew;
} }
static create(senderKey, olmSession, olm, pickleKey, timestamp) { static create(senderKey: string, olmSession: Olm.Session, olm: Olm, pickleKey: string, timestamp: number): Session {
const data = createSessionEntry(olmSession, senderKey, timestamp, pickleKey); const data = createSessionEntry(olmSession, senderKey, timestamp, pickleKey);
return new Session(data, pickleKey, olm, true); return new Session(data, pickleKey, olm, true);
} }
get id() { get id(): string {
return this.data.sessionId; return this.data.sessionId;
} }
load() { load(): Olm.Session {
const session = new this._olm.Session(); const session = new this.olm.Session();
session.unpickle(this._pickleKey, this.data.session); session.unpickle(this.pickleKey, this.data.session);
return session; return session;
} }
unload(olmSession) { unload(olmSession: Olm.Session): void {
olmSession.free(); olmSession.free();
} }
save(olmSession) { save(olmSession: Olm.Session): void {
this.data.session = olmSession.pickle(this._pickleKey); this.data.session = olmSession.pickle(this.pickleKey);
this.isModified = true; this.isModified = true;
} }
} }

View file

@ -24,19 +24,19 @@ function decodeKey(key: string): { senderKey: string, sessionId: string } {
return {senderKey, sessionId}; return {senderKey, sessionId};
} }
interface OlmSession { export type OlmSessionEntry = {
session: string; session: string;
sessionId: string; sessionId: string;
senderKey: string; senderKey: string;
lastUsed: number; lastUsed: number;
} }
type OlmSessionEntry = OlmSession & { key: string }; type OlmSessionStoredEntry = OlmSessionEntry & { key: string };
export class OlmSessionStore { export class OlmSessionStore {
private _store: Store<OlmSessionEntry>; private _store: Store<OlmSessionStoredEntry>;
constructor(store: Store<OlmSessionEntry>) { constructor(store: Store<OlmSessionStoredEntry>) {
this._store = store; this._store = store;
} }
@ -55,20 +55,20 @@ export class OlmSessionStore {
return sessionIds; return sessionIds;
} }
getAll(senderKey: string): Promise<OlmSession[]> { getAll(senderKey: string): Promise<OlmSessionEntry[]> {
const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, "")); const range = this._store.IDBKeyRange.lowerBound(encodeKey(senderKey, ""));
return this._store.selectWhile(range, session => { return this._store.selectWhile(range, session => {
return session.senderKey === senderKey; return session.senderKey === senderKey;
}); });
} }
get(senderKey: string, sessionId: string): Promise<OlmSession | undefined> { get(senderKey: string, sessionId: string): Promise<OlmSessionEntry | undefined> {
return this._store.get(encodeKey(senderKey, sessionId)); return this._store.get(encodeKey(senderKey, sessionId));
} }
set(session: OlmSession): void { set(session: OlmSessionEntry): void {
(session as OlmSessionEntry).key = encodeKey(session.senderKey, session.sessionId); (session as OlmSessionStoredEntry).key = encodeKey(session.senderKey, session.sessionId);
this._store.put(session as OlmSessionEntry); this._store.put(session as OlmSessionStoredEntry);
} }
remove(senderKey: string, sessionId: string): void { remove(senderKey: string, sessionId: string): void {