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 {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js";
import {createSessionEntry} from "./Session.js";
import {createSessionEntry} from "./Session";
function findFirstSessionId(sessionIds) {
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.
*/
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 {
session: olmSession.pickle(pickleKey),
sessionId: olmSession.session_id(),
@ -24,35 +28,38 @@ export function createSessionEntry(olmSession, senderKey, timestamp, pickleKey)
}
export class Session {
constructor(data, pickleKey, olm, isNew = false) {
this.data = data;
this._olm = olm;
this._pickleKey = pickleKey;
this.isNew = isNew;
public isModified: boolean;
constructor(
public readonly data: OlmSessionEntry,
private readonly pickleKey: string,
private readonly olm: Olm,
public isNew: boolean = false
) {
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);
return new Session(data, pickleKey, olm, true);
}
get id() {
get id(): string {
return this.data.sessionId;
}
load() {
const session = new this._olm.Session();
session.unpickle(this._pickleKey, this.data.session);
load(): Olm.Session {
const session = new this.olm.Session();
session.unpickle(this.pickleKey, this.data.session);
return session;
}
unload(olmSession) {
unload(olmSession: Olm.Session): void {
olmSession.free();
}
save(olmSession) {
this.data.session = olmSession.pickle(this._pickleKey);
save(olmSession: Olm.Session): void {
this.data.session = olmSession.pickle(this.pickleKey);
this.isModified = true;
}
}

View file

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