mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
MM - 15795 Migrate "Session.GetSessions" to Sync by default (#11000)
This commit is contained in:
@@ -89,21 +89,15 @@ func (a *App) GetSession(token string) (*model.Session, *model.AppError) {
|
||||
}
|
||||
|
||||
func (a *App) GetSessions(userId string) ([]*model.Session, *model.AppError) {
|
||||
result := <-a.Srv.Store.Session().GetSessions(userId)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
return result.Data.([]*model.Session), nil
|
||||
|
||||
return a.Srv.Store.Session().GetSessions(userId)
|
||||
}
|
||||
|
||||
func (a *App) RevokeAllSessions(userId string) *model.AppError {
|
||||
result := <-a.Srv.Store.Session().GetSessions(userId)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
sessions, err := a.Srv.Store.Session().GetSessions(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessions := result.Data.([]*model.Session)
|
||||
|
||||
for _, session := range sessions {
|
||||
if session.IsOAuth {
|
||||
a.RevokeAccessToken(session.Token)
|
||||
@@ -159,11 +153,10 @@ func (a *App) SessionCacheLength() int {
|
||||
}
|
||||
|
||||
func (a *App) RevokeSessionsForDeviceId(userId string, deviceId string, currentSessionId string) *model.AppError {
|
||||
result := <-a.Srv.Store.Session().GetSessions(userId)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
sessions, err := a.Srv.Store.Session().GetSessions(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sessions := result.Data.([]*model.Session)
|
||||
for _, session := range sessions {
|
||||
if session.DeviceId == deviceId && session.Id != currentSessionId {
|
||||
mlog.Debug(fmt.Sprintf("Revoking sessionId=%v for userId=%v re-login with same device Id", session.Id, userId), mlog.String("user_id", userId))
|
||||
|
||||
@@ -99,34 +99,29 @@ func (me SqlSessionStore) Get(sessionIdOrToken string) (*model.Session, *model.A
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (me SqlSessionStore) GetSessions(userId string) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var sessions []*model.Session
|
||||
func (me SqlSessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) {
|
||||
var sessions []*model.Session
|
||||
|
||||
tcs := me.Team().GetTeamsForUser(userId)
|
||||
tcs := me.Team().GetTeamsForUser(userId)
|
||||
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)
|
||||
}
|
||||
|
||||
if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = :UserId ORDER BY LastActivityAt DESC", map[string]interface{}{"UserId": userId}); err != nil {
|
||||
result.Err = model.NewAppError("SqlSessionStore.GetSessions", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
rtcs := <-tcs
|
||||
if rtcs.Err != nil {
|
||||
return nil, model.NewAppError("SqlSessionStore.GetSessions", "store.sql_session.get_sessions.app_error", nil, rtcs.Err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
result.Data = sessions
|
||||
}
|
||||
|
||||
if rtcs := <-tcs; rtcs.Err != nil {
|
||||
result.Err = model.NewAppError("SqlSessionStore.GetSessions", "store.sql_session.get_sessions.app_error", nil, rtcs.Err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
} else {
|
||||
for _, session := range sessions {
|
||||
tempMembers := rtcs.Data.([]*model.TeamMember)
|
||||
session.TeamMembers = make([]*model.TeamMember, 0, len(tempMembers))
|
||||
for _, tm := range tempMembers {
|
||||
if tm.DeleteAt == 0 {
|
||||
session.TeamMembers = append(session.TeamMembers, tm)
|
||||
}
|
||||
}
|
||||
for _, session := range sessions {
|
||||
tempMembers := rtcs.Data.([]*model.TeamMember)
|
||||
session.TeamMembers = make([]*model.TeamMember, 0, len(tempMembers))
|
||||
for _, tm := range tempMembers {
|
||||
if tm.DeleteAt == 0 {
|
||||
session.TeamMembers = append(session.TeamMembers, tm)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
return sessions, nil
|
||||
}
|
||||
|
||||
func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
|
||||
|
||||
@@ -314,7 +314,7 @@ type BotStore interface {
|
||||
type SessionStore interface {
|
||||
Get(sessionIdOrToken string) (*model.Session, *model.AppError)
|
||||
Save(session *model.Session) (*model.Session, *model.AppError)
|
||||
GetSessions(userId string) StoreChannel
|
||||
GetSessions(userId string) ([]*model.Session, *model.AppError)
|
||||
GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError)
|
||||
Remove(sessionIdOrToken string) StoreChannel
|
||||
RemoveAllSessions() StoreChannel
|
||||
|
||||
@@ -67,19 +67,28 @@ func (_m *SessionStore) Get(sessionIdOrToken string) (*model.Session, *model.App
|
||||
}
|
||||
|
||||
// GetSessions provides a mock function with given fields: userId
|
||||
func (_m *SessionStore) GetSessions(userId string) store.StoreChannel {
|
||||
func (_m *SessionStore) GetSessions(userId string) ([]*model.Session, *model.AppError) {
|
||||
ret := _m.Called(userId)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
|
||||
var r0 []*model.Session
|
||||
if rf, ok := ret.Get(0).(func(string) []*model.Session); ok {
|
||||
r0 = rf(userId)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
r0 = ret.Get(0).([]*model.Session)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
|
||||
r1 = rf(userId)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetSessionsWithActiveDeviceIds provides a mock function with given fields: userId
|
||||
|
||||
@@ -67,10 +67,10 @@ func testSessionGet(t *testing.T, ss store.Store) {
|
||||
}
|
||||
}
|
||||
|
||||
if rs2 := (<-ss.Session().GetSessions(s1.UserId)); rs2.Err != nil {
|
||||
t.Fatal(rs2.Err)
|
||||
if session, err := ss.Session().GetSessions(s1.UserId); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
if len(rs2.Data.([]*model.Session)) != 3 {
|
||||
if len(session) != 3 {
|
||||
t.Fatal("should match len")
|
||||
}
|
||||
}
|
||||
@@ -197,10 +197,10 @@ func testSessionRemoveToken(t *testing.T, ss store.Store) {
|
||||
t.Fatal("should have been removed")
|
||||
}
|
||||
|
||||
if rs3 := (<-ss.Session().GetSessions(s1.UserId)); rs3.Err != nil {
|
||||
t.Fatal(rs3.Err)
|
||||
if session, err := ss.Session().GetSessions(s1.UserId); err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
if len(rs3.Data.([]*model.Session)) != 0 {
|
||||
if len(session) != 0 {
|
||||
t.Fatal("should match len")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user