~geb/sup

b4a3cba7178a538149cbaf190c67c3072393abb1 — John Gebbie 1 year, 11 months ago 9f6afa7
simplify event hooks
1 files changed, 16 insertions(+), 15 deletions(-)

M main.go
M main.go => main.go +16 -15
@@ 365,54 365,55 @@ func (app *App) sync() chan func() {
		}
	})

	onEvent := func(t event.Type, h mautrix.EventHandler) {
		syncer.OnEventType(t, func(src mautrix.EventSource, ev *event.Event) {
	hook := func(t event.Type, f func(ev *event.Event)) {
		syncer.OnEventType(t, func(_ mautrix.EventSource, ev *event.Event) {
			updates <- func() {
				h(src, ev)
				f(ev)
			}
		})
	}

	onEvent(event.StateEncryption, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateEncryption, func(ev *event.Event) {
		app.refreshRoomDetails(ev.RoomID)
	})
	onEvent(event.StateCanonicalAlias, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateCanonicalAlias, func(ev *event.Event) {
		app.refreshRoomDetails(ev.RoomID)
	})
	onEvent(event.StateCreate, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateCreate, func(ev *event.Event) {
		app.refreshRoomSoul(ev.RoomID)
	})
	onEvent(event.StateHistoryVisibility, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateHistoryVisibility, func(ev *event.Event) {
		app.refreshRoomDetails(ev.RoomID)
	})
	onEvent(event.StateJoinRules, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateJoinRules, func(ev *event.Event) {
		app.refreshRoomDetails(ev.RoomID)
	})
	onEvent(event.StateMember, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateMember, func(ev *event.Event) {
		if ev.Sender == app.Client.UserID {
			app.refreshRoomSoul(ev.RoomID)
		}
		app.AddMessage(eventMessage(ev))
	})
	onEvent(event.StateRoomName, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateRoomName, func(ev *event.Event) {
		app.refreshRoomName(ev.RoomID)
	})
	onEvent(event.StateTopic, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.StateTopic, func(ev *event.Event) {
		app.refreshRoomDetails(ev.RoomID)
	})

	onEvent(event.EventEncrypted, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.EventEncrypted, func(ev *event.Event) {
		app.AddMessage(encryptedMessage(ev))
	})
	onEvent(event.EventMessage, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.EventMessage, func(ev *event.Event) {
		app.AddMessage(normalMessage(ev))
	})

	onEvent(event.AccountDataDirectChats, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.AccountDataDirectChats, func(ev *event.Event) {
		app.storeDirectChats(ev.Content.AsDirectChats())
		app.refreshRoomSoul(ev.RoomID)
	})

	onEvent(event.EphemeralEventReceipt, func(src mautrix.EventSource, ev *event.Event) {
	hook(event.EphemeralEventReceipt, func(ev *event.Event) {
		c := ev.Content.AsReceipt()
		for eid, receipts := range *c {
			if userReceipts, ok := receipts[event.ReceiptTypeRead]; ok {