-
-
Notifications
You must be signed in to change notification settings - Fork 607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch events that read receipts point to if we don't have them #3960
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6efde6f
Fetch events that read receipts point to if we don't have them
dbkr 5a3203f
Add test for checkForMissingReceiptEvent
dbkr 7bf0985
Throttle calls to checkForMissingReceiptEvent
dbkr 839c17f
Comment
dbkr 1c35fbc
Update test for throttled version
dbkr ecb383e
Make inner function async
dbkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { throttle } from "../../../src/utils/throttle"; | ||
|
||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
describe("throttle", () => { | ||
let mockFn: jest.Mock; | ||
let throttledFn: ReturnType<typeof throttle>; | ||
|
||
beforeEach(() => { | ||
mockFn = jest.fn(); | ||
throttledFn = throttle(mockFn, 100); | ||
|
||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
|
||
it("should throttle multiple successive calls to a single call", () => { | ||
throttledFn(); | ||
throttledFn(); | ||
throttledFn(); | ||
|
||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should execute the function each time the delay elapses", () => { | ||
throttledFn(); | ||
// should execute here (leading edge) | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(40); | ||
throttledFn(); | ||
// delay hasn't elapsed yet: don't execute | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(40); | ||
throttledFn(); | ||
// still hasn't elapsed | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(40); | ||
throttledFn(); | ||
// delay has now elapsed | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should execute the function on leading & trailing edge", () => { | ||
// call it twice... | ||
throttledFn(); | ||
throttledFn(); | ||
// This should have executed the first call but not the second | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(110); | ||
// now the second call should have been executed | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should only execute once if the function is called once", () => { | ||
// call it once... | ||
throttledFn(); | ||
// This should have executed | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
|
||
jest.advanceTimersByTime(110); | ||
// still should only have executed once | ||
expect(mockFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should pass arguments to the throttled function", () => { | ||
const arg1 = "arg1"; | ||
const arg2 = 123; | ||
|
||
throttledFn(arg1, arg2); | ||
|
||
expect(mockFn).toHaveBeenCalledWith(arg1, arg2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import { logger } from "../logger"; | |
import { ReadReceipt } from "./read-receipt"; | ||
import { CachedReceiptStructure, Receipt, ReceiptType } from "../@types/read_receipts"; | ||
import { Feature, ServerSupport } from "../feature"; | ||
import { throttle } from "../utils/throttle"; | ||
|
||
export enum ThreadEvent { | ||
New = "Thread.new", | ||
|
@@ -140,6 +141,8 @@ export class Thread extends ReadReceipt<ThreadEmittedEvents, ThreadEventHandlerM | |
*/ | ||
public replayEvents: MatrixEvent[] | null = []; | ||
|
||
private isCheckingForMissingReceiptEvent = false; | ||
|
||
public constructor(public readonly id: string, public rootEvent: MatrixEvent | undefined, opts: IThreadOpts) { | ||
super(); | ||
|
||
|
@@ -624,6 +627,8 @@ export class Thread extends ReadReceipt<ThreadEmittedEvents, ThreadEventHandlerM | |
} | ||
} | ||
|
||
this.checkForMissingReceiptEvent(); | ||
|
||
this.emit(ThreadEvent.Update, this); | ||
} | ||
|
||
|
@@ -656,6 +661,49 @@ export class Thread extends ReadReceipt<ThreadEmittedEvents, ThreadEventHandlerM | |
} | ||
} | ||
|
||
/** | ||
* Workaround for servers that don't support MSC3981: if we can't see the event that the threaded | ||
* read receipt is pointing to, fetch it by event ID so that we can use it in our unread calculation. | ||
* This is mainly for situations where the read receipt is pointing to a relation event that we don't | ||
* have because it's too far back in the scrollback for us to have paginated it and it doesn't come down | ||
* with the thread events due to lack of MSC3981 support. | ||
* | ||
* If you are reading this in the future and MSC3981 is merged and widely supported then you, future | ||
* space person, get the honour of building a large pyre to incinerate this piece of code and revel | ||
* in the delicious Typescript fumes. | ||
*/ | ||
private checkForMissingReceiptEvent = throttle((): void => { | ||
if (this.isCheckingForMissingReceiptEvent) return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it might want to queue/debounce rather than drop given the async action may be looking for eventId123 when the RR has moved onto eventId456 via a gappy sync during startup |
||
this.isCheckingForMissingReceiptEvent = true; | ||
|
||
(async (): Promise<void> => { | ||
try { | ||
const myThreadedReceipt = this.getLatestReceipt(this.client.getUserId()!, true); | ||
// We check there are messages in the timeline as it doesn't really make sense to check for missing | ||
// events if there are no events in the thread at all (yet). | ||
if (myThreadedReceipt && this.timeline.length > 0 && !this.findEventById(myThreadedReceipt.eventId)) { | ||
logger.info( | ||
`Found threaded read receipt in thread ${this.id} referencing unknown event ${myThreadedReceipt.eventId}: attempting to fetch event`, | ||
); | ||
|
||
try { | ||
const rawEvent = await this.client.fetchRoomEvent(this.roomId, myThreadedReceipt.eventId); | ||
const ev = new MatrixEvent(rawEvent); | ||
await this.client.decryptEventIfNeeded(ev); | ||
this.insertEventIntoTimeline(ev); | ||
logger.info( | ||
`Found and inserted event for read receipt: ${myThreadedReceipt.eventId} in thread ${this.id}`, | ||
); | ||
} catch (e) { | ||
logger.warn("Failed to fetch event referenced by read receipt", e); | ||
} | ||
} | ||
} finally { | ||
this.isCheckingForMissingReceiptEvent = false; | ||
} | ||
})(); | ||
}, 1000); | ||
|
||
public setEventMetadata(event: Optional<MatrixEvent>): void { | ||
if (event) { | ||
EventTimeline.setEventMetadata(event, this.roomState, false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
type ThrottleFunction = (...args: any[]) => void; | ||
|
||
/** | ||
* Throttles a function to only be called once per `delay` milliseconds. | ||
* Simple, dependency-free replacement for lodash's throttle. This version executes | ||
* on the leading and trailing edge. | ||
* @param fn - The function to throttle. | ||
* @param delay - The delay in milliseconds. | ||
* @returns The throttled function. | ||
*/ | ||
export function throttle(fn: ThrottleFunction, delay: number): ThrottleFunction { | ||
let lastExecutionTime = 0; | ||
let timeout: ReturnType<typeof setTimeout>; | ||
|
||
return (...args: any[]) => { | ||
const currentTime = Date.now(); | ||
|
||
if (currentTime - lastExecutionTime < delay) { | ||
if (timeout !== undefined) { | ||
clearTimeout(timeout); | ||
} | ||
|
||
timeout = setTimeout(() => { | ||
lastExecutionTime = currentTime; | ||
fn(...args); | ||
}, delay); | ||
} else { | ||
lastExecutionTime = currentTime; | ||
fn(...args); | ||
} | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it deliberate that this lacks an
await
? If so, might be good to comment it to highlight and explain why.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't, but it now has the throttle where the wrapper doesn't return a promise.