diff --git a/server/handlers.go b/server/handlers.go index 9c928a69..0ec5142a 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -6,7 +6,6 @@ import ( "regexp" "strings" "sync" - "time" "github.com/enescakir/emoji" "github.com/mattermost/mattermost-plugin-msteams/server/metrics" @@ -31,7 +30,6 @@ type ActivityHandler struct { quit chan bool workersWaitGroup sync.WaitGroup IgnorePluginHooksMap sync.Map - lastUpdateAtMap sync.Map } func NewActivityHandler(plugin *Plugin) *ActivityHandler { @@ -80,30 +78,6 @@ func (ah *ActivityHandler) Start() { ah.workersWaitGroup.Done() } - // doStart is the meat of the activity handler worker - doStartLastActivityAt := func() { - updateLastActivityAt := func(subscriptionID, lastUpdateAt any) bool { - if time.Since(lastUpdateAt.(time.Time)) <= 5*time.Minute { - if err := ah.plugin.GetStore().UpdateSubscriptionLastActivityAt(subscriptionID.(string), lastUpdateAt.(time.Time)); err != nil { - ah.plugin.GetAPI().LogWarn("Error storing the subscription last activity at", "error", err, "subscription_id", subscriptionID.(string), "last_update_at", lastUpdateAt.(time.Time)) - } - } - return true - } - for { - timer := time.NewTimer(5 * time.Minute) - select { - case <-timer.C: - ah.lastUpdateAtMap.Range(updateLastActivityAt) - case <-ah.quit: - // we have received a signal to stop - timer.Stop() - ah.lastUpdateAtMap.Range(updateLastActivityAt) - return - } - } - } - // isQuitting informs the recovery handler if the shutdown is intentional isQuitting := func() bool { select { @@ -120,8 +94,6 @@ func (ah *ActivityHandler) Start() { ah.workersWaitGroup.Add(1) startWorker(logError, ah.plugin.GetMetrics(), isQuitting, doStart, doQuit) } - ah.workersWaitGroup.Add(1) - startWorker(logError, ah.plugin.GetMetrics(), isQuitting, doStartLastActivityAt, doQuit) } func (ah *ActivityHandler) Stop() { diff --git a/server/metrics/mocks/Metrics.go b/server/metrics/mocks/Metrics.go index 4845f921..da16cfbd 100644 --- a/server/metrics/mocks/Metrics.go +++ b/server/metrics/mocks/Metrics.go @@ -140,9 +140,9 @@ func (_m *Metrics) ObserveMessageDelay(action string, source string, isDirectOrG _m.Called(action, source, isDirectOrGroupMessage, delay) } -// ObserveNotification provides a mock function with given fields: isGroupChat, hasAttachments -func (_m *Metrics) ObserveNotification(isGroupChat bool, hasAttachments bool) { - _m.Called(isGroupChat, hasAttachments) +// ObserveNotification provides a mock function with given fields: isGroupChat, hasAttachments, discardedReason +func (_m *Metrics) ObserveNotification(isGroupChat bool, hasAttachments bool, discardedReason string) { + _m.Called(isGroupChat, hasAttachments, discardedReason) } // ObserveOAuthTokenInvalidated provides a mock function with given fields: diff --git a/server/store/mocks/Store.go b/server/store/mocks/Store.go index 1f3f7b45..e39d4d2d 100644 --- a/server/store/mocks/Store.go +++ b/server/store/mocks/Store.go @@ -442,29 +442,6 @@ func (_m *Store) GetSubscriptionType(subscriptionID string) (string, error) { return r0, r1 } -// GetSubscriptionsLastActivityAt provides a mock function with given fields: -func (_m *Store) GetSubscriptionsLastActivityAt() (map[string]time.Time, error) { - ret := _m.Called() - - var r0 map[string]time.Time - if rf, ok := ret.Get(0).(func() map[string]time.Time); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(map[string]time.Time) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // GetTokenForMSTeamsUser provides a mock function with given fields: userID func (_m *Store) GetTokenForMSTeamsUser(userID string) (*oauth2.Token, error) { ret := _m.Called(userID) @@ -1054,20 +1031,6 @@ func (_m *Store) UpdateSubscriptionExpiresOn(subscriptionID string, expiresOn ti return r0 } -// UpdateSubscriptionLastActivityAt provides a mock function with given fields: subscriptionID, lastActivityAt -func (_m *Store) UpdateSubscriptionLastActivityAt(subscriptionID string, lastActivityAt time.Time) error { - ret := _m.Called(subscriptionID, lastActivityAt) - - var r0 error - if rf, ok := ret.Get(0).(func(string, time.Time) error); ok { - r0 = rf(subscriptionID, lastActivityAt) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // UserHasConnected provides a mock function with given fields: mmUserID func (_m *Store) UserHasConnected(mmUserID string) (bool, error) { ret := _m.Called(mmUserID) diff --git a/server/store/sqlstore/public_methods.go b/server/store/sqlstore/public_methods.go index e804b7c7..41794dd2 100644 --- a/server/store/sqlstore/public_methods.go +++ b/server/store/sqlstore/public_methods.go @@ -106,10 +106,6 @@ func (s *SQLStore) GetSubscriptionType(subscriptionID string) (string, error) { return s.getSubscriptionType(s.replica, subscriptionID) } -func (s *SQLStore) GetSubscriptionsLastActivityAt() (map[string]time.Time, error) { - return s.getSubscriptionsLastActivityAt(s.replica) -} - func (s *SQLStore) GetTokenForMSTeamsUser(userID string) (*oauth2.Token, error) { return s.getTokenForMSTeamsUser(s.replica, userID) } @@ -313,7 +309,3 @@ func (s *SQLStore) TeamsToMattermostUserID(userID string) (string, error) { func (s *SQLStore) UpdateSubscriptionExpiresOn(subscriptionID string, expiresOn time.Time) error { return s.updateSubscriptionExpiresOn(s.db, subscriptionID, expiresOn) } - -func (s *SQLStore) UpdateSubscriptionLastActivityAt(subscriptionID string, lastActivityAt time.Time) error { - return s.updateSubscriptionLastActivityAt(s.db, subscriptionID, lastActivityAt) -} diff --git a/server/store/sqlstore/store.go b/server/store/sqlstore/store.go index 210db171..d8ef5cd2 100644 --- a/server/store/sqlstore/store.go +++ b/server/store/sqlstore/store.go @@ -645,48 +645,6 @@ func (s *SQLStore) updateSubscriptionExpiresOn(db sq.BaseRunner, subscriptionID return nil } -func (s *SQLStore) updateSubscriptionLastActivityAt(db sq.BaseRunner, subscriptionID string, lastActivityAt time.Time) error { - query := s.getQueryBuilder(db). - Update(subscriptionsTableName). - Set("lastActivityAt", lastActivityAt.UnixMicro()). - Where(sq.And{ - sq.Eq{"subscriptionID": subscriptionID}, - sq.Or{sq.Lt{"lastActivityAt": lastActivityAt.UnixMicro()}, sq.Eq{"lastActivityAt": nil}}, - }) - _, err := query.Exec() - if err != nil { - return err - } - return nil -} - -//db:withReplica -func (s *SQLStore) getSubscriptionsLastActivityAt(db sq.BaseRunner) (map[string]time.Time, error) { - query := s.getQueryBuilder(db). - Select("subscriptionID, lastActivityAt"). - From(subscriptionsTableName). - Where( - sq.NotEq{"lastActivityAt": nil}, - sq.NotEq{"lastActivityAt": 0}, - ) - rows, err := query.Query() - if err != nil { - return nil, err - } - defer rows.Close() - - result := map[string]time.Time{} - for rows.Next() { - var lastActivityAt int64 - var subscriptionID string - if scanErr := rows.Scan(&subscriptionID, &lastActivityAt); scanErr != nil { - return nil, scanErr - } - result[subscriptionID] = time.UnixMicro(lastActivityAt) - } - return result, nil -} - func (s *SQLStore) deleteSubscription(db sq.BaseRunner, subscriptionID string) error { if _, err := s.getQueryBuilder(db).Delete(subscriptionsTableName).Where(sq.Eq{"subscriptionID": subscriptionID}).Exec(); err != nil { return err diff --git a/server/store/store.go b/server/store/store.go index b2926d6a..43c6adbb 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -78,6 +78,4 @@ type Store interface { GetChatSubscription(subscriptionID string) (*storemodels.ChatSubscription, error) GetGlobalSubscription(subscriptionID string) (*storemodels.GlobalSubscription, error) GetSubscriptionType(subscriptionID string) (string, error) - UpdateSubscriptionLastActivityAt(subscriptionID string, lastActivityAt time.Time) error - GetSubscriptionsLastActivityAt() (map[string]time.Time, error) } diff --git a/server/store/timerlayer/timerlayer.go b/server/store/timerlayer/timerlayer.go index 2a8a33d2..fca349b6 100644 --- a/server/store/timerlayer/timerlayer.go +++ b/server/store/timerlayer/timerlayer.go @@ -315,20 +315,6 @@ func (s *TimerLayer) GetSubscriptionType(subscriptionID string) (string, error) return result, err } -func (s *TimerLayer) GetSubscriptionsLastActivityAt() (map[string]time.Time, error) { - start := time.Now() - - result, err := s.Store.GetSubscriptionsLastActivityAt() - - elapsed := float64(time.Since(start)) / float64(time.Second) - success := "false" - if err == nil { - success = "true" - } - s.metrics.ObserveStoreMethodDuration("Store.GetSubscriptionsLastActivityAt", success, elapsed) - return result, err -} - func (s *TimerLayer) GetTokenForMSTeamsUser(userID string) (*oauth2.Token, error) { start := time.Now() @@ -791,20 +777,6 @@ func (s *TimerLayer) UpdateSubscriptionExpiresOn(subscriptionID string, expiresO return err } -func (s *TimerLayer) UpdateSubscriptionLastActivityAt(subscriptionID string, lastActivityAt time.Time) error { - start := time.Now() - - err := s.Store.UpdateSubscriptionLastActivityAt(subscriptionID, lastActivityAt) - - elapsed := float64(time.Since(start)) / float64(time.Second) - success := "false" - if err == nil { - success = "true" - } - s.metrics.ObserveStoreMethodDuration("Store.UpdateSubscriptionLastActivityAt", success, elapsed) - return err -} - func (s *TimerLayer) UserHasConnected(mmUserID string) (bool, error) { start := time.Now()