2018-03-21 22:29:58 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Based on https://github.com/matrix-org/gomatrix/blob/master/sync.go
|
|
|
|
|
2018-03-18 20:24:03 +01:00
|
|
|
package matrix
|
2018-03-18 16:34:42 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"maunium.net/go/gomatrix"
|
2018-03-23 22:39:17 +01:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-03-18 16:34:42 +01:00
|
|
|
)
|
|
|
|
|
2018-04-22 22:26:56 +02:00
|
|
|
type SyncerSession interface {
|
|
|
|
GetRoom(id string) *rooms.Room
|
|
|
|
GetUserID() string
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:28:29 +02:00
|
|
|
type EventSource int
|
|
|
|
|
|
|
|
const (
|
2018-05-15 14:39:40 +02:00
|
|
|
EventSourcePresence EventSource = 1 << iota
|
2018-04-30 21:28:29 +02:00
|
|
|
EventSourceJoin
|
|
|
|
EventSourceInvite
|
|
|
|
EventSourceLeave
|
2018-05-15 14:39:40 +02:00
|
|
|
EventSourceAccountData
|
|
|
|
EventSourceTimeline
|
|
|
|
EventSourceState
|
|
|
|
EventSourceEphemeral
|
2018-04-30 21:28:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type EventHandler func(source EventSource, event *gomatrix.Event)
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// GomuksSyncer is the default syncing implementation. You can either write your own syncer, or selectively
|
|
|
|
// replace parts of this default syncer (e.g. the ProcessResponse method). The default syncer uses the observer
|
|
|
|
// pattern to notify callers about incoming events. See GomuksSyncer.OnEventType for more information.
|
|
|
|
type GomuksSyncer struct {
|
2018-04-24 16:12:08 +02:00
|
|
|
Session SyncerSession
|
2018-04-30 21:28:29 +02:00
|
|
|
listeners map[string][]EventHandler // event type to listeners array
|
2018-04-24 16:12:08 +02:00
|
|
|
FirstSyncDone bool
|
|
|
|
InitDoneCallback func()
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGomuksSyncer returns an instantiated GomuksSyncer
|
2018-04-22 22:26:56 +02:00
|
|
|
func NewGomuksSyncer(session SyncerSession) *GomuksSyncer {
|
2018-03-18 16:34:42 +01:00
|
|
|
return &GomuksSyncer{
|
2018-04-01 08:53:00 +02:00
|
|
|
Session: session,
|
2018-04-30 21:28:29 +02:00
|
|
|
listeners: make(map[string][]EventHandler),
|
2018-04-01 08:53:00 +02:00
|
|
|
FirstSyncDone: false,
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:29:58 +01:00
|
|
|
// ProcessResponse processes a Matrix sync response.
|
2018-03-18 16:34:42 +01:00
|
|
|
func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (err error) {
|
2018-05-15 14:39:40 +02:00
|
|
|
s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence, false)
|
|
|
|
s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData, false)
|
2018-03-23 22:39:17 +01:00
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
for roomID, roomData := range res.Rooms.Join {
|
|
|
|
room := s.Session.GetRoom(roomID)
|
2018-05-15 14:39:40 +02:00
|
|
|
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)
|
2018-03-18 16:34:42 +01:00
|
|
|
|
|
|
|
if len(room.PrevBatch) == 0 {
|
|
|
|
room.PrevBatch = roomData.Timeline.PrevBatch
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 22:39:17 +01:00
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
for roomID, roomData := range res.Rooms.Invite {
|
|
|
|
room := s.Session.GetRoom(roomID)
|
2018-05-15 14:39:40 +02:00
|
|
|
s.processSyncEvents(room, roomData.State.Events, EventSourceInvite | EventSourceState, false)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2018-03-23 22:39:17 +01:00
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
for roomID, roomData := range res.Rooms.Leave {
|
|
|
|
room := s.Session.GetRoom(roomID)
|
2018-04-30 21:28:29 +02:00
|
|
|
room.HasLeft = true
|
2018-05-15 14:39:40 +02:00
|
|
|
s.processSyncEvents(room, roomData.State.Events, EventSourceLeave | EventSourceState, true)
|
|
|
|
s.processSyncEvents(room, roomData.Timeline.Events, EventSourceLeave | EventSourceTimeline, false)
|
2018-03-18 16:34:42 +01:00
|
|
|
|
|
|
|
if len(room.PrevBatch) == 0 {
|
|
|
|
room.PrevBatch = roomData.Timeline.PrevBatch
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 22:39:17 +01:00
|
|
|
|
2018-04-24 16:12:08 +02:00
|
|
|
if since == "" && s.InitDoneCallback != nil {
|
|
|
|
s.InitDoneCallback()
|
2018-04-24 01:11:32 +02:00
|
|
|
}
|
2018-04-01 08:53:00 +02:00
|
|
|
s.FirstSyncDone = true
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:39:40 +02:00
|
|
|
func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*gomatrix.Event, source EventSource, checkStateKey bool) {
|
2018-03-23 22:39:17 +01:00
|
|
|
for _, event := range events {
|
|
|
|
if !checkStateKey || event.StateKey != nil {
|
2018-05-15 14:39:40 +02:00
|
|
|
s.processSyncEvent(room, event, source)
|
2018-03-23 22:39:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:39:40 +02:00
|
|
|
func isState(event *gomatrix.Event) bool {
|
|
|
|
switch event.Type {
|
|
|
|
case "m.room.member", "m.room.name", "m.room.topic", "m.room.aliases", "m.room.canonical_alias":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, event *gomatrix.Event, source EventSource) {
|
2018-03-23 22:39:17 +01:00
|
|
|
if room != nil {
|
|
|
|
event.RoomID = room.ID
|
|
|
|
}
|
2018-05-15 14:39:40 +02:00
|
|
|
if isState(event) {
|
2018-03-23 22:39:17 +01:00
|
|
|
room.UpdateState(event)
|
|
|
|
}
|
2018-04-30 21:28:29 +02:00
|
|
|
s.notifyListeners(source, event)
|
2018-03-23 22:39:17 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
// OnEventType allows callers to be notified when there are new events for the given event type.
|
|
|
|
// There are no duplicate checks.
|
2018-04-30 21:28:29 +02:00
|
|
|
func (s *GomuksSyncer) OnEventType(eventType string, callback EventHandler) {
|
2018-03-18 16:34:42 +01:00
|
|
|
_, exists := s.listeners[eventType]
|
|
|
|
if !exists {
|
2018-04-30 21:28:29 +02:00
|
|
|
s.listeners[eventType] = []EventHandler{}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
s.listeners[eventType] = append(s.listeners[eventType], callback)
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:28:29 +02:00
|
|
|
func (s *GomuksSyncer) notifyListeners(source EventSource, event *gomatrix.Event) {
|
2018-03-18 16:34:42 +01:00
|
|
|
listeners, exists := s.listeners[event.Type]
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, fn := range listeners {
|
2018-04-30 21:28:29 +02:00
|
|
|
fn(source, event)
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnFailedSync always returns a 10 second wait period between failed /syncs, never a fatal error.
|
|
|
|
func (s *GomuksSyncer) OnFailedSync(res *gomatrix.RespSync, err error) (time.Duration, error) {
|
|
|
|
return 10 * time.Second, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilterJSON returns a filter with a timeline limit of 50.
|
|
|
|
func (s *GomuksSyncer) GetFilterJSON(userID string) json.RawMessage {
|
2018-05-10 19:56:46 +02:00
|
|
|
filter := &gomatrix.Filter{
|
|
|
|
Room: gomatrix.RoomFilter{
|
|
|
|
IncludeLeave: false,
|
|
|
|
State: gomatrix.FilterPart{
|
|
|
|
Types: []string{
|
2018-04-24 15:51:40 +02:00
|
|
|
"m.room.member",
|
|
|
|
"m.room.name",
|
|
|
|
"m.room.topic",
|
|
|
|
"m.room.canonical_alias",
|
2018-05-10 19:56:46 +02:00
|
|
|
"m.room.aliases",
|
|
|
|
},
|
2018-03-24 12:27:13 +01:00
|
|
|
},
|
2018-05-10 19:56:46 +02:00
|
|
|
Timeline: gomatrix.FilterPart{
|
2018-05-15 14:39:40 +02:00
|
|
|
Types: []string{"m.room.message", "m.room.member"},
|
2018-05-10 19:56:46 +02:00
|
|
|
Limit: 50,
|
2018-03-24 12:27:13 +01:00
|
|
|
},
|
2018-05-10 19:56:46 +02:00
|
|
|
Ephemeral: gomatrix.FilterPart{
|
2018-05-16 19:09:09 +02:00
|
|
|
Types: []string{"m.typing", "m.receipt"},
|
2018-03-24 12:27:13 +01:00
|
|
|
},
|
2018-05-10 19:56:46 +02:00
|
|
|
AccountData: gomatrix.FilterPart{
|
|
|
|
Types: []string{"m.tag"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AccountData: gomatrix.FilterPart{
|
2018-05-16 19:09:09 +02:00
|
|
|
Types: []string{"m.push_rules", "m.direct"},
|
2018-03-24 12:27:13 +01:00
|
|
|
},
|
2018-05-10 19:56:46 +02:00
|
|
|
Presence: gomatrix.FilterPart{
|
|
|
|
Types: []string{},
|
2018-03-24 12:27:13 +01:00
|
|
|
},
|
2018-05-10 19:56:46 +02:00
|
|
|
}
|
|
|
|
rawFilter, _ := json.Marshal(&filter)
|
|
|
|
return rawFilter
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|