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) {

Not sure how I feel about a function that creates classes. Can we inherit from a base class instead?

Not sure how I feel about a function that creates classes. Can we inherit from a base class instead?
Outdated
Review

That's a lot more verbose, which I don't really like, but sure

That's a lot more verbose, which I don't really like, but sure
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) { function createMembershipEvent(membership, message) {
return class extends MatrixEvent { return simpleEvent((e) => e.type == "m.room.member" && e.content.membership === membership, message)
bad marked this conversation as resolved Outdated

Again here. I think a better way would be to have a base class like MembershipEvent, and then for each kind of membership we subclass it and only implement the canRender method on each.

Again here. I think a better way would be to have a base class like MembershipEvent, and then for each kind of membership we subclass it and only implement the canRender method on each.
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"
}
} }
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 InvitedEvent = createMembershipEvent("invite", (e) => `invited ${e.content.displayname} the room`)
const LeaveEvent = createMembershipEvent("leave", () => "left the room") const LeaveEvent = createMembershipEvent("leave", () => "left the room")

View file

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