Skip to content

Commit

Permalink
Save last viewed at state and use for replaying logs (#361)
Browse files Browse the repository at this point in the history
* Fixed logging in with personal access token

* Save last viewed at state and use for replaying logs

Per #245, we can't trust
that Mattermost has updated the last viewed for channel. Instead, we
save this locally and use that for replaying of logs. Useful for when
experiencing flaky network causing matterircd to reconnect.

* Fixed using defer in range loops

* Also make the map
  • Loading branch information
hloeung authored Dec 18, 2020
1 parent 6f4f1f3 commit 86e88c2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions mm-go-irckit/userbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type UserBridge struct {
Credentials bridge.Credentials
br bridge.Bridger //nolint:structcheck
inprogress bool //nolint:structcheck
lastViewedAt map[string]int64 //nolint:structcheck
lastViewedAtMutex sync.RWMutex //nolint:structcheck
msgLast map[string]string //nolint:structcheck
msgLastMutex sync.RWMutex //nolint:structcheck
msgMap map[string]map[string]int
Expand All @@ -41,6 +43,7 @@ func NewUserBridge(c net.Conn, srv Server, cfg *viper.Viper) *User {

u.Srv = srv
u.v = cfg
u.lastViewedAt = make(map[string]int64)
u.msgLast = make(map[string]string)
u.msgMap = make(map[string]map[string]int)
u.msgCounter = make(map[string]int)
Expand Down Expand Up @@ -147,6 +150,9 @@ func (u *User) handleDirectMessageEvent(event *bridge.DirectMessageEvent) {
if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
u.updateLastViewed(event.ChannelID)
}
u.lastViewedAtMutex.Lock()
defer u.lastViewedAtMutex.Unlock()
u.lastViewedAt[event.ChannelID] = model.GetMillis()
}

func (u *User) handleChannelAddEvent(event *bridge.ChannelAddEvent) {
Expand All @@ -170,6 +176,9 @@ func (u *User) handleChannelAddEvent(event *bridge.ChannelAddEvent) {
if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
u.updateLastViewed(event.ChannelID)
}
u.lastViewedAtMutex.Lock()
defer u.lastViewedAtMutex.Unlock()
u.lastViewedAt[event.ChannelID] = model.GetMillis()
}

func (u *User) handleChannelRemoveEvent(event *bridge.ChannelRemoveEvent) {
Expand Down Expand Up @@ -275,6 +284,9 @@ func (u *User) handleChannelMessageEvent(event *bridge.ChannelMessageEvent) {
if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
u.updateLastViewed(event.ChannelID)
}
u.lastViewedAtMutex.Lock()
defer u.lastViewedAtMutex.Unlock()
u.lastViewedAt[event.ChannelID] = model.GetMillis()
}

func (u *User) handleFileEvent(event *bridge.FileEvent) {
Expand Down Expand Up @@ -536,6 +548,12 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt
if since == 0 {
continue
}
// We used to stored last viewed at if present.
u.lastViewedAtMutex.RLock()
if lastViewedAt, ok := u.lastViewedAt[brchannel.ID]; ok {
since = lastViewedAt
}
u.lastViewedAtMutex.RUnlock()
// post everything to the channel you haven't seen yet
postlist := u.br.GetPostsSince(brchannel.ID, since)
if postlist == nil {
Expand Down Expand Up @@ -606,6 +624,9 @@ func (u *User) addUserToChannelWorker(channels <-chan *bridge.ChannelInfo, throt
if !u.v.GetBool(u.br.Protocol() + ".disableautoview") {
u.updateLastViewed(brchannel.ID)
}
u.lastViewedAtMutex.Lock()
u.lastViewedAt[brchannel.ID] = model.GetMillis()
u.lastViewedAtMutex.Unlock()
}
}

Expand Down

0 comments on commit 86e88c2

Please sign in to comment.