mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2025-01-22 18:21:39 +01:00
use enum for device tracking status
This commit is contained in:
parent
9789e5881d
commit
1dc3acad03
@ -29,20 +29,22 @@ import type {Transaction} from "../storage/idb/Transaction";
|
|||||||
import type * as OlmNamespace from "@matrix-org/olm";
|
import type * as OlmNamespace from "@matrix-org/olm";
|
||||||
type Olm = typeof OlmNamespace;
|
type Olm = typeof OlmNamespace;
|
||||||
|
|
||||||
const TRACKING_STATUS_OUTDATED = 0;
|
enum DeviceTrackingStatus {
|
||||||
const TRACKING_STATUS_UPTODATE = 1;
|
Outdated = 0,
|
||||||
|
UpToDate = 1
|
||||||
|
}
|
||||||
|
|
||||||
export type UserIdentity = {
|
export type UserIdentity = {
|
||||||
userId: string,
|
userId: string,
|
||||||
roomIds: string[],
|
roomIds: string[],
|
||||||
deviceTrackingStatus: number,
|
deviceTrackingStatus: DeviceTrackingStatus,
|
||||||
}
|
}
|
||||||
|
|
||||||
function createUserIdentity(userId: string, initialRoomId?: string): UserIdentity {
|
function createUserIdentity(userId: string, initialRoomId?: string): UserIdentity {
|
||||||
return {
|
return {
|
||||||
userId: userId,
|
userId: userId,
|
||||||
roomIds: initialRoomId ? [initialRoomId] : [],
|
roomIds: initialRoomId ? [initialRoomId] : [],
|
||||||
deviceTrackingStatus: TRACKING_STATUS_OUTDATED,
|
deviceTrackingStatus: DeviceTrackingStatus.Outdated,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +89,7 @@ export class DeviceTracker {
|
|||||||
const user = await userIdentities.get(userId);
|
const user = await userIdentities.get(userId);
|
||||||
if (user) {
|
if (user) {
|
||||||
log.log({l: "outdated", id: userId});
|
log.log({l: "outdated", id: userId});
|
||||||
user.deviceTrackingStatus = TRACKING_STATUS_OUTDATED;
|
user.deviceTrackingStatus = DeviceTrackingStatus.Outdated;
|
||||||
userIdentities.set(user);
|
userIdentities.set(user);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@ -167,7 +169,7 @@ export class DeviceTracker {
|
|||||||
this._storage.storeNames.crossSigningKeys,
|
this._storage.storeNames.crossSigningKeys,
|
||||||
]);
|
]);
|
||||||
let userIdentity = await txn.userIdentities.get(userId);
|
let userIdentity = await txn.userIdentities.get(userId);
|
||||||
if (userIdentity && userIdentity.deviceTrackingStatus !== TRACKING_STATUS_OUTDATED) {
|
if (userIdentity && userIdentity.deviceTrackingStatus !== DeviceTrackingStatus.Outdated) {
|
||||||
return await txn.crossSigningKeys.get(userId, usage);
|
return await txn.crossSigningKeys.get(userId, usage);
|
||||||
}
|
}
|
||||||
// fetch from hs
|
// fetch from hs
|
||||||
@ -345,7 +347,7 @@ export class DeviceTracker {
|
|||||||
// checked, we could share keys with that user without them being in the room
|
// checked, we could share keys with that user without them being in the room
|
||||||
identity = createUserIdentity(userId);
|
identity = createUserIdentity(userId);
|
||||||
}
|
}
|
||||||
identity.deviceTrackingStatus = TRACKING_STATUS_UPTODATE;
|
identity.deviceTrackingStatus = DeviceTrackingStatus.UpToDate;
|
||||||
txn.userIdentities.set(identity);
|
txn.userIdentities.set(identity);
|
||||||
|
|
||||||
return allDeviceKeys;
|
return allDeviceKeys;
|
||||||
@ -518,9 +520,9 @@ export class DeviceTracker {
|
|||||||
const outdatedUserIds: string[] = [];
|
const outdatedUserIds: string[] = [];
|
||||||
await Promise.all(userIds.map(async userId => {
|
await Promise.all(userIds.map(async userId => {
|
||||||
const i = await txn.userIdentities.get(userId);
|
const i = await txn.userIdentities.get(userId);
|
||||||
if (i && i.deviceTrackingStatus === TRACKING_STATUS_UPTODATE) {
|
if (i && i.deviceTrackingStatus === DeviceTrackingStatus.UpToDate) {
|
||||||
upToDateIdentities.push(i);
|
upToDateIdentities.push(i);
|
||||||
} else if (!i || i.deviceTrackingStatus === TRACKING_STATUS_OUTDATED) {
|
} else if (!i || i.deviceTrackingStatus === DeviceTrackingStatus.Outdated) {
|
||||||
// allow fetching for userIdentities we don't know about yet,
|
// allow fetching for userIdentities we don't know about yet,
|
||||||
// as we don't assume the room is tracked here.
|
// as we don't assume the room is tracked here.
|
||||||
outdatedUserIds.push(userId);
|
outdatedUserIds.push(userId);
|
||||||
@ -599,9 +601,9 @@ export class DeviceTracker {
|
|||||||
// also exclude any userId which doesn't have a userIdentity yet.
|
// also exclude any userId which doesn't have a userIdentity yet.
|
||||||
return identity && identity.roomIds.includes(roomId);
|
return identity && identity.roomIds.includes(roomId);
|
||||||
}) as UserIdentity[]; // undefined has been filter out
|
}) as UserIdentity[]; // undefined has been filter out
|
||||||
const upToDateIdentities = identities.filter(i => i.deviceTrackingStatus === TRACKING_STATUS_UPTODATE);
|
const upToDateIdentities = identities.filter(i => i.deviceTrackingStatus === DeviceTrackingStatus.UpToDate);
|
||||||
const outdatedUserIds = identities
|
const outdatedUserIds = identities
|
||||||
.filter(i => i.deviceTrackingStatus === TRACKING_STATUS_OUTDATED)
|
.filter(i => i.deviceTrackingStatus === DeviceTrackingStatus.Outdated)
|
||||||
.map(i => i.userId);
|
.map(i => i.userId);
|
||||||
let devices = await this._devicesForUserIdentities(upToDateIdentities, outdatedUserIds, hsApi, log);
|
let devices = await this._devicesForUserIdentities(upToDateIdentities, outdatedUserIds, hsApi, log);
|
||||||
// filter out our own device as we should never share keys with it.
|
// filter out our own device as we should never share keys with it.
|
||||||
@ -761,12 +763,12 @@ export function tests() {
|
|||||||
assert.deepEqual(await txn.userIdentities.get("@alice:hs.tld"), {
|
assert.deepEqual(await txn.userIdentities.get("@alice:hs.tld"), {
|
||||||
userId: "@alice:hs.tld",
|
userId: "@alice:hs.tld",
|
||||||
roomIds: [roomId],
|
roomIds: [roomId],
|
||||||
deviceTrackingStatus: TRACKING_STATUS_OUTDATED
|
deviceTrackingStatus: DeviceTrackingStatus.Outdated
|
||||||
});
|
});
|
||||||
assert.deepEqual(await txn.userIdentities.get("@bob:hs.tld"), {
|
assert.deepEqual(await txn.userIdentities.get("@bob:hs.tld"), {
|
||||||
userId: "@bob:hs.tld",
|
userId: "@bob:hs.tld",
|
||||||
roomIds: [roomId],
|
roomIds: [roomId],
|
||||||
deviceTrackingStatus: TRACKING_STATUS_OUTDATED
|
deviceTrackingStatus: DeviceTrackingStatus.Outdated
|
||||||
});
|
});
|
||||||
assert.equal(await txn.userIdentities.get("@charly:hs.tld"), undefined);
|
assert.equal(await txn.userIdentities.get("@charly:hs.tld"), undefined);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user