Skip to content

Commit

Permalink
quick handle for redacted beacons
Browse files Browse the repository at this point in the history
Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry Archibald committed Apr 1, 2022
1 parent e2f122f commit 8223b77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/models/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export enum BeaconEvent {
Update = "Beacon.update",
LivenessChange = "Beacon.LivenessChange",
LocationUpdate = "LocationUpdate",
Destroy = "Destroy"
}

export type BeaconEventHandlerMap = {
[BeaconEvent.Update]: (event: MatrixEvent, beacon: Beacon) => void;
[BeaconEvent.LivenessChange]: (isLive: boolean, beacon: Beacon) => void;
[BeaconEvent.LocationUpdate]: (locationState: BeaconLocationState) => void;
[BeaconEvent.Destroy]: (beaconIdentifier: string) => void;
};

export const isTimestampInDuration = (
Expand Down Expand Up @@ -103,6 +105,7 @@ export class Beacon extends TypedEventEmitter<Exclude<BeaconEvent, BeaconEvent.N
if (this.livenessWatchInterval) {
clearInterval(this.livenessWatchInterval);
}
this.emit(BeaconEvent.Destroy, this.identifier);
}

/**
Expand All @@ -126,6 +129,8 @@ export class Beacon extends TypedEventEmitter<Exclude<BeaconEvent, BeaconEvent.N
// TODO is name confusing while these are m.beacon events ?
public addLocations(locationEvents: MatrixEvent[]): void {
// discard locations for beacons that are not live

console.log('hhh addLocation', JSON.stringify(locationEvents));
if (!this.isLive) {
return;
}
Expand Down
24 changes: 19 additions & 5 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,8 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
return;
}

// console.log('hhh', 'RS.processBeaconEvents', this.beacons.keys(), events);


// names are confusing here
// a Beacon is the 'parent' event, but event type is 'm.beacon_info'
// a Beacon is the parent event, but event type is 'm.beacon_info'
// a location is the 'child' related to the Beacon, but the event type is 'm.beacon'
// group locations by beaconInfoId
const locationEventsByBeaconId = events.reduce<Record<string, MatrixEvent[]>>((acc, event) => {
Expand Down Expand Up @@ -469,15 +466,32 @@ export class RoomState extends TypedEventEmitter<EmittedEvents, EventHandlerMap>
* @experimental
*/
private setBeacon(event: MatrixEvent): void {
console.log('hhh', event.getType(), event.isRedacted(), event.isRedaction(), event);
if (this.beacons.has(event.getType())) {
return this.beacons.get(event.getType()).update(event);
console.log('hhh', 'already got it')

const beacon = this.beacons.get(event.getType());

if (event.isRedacted()) {
beacon.destroy();
// TODO is this right? should be show something in TL
this.beacons.delete(event.getType());
return;
}

return beacon.update(event);
}

if (event.isRedacted()) {
return;
}

const beacon = new Beacon(event);

this.reEmitter.reEmit<BeaconEvent, BeaconEvent>(beacon, [
BeaconEvent.New,
BeaconEvent.Update,
BeaconEvent.Destroy,
BeaconEvent.LivenessChange,
]);

Expand Down
1 change: 1 addition & 0 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class SyncApi {
RoomStateEvent.Update,
BeaconEvent.New,
BeaconEvent.Update,
BeaconEvent.Destroy,
BeaconEvent.LivenessChange,
]);

Expand Down

0 comments on commit 8223b77

Please sign in to comment.