From 1a88d564bb674b6574a94404fea331c9110ec058 Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 31 Aug 2022 13:28:52 -0700 Subject: [PATCH] return different history types --- pkg/cmdrunner/cmdrunner.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 332a2c54..1e0ceea8 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -22,6 +22,12 @@ import ( "github.com/scripthaus-dev/sh2-server/pkg/sstore" ) +const ( + HistoryTypeWindow = "window" + HistoryTypeSession = "session" + HistoryTypeGlobal = "global" +) + const DefaultUserId = "sawka" const MaxNameLen = 50 @@ -927,17 +933,33 @@ func HistoryCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ssto if maxItems == 0 { maxItems = DefaultMaxHistoryItems } - hitems, err := sstore.GetHistoryItems(ctx, ids.SessionId, ids.WindowId, sstore.HistoryQueryOpts{MaxItems: maxItems}) + htype := HistoryTypeWindow + hSessionId := ids.SessionId + hWindowId := ids.WindowId + if pk.Kwargs["type"] != "" { + htype = pk.Kwargs["type"] + if htype != HistoryTypeWindow && htype != HistoryTypeSession && htype != HistoryTypeGlobal { + return nil, fmt.Errorf("invalid history type '%s', valid types: %s", htype, formatStrs([]string{HistoryTypeWindow, HistoryTypeSession, HistoryTypeGlobal}, "or", false)) + } + } + if htype == HistoryTypeGlobal { + hSessionId = "" + hWindowId = "" + } else if htype == HistoryTypeSession { + hWindowId = "" + } + hitems, err := sstore.GetHistoryItems(ctx, hSessionId, hWindowId, sstore.HistoryQueryOpts{MaxItems: maxItems}) if err != nil { return nil, err } show := !resolveBool(pk.Kwargs["noshow"], false) update := &sstore.ModelUpdate{} update.History = &sstore.HistoryInfoType{ - SessionId: ids.SessionId, - WindowId: ids.WindowId, - Items: hitems, - Show: show, + HistoryType: htype, + SessionId: ids.SessionId, + WindowId: ids.WindowId, + Items: hitems, + Show: show, } return update, nil }