-
-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69358f8
commit 1811390
Showing
4 changed files
with
297 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { MatrixEvent } from "../../../src/models/event"; | ||
import { Thread } from "../../../src/models/thread"; | ||
|
||
const mockMessage = (id: string, body: string, sender: string, replyId?: string) => { | ||
const opts = { | ||
content: { | ||
body, | ||
}, | ||
event_id: id, | ||
origin_server_ts: 1628082832517, | ||
sender: sender, | ||
type: "m.room.message", | ||
room_id: "!room123:hs1", | ||
}; | ||
|
||
if (replyId) { | ||
opts.content["m.relates_to"] = { | ||
"m.in_reply_to": { | ||
"event_id": replyId, | ||
}, | ||
}; | ||
} | ||
|
||
return new MatrixEvent(opts); | ||
}; | ||
|
||
const mockAnnotation = (id: string, body: string, sender: string, replyId: string) => { | ||
return new MatrixEvent({ | ||
"content": { | ||
"m.relates_to": { | ||
"event_id": replyId, | ||
"key": body, | ||
"rel_type": "m.annotation", | ||
}, | ||
}, | ||
"origin_server_ts": 1628084947352, | ||
"sender": sender, | ||
"type": "m.reaction", | ||
"event_id": id, | ||
"room_id": "!room123:hs1", | ||
}); | ||
}; | ||
|
||
const mockThread = () => { | ||
const events = [ | ||
mockMessage("event1", "Hello", "alice"), | ||
mockMessage("event2", "Bonjour", "bob", "event1"), | ||
mockMessage("event3", "How are you?", "bob", "event2"), | ||
]; | ||
|
||
return new Thread(events); | ||
}; | ||
|
||
describe('Thread', () => { | ||
it('should count participants', () => { | ||
const thread = mockThread(); | ||
|
||
expect(thread.participants.size).toBe(2); | ||
|
||
thread.addEvent(mockMessage("event4", "Ça va?", "bob", "event2")); | ||
thread.addEvent(mockMessage("event5", "Cześć", "charlie", "event2")); | ||
|
||
expect(thread.participants.size).toBe(3); | ||
}); | ||
|
||
it('should store reference to root and tails', () => { | ||
const thread = mockThread(); | ||
expect(thread.id).toBe("event1"); | ||
expect(thread.tail.size).toBe(1); | ||
|
||
thread.addEvent(mockMessage("event4", "Ça va?", "bob", "event2")); | ||
expect(thread.tail.size).toBe(2); | ||
|
||
thread.addEvent(mockMessage("event5", "Ça va?", "bob", "event4")); | ||
expect(thread.tail.size).toBe(2); | ||
|
||
thread.addEvent(mockMessage("event6", "Ça va?", "bob", "event1")); | ||
expect(thread.tail.size).toBe(3); | ||
}); | ||
|
||
it('should only count message events', () => { | ||
const thread = mockThread(); | ||
expect(thread.length).toBe(3); | ||
|
||
thread.addEvent(mockMessage("event4", "Ça va?", "bob", "event2")); | ||
expect(thread.length).toBe(4); | ||
|
||
const reaction = mockAnnotation("event6", "✅", "bob", "event2"); | ||
|
||
thread.addEvent(reaction); | ||
|
||
expect(thread.length).toBe(4); | ||
expect(thread.eventTimeline.length).toBe(5); | ||
}); | ||
|
||
it('tails event can only be m.room.message', () => { | ||
const thread = mockThread(); | ||
expect(thread.length).toBe(3); | ||
|
||
const reaction = mockAnnotation("event10", "✅", "bob", "event2"); | ||
thread.addEvent(reaction); | ||
|
||
expect(Array.from(thread.tail)[0]).toBe("event3"); | ||
}); | ||
}); |
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
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,101 @@ | ||
/* | ||
Copyright 2021 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. | ||
*/ | ||
|
||
import { EventType } from "../@types/event"; | ||
import { MatrixEvent } from "./event"; | ||
|
||
export class Thread { | ||
private root: string; | ||
public tail = new Set<string>(); | ||
private events = new Map<string, MatrixEvent>(); | ||
private _messageCount = 0; | ||
|
||
constructor(events: MatrixEvent[] = []) { | ||
events.forEach(event => this.addEvent(event)); | ||
} | ||
|
||
/** | ||
* Add an event to the thread and updates | ||
* the tail/root references if needed | ||
* @param event The event to add | ||
*/ | ||
public addEvent(event: MatrixEvent): void { | ||
if (this.events.has(event.getId())) { | ||
return; | ||
} | ||
|
||
const isRoomMessage = event.getType() === EventType.RoomMessage; | ||
|
||
if (this.tail.has(event.replyEventId)) { | ||
this.tail.delete(event.replyEventId); | ||
} | ||
this.tail.add(event.getId()); | ||
|
||
if (!event.replyEventId && isRoomMessage) { | ||
this.root = event.getId(); | ||
this.events.forEach(event => event.setThreadRoot(this.root)); | ||
} | ||
|
||
if (isRoomMessage) { | ||
this._messageCount++; | ||
} | ||
|
||
this.events.set(event.getId(), event); | ||
|
||
if (this.root) { | ||
event.setThreadRoot(this.root); | ||
} | ||
} | ||
|
||
/** | ||
* A sorted list of events to display | ||
*/ | ||
public get eventTimeline(): MatrixEvent[] { | ||
return Array.from(this.events.values()) | ||
.sort((a, b) => a.getTs() - b.getTs()); | ||
} | ||
|
||
/** | ||
* The thread ID, which is the same as the root event ID | ||
*/ | ||
public get id(): string { | ||
return this.root; | ||
} | ||
|
||
public get rootEvent(): MatrixEvent { | ||
return this.events.get(this.root); | ||
} | ||
|
||
/** | ||
* The number of messages in the thread | ||
*/ | ||
public get length(): number { | ||
return this._messageCount; | ||
} | ||
|
||
/** | ||
* A set of mxid participating to the thread | ||
*/ | ||
public get participants(): Set<string> { | ||
const participants = new Set<string>(); | ||
this.events.forEach(event => { | ||
if (event.getType() === EventType.RoomMessage) { | ||
participants.add(event.getSender()); | ||
} | ||
}); | ||
return participants; | ||
} | ||
} |