Rich message rendering #24
3 changed files with 20 additions and 39 deletions
|
@ -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}
|
||||||
|
|
|
@ -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
cadence
commented
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")
|
||||||
|
|
||||||
|
|
|
@ -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]
|
||||||
|
|
Loading…
Reference in a new issue
Not sure how I feel about a function that creates classes. Can we inherit from a base class instead?
That's a lot more verbose, which I don't really like, but sure