simplify updates, remove line:archived (now just line:del)

This commit is contained in:
sawka 2023-03-30 12:59:58 -07:00
parent 83ce367ace
commit 652c820844
4 changed files with 49 additions and 73 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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 = ?`

View File

@ -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"