Stop showing rooms the user has left in the room list. Fixes #35

This commit is contained in:
Tulir Asokan 2018-04-30 22:28:29 +03:00
parent a6b8c20b20
commit b3543e9090
5 changed files with 52 additions and 33 deletions

View File

@ -217,7 +217,10 @@ func (c *Container) Start() {
}
// HandleMessage is the event handler for the m.room.message timeline event.
func (c *Container) HandleMessage(evt *gomatrix.Event) {
func (c *Container) HandleMessage(source EventSource, evt *gomatrix.Event) {
if source == EventSourceLeave {
return
}
mainView := c.ui.MainView()
roomView := mainView.GetRoom(evt.RoomID)
@ -240,7 +243,7 @@ func (c *Container) HandleMessage(evt *gomatrix.Event) {
}
// HandlePushRules is the event handler for the m.push_rules account data event.
func (c *Container) HandlePushRules(evt *gomatrix.Event) {
func (c *Container) HandlePushRules(source EventSource, evt *gomatrix.Event) {
debug.Print("Received updated push rules")
var err error
c.config.Session.PushRules, err = pushrules.EventToPushRules(evt)
@ -250,7 +253,7 @@ func (c *Container) HandlePushRules(evt *gomatrix.Event) {
}
// HandleTag is the event handler for the m.tag account data event.
func (c *Container) HandleTag(evt *gomatrix.Event) {
func (c *Container) HandleTag(source EventSource, evt *gomatrix.Event) {
room := c.config.Session.GetRoom(evt.RoomID)
tags, _ := evt.Content["tags"].(map[string]interface{})
@ -292,11 +295,13 @@ func (c *Container) processOwnMembershipChange(evt *gomatrix.Event) {
}
// HandleMembership is the event handler for the m.room.membership state event.
func (c *Container) HandleMembership(evt *gomatrix.Event) {
if evt.StateKey != nil && *evt.StateKey == c.config.Session.UserID {
func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) {
if !c.config.Session.InitialSyncDone && source == EventSourceLeave {
return
} else if evt.StateKey != nil && *evt.StateKey == c.config.Session.UserID {
c.processOwnMembershipChange(evt)
} else if !c.config.Session.InitialSyncDone /*&& evt.Timestamp < time.Now().Add(-1*time.Hour).Unix()*/ {
// We don't care about other users' membership events in the initial sync.
} else if !c.config.Session.InitialSyncDone || source == EventSourceLeave {
// We don't care about other users' membership events in the initial sync or chats we've left.
return
}
@ -308,11 +313,6 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
message := mainView.ParseEvent(roomView, evt)
if message != nil {
// TODO this shouldn't be necessary
//roomView.MxRoom().UpdateState(evt)
// TODO This should probably also be in a different place
//roomView.UpdateUserList()
roomView.AddMessage(message, ifc.AppendMessage)
// We don't want notifications at startup.
if c.syncer.FirstSyncDone {
@ -324,7 +324,7 @@ func (c *Container) HandleMembership(evt *gomatrix.Event) {
}
// HandleTyping is the event handler for the m.typing event.
func (c *Container) HandleTyping(evt *gomatrix.Event) {
func (c *Container) HandleTyping(source EventSource, evt *gomatrix.Event) {
users := evt.Content["user_ids"].([]interface{})
strUsers := make([]string, len(users))

View File

@ -47,6 +47,9 @@ type RoomTag struct {
type Room struct {
*gomatrix.Room
// Whether or not the user has left the room.
HasLeft bool
// The first batch of events that has been fetched for this room.
// Used for fetching additional history.
PrevBatch string

View File

@ -31,12 +31,24 @@ type SyncerSession interface {
GetUserID() string
}
type EventSource int
const (
EventSourcePresence EventSource = iota
EventSourceAccountData
EventSourceJoin
EventSourceInvite
EventSourceLeave
)
type EventHandler func(source EventSource, event *gomatrix.Event)
// 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 {
Session SyncerSession
listeners map[string][]gomatrix.OnEventListener // event type to listeners array
listeners map[string][]EventHandler // event type to listeners array
FirstSyncDone bool
InitDoneCallback func()
}
@ -45,22 +57,22 @@ type GomuksSyncer struct {
func NewGomuksSyncer(session SyncerSession) *GomuksSyncer {
return &GomuksSyncer{
Session: session,
listeners: make(map[string][]gomatrix.OnEventListener),
listeners: make(map[string][]EventHandler),
FirstSyncDone: false,
}
}
// ProcessResponse processes a Matrix sync response.
func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (err error) {
s.processSyncEvents(nil, res.Presence.Events, false, false)
s.processSyncEvents(nil, res.AccountData.Events, false, false)
s.processSyncEvents(nil, res.Presence.Events, EventSourcePresence, false, false)
s.processSyncEvents(nil, res.AccountData.Events, EventSourceAccountData, false, false)
for roomID, roomData := range res.Rooms.Join {
room := s.Session.GetRoom(roomID)
s.processSyncEvents(room, roomData.State.Events, true, false)
s.processSyncEvents(room, roomData.Timeline.Events, false, false)
s.processSyncEvents(room, roomData.Ephemeral.Events, false, false)
s.processSyncEvents(room, roomData.AccountData.Events, false, false)
s.processSyncEvents(room, roomData.State.Events, EventSourceJoin, true, false)
s.processSyncEvents(room, roomData.Timeline.Events, EventSourceJoin, false, false)
s.processSyncEvents(room, roomData.Ephemeral.Events, EventSourceJoin, false, false)
s.processSyncEvents(room, roomData.AccountData.Events, EventSourceJoin, false, false)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@ -69,13 +81,14 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
for roomID, roomData := range res.Rooms.Invite {
room := s.Session.GetRoom(roomID)
s.processSyncEvents(room, roomData.State.Events, true, false)
s.processSyncEvents(room, roomData.State.Events, EventSourceInvite, true, false)
}
for roomID, roomData := range res.Rooms.Leave {
room := s.Session.GetRoom(roomID)
s.processSyncEvents(room, roomData.State.Events, true, true)
s.processSyncEvents(room, roomData.Timeline.Events, false, false)
room.HasLeft = true
s.processSyncEvents(room, roomData.State.Events, EventSourceLeave, true, true)
s.processSyncEvents(room, roomData.Timeline.Events, EventSourceLeave, false, false)
if len(room.PrevBatch) == 0 {
room.PrevBatch = roomData.Timeline.PrevBatch
@ -90,41 +103,41 @@ func (s *GomuksSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
return
}
func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*gomatrix.Event, isState bool, checkStateKey bool) {
func (s *GomuksSyncer) processSyncEvents(room *rooms.Room, events []*gomatrix.Event, source EventSource, isState bool, checkStateKey bool) {
for _, event := range events {
if !checkStateKey || event.StateKey != nil {
s.processSyncEvent(room, event, isState)
s.processSyncEvent(room, event, source, isState)
}
}
}
func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, event *gomatrix.Event, isState bool) {
func (s *GomuksSyncer) processSyncEvent(room *rooms.Room, event *gomatrix.Event, source EventSource, isState bool) {
if room != nil {
event.RoomID = room.ID
}
if isState {
room.UpdateState(event)
}
s.notifyListeners(event)
s.notifyListeners(source, event)
}
// OnEventType allows callers to be notified when there are new events for the given event type.
// There are no duplicate checks.
func (s *GomuksSyncer) OnEventType(eventType string, callback gomatrix.OnEventListener) {
func (s *GomuksSyncer) OnEventType(eventType string, callback EventHandler) {
_, exists := s.listeners[eventType]
if !exists {
s.listeners[eventType] = []gomatrix.OnEventListener{}
s.listeners[eventType] = []EventHandler{}
}
s.listeners[eventType] = append(s.listeners[eventType], callback)
}
func (s *GomuksSyncer) notifyListeners(event *gomatrix.Event) {
func (s *GomuksSyncer) notifyListeners(source EventSource, event *gomatrix.Event) {
listeners, exists := s.listeners[event.Type]
if !exists {
return
}
for _, fn := range listeners {
fn(event)
fn(source, event)
}
}

View File

@ -177,7 +177,7 @@ type mockListener struct {
received []*gomatrix.Event
}
func (ml *mockListener) receive(evt *gomatrix.Event) {
func (ml *mockListener) receive(source matrix.EventSource, evt *gomatrix.Event) {
ml.received = append(ml.received, evt)
}

View File

@ -358,6 +358,9 @@ func (view *MainView) SetRooms(rooms map[string]*rooms.Room) {
view.roomView.Clear()
view.rooms = make(map[string]*RoomView)
for _, room := range rooms {
if room.HasLeft {
continue
}
view.roomList.Add(room)
view.addRoomPage(room)
}