Skip to content

Commit

Permalink
Merge pull request #75 from Alkorin/hideAddLeaveEvents
Browse files Browse the repository at this point in the history
Handle Add Hide Leave events
  • Loading branch information
Alkorin authored Feb 13, 2019
2 parents 611a906 + 9a59f0e commit 65ea2fc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Activity struct {
Object struct {
Id string
DisplayName string
EntryUUID string
Mentions struct {
Items []struct {
Id string
Expand Down
77 changes: 68 additions & 9 deletions conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Conversation struct {
teamsMutex sync.RWMutex

newSpaceEventHandlers []func(*Space)
removeSpaceEventHandlers []func(*Space)
newActivityEventHandlers []func(*Space, *Activity)

activityQueue chan io.Reader
Expand Down Expand Up @@ -83,23 +84,71 @@ func (c *Conversation) ParseActivity(msg []byte) {
for _, f := range c.newActivityEventHandlers {
f(space, a)
}
logger.Trace("New space")
case "add":
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Target.Id).WithField("verb", mercuryConversationActivity.Data.Activity.Verb)
logger.Trace("New space")

if mercuryConversationActivity.Data.Activity.Object.EntryUUID == c.device.UserID {
space, err := c.GetSpace(mercuryConversationActivity.Data.Activity.Target.Id)
if err != nil {
logger.WithError(err).Error("Failed to get space")
return
}

for _, f := range c.newActivityEventHandlers {
f(space, &mercuryConversationActivity.Data.Activity)
}
}
case "create":
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Object.Id)
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Object.Id).WithField("verb", mercuryConversationActivity.Data.Activity.Verb)
logger.Trace("New space")
space, err := c.GetSpace(mercuryConversationActivity.Data.Activity.Object.Id)
if err != nil {
logger.WithError(err).Error("Failed to get space")
return

if mercuryConversationActivity.Data.Activity.Actor.EntryUUID == c.device.UserID {
space, err := c.GetSpace(mercuryConversationActivity.Data.Activity.Object.Id)
if err != nil {
logger.WithError(err).Error("Failed to get space")
return
}

for _, f := range c.newActivityEventHandlers {
f(space, &mercuryConversationActivity.Data.Activity)
}
}
for _, f := range c.newActivityEventHandlers {
f(space, &mercuryConversationActivity.Data.Activity)
case "hide":
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Object.Id).WithField("verb", mercuryConversationActivity.Data.Activity.Verb)
logger.Trace("Leave space")

if mercuryConversationActivity.Data.Activity.Actor.EntryUUID == c.device.UserID {
space, err := c.GetSpace(mercuryConversationActivity.Data.Activity.Object.Id)
if err != nil {
logger.WithError(err).Error("Failed to get space")
return
}

for _, f := range c.removeSpaceEventHandlers {
f(space)
}

c.RemoveSpace(space)
}
case "leave":
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Target.Id).WithField("verb", mercuryConversationActivity.Data.Activity.Verb)
logger.Trace("Leave space")
case "hide":
logger.Trace("Leave space")

if mercuryConversationActivity.Data.Activity.Object.EntryUUID == c.device.UserID {
space, err := c.GetSpace(mercuryConversationActivity.Data.Activity.Target.Id)
if err != nil {
logger.WithError(err).Error("Failed to get space")
return
}

for _, f := range c.removeSpaceEventHandlers {
f(space)
}

c.RemoveSpace(space)
}
case "update":
logger = logger.WithField("space", mercuryConversationActivity.Data.Activity.Target.Id)
logger.Trace("Update space")
Expand Down Expand Up @@ -247,6 +296,12 @@ func (c *Conversation) AddSpace(r RawSpace) (*Space, error) {
}
}

func (c *Conversation) RemoveSpace(space *Space) {
c.spacesMutex.Lock()
delete(c.spaces, space.Id)
c.spacesMutex.Unlock()
}

func (c *Conversation) GetTeam(uuid string) (*Team, error) {
logger := c.logger.WithField("func", "GetTeam").WithField("uuid", uuid)
c.teamsMutex.RLock()
Expand Down Expand Up @@ -298,6 +353,10 @@ func (c *Conversation) AddNewSpaceEventHandler(f func(*Space)) {
c.newSpaceEventHandlers = append(c.newSpaceEventHandlers, f)
}

func (c *Conversation) AddRemoveSpaceEventHandler(f func(*Space)) {
c.removeSpaceEventHandlers = append(c.removeSpaceEventHandlers, f)
}

func (c *Conversation) AddNewActivityEventHandler(f func(*Space, *Activity)) {
c.newActivityEventHandlers = append(c.newActivityEventHandlers, f)
}
Expand Down
30 changes: 29 additions & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewGoCUI(c *Conversation) (*GoCUI, error) {
}

c.AddNewSpaceEventHandler(gui.NewSpaceHandler)
c.AddRemoveSpaceEventHandler(gui.RemoveSpaceHandler)
c.AddNewActivityEventHandler(gui.NewActivityHandler)

return gui, nil
Expand All @@ -77,9 +78,36 @@ func (gui *GoCUI) NewSpaceHandler(s *Space) {
gui.spacesMap[s.Id] = pos
gui.spacesMutex.Unlock()

gui.updateSpaceList()
if len(gui.spacesList) == 1 {
gui.moveToSpace(0)
} else {
gui.updateSpaceList()
}
}

func (gui *GoCUI) RemoveSpaceHandler(s *Space) {
gui.spacesMutex.Lock()

if spaceIndex, ok := gui.spacesMap[s.Id]; ok {
// Remove space from list
gui.spacesList = append(gui.spacesList[:spaceIndex], gui.spacesList[spaceIndex+1:]...)

// Recompute map
gui.spacesMap = make(map[string]int)
for i, v := range gui.spacesList {
gui.spacesMap[v.Id] = i
}

gui.spacesMutex.Unlock()

// Update UI
if spaceIndex == gui.currentSpaceIndex {
gui.moveToSpace(gui.currentSpaceIndex)
} else {
gui.updateSpaceList()
}
} else {
gui.spacesMutex.Unlock()
}
}

Expand Down

0 comments on commit 65ea2fc

Please sign in to comment.