diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index e8973501f..fbc682461 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -51,6 +51,8 @@ const MaxCommandLen = 4096 const MaxSignalLen = 12 const MaxSignalNum = 64 const MaxEvalDepth = 5 +const DevWebScreenUrlFmt = "http://devtest.getprompt.com:9001/static/index.html?screenid=%s&viewkey=%s" +const ProdWebScreenUrlFmt = "https://share.getprompt.dev/s/%s?viewkey=%s" var ColorNames = []string{"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} var RemoteColorNames = []string{"red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} @@ -220,6 +222,13 @@ func GetCmdStr(pk *scpacket.FeCommandPacketType) string { return pk.MetaCmd + ":" + pk.MetaSubCmd } +func GetWebShareUrl(screenId string, viewKey string) string { + if scbase.IsDevMode() { + return fmt.Sprintf(DevWebScreenUrlFmt, screenId, viewKey) + } + return fmt.Sprintf(ProdWebScreenUrlFmt, screenId, viewKey) +} + func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { metaCmd := SubMetaCmd(pk.MetaCmd) var cmdName string @@ -1658,7 +1667,7 @@ func ScreenWebShareCommand(ctx context.Context, pk *scpacket.FeCommandPacketType if err != nil { return nil, fmt.Errorf("cannot web-share screen: %v", err) } - infoMsg = fmt.Sprintf("screen is now shared to the web at %s", fmt.Sprintf("screenid=%s viewkey=%s", ids.ScreenId, viewKey)) + infoMsg = fmt.Sprintf("screen is now shared to the web at %s", GetWebShareUrl(ids.ScreenId, viewKey)) } else { err = sstore.ScreenWebShareStop(ctx, ids.ScreenId) if err != nil { diff --git a/pkg/pcloud/pcloud.go b/pkg/pcloud/pcloud.go index c31d95f57..9f3465ffc 100644 --- a/pkg/pcloud/pcloud.go +++ b/pkg/pcloud/pcloud.go @@ -237,13 +237,6 @@ func makeWebShareUpdate(ctx context.Context, update *sstore.ScreenUpdateType) (* case sstore.UpdateType_LineDel: break - case sstore.UpdateType_LineArchived: - line, err := sstore.GetLineById(ctx, update.ScreenId, update.LineId) - if err != nil || line == nil { - return nil, fmt.Errorf("error getting line: %v", defaultError(err, "not found")) - } - rtn.BVal = line.Archived - case sstore.UpdateType_LineRenderer: line, err := sstore.GetLineById(ctx, update.ScreenId, update.LineId) if err != nil || line == nil { @@ -292,7 +285,6 @@ func makeWebShareUpdate(ctx context.Context, update *sstore.ScreenUpdateType) (* if err != nil { return nil, fmt.Errorf("error getting ptypos: %v", err) } - sstore.SetWebScreenPtyPosDelIntent(update.ScreenId, update.LineId) realOffset, data, err := sstore.ReadPtyOutFile(ctx, update.ScreenId, cmdId, ptyPos, MaxPtyUpdateSize+1) if err != nil { return nil, fmt.Errorf("error getting ptydata: %v", err) @@ -315,30 +307,23 @@ func makeWebShareUpdate(ctx context.Context, update *sstore.ScreenUpdateType) (* func finalizeWebScreenUpdate(ctx context.Context, webUpdate *WebShareUpdateType) error { switch webUpdate.UpdateType { case sstore.UpdateType_PtyPos: - dataEof := webUpdate.PtyData.Eof newPos := webUpdate.PtyData.PtyPos + int64(len(webUpdate.PtyData.Data)) - if dataEof { - err := sstore.RemoveScreenUpdate(ctx, webUpdate.UpdateId) - if err != nil { - return err - } - } err := sstore.SetWebPtyPos(ctx, webUpdate.ScreenId, webUpdate.LineId, newPos) if err != nil { return err } - err = sstore.MaybeRemovePtyPosUpdate(ctx, webUpdate.ScreenId, webUpdate.LineId, webUpdate.UpdateId) - if err != nil { - return err - } - default: - err := sstore.RemoveScreenUpdate(ctx, webUpdate.UpdateId) + case sstore.UpdateType_LineDel: + err := sstore.DeleteWebPtyPos(ctx, webUpdate.ScreenId, webUpdate.LineId) if err != nil { - // this is not great, this *should* never fail and is not easy to recover from return err } } + err := sstore.RemoveScreenUpdate(ctx, webUpdate.UpdateId) + if err != nil { + // this is not great, this *should* never fail and is not easy to recover from + return err + } return nil } @@ -357,8 +342,6 @@ func DoWebScreenUpdates(authInfo AuthInfo, updateArr []*sstore.ScreenUpdateType) log.Printf("[pcloud] error create web-share update updateid:%d: %v", update.UpdateId, err) } if update.UpdateType == sstore.UpdateType_PtyPos { - err = sstore.MaybeRemovePtyPosUpdate(context.Background(), update.ScreenId, update.LineId, update.UpdateId) - } else { err = sstore.RemoveScreenUpdate(context.Background(), update.UpdateId) } if err != nil { diff --git a/pkg/sstore/dbops.go b/pkg/sstore/dbops.go index f2637829d..2c1f2fd41 100644 --- a/pkg/sstore/dbops.go +++ b/pkg/sstore/dbops.go @@ -1365,10 +1365,15 @@ func ArchiveScreenLines(ctx context.Context, screenId string) (*ModelUpdate, err if !tx.Exists(query, screenId) { return fmt.Errorf("screen does not exist") } + fmt.Printf("** archive-screen-lines: %s\n", screenId) if isWebShare(tx, screenId) { query = `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) SELECT screenid, lineid, ?, ? FROM line WHERE screenid = ? AND archived = 0` - tx.Exec(query, UpdateType_LineArchived, time.Now().UnixMilli(), screenId) + tx.Exec(query, UpdateType_LineDel, time.Now().UnixMilli(), screenId) + NotifyUpdateWriter() + query = `SELECT count(*) FROM line WHERE screenid = ? AND archived = 0` + count := tx.GetInt(query, screenId) + fmt.Printf("** archive-screen-lines: wrote into screenupdate: %d\n", count) } query = `UPDATE line SET archived = 1 WHERE screenid = ? AND archived = 0` tx.Exec(query, screenId) @@ -1924,7 +1929,11 @@ func SetLineArchivedById(ctx context.Context, screenId string, lineId string, ar query := `UPDATE line SET archived = ? WHERE lineid = ?` tx.Exec(query, archived, lineId) if isWebShare(tx, screenId) { - insertScreenLineUpdate(tx, screenId, lineId, UpdateType_LineArchived) + if archived { + insertScreenLineUpdate(tx, screenId, lineId, UpdateType_LineDel) + } else { + insertScreenLineUpdate(tx, screenId, lineId, UpdateType_LineNew) + } } return nil }) @@ -2420,10 +2429,10 @@ func ScreenWebShareStart(ctx context.Context, screenId string, shareOpts ScreenW tx.Exec(query, ShareModeWeb, quickJson(shareOpts), screenId) insertScreenUpdate(tx, screenId, UpdateType_ScreenNew) query = `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) - SELECT screenid, lineid, ?, ? FROM line WHERE screenid = ? ORDER BY linenum` + SELECT screenid, lineid, ?, ? FROM line WHERE screenid = ? AND NOT archived ORDER BY linenum` tx.Exec(query, UpdateType_LineNew, nowTs, screenId) query = `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) - SELECT c.screenid, l.lineid, ?, ? FROM cmd c, line l WHERE c.screenid = ? AND l.cmdid = c.cmdid` + SELECT c.screenid, l.lineid, ?, ? FROM cmd c, line l WHERE c.screenid = ? AND l.cmdid = c.cmdid AND NOT l.archived` tx.Exec(query, UpdateType_PtyPos, nowTs, screenId) NotifyUpdateWriter() return nil @@ -2474,12 +2483,16 @@ func insertScreenLineUpdate(tx *TxWrap, screenId string, lineId string, updateTy tx.SetErr(errors.New("invalid screen-update, lineid is empty")) return } - query := `SELECT updateid FROM screenupdate WHERE screenid = ? AND lineid = ? AND (updatetype = ? OR updatetype = ?)` - if !tx.Exists(query, screenId, lineId, updateType, UpdateType_LineNew) { - query = `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) VALUES (?, ?, ?, ?)` - tx.Exec(query, screenId, lineId, updateType, time.Now().UnixMilli()) - NotifyUpdateWriter() + if updateType == UpdateType_LineNew || updateType == UpdateType_LineDel { + query := `DELETE FROM screenupdate WHERE screenid = ? AND lineid = ?` + tx.Exec(query, screenId, lineId) } + query := `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) VALUES (?, ?, ?, ?)` + tx.Exec(query, screenId, lineId, updateType, time.Now().UnixMilli()) + if updateType == UpdateType_LineNew { + tx.Exec(query, screenId, lineId, UpdateType_PtyPos, time.Now().UnixMilli()) + } + NotifyUpdateWriter() } func insertScreenCmdUpdate(tx *TxWrap, screenId string, cmdId string, updateType string) { @@ -2515,49 +2528,12 @@ func RemoveScreenUpdate(ctx context.Context, updateId int64) error { }) } -func SetWebScreenPtyPosDelIntent(screenId string, lineId string) { - WebScreenPtyPosLock.Lock() - defer WebScreenPtyPosLock.Unlock() - WebScreenPtyPosDelIntent[screenId+":"+lineId] = true -} - -func ClearWebScreenPtyPosDelIntent(screenId string, lineId string) bool { - WebScreenPtyPosLock.Lock() - defer WebScreenPtyPosLock.Unlock() - rtn := WebScreenPtyPosDelIntent[screenId+":"+lineId] - delete(WebScreenPtyPosDelIntent, screenId+":"+lineId) - return rtn -} - func MaybeInsertPtyPosUpdate(ctx context.Context, screenId string, cmdId string) error { return WithTx(ctx, func(tx *TxWrap) error { if !isWebShare(tx, screenId) { return nil } - query := `SELECT lineid FROM line WHERE screenid = ? AND cmdid = ?` - lineId := tx.GetString(query, screenId, cmdId) - if lineId == "" { - return fmt.Errorf("invalid ptypos update, no lineid found for %s/%s", screenId, cmdId) - } - ClearWebScreenPtyPosDelIntent(screenId, lineId) // clear delete intention because we have a new update - query = `SELECT updateid FROM screenupdate WHERE screenid = ? AND lineid = ? AND updatetype = ?` - if !tx.Exists(query, screenId, lineId, UpdateType_PtyPos) { - query := `INSERT INTO screenupdate (screenid, lineid, updatetype, updatets) VALUES (?, ?, ?, ?)` - tx.Exec(query, screenId, lineId, UpdateType_PtyPos, time.Now().UnixMilli()) - NotifyUpdateWriter() - } - return nil - }) -} - -func MaybeRemovePtyPosUpdate(ctx context.Context, screenId string, lineId string, updateId int64) error { - return WithTx(ctx, func(tx *TxWrap) error { - intent := ClearWebScreenPtyPosDelIntent(screenId, lineId) // check for intention before deleting - if !intent { - return nil - } - query := `DELETE FROM screenupdate WHERE updateid = ?` - tx.Exec(query, updateId) + insertScreenCmdUpdate(tx, screenId, cmdId, UpdateType_PtyPos) return nil }) } @@ -2570,6 +2546,15 @@ func GetWebPtyPos(ctx context.Context, screenId string, lineId string) (int64, e }) } +func DeleteWebPtyPos(ctx context.Context, screenId string, lineId string) error { + fmt.Printf("del webptypos %s:%s\n", screenId, lineId) + return WithTx(ctx, func(tx *TxWrap) error { + query := `DELETE FROM webptypos WHERE screenid = ? AND lineid = ?` + tx.Exec(query, screenId, lineId) + return nil + }) +} + func SetWebPtyPos(ctx context.Context, screenId string, lineId string, ptyPos int64) error { return WithTx(ctx, func(tx *TxWrap) error { query := `SELECT screenid FROM webptypos WHERE screenid = ? AND lineid = ?` diff --git a/pkg/sstore/sstore.go b/pkg/sstore/sstore.go index 3357ec939..072dd8af5 100644 --- a/pkg/sstore/sstore.go +++ b/pkg/sstore/sstore.go @@ -86,7 +86,6 @@ const ( UpdateType_ScreenName = "screen:sharename" UpdateType_LineNew = "line:new" UpdateType_LineDel = "line:del" - UpdateType_LineArchived = "line:archived" UpdateType_LineRenderer = "line:renderer" UpdateType_CmdStatus = "cmd:status" UpdateType_CmdTermOpts = "cmd:termopts"