- 
          
- 
                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 3 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { type IEvent, MatrixEvent } from "../../../src"; | ||
| import { RoomStickyEvents, RoomStickyEventsEvent } from "../../../src/models/room-sticky-events"; | ||
|  | ||
| describe("RoomStickyEvents", () => { | ||
| let stickyEvents: RoomStickyEvents; | ||
| const stickyEvent: IEvent = { | ||
| 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 RoomStickyEvents(); | ||
| }); | ||
|  | ||
| afterEach(() => { | ||
| stickyEvents?.clear(); | ||
| }); | ||
|  | ||
| describe("addStickyEvents", () => { | ||
| it("should allow adding an event without a msc4354_sticky_key", () => { | ||
| stickyEvents._unstable_addStickyEvent(new MatrixEvent({ ...stickyEvent, content: {} })); | ||
| }); | ||
| it("should not allow adding an event without a msc4354_sticky property", () => { | ||
| expect(() => | ||
| stickyEvents._unstable_addStickyEvent(new MatrixEvent({ ...stickyEvent, msc4354_sticky: undefined })), | ||
| ).toThrow(`${stickyEvent.event_id} is missing msc4354_sticky.duration_ms`); | ||
| expect(() => | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ ...stickyEvent, msc4354_sticky: { duration_ms: undefined } as any }), | ||
| ), | ||
| ).toThrow(`${stickyEvent.event_id} is missing msc4354_sticky.duration_ms`); | ||
| }); | ||
| it("should not allow adding an event without a sender", () => { | ||
| expect(() => | ||
| stickyEvents._unstable_addStickyEvent(new MatrixEvent({ ...stickyEvent, sender: undefined })), | ||
| ).toThrow(`${stickyEvent.event_id} is missing a sender`); | ||
| }); | ||
| it("should ignore old events", () => { | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| origin_server_ts: 0, | ||
| msc4354_sticky: { | ||
| duration_ms: 1, | ||
| }, | ||
| }), | ||
| ), | ||
| ).toEqual({ added: false }); | ||
| }); | ||
| it("should not replace newer events", () => { | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| }), | ||
| ), | ||
| ).toEqual({ added: true }); | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| origin_server_ts: 1, | ||
| }), | ||
| ), | ||
| ).toEqual({ added: false }); | ||
| }); | ||
| it("should not replace events on ID tie break", () => { | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| }), | ||
| ), | ||
| ).toEqual({ added: true }); | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| event_id: "$abc:bar", | ||
| }), | ||
| ), | ||
| ).toEqual({ added: false }); | ||
| }); | ||
| it("should be able to just add an event", () => { | ||
| expect( | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| }), | ||
| ), | ||
| ).toEqual({ added: true }); | ||
| }); | ||
| }); | ||
|  | ||
| describe("_unstable_addStickyEvents(", () => { | ||
| 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._unstable_addStickyEvents([ev]); | ||
| expect([...stickyEvents._unstable_getStickyEvents()]).toEqual([ev]); | ||
| expect(emitSpy).toHaveBeenCalledWith([ev], []); | ||
| }); | ||
| it("should emit when a new unketed sticky event is added", () => { | ||
| const emitSpy = jest.fn(); | ||
| stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
| const ev = new MatrixEvent({ | ||
| ...stickyEvent, | ||
| content: {}, | ||
| }); | ||
| stickyEvents._unstable_addStickyEvents([ev]); | ||
| expect([...stickyEvents._unstable_getStickyEvents()]).toEqual([ev]); | ||
| expect(emitSpy).toHaveBeenCalledWith([ev], []); | ||
| }); | ||
| }); | ||
|  | ||
| describe("getStickyEvents", () => { | ||
| it("should have zero sticky events", () => { | ||
| expect([...stickyEvents._unstable_getStickyEvents()]).toHaveLength(0); | ||
| }); | ||
| it("should contain a sticky event", () => { | ||
| const ev = new MatrixEvent({ | ||
| ...stickyEvent, | ||
| }); | ||
| stickyEvents._unstable_addStickyEvent( | ||
| new MatrixEvent({ | ||
| ...stickyEvent, | ||
| }), | ||
| ); | ||
| expect([...stickyEvents._unstable_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._unstable_addStickyEvent(ev); | ||
| stickyEvents._unstable_addStickyEvent(ev2); | ||
| expect([...stickyEvents._unstable_getStickyEvents()]).toEqual([ev, ev2]); | ||
| }); | ||
| }); | ||
|  | ||
| describe("cleanExpiredStickyEvents", () => { | ||
| beforeAll(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
| afterAll(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|  | ||
| it("should emit when a sticky event expires", () => { | ||
| const emitSpy = jest.fn(); | ||
| stickyEvents.on(RoomStickyEventsEvent.Update, emitSpy); | ||
| jest.setSystemTime(0); | ||
| const ev = new MatrixEvent({ | ||
| ...stickyEvent, | ||
| origin_server_ts: Date.now(), | ||
| }); | ||
| stickyEvents._unstable_addStickyEvent(ev); | ||
| jest.setSystemTime(15000); | ||
| jest.advanceTimersByTime(15000); | ||
| expect(emitSpy).toHaveBeenCalledWith([], [ev]); | ||
| }); | ||
| 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._unstable_addStickyEvents([ev1, ev2]); | ||
| expect(emitSpy).toHaveBeenCalledWith([ev1, ev2], []); | ||
| jest.setSystemTime(15000); | ||
| 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._unstable_addStickyEvent(ev); | ||
| jest.setSystemTime(15000); | ||
| 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.