aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go34
-rw-r--r--matrix/sync.go86
2 files changed, 35 insertions, 85 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index 43020f9..a199dc5 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -343,7 +343,7 @@ func (c *Container) OnLogin() {
c.syncer = NewGomuksSyncer(c.config.Rooms)
if c.crypto != nil {
c.syncer.OnSync(c.crypto.ProcessSyncResponse)
- c.syncer.OnEventType(event.StateMember, func(source EventSource, evt *event.Event) {
+ c.syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
// Don't spam the crypto module with member events of an initial sync
// TODO invalidate all group sessions when clearing cache?
if c.config.AuthCache.InitialSyncDone {
@@ -440,8 +440,8 @@ func (c *Container) Start() {
}
}
-func (c *Container) HandlePreferences(source EventSource, evt *event.Event) {
- if source&EventSourceAccountData == 0 {
+func (c *Container) HandlePreferences(source mautrix.EventSource, evt *event.Event) {
+ if source&mautrix.EventSourceAccountData == 0 {
return
}
orig := c.config.Preferences
@@ -470,7 +470,7 @@ func (c *Container) SendPreferencesToMatrix() {
}
}
-func (c *Container) HandleRedaction(source EventSource, evt *event.Event) {
+func (c *Container) HandleRedaction(source mautrix.EventSource, evt *event.Event) {
room := c.GetOrCreateRoom(evt.RoomID)
var redactedEvt *muksevt.Event
err := c.history.Update(room, evt.Redacts, func(redacted *muksevt.Event) error {
@@ -554,7 +554,7 @@ func (c *Container) HandleReaction(room *rooms.Room, reactsTo id.EventID, reactE
}
}
-func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) {
+func (c *Container) HandleEncrypted(source mautrix.EventSource, mxEvent *event.Event) {
evt, err := c.crypto.DecryptMegolmEvent(mxEvent)
if err != nil {
debug.Print("Failed to decrypt event:", err)
@@ -566,12 +566,12 @@ func (c *Container) HandleEncrypted(source EventSource, mxEvent *event.Event) {
}
// HandleMessage is the event handler for the m.room.message timeline event.
-func (c *Container) HandleMessage(source EventSource, mxEvent *event.Event) {
+func (c *Container) HandleMessage(source mautrix.EventSource, mxEvent *event.Event) {
room := c.GetOrCreateRoom(mxEvent.RoomID)
- if source&EventSourceLeave != 0 {
+ if source&mautrix.EventSourceLeave != 0 {
room.HasLeft = true
return
- } else if source&EventSourceState != 0 {
+ } else if source&mautrix.EventSourceState != 0 {
return
}
@@ -631,9 +631,9 @@ func (c *Container) HandleMessage(source EventSource, mxEvent *event.Event) {
}
// HandleMembership is the event handler for the m.room.member state event.
-func (c *Container) HandleMembership(source EventSource, evt *event.Event) {
- isLeave := source&EventSourceLeave != 0
- isTimeline := source&EventSourceTimeline != 0
+func (c *Container) HandleMembership(source mautrix.EventSource, evt *event.Event) {
+ isLeave := source&mautrix.EventSourceLeave != 0
+ isTimeline := source&mautrix.EventSourceTimeline != 0
if isLeave {
c.GetOrCreateRoom(evt.RoomID).HasLeft = true
}
@@ -702,8 +702,8 @@ func (c *Container) parseReadReceipt(evt *event.Event) (largestTimestampEvent id
return
}
-func (c *Container) HandleReadReceipt(source EventSource, evt *event.Event) {
- if source&EventSourceLeave != 0 {
+func (c *Container) HandleReadReceipt(source mautrix.EventSource, evt *event.Event) {
+ if source&mautrix.EventSourceLeave != 0 {
return
}
@@ -735,7 +735,7 @@ func (c *Container) parseDirectChatInfo(evt *event.Event) map[*rooms.Room]bool {
return directChats
}
-func (c *Container) HandleDirectChatInfo(_ EventSource, evt *event.Event) {
+func (c *Container) HandleDirectChatInfo(_ mautrix.EventSource, evt *event.Event) {
directChats := c.parseDirectChatInfo(evt)
for _, room := range c.config.Rooms.Map {
shouldBeDirect := directChats[room]
@@ -749,7 +749,7 @@ func (c *Container) HandleDirectChatInfo(_ EventSource, evt *event.Event) {
}
// HandlePushRules is the event handler for the m.push_rules account data event.
-func (c *Container) HandlePushRules(_ EventSource, evt *event.Event) {
+func (c *Container) HandlePushRules(_ mautrix.EventSource, evt *event.Event) {
debug.Print("Received updated push rules")
var err error
c.config.PushRules, err = pushrules.EventToPushRules(evt)
@@ -761,7 +761,7 @@ func (c *Container) HandlePushRules(_ EventSource, evt *event.Event) {
}
// HandleTag is the event handler for the m.tag account data event.
-func (c *Container) HandleTag(_ EventSource, evt *event.Event) {
+func (c *Container) HandleTag(_ mautrix.EventSource, evt *event.Event) {
room := c.GetOrCreateRoom(evt.RoomID)
tags := evt.Content.AsTag().Tags
@@ -788,7 +788,7 @@ func (c *Container) HandleTag(_ EventSource, evt *event.Event) {
}
// HandleTyping is the event handler for the m.typing event.
-func (c *Container) HandleTyping(_ EventSource, evt *event.Event) {
+func (c *Container) HandleTyping(_ mautrix.EventSource, evt *event.Event) {
if !c.config.AuthCache.InitialSyncDone {
return
}
diff --git a/matrix/sync.go b/matrix/sync.go
index 2136088..268bf46 100644
--- a/matrix/sync.go
+++ b/matrix/sync.go
@@ -19,70 +19,20 @@
package matrix
import (
- "fmt"
"sync"
"time"
- ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
+ ifc "maunium.net/go/gomuks/interface"
+
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/matrix/rooms"
)
-type EventSource int
-
-const (
- EventSourcePresence EventSource = 1 << iota
- EventSourceJoin
- EventSourceInvite
- EventSourceLeave
- EventSourceAccountData
- EventSourceTimeline
- EventSourceState
- EventSourceEphemeral
- EventSourceToDevice
-)
-
-func (es EventSource) String() string {
- switch {
- case es == EventSourcePresence:
- return "presence"
- case es == EventSourceAccountData:
- return "user account data"
- case es&EventSourceJoin != 0:
- es -= EventSourceJoin
- switch es {
- case EventSourceState:
- return "joined state"
- case EventSourceTimeline:
- return "joined timeline"
- case EventSourceEphemeral:
- return "room ephemeral (joined)"
- case EventSourceAccountData:
- return "room account data (joined)"
- }
- case es&EventSourceInvite != 0:
- es -= EventSourceInvite
- switch es {
- case EventSourceState:
- return "invited state"
- }
- case es&EventSourceLeave != 0:
- es -= EventSourceLeave
- switch es {
- case EventSourceState:
- return "left state"
- case EventSourceTimeline:
- return "left timeline"
- }
- }
- return fmt.Sprintf("unknown (%d)", es)
-}
-
-type EventHandler func(source EventSource, event *event.Event)
+type EventHandler func(source mautrix.EventSource, event *event.Event)
type SyncHandler func(resp *mautrix.RespSync, since string)
type GomuksSyncer struct {
@@ -125,9 +75,9 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
s.notifyGlobalListeners(res, since, callback)
wait.Wait()
- s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence)
+ s.processSyncEvents(nil, res.Presence.Events, mautrix.EventSourcePresence)
s.Progress.Step()
- s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData)
+ s.processSyncEvents(nil, res.AccountData.Events, mautrix.EventSourceAccountData)
s.Progress.Step()
wait.Add(steps)
@@ -171,10 +121,10 @@ func (s *GomuksSyncer) processJoinedRoom(roomID id.RoomID, roomData mautrix.Sync
defer debug.Recover()
room := s.rooms.GetOrCreate(roomID)
room.UpdateSummary(roomData.Summary)
- s.processSyncEvents(room, roomData.State.Events, EventSourceJoin|EventSourceState)
- s.processSyncEvents(room, roomData.Timeline.Events, EventSourceJoin|EventSourceTimeline)
- s.processSyncEvents(room, roomData.Ephemeral.Events, EventSourceJoin|EventSourceEphemeral)
- s.processSyncEvents(room, roomData.AccountData.Events, EventSourceJoin|EventSourceAccountData)
+ s.processSyncEvents(room, roomData.State.Events, mautrix.EventSourceJoin|mautrix.EventSourceState)
+ s.processSyncEvents(room, roomData.Timeline.Events, mautrix.EventSourceJoin|mautrix.EventSourceTimeline)
+ s.processSyncEvents(room, roomData.Ephemeral.Events, mautrix.EventSourceJoin|mautrix.EventSourceEphemeral)
+ s.processSyncEvents(room, roomData.AccountData.Events, mautrix.EventSourceJoin|mautrix.EventSourceAccountData)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@@ -187,7 +137,7 @@ func (s *GomuksSyncer) processInvitedRoom(roomID id.RoomID, roomData mautrix.Syn
defer debug.Recover()
room := s.rooms.GetOrCreate(roomID)
room.UpdateSummary(roomData.Summary)
- s.processSyncEvents(room, roomData.State.Events, EventSourceInvite|EventSourceState)
+ s.processSyncEvents(room, roomData.State.Events, mautrix.EventSourceInvite|mautrix.EventSourceState)
callback()
}
@@ -196,8 +146,8 @@ func (s *GomuksSyncer) processLeftRoom(roomID id.RoomID, roomData mautrix.SyncLe
room := s.rooms.GetOrCreate(roomID)
room.HasLeft = true
room.UpdateSummary(roomData.Summary)
- s.processSyncEvents(room, roomData.State.Events, EventSourceLeave|EventSourceState)
- s.processSyncEvents(room, roomData.Timeline.Events, EventSourceLeave|EventSourceTimeline)
+ s.processSyncEvents(room, roomData.State.Events, mautrix.EventSourceLeave|mautrix.EventSourceState)
+ s.processSyncEvents(room, roomData.Timeline.Events, mautrix.EventSourceLeave|mautrix.EventSourceTimeline)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@@ -206,13 +156,13 @@ func (s *GomuksSyncer) processLeftRoom(roomID id.RoomID, roomData mautrix.SyncLe
callback()
}
-func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*event.Event, source EventSource) {
+func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*event.Event, source mautrix.EventSource) {
for _, evt := range events {
s.processSyncEvent(room, evt, source)
}
}
-func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, evt *event.Event, source EventSource) {
+func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, evt *event.Event, source mautrix.EventSource) {
if room != nil {
evt.RoomID = room.ID
}
@@ -221,11 +171,11 @@ func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, evt *event.Event, sour
switch {
case evt.StateKey != nil:
evt.Type.Class = event.StateEventType
- case source == EventSourcePresence, source&EventSourceEphemeral != 0:
+ case source == mautrix.EventSourcePresence, source&mautrix.EventSourceEphemeral != 0:
evt.Type.Class = event.EphemeralEventType
- case source&EventSourceAccountData != 0:
+ case source&mautrix.EventSourceAccountData != 0:
evt.Type.Class = event.AccountDataEventType
- case source == EventSourceToDevice:
+ case source == mautrix.EventSourceToDevice:
evt.Type.Class = event.ToDeviceEventType
default:
evt.Type.Class = event.MessageEventType
@@ -258,7 +208,7 @@ func (s *GomuksSyncer) OnSync(callback SyncHandler) {
s.globalListeners = append(s.globalListeners, callback)
}
-func (s *GomuksSyncer) notifyListeners(source EventSource, evt *event.Event) {
+func (s *GomuksSyncer) notifyListeners(source mautrix.EventSource, evt *event.Event) {
listeners, exists := s.listeners[evt.Type]
if !exists {
return