Rich message rendering #24
3 changed files with 55 additions and 24 deletions
|
@ -51,17 +51,4 @@ 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, simpleEvent}
|
||||
module.exports = {MatrixEvent}
|
||||
|
|
|
@ -1,13 +1,47 @@
|
|||
const {simpleEvent} = require("./event")
|
||||
const {MatrixEvent} = require("./event")
|
||||
|
||||
const UnknownMembership = simpleEvent((e) => e.type == "m.room.member", (e) => "unknown membership event")
|
||||
|
||||
function createMembershipEvent(membership, message) {
|
||||
return simpleEvent((e) => e.type == "m.room.member" && e.content.membership === membership, message)
|
||||
class MembershipEvent extends MatrixEvent {
|
||||
static canRender(event) {
|
||||
return event.type == "m.room.member"
|
||||
}
|
||||
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.
|
||||
}
|
||||
|
||||
const JoinedEvent = createMembershipEvent("join", (e) => "joined the room")
|
||||
const InvitedEvent = createMembershipEvent("invite", (e) => `invited ${e.content.displayname} the room`)
|
||||
const LeaveEvent = createMembershipEvent("leave", () => "left the room")
|
||||
|
||||
class JoinedEvent extends MembershipEvent {
|
||||
static canRender(event) {
|
||||
return super.canRender(event) && event.content.membership === "join"
|
||||
}
|
||||
render() {
|
||||
super.render()
|
||||
return this.text("joined the room")
|
||||
}
|
||||
}
|
||||
|
||||
class InvitedEvent extends MembershipEvent {
|
||||
static canRender(event) {
|
||||
return super.canRender(event) && event.content.membership === "invite"
|
||||
}
|
||||
render() {
|
||||
super.render()
|
||||
return this.text(`invited ${this.data.content.displayname}`)
|
||||
}
|
||||
}
|
||||
|
||||
class LeaveEvent extends MembershipEvent {
|
||||
static canRender(event) {
|
||||
return super.canRender(event) && event.content.membership === "leave"
|
||||
}
|
||||
render() {
|
||||
super.render()
|
||||
return this.text(`left the room`)
|
||||
}
|
||||
}
|
||||
|
||||
class UnknownMembership extends MembershipEvent {
|
||||
render() {
|
||||
super.render()
|
||||
return this.text("unknown membership event")
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = [JoinedEvent, InvitedEvent, LeaveEvent, UnknownMembership]
|
||||
|
|
|
@ -1,3 +1,13 @@
|
|||
const {simpleEvent} = require("./event")
|
||||
const UnknownEvent = simpleEvent(() => true, () => "Cannot render event")
|
||||
const {MatrixEvent} = require("./event")
|
||||
|
||||
class UnknownEvent extends MatrixEvent {
|
||||
static canRender() {
|
||||
return true
|
||||
}
|
||||
render() {
|
||||
super.render()
|
||||
return this.text(`cannot render event`)
|
||||
}
|
||||
}
|
||||
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