From 87dfbc13c0a4fff19558d250d50181b7ea93d9a2 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Wed, 15 Jul 2020 09:26:28 -0400 Subject: [PATCH] SessionStore migration (#15002) * Finished * Fiximports * Fix i18n --- app/analytics.go | 8 +- app/expirynotify.go | 4 +- app/notification_push.go | 7 +- app/oauth.go | 2 +- app/session.go | 55 +++++--- app/session_test.go | 20 +-- app/user.go | 8 +- i18n/en.json | 92 +++++-------- store/opentracing_layer.go | 30 ++--- store/sqlstore/session_store.go | 82 +++++------ store/store.go | 30 ++--- store/storetest/mocks/SessionStore.go | 150 +++++++++------------ store/storetest/oauth_store.go | 8 +- store/storetest/session_store.go | 6 +- store/storetest/user_access_token_store.go | 4 +- store/timer_layer.go | 30 ++--- 16 files changed, 259 insertions(+), 277 deletions(-) diff --git a/app/analytics.go b/app/analytics.go index ade0f1e4c8..f5baeb1b16 100644 --- a/app/analytics.go +++ b/app/analytics.go @@ -4,6 +4,8 @@ package app import ( + "net/http" + "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/store" @@ -251,7 +253,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo sessionChan := make(chan store.StoreResult, 1) go func() { count, err := a.Srv().Store.Session().AnalyticsSessionCount() - sessionChan <- store.StoreResult{Data: count, Err: err} + sessionChan <- store.StoreResult{Data: count, NErr: err} close(sessionChan) }() @@ -313,8 +315,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo rows[4].Value = float64(r.Data.(int64)) r = <-sessionChan - if r.Err != nil { - return nil, r.Err + if r.NErr != nil { + return nil, model.NewAppError("GetAnalytics", "app.session.analytics_session_count.app_error", nil, r.NErr.Error(), http.StatusInternalServerError) } rows[5].Value = float64(r.Data.(int64)) diff --git a/app/expirynotify.go b/app/expirynotify.go index f608dd7d98..ed297355a6 100644 --- a/app/expirynotify.go +++ b/app/expirynotify.go @@ -4,6 +4,8 @@ package app import ( + "net/http" + "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/utils" @@ -26,7 +28,7 @@ func (a *App) NotifySessionsExpired() *model.AppError { // Get all mobile sessions that expired within the last hour. sessions, err := a.srv.Store.Session().GetSessionsExpired(OneHourMillis, true, true) if err != nil { - return err + return model.NewAppError("NotifySessionsExpired", "app.session.analytics_session_count.app_error", nil, err.Error(), http.StatusInternalServerError) } msg := &model.PushNotification{ diff --git a/app/notification_push.go b/app/notification_push.go index 59cb2ba0c0..19cab67fe4 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -386,7 +386,12 @@ func (a *App) SendAckToPushProxy(ack *model.PushNotificationAck) error { } func (a *App) getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) { - return a.Srv().Store.Session().GetSessionsWithActiveDeviceIds(userId) + sessions, err := a.Srv().Store.Session().GetSessionsWithActiveDeviceIds(userId) + if err != nil { + return nil, model.NewAppError("getMobileAppSessions", "app.session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return sessions, nil } func ShouldSendPushNotification(user *model.User, channelNotifyProps model.StringMap, wasMentioned bool, status *model.Status, post *model.Post) bool { diff --git a/app/oauth.go b/app/oauth.go index 8ae7ab7e46..635a872ec1 100644 --- a/app/oauth.go +++ b/app/oauth.go @@ -450,7 +450,7 @@ func (a *App) RegenerateOAuthAppSecret(app *model.OAuthApp) (*model.OAuthApp, *m func (a *App) RevokeAccessToken(token string) *model.AppError { session, _ := a.GetSession(token) - schan := make(chan *model.AppError, 1) + schan := make(chan error, 1) go func() { schan <- a.Srv().Store.Session().Remove(token) close(schan) diff --git a/app/session.go b/app/session.go index 73fb9940a3..cf68577044 100644 --- a/app/session.go +++ b/app/session.go @@ -4,6 +4,7 @@ package app import ( + "errors" "math" "net/http" "time" @@ -11,6 +12,7 @@ import ( "github.com/mattermost/mattermost-server/v5/audit" "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" + "github.com/mattermost/mattermost-server/v5/store" ) func (a *App) CreateSession(session *model.Session) (*model.Session, *model.AppError) { @@ -18,7 +20,13 @@ func (a *App) CreateSession(session *model.Session) (*model.Session, *model.AppE session, err := a.Srv().Store.Session().Save(session) if err != nil { - return nil, err + var invErr *store.ErrInvalidInput + switch { + case errors.As(err, &invErr): + return nil, model.NewAppError("CreateSession", "app.session.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest) + default: + return nil, model.NewAppError("CreateSession", "app.session.save.app_error", nil, err.Error(), http.StatusInternalServerError) + } } a.AddSessionToCache(session) @@ -42,7 +50,8 @@ func (a *App) GetSession(token string) (*model.Session, *model.AppError) { } if session == nil { - if session, err = a.Srv().Store.Session().Get(token); err == nil { + var nErr error + if session, nErr = a.Srv().Store.Session().Get(token); nErr == nil { if session != nil { if session.Token != token { return nil, model.NewAppError("GetSession", "api.context.invalid_token.error", map[string]interface{}{"Token": token, "Error": ""}, "", http.StatusUnauthorized) @@ -52,8 +61,8 @@ func (a *App) GetSession(token string) (*model.Session, *model.AppError) { a.AddSessionToCache(session) } } - } else if err.StatusCode == http.StatusInternalServerError { - return nil, err + } else if nfErr := new(store.ErrNotFound); !errors.As(nErr, &nfErr) { + return nil, model.NewAppError("GetSession", "app.session.get.app_error", nil, nErr.Error(), http.StatusInternalServerError) } } @@ -91,7 +100,12 @@ func (a *App) GetSession(token string) (*model.Session, *model.AppError) { func (a *App) GetSessions(userId string) ([]*model.Session, *model.AppError) { - return a.Srv().Store.Session().GetSessions(userId) + sessions, err := a.Srv().Store.Session().GetSessions(userId) + if err != nil { + return nil, model.NewAppError("GetSessions", "app.session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return sessions, nil } func (a *App) UpdateSessionsIsGuest(userId string, isGuest bool) { @@ -118,14 +132,14 @@ func (a *App) UpdateSessionsIsGuest(userId string, isGuest bool) { func (a *App) RevokeAllSessions(userId string) *model.AppError { sessions, err := a.Srv().Store.Session().GetSessions(userId) if err != nil { - return err + return model.NewAppError("RevokeAllSessions", "app.session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) } for _, session := range sessions { if session.IsOAuth { a.RevokeAccessToken(session.Token) } else { if err := a.Srv().Store.Session().Remove(session.Id); err != nil { - return err + return model.NewAppError("RevokeAllSessions", "app.session.remove.app_error", nil, err.Error(), http.StatusInternalServerError) } } } @@ -145,7 +159,7 @@ func (a *App) RevokeSessionsFromAllUsers() *model.AppError { } err := a.Srv().Store.Session().RemoveAllSessions() if err != nil { - return err + return model.NewAppError("RevokeSessionsFromAllUsers", "app.session.remove_all_sessions_for_team.app_error", nil, err.Error(), http.StatusInternalServerError) } a.ClearSessionCacheForAllUsers() @@ -214,7 +228,7 @@ func (a *App) SessionCacheLength() int { func (a *App) RevokeSessionsForDeviceId(userId string, deviceId string, currentSessionId string) *model.AppError { sessions, err := a.Srv().Store.Session().GetSessions(userId) if err != nil { - return err + return model.NewAppError("RevokeSessionsForDeviceId", "app.session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) } for _, session := range sessions { if session.DeviceId == deviceId && session.Id != currentSessionId { @@ -232,17 +246,16 @@ func (a *App) RevokeSessionsForDeviceId(userId string, deviceId string, currentS func (a *App) GetSessionById(sessionId string) (*model.Session, *model.AppError) { session, err := a.Srv().Store.Session().Get(sessionId) if err != nil { - err.StatusCode = http.StatusBadRequest - return nil, err + return nil, model.NewAppError("GetSessionById", "app.session.get.app_error", nil, err.Error(), http.StatusBadRequest) } + return session, nil } func (a *App) RevokeSessionById(sessionId string) *model.AppError { session, err := a.Srv().Store.Session().Get(sessionId) if err != nil { - err.StatusCode = http.StatusBadRequest - return err + return model.NewAppError("RevokeSessionById", "app.session.get.app_error", nil, err.Error(), http.StatusBadRequest) } return a.RevokeSession(session) @@ -255,7 +268,7 @@ func (a *App) RevokeSession(session *model.Session) *model.AppError { } } else { if err := a.Srv().Store.Session().Remove(session.Id); err != nil { - return err + return model.NewAppError("RevokeSession", "app.session.remove.app_error", nil, err.Error(), http.StatusInternalServerError) } } @@ -267,7 +280,7 @@ func (a *App) RevokeSession(session *model.Session) *model.AppError { func (a *App) AttachDeviceId(sessionId string, deviceId string, expiresAt int64) *model.AppError { _, err := a.Srv().Store.Session().UpdateDeviceId(sessionId, deviceId, expiresAt) if err != nil { - return err + return model.NewAppError("AttachDeviceId", "app.session.update_device_id.app_error", nil, err.Error(), http.StatusInternalServerError) } return nil @@ -427,9 +440,15 @@ func (a *App) createSessionForUserAccessToken(tokenString string) (*model.Sessio } session.SetExpireInDays(model.SESSION_USER_ACCESS_TOKEN_EXPIRY) - session, err = a.Srv().Store.Session().Save(session) - if err != nil { - return nil, err + session, nErr := a.Srv().Store.Session().Save(session) + if nErr != nil { + var invErr *store.ErrInvalidInput + switch { + case errors.As(nErr, &invErr): + return nil, model.NewAppError("CreateSession", "app.session.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest) + default: + return nil, model.NewAppError("CreateSession", "app.session.save.app_error", nil, nErr.Error(), http.StatusInternalServerError) + } } a.AddSessionToCache(session) diff --git a/app/session_test.go b/app/session_test.go index 76cce2135d..801eeb0e54 100644 --- a/app/session_test.go +++ b/app/session_test.go @@ -70,8 +70,8 @@ func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { // Test regular session, should timeout time := session.LastActivityAt - (1000 * 60 * 6) - err = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) - require.Nil(t, err) + nErr := th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) + require.Nil(t, nErr) th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) rsession, err = th.App.GetSession(session.Token) @@ -88,8 +88,8 @@ func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { session, _ = th.App.CreateSession(session) time = session.LastActivityAt - (1000 * 60 * 6) - err = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) - require.Nil(t, err) + nErr = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) + require.Nil(t, nErr) th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) _, err = th.App.GetSession(session.Token) @@ -103,8 +103,8 @@ func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { session, _ = th.App.CreateSession(session) time = session.LastActivityAt - (1000 * 60 * 6) - err = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) - require.Nil(t, err) + nErr = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) + require.Nil(t, nErr) th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) _, err = th.App.GetSession(session.Token) @@ -121,8 +121,8 @@ func TestGetSessionIdleTimeoutInMinutes(t *testing.T) { session, _ = th.App.CreateSession(session) time = session.LastActivityAt - (1000 * 60 * 6) - err = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) - require.Nil(t, err) + nErr = th.App.Srv().Store.Session().UpdateLastActivityAt(session.Id, time) + require.Nil(t, nErr) th.App.ClearSessionCacheForUserSkipClusterSend(session.UserId) _, err = th.App.GetSession(session.Token) @@ -343,8 +343,8 @@ func TestApp_ExtendExpiryIfNeeded(t *testing.T) { require.Equal(t, session.ExpiresAt, cachedSession.ExpiresAt) // check database was updated. - storedSession, err := th.App.Srv().Store.Session().Get(session.Token) - require.Nil(t, err) + storedSession, nErr := th.App.Srv().Store.Session().Get(session.Token) + require.Nil(t, nErr) require.Equal(t, session.ExpiresAt, storedSession.ExpiresAt) }) } diff --git a/app/user.go b/app/user.go index 963be4e148..b51d51d998 100644 --- a/app/user.go +++ b/app/user.go @@ -1414,7 +1414,7 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent schan := make(chan store.StoreResult, 1) go func() { id, err := a.Srv().Store.Session().UpdateRoles(user.Id, newRoles) - schan <- store.StoreResult{Data: id, Err: err} + schan <- store.StoreResult{Data: id, NErr: err} close(schan) }() @@ -1424,9 +1424,9 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent } ruser := result.Data.(*model.UserUpdate).New - if result := <-schan; result.Err != nil { + if result := <-schan; result.NErr != nil { // soft error since the user roles were still updated - mlog.Error("Failed during updating user roles", mlog.Err(result.Err)) + mlog.Error("Failed during updating user roles", mlog.Err(result.NErr)) } a.InvalidateCacheForUser(userId) @@ -1453,7 +1453,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError { } if err := a.Srv().Store.Session().PermanentDeleteSessionsByUser(user.Id); err != nil { - return err + return model.NewAppError("PermanentDeleteUser", "app.session.permanent_delete_sessions_by_user.app_error", nil, err.Error(), http.StatusInternalServerError) } if err := a.Srv().Store.UserAccessToken().DeleteAllForUser(user.Id); err != nil { diff --git a/i18n/en.json b/i18n/en.json index 8d91aa2aa5..66a3bbd847 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -4022,6 +4022,42 @@ "id": "app.schemes.is_phase_2_migration_completed.not_completed.app_error", "translation": "This API endpoint is not accessible as required migrations have not yet completed." }, + { + "id": "app.session.analytics_session_count.app_error", + "translation": "Unable to count the sessions." + }, + { + "id": "app.session.get.app_error", + "translation": "We encountered an error finding the session." + }, + { + "id": "app.session.get_sessions.app_error", + "translation": "We encountered an error while finding user sessions." + }, + { + "id": "app.session.permanent_delete_sessions_by_user.app_error", + "translation": "Unable to remove all the sessions for the user." + }, + { + "id": "app.session.remove.app_error", + "translation": "Unable to remove the session." + }, + { + "id": "app.session.remove_all_sessions_for_team.app_error", + "translation": "Unable to remove all the sessions." + }, + { + "id": "app.session.save.app_error", + "translation": "Unable to save the session." + }, + { + "id": "app.session.save.existing.app_error", + "translation": "Unable to update existing session." + }, + { + "id": "app.session.update_device_id.app_error", + "translation": "Unable to update the device id." + }, { "id": "app.submit_interactive_dialog.json_error", "translation": "Encountered an error encoding JSON for the interactive dialog." @@ -6998,62 +7034,6 @@ "id": "store.sql_role.save_role.commit_transaction.app_error", "translation": "Failed to commit the transaction to save the role." }, - { - "id": "store.sql_session.analytics_session_count.app_error", - "translation": "Unable to count the sessions." - }, - { - "id": "store.sql_session.get.app_error", - "translation": "We encountered an error finding the session." - }, - { - "id": "store.sql_session.get_sessions.app_error", - "translation": "We encountered an error while finding user sessions." - }, - { - "id": "store.sql_session.permanent_delete_sessions_by_user.app_error", - "translation": "Unable to remove all the sessions for the user." - }, - { - "id": "store.sql_session.remove.app_error", - "translation": "Unable to remove the session." - }, - { - "id": "store.sql_session.remove_all_sessions_for_team.app_error", - "translation": "Unable to remove all the sessions." - }, - { - "id": "store.sql_session.save.app_error", - "translation": "Unable to save the session." - }, - { - "id": "store.sql_session.save.existing.app_error", - "translation": "Unable to update existing session." - }, - { - "id": "store.sql_session.update_device_id.app_error", - "translation": "Unable to update the device id." - }, - { - "id": "store.sql_session.update_expired_notify.app_error", - "translation": "Unable to update expired_notify." - }, - { - "id": "store.sql_session.update_expires_at.app_error", - "translation": "Unable to update expires_at." - }, - { - "id": "store.sql_session.update_last_activity.app_error", - "translation": "Unable to update the last_activity_at." - }, - { - "id": "store.sql_session.update_props.app_error", - "translation": "Unable to update session props." - }, - { - "id": "store.sql_session.update_roles.app_error", - "translation": "Unable to update the roles." - }, { "id": "store.sql_status.get.app_error", "translation": "Encountered an error retrieving the status." diff --git a/store/opentracing_layer.go b/store/opentracing_layer.go index a39287fbd9..cdf88e0303 100644 --- a/store/opentracing_layer.go +++ b/store/opentracing_layer.go @@ -5958,7 +5958,7 @@ func (s *OpenTracingLayerSchemeStore) Save(scheme *model.Scheme) (*model.Scheme, return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) AnalyticsSessionCount() (int64, *model.AppError) { +func (s *OpenTracingLayerSessionStore) AnalyticsSessionCount() (int64, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.AnalyticsSessionCount") s.Root.Store.SetContext(newCtx) @@ -5989,7 +5989,7 @@ func (s *OpenTracingLayerSessionStore) Cleanup(expiryTime int64, batchSize int64 } -func (s *OpenTracingLayerSessionStore) Get(sessionIdOrToken string) (*model.Session, *model.AppError) { +func (s *OpenTracingLayerSessionStore) Get(sessionIdOrToken string) (*model.Session, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.Get") s.Root.Store.SetContext(newCtx) @@ -6007,7 +6007,7 @@ func (s *OpenTracingLayerSessionStore) Get(sessionIdOrToken string) (*model.Sess return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) { +func (s *OpenTracingLayerSessionStore) GetSessions(userId string) ([]*model.Session, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.GetSessions") s.Root.Store.SetContext(newCtx) @@ -6025,7 +6025,7 @@ func (s *OpenTracingLayerSessionStore) GetSessions(userId string) ([]*model.Sess return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) { +func (s *OpenTracingLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.GetSessionsExpired") s.Root.Store.SetContext(newCtx) @@ -6043,7 +6043,7 @@ func (s *OpenTracingLayerSessionStore) GetSessionsExpired(thresholdMillis int64, return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) { +func (s *OpenTracingLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.GetSessionsWithActiveDeviceIds") s.Root.Store.SetContext(newCtx) @@ -6061,7 +6061,7 @@ func (s *OpenTracingLayerSessionStore) GetSessionsWithActiveDeviceIds(userId str return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) PermanentDeleteSessionsByUser(teamId string) *model.AppError { +func (s *OpenTracingLayerSessionStore) PermanentDeleteSessionsByUser(teamId string) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.PermanentDeleteSessionsByUser") s.Root.Store.SetContext(newCtx) @@ -6079,7 +6079,7 @@ func (s *OpenTracingLayerSessionStore) PermanentDeleteSessionsByUser(teamId stri return resultVar0 } -func (s *OpenTracingLayerSessionStore) Remove(sessionIdOrToken string) *model.AppError { +func (s *OpenTracingLayerSessionStore) Remove(sessionIdOrToken string) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.Remove") s.Root.Store.SetContext(newCtx) @@ -6097,7 +6097,7 @@ func (s *OpenTracingLayerSessionStore) Remove(sessionIdOrToken string) *model.Ap return resultVar0 } -func (s *OpenTracingLayerSessionStore) RemoveAllSessions() *model.AppError { +func (s *OpenTracingLayerSessionStore) RemoveAllSessions() error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.RemoveAllSessions") s.Root.Store.SetContext(newCtx) @@ -6115,7 +6115,7 @@ func (s *OpenTracingLayerSessionStore) RemoveAllSessions() *model.AppError { return resultVar0 } -func (s *OpenTracingLayerSessionStore) Save(session *model.Session) (*model.Session, *model.AppError) { +func (s *OpenTracingLayerSessionStore) Save(session *model.Session) (*model.Session, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.Save") s.Root.Store.SetContext(newCtx) @@ -6133,7 +6133,7 @@ func (s *OpenTracingLayerSessionStore) Save(session *model.Session) (*model.Sess return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) { +func (s *OpenTracingLayerSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateDeviceId") s.Root.Store.SetContext(newCtx) @@ -6151,7 +6151,7 @@ func (s *OpenTracingLayerSessionStore) UpdateDeviceId(id string, deviceId string return resultVar0, resultVar1 } -func (s *OpenTracingLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError { +func (s *OpenTracingLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateExpiredNotify") s.Root.Store.SetContext(newCtx) @@ -6169,7 +6169,7 @@ func (s *OpenTracingLayerSessionStore) UpdateExpiredNotify(sessionid string, not return resultVar0 } -func (s *OpenTracingLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError { +func (s *OpenTracingLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateExpiresAt") s.Root.Store.SetContext(newCtx) @@ -6187,7 +6187,7 @@ func (s *OpenTracingLayerSessionStore) UpdateExpiresAt(sessionId string, time in return resultVar0 } -func (s *OpenTracingLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError { +func (s *OpenTracingLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateLastActivityAt") s.Root.Store.SetContext(newCtx) @@ -6205,7 +6205,7 @@ func (s *OpenTracingLayerSessionStore) UpdateLastActivityAt(sessionId string, ti return resultVar0 } -func (s *OpenTracingLayerSessionStore) UpdateProps(session *model.Session) *model.AppError { +func (s *OpenTracingLayerSessionStore) UpdateProps(session *model.Session) error { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateProps") s.Root.Store.SetContext(newCtx) @@ -6223,7 +6223,7 @@ func (s *OpenTracingLayerSessionStore) UpdateProps(session *model.Session) *mode return resultVar0 } -func (s *OpenTracingLayerSessionStore) UpdateRoles(userId string, roles string) (string, *model.AppError) { +func (s *OpenTracingLayerSessionStore) UpdateRoles(userId string, roles string) (string, error) { origCtx := s.Root.Store.Context() span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateRoles") s.Root.Store.SetContext(newCtx) diff --git a/store/sqlstore/session_store.go b/store/sqlstore/session_store.go index b55ab8eef4..b7b3e1bbea 100644 --- a/store/sqlstore/session_store.go +++ b/store/sqlstore/session_store.go @@ -4,10 +4,12 @@ package sqlstore import ( - "net/http" + "fmt" "time" sq "github.com/Masterminds/squirrel" + "github.com/pkg/errors" + "github.com/mattermost/mattermost-server/v5/mlog" "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/store" @@ -45,19 +47,19 @@ func (me SqlSessionStore) createIndexesIfNotExists() { me.CreateIndexIfNotExists("idx_sessions_last_activity_at", "Sessions", "LastActivityAt") } -func (me SqlSessionStore) Save(session *model.Session) (*model.Session, *model.AppError) { +func (me SqlSessionStore) Save(session *model.Session) (*model.Session, error) { if len(session.Id) > 0 { - return nil, model.NewAppError("SqlSessionStore.Save", "store.sql_session.save.existing.app_error", nil, "id="+session.Id, http.StatusBadRequest) + return nil, store.NewErrInvalidInput("Session", "id", session.Id) } session.PreSave() if err := me.GetMaster().Insert(session); err != nil { - return nil, model.NewAppError("SqlSessionStore.Save", "store.sql_session.save.app_error", nil, "id="+session.Id+", "+err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to save Session with id=%s", session.Id) } teamMembers, err := me.Team().GetTeamsForUser(session.UserId) if err != nil { - return nil, model.NewAppError("SqlSessionStore.Save", "store.sql_session.save.app_error", nil, "id="+session.Id+", "+err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find TeamMembers for Session with userId=%s", session.UserId) } session.TeamMembers = make([]*model.TeamMember, 0, len(teamMembers)) @@ -70,19 +72,19 @@ func (me SqlSessionStore) Save(session *model.Session) (*model.Session, *model.A return session, nil } -func (me SqlSessionStore) Get(sessionIdOrToken string) (*model.Session, *model.AppError) { +func (me SqlSessionStore) Get(sessionIdOrToken string) (*model.Session, error) { var sessions []*model.Session if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE Token = :Token OR Id = :Id LIMIT 1", map[string]interface{}{"Token": sessionIdOrToken, "Id": sessionIdOrToken}); err != nil { - return nil, model.NewAppError("SqlSessionStore.Get", "store.sql_session.get.app_error", nil, "sessionIdOrToken="+sessionIdOrToken+", "+err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find Sessions with sessionIdOrToken=%s", sessionIdOrToken) } else if len(sessions) == 0 { - return nil, model.NewAppError("SqlSessionStore.Get", "store.sql_session.get.app_error", nil, "sessionIdOrToken="+sessionIdOrToken, http.StatusNotFound) + return nil, store.NewErrNotFound("Session", fmt.Sprintf("sessionIdOrToken=%s", sessionIdOrToken)) } session := sessions[0] - tempMembers, err := me.Team().GetTeamsForUser(sessions[0].UserId) + tempMembers, err := me.Team().GetTeamsForUser(session.UserId) if err != nil { - return nil, model.NewAppError("SqlSessionStore.Get", "store.sql_session.get.app_error", nil, "sessionIdOrToken="+sessionIdOrToken+", "+err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find TeamMembers for Session with userId=%s", session.UserId) } sessions[0].TeamMembers = make([]*model.TeamMember, 0, len(tempMembers)) for _, tm := range tempMembers { @@ -93,16 +95,16 @@ func (me SqlSessionStore) Get(sessionIdOrToken string) (*model.Session, *model.A return session, nil } -func (me SqlSessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) { +func (me SqlSessionStore) GetSessions(userId string) ([]*model.Session, error) { var sessions []*model.Session if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = :UserId ORDER BY LastActivityAt DESC", map[string]interface{}{"UserId": userId}); err != nil { - return nil, model.NewAppError("SqlSessionStore.GetSessions", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find Sessions with userId=%s", userId) } teamMembers, err := me.Team().GetTeamsForUser(userId) if err != nil { - return nil, model.NewAppError("SqlSessionStore.GetSessions", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find TeamMembers for Session with userId=%s", userId) } for _, session := range sessions { @@ -116,7 +118,7 @@ func (me SqlSessionStore) GetSessions(userId string) ([]*model.Session, *model.A return sessions, nil } -func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) { +func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, error) { query := `SELECT * FROM @@ -131,12 +133,12 @@ func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*mode _, err := me.GetReplica().Select(&sessions, query, map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()}) if err != nil { - return nil, model.NewAppError("SqlSessionStore.GetActiveSessionsWithDeviceIds", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrapf(err, "failed to find Sessions with userId=%s", userId) } return sessions, nil } -func (me SqlSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) { +func (me SqlSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, error) { now := model.GetMillis() builder := me.getQueryBuilder(). Select("*"). @@ -153,97 +155,97 @@ func (me SqlSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly b query, args, err := builder.ToSql() if err != nil { - return nil, model.NewAppError("SqlSessionStore.GetSessionsExpired", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrap(err, "sessions_tosql") } var sessions []*model.Session _, err = me.GetReplica().Select(&sessions, query, args...) if err != nil { - return nil, model.NewAppError("SqlSessionStore.GetSessionsExpired", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) + return nil, errors.Wrap(err, "failed to find Sessions") } return sessions, nil } -func (me SqlSessionStore) UpdateExpiredNotify(sessionId string, notified bool) *model.AppError { +func (me SqlSessionStore) UpdateExpiredNotify(sessionId string, notified bool) error { query, args, err := me.getQueryBuilder(). Update("Sessions"). Set("ExpiredNotify", notified). Where(sq.Eq{"Id": sessionId}). ToSql() if err != nil { - return model.NewAppError("SqlSessionStore.UpdateExpiredNotifyAt", "store.sql.build_query.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError) + return errors.Wrap(err, "sessions_tosql") } _, err = me.GetMaster().Exec(query, args...) if err != nil { - return model.NewAppError("SqlSessionStore.UpdateExpiredNotifyAt", "store.sql_session.update_expired_notify.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError) + return errors.Wrapf(err, "failed to update Session with id=%s", sessionId) } return nil } -func (me SqlSessionStore) Remove(sessionIdOrToken string) *model.AppError { +func (me SqlSessionStore) Remove(sessionIdOrToken string) error { _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE Id = :Id Or Token = :Token", map[string]interface{}{"Id": sessionIdOrToken, "Token": sessionIdOrToken}) if err != nil { - return model.NewAppError("SqlSessionStore.RemoveSession", "store.sql_session.remove.app_error", nil, "id="+sessionIdOrToken+", err="+err.Error(), http.StatusInternalServerError) + return errors.Wrapf(err, "failed to delete Session with sessionIdOrToken=%s", sessionIdOrToken) } return nil } -func (me SqlSessionStore) RemoveAllSessions() *model.AppError { +func (me SqlSessionStore) RemoveAllSessions() error { _, err := me.GetMaster().Exec("DELETE FROM Sessions") if err != nil { - return model.NewAppError("SqlSessionStore.RemoveAllSessions", "store.sql_session.remove_all_sessions_for_team.app_error", nil, err.Error(), http.StatusInternalServerError) + return errors.Wrap(err, "failed to delete all Sessions") } return nil } -func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) *model.AppError { +func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) error { _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { - return model.NewAppError("SqlSessionStore.RemoveAllSessionsForUser", "store.sql_session.permanent_delete_sessions_by_user.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) + return errors.Wrapf(err, "failed to delete Session with userId=%s", userId) } return nil } -func (me SqlSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError { +func (me SqlSessionStore) UpdateExpiresAt(sessionId string, time int64) error { _, err := me.GetMaster().Exec("UPDATE Sessions SET ExpiresAt = :ExpiresAt, ExpiredNotify = false WHERE Id = :Id", map[string]interface{}{"ExpiresAt": time, "Id": sessionId}) if err != nil { - return model.NewAppError("SqlSessionStore.UpdateExpiresAt", "store.sql_session.update_expires_at.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError) + return errors.Wrapf(err, "failed to update Session with sessionId=%s", sessionId) } return nil } -func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError { +func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) error { _, err := me.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = :LastActivityAt WHERE Id = :Id", map[string]interface{}{"LastActivityAt": time, "Id": sessionId}) if err != nil { - return model.NewAppError("SqlSessionStore.UpdateLastActivityAt", "store.sql_session.update_last_activity.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError) + return errors.Wrapf(err, "failed to update Session with id=%s", sessionId) } return nil } -func (me SqlSessionStore) UpdateRoles(userId, roles string) (string, *model.AppError) { +func (me SqlSessionStore) UpdateRoles(userId, roles string) (string, error) { query := "UPDATE Sessions SET Roles = :Roles WHERE UserId = :UserId" _, err := me.GetMaster().Exec(query, map[string]interface{}{"Roles": roles, "UserId": userId}) if err != nil { - return "", model.NewAppError("SqlSessionStore.UpdateRoles", "store.sql_session.update_roles.app_error", nil, "userId="+userId, http.StatusInternalServerError) + return "", errors.Wrapf(err, "failed to update Session with userId=%s and roles=%s", userId, roles) } return userId, nil } -func (me SqlSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) { +func (me SqlSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, error) { query := "UPDATE Sessions SET DeviceId = :DeviceId, ExpiresAt = :ExpiresAt, ExpiredNotify = false WHERE Id = :Id" _, err := me.GetMaster().Exec(query, map[string]interface{}{"DeviceId": deviceId, "Id": id, "ExpiresAt": expiresAt}) if err != nil { - return "", model.NewAppError("SqlSessionStore.UpdateDeviceId", "store.sql_session.update_device_id.app_error", nil, err.Error(), http.StatusInternalServerError) + return "", errors.Wrapf(err, "failed to update Session with id=%s", id) } return deviceId, nil } -func (me SqlSessionStore) UpdateProps(session *model.Session) *model.AppError { +func (me SqlSessionStore) UpdateProps(session *model.Session) error { oldSession, appErr := me.Get(session.Id) if appErr != nil { return appErr @@ -252,15 +254,15 @@ func (me SqlSessionStore) UpdateProps(session *model.Session) *model.AppError { count, err := me.GetMaster().Update(oldSession) if err != nil { - return model.NewAppError("SqlSessionStore.UpdateProps", "store.sql_session.update_props.app_error", nil, err.Error(), http.StatusInternalServerError) + return errors.Wrap(err, "failed to update Session") } if count != 1 { - return model.NewAppError("SqlSessionStore.UpdateProps", "store.sql_session.update_props.app_error", nil, "", http.StatusInternalServerError) + return fmt.Errorf("updated Sessions were %d, expected 1", count) } return nil } -func (me SqlSessionStore) AnalyticsSessionCount() (int64, *model.AppError) { +func (me SqlSessionStore) AnalyticsSessionCount() (int64, error) { query := `SELECT COUNT(*) @@ -269,7 +271,7 @@ func (me SqlSessionStore) AnalyticsSessionCount() (int64, *model.AppError) { WHERE ExpiresAt > :Time` count, err := me.GetReplica().SelectInt(query, map[string]interface{}{"Time": model.GetMillis()}) if err != nil { - return int64(0), model.NewAppError("SqlSessionStore.AnalyticsSessionCount", "store.sql_session.analytics_session_count.app_error", nil, err.Error(), http.StatusInternalServerError) + return int64(0), errors.Wrap(err, "failed to count Sessions") } return count, nil } diff --git a/store/store.go b/store/store.go index e87a3cc5a0..9d0eea27ad 100644 --- a/store/store.go +++ b/store/store.go @@ -369,21 +369,21 @@ type BotStore interface { } type SessionStore interface { - Get(sessionIdOrToken string) (*model.Session, *model.AppError) - Save(session *model.Session) (*model.Session, *model.AppError) - GetSessions(userId string) ([]*model.Session, *model.AppError) - GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) - GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) - UpdateExpiredNotify(sessionid string, notified bool) *model.AppError - Remove(sessionIdOrToken string) *model.AppError - RemoveAllSessions() *model.AppError - PermanentDeleteSessionsByUser(teamId string) *model.AppError - UpdateExpiresAt(sessionId string, time int64) *model.AppError - UpdateLastActivityAt(sessionId string, time int64) *model.AppError - UpdateRoles(userId string, roles string) (string, *model.AppError) - UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) - UpdateProps(session *model.Session) *model.AppError - AnalyticsSessionCount() (int64, *model.AppError) + Get(sessionIdOrToken string) (*model.Session, error) + Save(session *model.Session) (*model.Session, error) + GetSessions(userId string) ([]*model.Session, error) + GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, error) + GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, error) + UpdateExpiredNotify(sessionid string, notified bool) error + Remove(sessionIdOrToken string) error + RemoveAllSessions() error + PermanentDeleteSessionsByUser(teamId string) error + UpdateExpiresAt(sessionId string, time int64) error + UpdateLastActivityAt(sessionId string, time int64) error + UpdateRoles(userId string, roles string) (string, error) + UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, error) + UpdateProps(session *model.Session) error + AnalyticsSessionCount() (int64, error) Cleanup(expiryTime int64, batchSize int64) } diff --git a/store/storetest/mocks/SessionStore.go b/store/storetest/mocks/SessionStore.go index 1c38e2263f..e7d2465758 100644 --- a/store/storetest/mocks/SessionStore.go +++ b/store/storetest/mocks/SessionStore.go @@ -15,7 +15,7 @@ type SessionStore struct { } // AnalyticsSessionCount provides a mock function with given fields: -func (_m *SessionStore) AnalyticsSessionCount() (int64, *model.AppError) { +func (_m *SessionStore) AnalyticsSessionCount() (int64, error) { ret := _m.Called() var r0 int64 @@ -25,13 +25,11 @@ func (_m *SessionStore) AnalyticsSessionCount() (int64, *model.AppError) { r0 = ret.Get(0).(int64) } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func() *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func() error); ok { r1 = rf() } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 @@ -43,7 +41,7 @@ func (_m *SessionStore) Cleanup(expiryTime int64, batchSize int64) { } // Get provides a mock function with given fields: sessionIdOrToken -func (_m *SessionStore) Get(sessionIdOrToken string) (*model.Session, *model.AppError) { +func (_m *SessionStore) Get(sessionIdOrToken string) (*model.Session, error) { ret := _m.Called(sessionIdOrToken) var r0 *model.Session @@ -55,20 +53,18 @@ func (_m *SessionStore) Get(sessionIdOrToken string) (*model.Session, *model.App } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { r1 = rf(sessionIdOrToken) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // GetSessions provides a mock function with given fields: userId -func (_m *SessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) { +func (_m *SessionStore) GetSessions(userId string) ([]*model.Session, error) { ret := _m.Called(userId) var r0 []*model.Session @@ -80,20 +76,18 @@ func (_m *SessionStore) GetSessions(userId string) ([]*model.Session, *model.App } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { r1 = rf(userId) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // GetSessionsExpired provides a mock function with given fields: thresholdMillis, mobileOnly, unnotifiedOnly -func (_m *SessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) { +func (_m *SessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, error) { ret := _m.Called(thresholdMillis, mobileOnly, unnotifiedOnly) var r0 []*model.Session @@ -105,20 +99,18 @@ func (_m *SessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly boo } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(int64, bool, bool) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(int64, bool, bool) error); ok { r1 = rf(thresholdMillis, mobileOnly, unnotifiedOnly) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // GetSessionsWithActiveDeviceIds provides a mock function with given fields: userId -func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) { +func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, error) { ret := _m.Called(userId) var r0 []*model.Session @@ -130,68 +122,60 @@ func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model. } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { r1 = rf(userId) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // PermanentDeleteSessionsByUser provides a mock function with given fields: teamId -func (_m *SessionStore) PermanentDeleteSessionsByUser(teamId string) *model.AppError { +func (_m *SessionStore) PermanentDeleteSessionsByUser(teamId string) error { ret := _m.Called(teamId) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { r0 = rf(teamId) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // Remove provides a mock function with given fields: sessionIdOrToken -func (_m *SessionStore) Remove(sessionIdOrToken string) *model.AppError { +func (_m *SessionStore) Remove(sessionIdOrToken string) error { ret := _m.Called(sessionIdOrToken) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(string) error); ok { r0 = rf(sessionIdOrToken) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // RemoveAllSessions provides a mock function with given fields: -func (_m *SessionStore) RemoveAllSessions() *model.AppError { +func (_m *SessionStore) RemoveAllSessions() error { ret := _m.Called() - var r0 *model.AppError - if rf, ok := ret.Get(0).(func() *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func() error); ok { r0 = rf() } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // Save provides a mock function with given fields: session -func (_m *SessionStore) Save(session *model.Session) (*model.Session, *model.AppError) { +func (_m *SessionStore) Save(session *model.Session) (*model.Session, error) { ret := _m.Called(session) var r0 *model.Session @@ -203,20 +187,18 @@ func (_m *SessionStore) Save(session *model.Session) (*model.Session, *model.App } } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(*model.Session) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(*model.Session) error); ok { r1 = rf(session) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // UpdateDeviceId provides a mock function with given fields: id, deviceId, expiresAt -func (_m *SessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) { +func (_m *SessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, error) { ret := _m.Called(id, deviceId, expiresAt) var r0 string @@ -226,84 +208,74 @@ func (_m *SessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int r0 = ret.Get(0).(string) } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string, string, int64) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(string, string, int64) error); ok { r1 = rf(id, deviceId, expiresAt) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 } // UpdateExpiredNotify provides a mock function with given fields: sessionid, notified -func (_m *SessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError { +func (_m *SessionStore) UpdateExpiredNotify(sessionid string, notified bool) error { ret := _m.Called(sessionid, notified) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(string, bool) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(string, bool) error); ok { r0 = rf(sessionid, notified) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // UpdateExpiresAt provides a mock function with given fields: sessionId, time -func (_m *SessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError { +func (_m *SessionStore) UpdateExpiresAt(sessionId string, time int64) error { ret := _m.Called(sessionId, time) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(string, int64) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(string, int64) error); ok { r0 = rf(sessionId, time) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // UpdateLastActivityAt provides a mock function with given fields: sessionId, time -func (_m *SessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError { +func (_m *SessionStore) UpdateLastActivityAt(sessionId string, time int64) error { ret := _m.Called(sessionId, time) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(string, int64) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(string, int64) error); ok { r0 = rf(sessionId, time) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // UpdateProps provides a mock function with given fields: session -func (_m *SessionStore) UpdateProps(session *model.Session) *model.AppError { +func (_m *SessionStore) UpdateProps(session *model.Session) error { ret := _m.Called(session) - var r0 *model.AppError - if rf, ok := ret.Get(0).(func(*model.Session) *model.AppError); ok { + var r0 error + if rf, ok := ret.Get(0).(func(*model.Session) error); ok { r0 = rf(session) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.AppError) - } + r0 = ret.Error(0) } return r0 } // UpdateRoles provides a mock function with given fields: userId, roles -func (_m *SessionStore) UpdateRoles(userId string, roles string) (string, *model.AppError) { +func (_m *SessionStore) UpdateRoles(userId string, roles string) (string, error) { ret := _m.Called(userId, roles) var r0 string @@ -313,13 +285,11 @@ func (_m *SessionStore) UpdateRoles(userId string, roles string) (string, *model r0 = ret.Get(0).(string) } - var r1 *model.AppError - if rf, ok := ret.Get(1).(func(string, string) *model.AppError); ok { + var r1 error + if rf, ok := ret.Get(1).(func(string, string) error); ok { r1 = rf(userId, roles) } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(*model.AppError) - } + r1 = ret.Error(1) } return r0, r1 diff --git a/store/storetest/oauth_store.go b/store/storetest/oauth_store.go index 68c26c5750..19d4c23fb1 100644 --- a/store/storetest/oauth_store.go +++ b/store/storetest/oauth_store.go @@ -366,8 +366,8 @@ func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) { s1.Token = model.NewId() s1.IsOAuth = true - s1, err = ss.Session().Save(s1) - require.Nil(t, err) + s1, nErr := ss.Session().Save(s1) + require.Nil(t, nErr) ad1 := model.AccessData{} ad1.ClientId = a1.Id @@ -382,8 +382,8 @@ func testOAuthStoreDeleteApp(t *testing.T, ss store.Store) { err = ss.OAuth().DeleteApp(a1.Id) require.Nil(t, err) - _, err = ss.Session().Get(s1.Token) - require.NotNil(t, err, "should error - session should be deleted") + _, nErr = ss.Session().Get(s1.Token) + require.NotNil(t, nErr, "should error - session should be deleted") _, err = ss.OAuth().GetAccessData(s1.Token) require.NotNil(t, err, "should error - access data should be deleted") diff --git a/store/storetest/session_store.go b/store/storetest/session_store.go index 8a4d143789..84aeef2374 100644 --- a/store/storetest/session_store.go +++ b/store/storetest/session_store.go @@ -381,12 +381,14 @@ func testUpdateExpiredNotify(t *testing.T, ss store.Store) { require.Nil(t, err) require.False(t, session.ExpiredNotify) - ss.Session().UpdateExpiredNotify(session.Id, true) + err = ss.Session().UpdateExpiredNotify(session.Id, true) + require.Nil(t, err) session, err = ss.Session().Get(s1.Id) require.Nil(t, err) require.True(t, session.ExpiredNotify) - ss.Session().UpdateExpiredNotify(session.Id, false) + err = ss.Session().UpdateExpiredNotify(session.Id, false) + require.Nil(t, err) session, err = ss.Session().Get(s1.Id) require.Nil(t, err) require.False(t, session.ExpiredNotify) diff --git a/store/storetest/user_access_token_store.go b/store/storetest/user_access_token_store.go index f86945df2e..f59eb55baa 100644 --- a/store/storetest/user_access_token_store.go +++ b/store/storetest/user_access_token_store.go @@ -134,8 +134,8 @@ func testUserAccessTokenSearch(t *testing.T, ss store.Store) { s1.UserId = uat.UserId s1.Token = uat.Token - s1, err = ss.Session().Save(s1) - require.Nil(t, err) + s1, nErr := ss.Session().Save(s1) + require.Nil(t, nErr) _, err = ss.UserAccessToken().Save(uat) require.Nil(t, err) diff --git a/store/timer_layer.go b/store/timer_layer.go index d9983e36b1..c26e4992e7 100644 --- a/store/timer_layer.go +++ b/store/timer_layer.go @@ -5388,7 +5388,7 @@ func (s *TimerLayerSchemeStore) Save(scheme *model.Scheme) (*model.Scheme, error return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) AnalyticsSessionCount() (int64, *model.AppError) { +func (s *TimerLayerSessionStore) AnalyticsSessionCount() (int64, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.AnalyticsSessionCount() @@ -5419,7 +5419,7 @@ func (s *TimerLayerSessionStore) Cleanup(expiryTime int64, batchSize int64) { } } -func (s *TimerLayerSessionStore) Get(sessionIdOrToken string) (*model.Session, *model.AppError) { +func (s *TimerLayerSessionStore) Get(sessionIdOrToken string) (*model.Session, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.Get(sessionIdOrToken) @@ -5435,7 +5435,7 @@ func (s *TimerLayerSessionStore) Get(sessionIdOrToken string) (*model.Session, * return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) { +func (s *TimerLayerSessionStore) GetSessions(userId string) ([]*model.Session, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.GetSessions(userId) @@ -5451,7 +5451,7 @@ func (s *TimerLayerSessionStore) GetSessions(userId string) ([]*model.Session, * return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) { +func (s *TimerLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.GetSessionsExpired(thresholdMillis, mobileOnly, unnotifiedOnly) @@ -5467,7 +5467,7 @@ func (s *TimerLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobil return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) { +func (s *TimerLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.GetSessionsWithActiveDeviceIds(userId) @@ -5483,7 +5483,7 @@ func (s *TimerLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ( return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) PermanentDeleteSessionsByUser(teamId string) *model.AppError { +func (s *TimerLayerSessionStore) PermanentDeleteSessionsByUser(teamId string) error { start := timemodule.Now() resultVar0 := s.SessionStore.PermanentDeleteSessionsByUser(teamId) @@ -5499,7 +5499,7 @@ func (s *TimerLayerSessionStore) PermanentDeleteSessionsByUser(teamId string) *m return resultVar0 } -func (s *TimerLayerSessionStore) Remove(sessionIdOrToken string) *model.AppError { +func (s *TimerLayerSessionStore) Remove(sessionIdOrToken string) error { start := timemodule.Now() resultVar0 := s.SessionStore.Remove(sessionIdOrToken) @@ -5515,7 +5515,7 @@ func (s *TimerLayerSessionStore) Remove(sessionIdOrToken string) *model.AppError return resultVar0 } -func (s *TimerLayerSessionStore) RemoveAllSessions() *model.AppError { +func (s *TimerLayerSessionStore) RemoveAllSessions() error { start := timemodule.Now() resultVar0 := s.SessionStore.RemoveAllSessions() @@ -5531,7 +5531,7 @@ func (s *TimerLayerSessionStore) RemoveAllSessions() *model.AppError { return resultVar0 } -func (s *TimerLayerSessionStore) Save(session *model.Session) (*model.Session, *model.AppError) { +func (s *TimerLayerSessionStore) Save(session *model.Session) (*model.Session, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.Save(session) @@ -5547,7 +5547,7 @@ func (s *TimerLayerSessionStore) Save(session *model.Session) (*model.Session, * return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) { +func (s *TimerLayerSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.UpdateDeviceId(id, deviceId, expiresAt) @@ -5563,7 +5563,7 @@ func (s *TimerLayerSessionStore) UpdateDeviceId(id string, deviceId string, expi return resultVar0, resultVar1 } -func (s *TimerLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError { +func (s *TimerLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) error { start := timemodule.Now() resultVar0 := s.SessionStore.UpdateExpiredNotify(sessionid, notified) @@ -5579,7 +5579,7 @@ func (s *TimerLayerSessionStore) UpdateExpiredNotify(sessionid string, notified return resultVar0 } -func (s *TimerLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError { +func (s *TimerLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) error { start := timemodule.Now() resultVar0 := s.SessionStore.UpdateExpiresAt(sessionId, time) @@ -5595,7 +5595,7 @@ func (s *TimerLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) * return resultVar0 } -func (s *TimerLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) *model.AppError { +func (s *TimerLayerSessionStore) UpdateLastActivityAt(sessionId string, time int64) error { start := timemodule.Now() resultVar0 := s.SessionStore.UpdateLastActivityAt(sessionId, time) @@ -5611,7 +5611,7 @@ func (s *TimerLayerSessionStore) UpdateLastActivityAt(sessionId string, time int return resultVar0 } -func (s *TimerLayerSessionStore) UpdateProps(session *model.Session) *model.AppError { +func (s *TimerLayerSessionStore) UpdateProps(session *model.Session) error { start := timemodule.Now() resultVar0 := s.SessionStore.UpdateProps(session) @@ -5627,7 +5627,7 @@ func (s *TimerLayerSessionStore) UpdateProps(session *model.Session) *model.AppE return resultVar0 } -func (s *TimerLayerSessionStore) UpdateRoles(userId string, roles string) (string, *model.AppError) { +func (s *TimerLayerSessionStore) UpdateRoles(userId string, roles string) (string, error) { start := timemodule.Now() resultVar0, resultVar1 := s.SessionStore.UpdateRoles(userId, roles)