aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go33
-rw-r--r--matrix/sync.go74
2 files changed, 69 insertions, 38 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index cb986ee..6d94e2e 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -40,7 +40,6 @@ import (
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface"
- "maunium.net/go/gomuks/lib/bfhtml"
"maunium.net/go/gomuks/matrix/pushrules"
"maunium.net/go/gomuks/matrix/rooms"
)
@@ -474,28 +473,18 @@ func (c *Container) SendMessage(roomID string, msgtype mautrix.MessageType, text
}
func (c *Container) renderMarkdown(text string) string {
- parser := blackfriday.New(
- blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
- blackfriday.Tables |
- blackfriday.FencedCode |
- blackfriday.Strikethrough |
- blackfriday.SpaceHeadings |
- blackfriday.DefinitionLists))
- ast := parser.Parse([]byte(text))
-
- renderer := bfhtml.HTMLRenderer{
- HTMLRenderer: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
- Flags: blackfriday.UseXHTML,
- }),
- }
-
- var buf strings.Builder
- renderer.RenderHeader(&buf, ast)
- ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
- return renderer.RenderNode(&buf, node, entering)
+ renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
+ Flags: blackfriday.UseXHTML,
})
- renderer.RenderFooter(&buf, ast)
- return buf.String()
+
+ return strings.ReplaceAll(string(blackfriday.Run([]byte(text),
+ blackfriday.WithExtensions(blackfriday.NoIntraEmphasis|
+ blackfriday.Tables|
+ blackfriday.FencedCode|
+ blackfriday.Strikethrough|
+ blackfriday.SpaceHeadings|
+ blackfriday.DefinitionLists),
+ blackfriday.WithRenderer(renderer))), "\n", "")
}
var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)")
diff --git a/matrix/sync.go b/matrix/sync.go
index 837a340..260ba91 100644
--- a/matrix/sync.go
+++ b/matrix/sync.go
@@ -20,6 +20,7 @@ package matrix
import (
"encoding/json"
+ "fmt"
"time"
"maunium.net/go/mautrix"
@@ -46,6 +47,42 @@ const (
EventSourceEphemeral
)
+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 *mautrix.Event)
// GomuksSyncer is the default syncing implementation. You can either write your own syncer, or selectively
@@ -70,15 +107,15 @@ func NewGomuksSyncer(session SyncerSession) *GomuksSyncer {
// ProcessResponse processes a Matrix sync response.
func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err error) {
debug.Print("Received sync response")
- s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence, false)
- s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData, false)
+ s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence)
+ s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData)
for roomID, roomData := range res.Rooms.Join {
room := s.Session.GetRoom(roomID)
- s.processSyncEvents(room, roomData.State.Events, EventSourceJoin|EventSourceState, false)
- s.processSyncEvents(room, roomData.Timeline.Events, EventSourceJoin|EventSourceTimeline, false)
- s.processSyncEvents(room, roomData.Ephemeral.Events, EventSourceJoin|EventSourceEphemeral, false)
- s.processSyncEvents(room, roomData.AccountData.Events, EventSourceJoin|EventSourceAccountData, false)
+ 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)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@@ -87,14 +124,14 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
for roomID, roomData := range res.Rooms.Invite {
room := s.Session.GetRoom(roomID)
- s.processSyncEvents(room, roomData.State.Events, EventSourceInvite|EventSourceState, false)
+ s.processSyncEvents(room, roomData.State.Events, EventSourceInvite|EventSourceState)
}
for roomID, roomData := range res.Rooms.Leave {
room := s.Session.GetRoom(roomID)
room.HasLeft = true
- s.processSyncEvents(room, roomData.State.Events, EventSourceLeave|EventSourceState, true)
- s.processSyncEvents(room, roomData.Timeline.Events, EventSourceLeave|EventSourceTimeline, false)
+ s.processSyncEvents(room, roomData.State.Events, EventSourceLeave|EventSourceState)
+ s.processSyncEvents(room, roomData.Timeline.Events, EventSourceLeave|EventSourceTimeline)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@@ -109,20 +146,18 @@ func (s *GomuksSyncer) ProcessResponse(res *mautrix.RespSync, since string) (err
return
}
-func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*mautrix.Event, source EventSource, checkStateKey bool) {
+func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*mautrix.Event, source EventSource) {
for _, event := range events {
- if !checkStateKey || event.StateKey != nil {
- s.processSyncEvent(room, event, source)
- }
+ s.processSyncEvent(room, event, source)
}
}
func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, event *mautrix.Event, source EventSource) {
if room != nil {
event.RoomID = room.ID
- }
- if event.Type.Class == mautrix.StateEventType {
- room.UpdateState(event)
+ if source&EventSourceState != 0 || (source&EventSourceTimeline != 0 && event.Type.IsState() && event.StateKey != nil) {
+ room.UpdateState(event)
+ }
}
s.notifyListeners(source, event)
}
@@ -138,6 +173,13 @@ func (s *GomuksSyncer) OnEventType(eventType mautrix.EventType, callback EventHa
}
func (s *GomuksSyncer) notifyListeners(source EventSource, event *mautrix.Event) {
+ if (event.Type.IsState() && source&EventSourceState == 0 && event.StateKey == nil) ||
+ (event.Type.IsAccountData() && source&EventSourceAccountData == 0) ||
+ (event.Type.IsEphemeral() && source&EventSourceEphemeral == 0) {
+ evtJson, _ := json.Marshal(event)
+ debug.Printf("Event of type %s received from mismatching source %s: %s", event.Type.String(), source.String(), string(evtJson))
+ return
+ }
listeners, exists := s.listeners[event.Type]
if !exists {
return