call to purge history items

This commit is contained in:
sawka 2023-03-03 13:31:16 -08:00
parent 60e17f160f
commit 22d0c5687e
3 changed files with 103 additions and 31 deletions

View File

@ -178,6 +178,7 @@ func init() {
registerCmdFn("history", HistoryCommand)
registerCmdFn("history:viewall", HistoryViewAllCommand)
registerCmdFn("history:purge", HistoryPurgeCommand)
registerCmdFn("bookmarks:show", BookmarksShowCommand)
@ -1814,6 +1815,38 @@ func ClearCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore
}
func HistoryPurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
if len(pk.Args) == 0 {
return nil, fmt.Errorf("/history:purge requires at least one argument (history id)")
}
var historyIds []string
for _, historyArg := range pk.Args {
_, err := uuid.Parse(historyArg)
if err != nil {
return nil, fmt.Errorf("invalid historyid (must be uuid)")
}
historyIds = append(historyIds, historyArg)
}
historyItemsRemoved, err := sstore.PurgeHistoryByIds(ctx, historyIds)
if err != nil {
return nil, fmt.Errorf("/history:purge error purging items: %v", err)
}
update := sstore.ModelUpdate{}
for _, historyItem := range historyItemsRemoved {
if historyItem.LineId == "" {
continue
}
lineObj := &sstore.LineType{
SessionId: historyItem.SessionId,
WindowId: historyItem.WindowId,
LineId: historyItem.LineId,
Remove: true,
}
update.Lines = append(update.Lines, lineObj)
}
return update, nil
}
const HistoryViewPageSize = 50
func HistoryViewAllCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
@ -2258,27 +2291,34 @@ func LinePurgeCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (ss
return nil, err
}
if len(pk.Args) == 0 {
return nil, fmt.Errorf("/line:purge requires an argument (line number or id)")
return nil, fmt.Errorf("/line:purge requires at least one argument (line number or id)")
}
lineArg := pk.Args[0]
lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg)
var lineIds []string
for _, lineArg := range pk.Args {
lineId, err := sstore.FindLineIdByArg(ctx, ids.SessionId, ids.WindowId, lineArg)
if err != nil {
return nil, fmt.Errorf("error looking up lineid: %v", err)
}
if lineId == "" {
return nil, fmt.Errorf("line %q not found", lineArg)
}
lineIds = append(lineIds, lineId)
}
err = sstore.PurgeLinesByIds(ctx, ids.SessionId, lineIds)
if err != nil {
return nil, fmt.Errorf("error looking up lineid: %v", err)
return nil, fmt.Errorf("/line:purge error purging lines: %v", err)
}
if lineId == "" {
return nil, fmt.Errorf("line %q not found", lineArg)
update := sstore.ModelUpdate{}
for _, lineId := range lineIds {
lineObj := &sstore.LineType{
SessionId: ids.SessionId,
WindowId: ids.WindowId,
LineId: lineId,
Remove: true,
}
update.Lines = append(update.Lines, lineObj)
}
err = sstore.PurgeLineById(ctx, ids.SessionId, lineId)
if err != nil {
return nil, fmt.Errorf("/line:purge error purging line: %v", err)
}
lineObj := &sstore.LineType{
SessionId: ids.SessionId,
WindowId: ids.WindowId,
LineId: lineId,
Remove: true,
}
return sstore.ModelUpdate{Line: lineObj}, nil
return update, nil
}
func LineShowCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {

View File

@ -1885,21 +1885,23 @@ func purgeCmdById(ctx context.Context, sessionId string, cmdId string) error {
return txErr
}
func PurgeLineById(ctx context.Context, sessionId string, lineId string) error {
func PurgeLinesByIds(ctx context.Context, sessionId string, lineIds []string) error {
txErr := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT cmdid FROM line WHERE sessionid = ? AND lineid = ?`
cmdId := tx.GetString(query, sessionId, lineId)
query = `DELETE FROM line WHERE sessionid = ? AND lineid = ?`
tx.Exec(query, sessionId, lineId)
query = `DELETE FROM history WHERE sessionid = ? AND lineid = ?`
tx.Exec(query, sessionId, lineId)
if cmdId != "" {
query = `SELECT count(*) FROM line WHERE sessionid = ? AND cmdid = ?`
cmdRefCount := tx.GetInt(query, sessionId, cmdId)
if cmdRefCount == 0 {
err := purgeCmdById(tx.Context(), sessionId, cmdId)
if err != nil {
return err
for _, lineId := range lineIds {
query := `SELECT cmdid FROM line WHERE sessionid = ? AND lineid = ?`
cmdId := tx.GetString(query, sessionId, lineId)
query = `DELETE FROM line WHERE sessionid = ? AND lineid = ?`
tx.Exec(query, sessionId, lineId)
query = `DELETE FROM history WHERE sessionid = ? AND lineid = ?`
tx.Exec(query, sessionId, lineId)
if cmdId != "" {
query = `SELECT count(*) FROM line WHERE sessionid = ? AND cmdid = ?`
cmdRefCount := tx.GetInt(query, sessionId, cmdId)
if cmdRefCount == 0 {
err := purgeCmdById(tx.Context(), sessionId, cmdId)
if err != nil {
return err
}
}
}
}
@ -2388,3 +2390,32 @@ func GetLineCmdsFromHistoryItems(ctx context.Context, historyItems []*HistoryIte
}
return lineArr, cmdArr, nil
}
func PurgeHistoryByIds(ctx context.Context, historyIds []string) ([]*HistoryItemType, error) {
var rtn []*HistoryItemType
txErr := WithTx(ctx, func(tx *TxWrap) error {
query := `SELECT * FROM history WHERE historyid IN (SELECT value FROM json_each(?))`
marr := tx.SelectMaps(query, quickJsonArr(historyIds))
for _, m := range marr {
hitem := HistoryItemFromMap(m)
if hitem != nil {
rtn = append(rtn, hitem)
}
}
query = `DELETE FROM history WHERE historyid IN (SELECT value FROM json_each(?))`
tx.Exec(query, quickJsonArr(historyIds))
for _, hitem := range rtn {
if hitem.LineId != "" {
err := PurgeLinesByIds(tx.Context(), hitem.SessionId, []string{hitem.LineId})
if err != nil {
return err
}
}
}
return nil
})
if txErr != nil {
return nil, txErr
}
return rtn, nil
}

View File

@ -35,6 +35,7 @@ type ModelUpdate struct {
Windows []*WindowType `json:"windows,omitempty"`
ScreenWindows []*ScreenWindowType `json:"screenwindows,omitempty"`
Line *LineType `json:"line,omitempty"`
Lines []*LineType `json:"lines,omitempty"`
Cmd *CmdType `json:"cmd,omitempty"`
CmdLine *CmdLineType `json:"cmdline,omitempty"`
Info *InfoMsgType `json:"info,omitempty"`