-
-
Notifications
You must be signed in to change notification settings - Fork 648
Implement Sticky Events MSC4354 #5028
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bf14e37
Implement Sticky Events MSC
Half-Shot aef07fc
Renames
Half-Shot dc69e8b
lint
Half-Shot 437a37a
some review work
Half-Shot 831a87e
Update for support for 4-ples
Half-Shot bec246d
fix lint
Half-Shot 3019576
pull through method
Half-Shot dae2f39
Fix the mistake
Half-Shot 48a8e37
More tests to appease SC
Half-Shot bf2835b
Merge branch 'develop' into hs/sticky-events
Half-Shot 79aa439
Cleaner code
Half-Shot 7b7f74d
Review cleanup
Half-Shot ffdca00
Refactors based on review.
Half-Shot 5601a0d
lint
Half-Shot ba557ed
Merge remote-tracking branch 'origin/develop' into hs/sticky-events
Half-Shot ff77848
Store sticky event expiry TS at insertion time.
Half-Shot 5a7de2e
proper type
Half-Shot 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 hidden or 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 hidden or 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,259 @@ | ||
import { type IStickyEvent, MatrixEvent } from "../../../src"; | ||
import { RoomStickyEventsStore, RoomStickyEventsEvent } from "../../../src/models/room-sticky-events"; | ||
|
||
describe("RoomStickyEvents", () => { | ||
let stickyEvents: RoomStickyEventsStore; | ||
const stickyEvent: IStickyEvent = { | ||
event_id: "$foo:bar", | ||
room_id: "!roomId", | ||
type: "org.example.any_type", | ||
msc4354_sticky: { | ||
duration_ms: 15000, | ||
}, | ||
content: { | ||
msc4354_sticky_key: "foobar", | ||
}, | ||
sender: "@alice:example.org", | ||
origin_server_ts: Date.now(), | ||
unsigned: {}, | ||
}; | ||
|
||
beforeEach(() => { | ||
stickyEvents = new RoomStickyEventsStore(); | ||
}); | ||
|
||
afterEach(() => { | ||
stickyEvents?.clear(); | ||
}); | ||
|
||
describe("addStickyEvents", () => { | ||
it("should allow adding an event without a msc4354_sticky_key", () => { | ||
stickyEvents.addStickyEvents([new MatrixEvent({ ...stickyEvent, content: {} })]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(1); | ||
}); | ||
it("should not allow adding an event without a msc4354_sticky property", () => { | ||
stickyEvents.addStickyEvents([new MatrixEvent({ ...stickyEvent, msc4354_sticky: undefined })]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
stickyEvents.addStickyEvents([ | ||
new MatrixEvent({ ...stickyEvent, msc4354_sticky: { duration_ms: undefined } as any }), | ||
]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
}); | ||
it("should not allow adding an event without a sender", () => { | ||
stickyEvents.addStickyEvents([new MatrixEvent({ ...stickyEvent, sender: undefined })]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
}); | ||
it("should not allow adding an event with an invalid sender", () => { | ||
stickyEvents.addStickyEvents([new MatrixEvent({ ...stickyEvent, sender: "not_a_real_sender" })]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
}); | ||
it("should ignore old events", () => { | ||
stickyEvents.addStickyEvents([ | ||
new MatrixEvent({ ...stickyEvent, origin_server_ts: 0, msc4354_sticky: { duration_ms: 1 } }), | ||
]); | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
}); | ||
it("should be able to just add an event", () => { | ||
const originalEv = new MatrixEvent({ ...stickyEvent }); | ||
stickyEvents.addStickyEvents([originalEv]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([originalEv]); | ||
}); | ||
it("should not replace events on ID tie break", () => { | ||
const originalEv = new MatrixEvent({ ...stickyEvent }); | ||
stickyEvents.addStickyEvents([originalEv]); | ||
stickyEvents.addStickyEvents([ | ||
new MatrixEvent({ | ||
...stickyEvent, | ||
event_id: "$abc:bar", | ||
}), | ||
]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([originalEv]); | ||
}); | ||
it("should not replace a newer event with an older event", () => { | ||
const originalEv = new MatrixEvent({ ...stickyEvent }); | ||
stickyEvents.addStickyEvents([originalEv]); | ||
stickyEvents.addStickyEvents([ | ||
new MatrixEvent({ | ||
...stickyEvent, | ||
origin_server_ts: 1, | ||
}), | ||
]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([originalEv]); | ||
}); | ||
it("should replace an older event with a newer event", () => { | ||
const originalEv = new MatrixEvent({ ...stickyEvent }); | ||
const newerEv = new MatrixEvent({ | ||
...stickyEvent, | ||
origin_server_ts: Date.now() + 2000, | ||
}); | ||
stickyEvents.addStickyEvents([originalEv]); | ||
stickyEvents.addStickyEvents([newerEv]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([newerEv]); | ||
}); | ||
it("should allow multiple events with the same sticky key for different event types", () => { | ||
const originalEv = new MatrixEvent({ ...stickyEvent }); | ||
const anotherEv = new MatrixEvent({ | ||
...stickyEvent, | ||
type: "org.example.another_type", | ||
}); | ||
stickyEvents.addStickyEvents([originalEv, anotherEv]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([originalEv, anotherEv]); | ||
}); | ||
|
||
it("should emit when a new sticky event is added", () => { | ||
const emitSpy = jest.fn(); | ||
stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([ev]); | ||
expect(emitSpy).toHaveBeenCalledWith([ev], []); | ||
}); | ||
it("should emit when a new unkeyed sticky event is added", () => { | ||
const emitSpy = jest.fn(); | ||
stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
content: {}, | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([ev]); | ||
expect(emitSpy).toHaveBeenCalledWith([ev], []); | ||
}); | ||
}); | ||
|
||
describe("getStickyEvents", () => { | ||
it("should have zero sticky events", () => { | ||
expect([...stickyEvents.getStickyEvents()]).toHaveLength(0); | ||
}); | ||
it("should contain a sticky event", () => { | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([ev]); | ||
}); | ||
it("should contain two sticky events", () => { | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
}); | ||
const ev2 = new MatrixEvent({ | ||
...stickyEvent, | ||
sender: "@fibble:bobble", | ||
content: { | ||
msc4354_sticky_key: "bibble", | ||
}, | ||
}); | ||
stickyEvents.addStickyEvents([ev, ev2]); | ||
expect([...stickyEvents.getStickyEvents()]).toEqual([ev, ev2]); | ||
}); | ||
}); | ||
|
||
describe("getKeyedStickyEvent", () => { | ||
it("should have zero sticky events", () => { | ||
expect( | ||
stickyEvents.getKeyedStickyEvent( | ||
stickyEvent.sender, | ||
stickyEvent.type, | ||
stickyEvent.content.msc4354_sticky_key!, | ||
), | ||
).toBeUndefined(); | ||
}); | ||
it("should return a sticky event", () => { | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
expect( | ||
stickyEvents.getKeyedStickyEvent( | ||
stickyEvent.sender, | ||
stickyEvent.type, | ||
stickyEvent.content.msc4354_sticky_key!, | ||
), | ||
).toEqual(ev); | ||
}); | ||
}); | ||
|
||
describe("getUnkeyedStickyEvent", () => { | ||
it("should have zero sticky events", () => { | ||
expect(stickyEvents.getUnkeyedStickyEvent(stickyEvent.sender, stickyEvent.type)).toEqual([]); | ||
}); | ||
it("should return a sticky event", () => { | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
content: { | ||
msc4354_sticky_key: undefined, | ||
}, | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
expect(stickyEvents.getUnkeyedStickyEvent(stickyEvent.sender, stickyEvent.type)).toEqual([ev]); | ||
}); | ||
}); | ||
|
||
describe("cleanExpiredStickyEvents", () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it("should emit when a sticky event expires", () => { | ||
jest.setSystemTime(0); | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
origin_server_ts: Date.now(), | ||
}); | ||
const evLater = new MatrixEvent({ | ||
...stickyEvent, | ||
event_id: "$baz:bar", | ||
sender: "@bob:example.org", | ||
origin_server_ts: Date.now() + 1000, | ||
}); | ||
stickyEvents.addStickyEvents([ev, evLater]); | ||
const emitSpy = jest.fn(); | ||
stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
jest.advanceTimersByTime(15000); | ||
expect(emitSpy).toHaveBeenCalledWith([], [ev]); | ||
// Then expire the next event | ||
jest.advanceTimersByTime(1000); | ||
expect(emitSpy).toHaveBeenCalledWith([], [evLater]); | ||
}); | ||
it("should emit two events when both expire at the same time", () => { | ||
const emitSpy = jest.fn(); | ||
stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
jest.setSystemTime(0); | ||
const ev1 = new MatrixEvent({ | ||
...stickyEvent, | ||
event_id: "$eventA", | ||
origin_server_ts: 0, | ||
}); | ||
const ev2 = new MatrixEvent({ | ||
...stickyEvent, | ||
event_id: "$eventB", | ||
content: { | ||
msc4354_sticky_key: "key_2", | ||
}, | ||
origin_server_ts: 0, | ||
}); | ||
stickyEvents.addStickyEvents([ev1, ev2]); | ||
expect(emitSpy).toHaveBeenCalledWith([ev1, ev2], []); | ||
jest.advanceTimersByTime(15000); | ||
expect(emitSpy).toHaveBeenCalledWith([], [ev1, ev2]); | ||
}); | ||
it("should emit when a unkeyed sticky event expires", () => { | ||
const emitSpy = jest.fn(); | ||
stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
jest.setSystemTime(0); | ||
const ev = new MatrixEvent({ | ||
...stickyEvent, | ||
content: {}, | ||
origin_server_ts: Date.now(), | ||
}); | ||
stickyEvents.addStickyEvents([ev]); | ||
jest.advanceTimersByTime(15000); | ||
expect(emitSpy).toHaveBeenCalledWith([], [ev]); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.