2021-08-09 22:56:20 +02:00
|
|
|
import {iterateCursor, reqAsPromise} from "./utils";
|
2020-08-19 16:28:09 +02:00
|
|
|
import {RoomMember, EVENT_TYPE as MEMBER_EVENT_TYPE} from "../../room/members/RoomMember.js";
|
2021-08-11 23:59:52 +02:00
|
|
|
import {RoomMemberStore} from "./stores/RoomMemberStore";
|
2021-08-11 01:10:55 +02:00
|
|
|
import {SessionStore} from "./stores/SessionStore";
|
2021-05-12 15:36:48 +02:00
|
|
|
import {encodeScopeTypeKey} from "./stores/OperationStore.js";
|
2020-06-26 23:26:24 +02:00
|
|
|
|
|
|
|
// FUNCTIONS SHOULD ONLY BE APPENDED!!
|
|
|
|
// the index in the array is the database version
|
|
|
|
export const schema = [
|
|
|
|
createInitialStores,
|
|
|
|
createMemberStore,
|
2020-08-27 14:28:40 +02:00
|
|
|
migrateSession,
|
2020-09-11 16:48:04 +02:00
|
|
|
createE2EEStores,
|
2020-09-17 10:39:51 +02:00
|
|
|
migrateEncryptionFlag,
|
2021-04-20 13:02:50 +02:00
|
|
|
createAccountDataStore,
|
2021-05-04 13:34:42 +02:00
|
|
|
createInviteStore,
|
|
|
|
createArchivedRoomSummaryStore,
|
2021-05-12 15:36:48 +02:00
|
|
|
migrateOperationScopeIndex,
|
2021-06-03 16:44:35 +02:00
|
|
|
createTimelineRelationsStore,
|
2020-06-26 23:26:24 +02:00
|
|
|
];
|
|
|
|
// TODO: how to deal with git merge conflicts of this array?
|
|
|
|
|
|
|
|
|
|
|
|
// how do we deal with schema updates vs existing data migration in a way that
|
|
|
|
//v1
|
|
|
|
function createInitialStores(db) {
|
|
|
|
db.createObjectStore("session", {keyPath: "key"});
|
|
|
|
// any way to make keys unique here? (just use put?)
|
|
|
|
db.createObjectStore("roomSummary", {keyPath: "roomId"});
|
|
|
|
|
|
|
|
// need index to find live fragment? prooobably ok without for now
|
|
|
|
//key = room_id | fragment_id
|
|
|
|
db.createObjectStore("timelineFragments", {keyPath: "key"});
|
|
|
|
//key = room_id | fragment_id | event_index
|
|
|
|
const timelineEvents = db.createObjectStore("timelineEvents", {keyPath: "key"});
|
|
|
|
//eventIdKey = room_id | event_id
|
|
|
|
timelineEvents.createIndex("byEventId", "eventIdKey", {unique: true});
|
|
|
|
//key = room_id | event.type | event.state_key,
|
|
|
|
db.createObjectStore("roomState", {keyPath: "key"});
|
|
|
|
db.createObjectStore("pendingEvents", {keyPath: "key"});
|
|
|
|
}
|
|
|
|
//v2
|
|
|
|
async function createMemberStore(db, txn) {
|
2020-08-19 16:07:58 +02:00
|
|
|
const roomMembers = new RoomMemberStore(db.createObjectStore("roomMembers", {keyPath: "key"}));
|
2020-06-26 23:26:24 +02:00
|
|
|
// migrate existing member state events over
|
|
|
|
const roomState = txn.objectStore("roomState");
|
|
|
|
await iterateCursor(roomState.openCursor(), entry => {
|
|
|
|
if (entry.event.type === MEMBER_EVENT_TYPE) {
|
|
|
|
roomState.delete(entry.key);
|
|
|
|
const member = RoomMember.fromMemberEvent(entry.roomId, entry.event);
|
|
|
|
if (member) {
|
2020-08-19 16:07:58 +02:00
|
|
|
roomMembers.set(member.serialize());
|
2020-06-26 23:26:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-31 14:38:03 +02:00
|
|
|
//v3
|
2020-08-27 14:28:40 +02:00
|
|
|
async function migrateSession(db, txn) {
|
|
|
|
const session = txn.objectStore("session");
|
|
|
|
try {
|
|
|
|
const PRE_MIGRATION_KEY = 1;
|
|
|
|
const entry = await reqAsPromise(session.get(PRE_MIGRATION_KEY));
|
|
|
|
if (entry) {
|
|
|
|
session.delete(PRE_MIGRATION_KEY);
|
2020-08-27 14:36:50 +02:00
|
|
|
const {syncToken, syncFilterId, serverVersions} = entry.value;
|
2020-08-27 14:28:40 +02:00
|
|
|
const store = new SessionStore(session);
|
2020-08-27 14:36:50 +02:00
|
|
|
store.set("sync", {token: syncToken, filterId: syncFilterId});
|
|
|
|
store.set("serverVersions", serverVersions);
|
2020-08-27 14:28:40 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
console.error("could not migrate session", err.stack);
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 14:38:03 +02:00
|
|
|
//v4
|
2020-09-11 08:40:43 +02:00
|
|
|
function createE2EEStores(db) {
|
2020-08-31 14:38:03 +02:00
|
|
|
db.createObjectStore("userIdentities", {keyPath: "userId"});
|
2020-09-11 08:40:43 +02:00
|
|
|
const deviceIdentities = db.createObjectStore("deviceIdentities", {keyPath: "key"});
|
|
|
|
deviceIdentities.createIndex("byCurve25519Key", "curve25519Key", {unique: true});
|
2020-09-01 17:59:59 +02:00
|
|
|
db.createObjectStore("olmSessions", {keyPath: "key"});
|
2020-09-02 14:24:38 +02:00
|
|
|
db.createObjectStore("inboundGroupSessions", {keyPath: "key"});
|
2020-09-03 17:49:20 +02:00
|
|
|
db.createObjectStore("outboundGroupSessions", {keyPath: "roomId"});
|
2020-09-04 15:31:00 +02:00
|
|
|
db.createObjectStore("groupSessionDecryptions", {keyPath: "key"});
|
2020-09-11 14:40:05 +02:00
|
|
|
const operations = db.createObjectStore("operations", {keyPath: "id"});
|
|
|
|
operations.createIndex("byTypeAndScope", "typeScopeKey", {unique: false});
|
2020-09-04 15:31:00 +02:00
|
|
|
}
|
2020-09-11 16:48:04 +02:00
|
|
|
|
|
|
|
// v5
|
|
|
|
async function migrateEncryptionFlag(db, txn) {
|
|
|
|
// migrate room summary isEncrypted -> encryption prop
|
|
|
|
const roomSummary = txn.objectStore("roomSummary");
|
|
|
|
const roomState = txn.objectStore("roomState");
|
|
|
|
const summaries = [];
|
|
|
|
await iterateCursor(roomSummary.openCursor(), summary => {
|
|
|
|
summaries.push(summary);
|
|
|
|
});
|
|
|
|
for (const summary of summaries) {
|
|
|
|
const encryptionEntry = await reqAsPromise(roomState.get(`${summary.roomId}|m.room.encryption|`));
|
|
|
|
if (encryptionEntry) {
|
|
|
|
summary.encryption = encryptionEntry?.event?.content;
|
|
|
|
delete summary.isEncrypted;
|
|
|
|
roomSummary.put(summary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 10:39:51 +02:00
|
|
|
|
|
|
|
// v6
|
|
|
|
function createAccountDataStore(db) {
|
|
|
|
db.createObjectStore("accountData", {keyPath: "type"});
|
|
|
|
}
|
2021-04-20 13:02:50 +02:00
|
|
|
|
|
|
|
// v7
|
|
|
|
function createInviteStore(db) {
|
|
|
|
db.createObjectStore("invites", {keyPath: "roomId"});
|
|
|
|
}
|
2021-05-04 13:34:42 +02:00
|
|
|
|
|
|
|
// v8
|
|
|
|
function createArchivedRoomSummaryStore(db) {
|
2021-05-11 13:01:19 +02:00
|
|
|
db.createObjectStore("archivedRoomSummary", {keyPath: "summary.roomId"});
|
2021-05-12 15:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// v9
|
|
|
|
async function migrateOperationScopeIndex(db, txn) {
|
|
|
|
try {
|
|
|
|
const operations = txn.objectStore("operations");
|
|
|
|
operations.deleteIndex("byTypeAndScope");
|
|
|
|
await iterateCursor(operations.openCursor(), (op, key, cur) => {
|
|
|
|
const {typeScopeKey} = op;
|
|
|
|
delete op.typeScopeKey;
|
|
|
|
const [type, scope] = typeScopeKey.split("|");
|
|
|
|
op.scopeTypeKey = encodeScopeTypeKey(scope, type);
|
|
|
|
cur.update(op);
|
|
|
|
});
|
|
|
|
operations.createIndex("byScopeAndType", "scopeTypeKey", {unique: false});
|
|
|
|
} catch (err) {
|
|
|
|
txn.abort();
|
|
|
|
console.error("could not migrate operations", err.stack);
|
|
|
|
}
|
2021-06-03 16:44:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//v10
|
|
|
|
function createTimelineRelationsStore(db) {
|
2021-06-24 16:16:15 +02:00
|
|
|
db.createObjectStore("timelineRelations", {keyPath: "key"});
|
|
|
|
}
|