From 5933287e0db82c9b413c71fd2c2103a345eda801 Mon Sep 17 00:00:00 2001 From: sawka Date: Mon, 26 Dec 2022 18:42:55 -0800 Subject: [PATCH] unarchive session --- pkg/cmdrunner/cmdrunner.go | 9 ++++++++- pkg/sstore/dbops.go | 40 ++++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 8d218b20..340a0f1e 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -1428,12 +1428,19 @@ func SessionArchiveCommand(ctx context.Context, pk *scpacket.FeCommandPacketType if err != nil { return nil, fmt.Errorf("cannot archive session: %v", err) } + update.Info = &sstore.InfoMsgType{ + InfoMsg: "session archived", + } return update, nil } else { - update, err := sstore.UnArchiveSession(ctx, sessionId) + activate := resolveBool(pk.Kwargs["activate"], false) + update, err := sstore.UnArchiveSession(ctx, sessionId, activate) if err != nil { return nil, fmt.Errorf("cannot un-archive session: %v", err) } + update.Info = &sstore.InfoMsgType{ + InfoMsg: "session un-archived", + } return update, nil } } diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index 54360647..c293462b 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1344,7 +1344,7 @@ func fixActiveSessionId(ctx context.Context) (string, error) { return newActiveSessionId, nil } -func ArchiveSession(ctx context.Context, sessionId string) (UpdatePacket, error) { +func ArchiveSession(ctx context.Context, sessionId string) (*ModelUpdate, error) { if sessionId == "" { return nil, fmt.Errorf("invalid blank sessionid") } @@ -1368,7 +1368,7 @@ func ArchiveSession(ctx context.Context, sessionId string) (UpdatePacket, error) return nil, txErr } bareSession, _ := GetBareSessionById(ctx, sessionId) - update := ModelUpdate{} + update := &ModelUpdate{} if bareSession != nil { update.Sessions = append(update.Sessions, bareSession) } @@ -1378,8 +1378,40 @@ func ArchiveSession(ctx context.Context, sessionId string) (UpdatePacket, error) return update, nil } -func UnArchiveSession(ctx context.Context, sessionId string) (UpdatePacket, error) { - return nil, nil +func UnArchiveSession(ctx context.Context, sessionId string, activate bool) (*ModelUpdate, error) { + if sessionId == "" { + return nil, fmt.Errorf("invalid blank sessionid") + } + txErr := WithTx(ctx, func(tx *TxWrap) error { + query := `SELECT sessionid FROM session WHERE sessionid = ?` + if !tx.Exists(query, sessionId) { + return fmt.Errorf("session does not exist") + } + query = `SELECT archived FROM session WHERE sessionid = ?` + isArchived := tx.GetBool(query, sessionId) + if !isArchived { + return nil + } + query = `UPDATE session SET archived = 0, archivedts = 0 WHERE sessionid = ?` + tx.ExecWrap(query, sessionId) + if activate { + query = `UPDATE client SET activesessionid = ?` + tx.ExecWrap(query, sessionId) + } + return nil + }) + if txErr != nil { + return nil, txErr + } + bareSession, _ := GetBareSessionById(ctx, sessionId) + update := &ModelUpdate{} + if bareSession != nil { + update.Sessions = append(update.Sessions, bareSession) + } + if activate { + update.ActiveSessionId = sessionId + } + return update, nil } func GetSessionStats(ctx context.Context, sessionId string) (*SessionStatsType, error) {