Rich message rendering #24

Merged
cadence merged 28 commits from rich-messages into princess 2020-11-07 10:46:48 +00:00
3 changed files with 20 additions and 39 deletions
Showing only changes of commit e08b895694 - Show all commits

View file

@ -50,6 +50,17 @@ class MatrixEvent extends ElemJS {
}
}
function simpleEvent(filter, render) {
return class extends MatrixEvent {
render() {
super.render()
return this.text(render(this.data))
}
static canRender(event) {
return filter(event)
}
}
}
module.exports = {MatrixEvent}
module.exports = {MatrixEvent, simpleEvent}

View file

@ -1,31 +1,12 @@
const {MatrixEvent} = require("./event")
const {simpleEvent} = require("./event")
const UnknownMembership = simpleEvent((e) => e.type == "m.room.member", (e) => "unknown membership event")
function createMembershipEvent(membership, message) {
return class extends MatrixEvent {
render() {
super.render()
return this.text(message(this.data))
}
static canRender(event) {
return event.type == "m.room.member" && event.content.membership == membership
}
}
}
class UnknownMembership extends MatrixEvent {
render() {
super.render()
return this.text("Unsupported membership event")
}
static canRender(event) {
return event.type == "m.room.member"
}
return simpleEvent((e) => e.type == "m.room.member" && e.content.membership === membership, message)
}
const JoinedEvent = createMembershipEvent("join", () => "joined the room")
const JoinedEvent = createMembershipEvent("join", (e) => {console.log(e); return "joined the room"})
const InvitedEvent = createMembershipEvent("invite", (e) => `invited ${e.content.displayname} the room`)
const LeaveEvent = createMembershipEvent("leave", () => "left the room")

View file

@ -1,15 +1,4 @@
const {MatrixEvent} = require("./event")
class UnknownEvent extends MatrixEvent {
render() {
super.render()
this.text("Cannot render event")
}
static canRender(_event) {
return true
}
}
const {simpleEvent} = require("./event")
const UnknownEvent = simpleEvent(() => true, () => "Cannot render event")
console.log(UnknownEvent)
module.exports = [UnknownEvent]