Skip to content

Commit

Permalink
add notification in update root
Browse files Browse the repository at this point in the history
  • Loading branch information
ddvk committed Sep 24, 2024
1 parent d236973 commit 4af294b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
35 changes: 24 additions & 11 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ func (app *App) blobStorageUpload(c *gin.Context) {
}

func (app *App) syncUpdateRootV3(c *gin.Context) {
var rootv3 messages.SyncRootV3
err := json.NewDecoder(c.Request.Body).Decode(&rootv3)
var rootv3 messages.SyncRootV3Request
err := c.BindJSON(&rootv3)
if err != nil {
log.Error(err)
c.AbortWithStatus(http.StatusBadRequest)
Expand All @@ -750,12 +750,23 @@ func (app *App) syncUpdateRootV3(c *gin.Context) {
return
}

c.JSON(http.StatusOK, messages.SyncRootV3{
Generation: newgeneration,
Hash: rootv3.Hash,
if rootv3.Broadcast {
deviceID := c.GetString(deviceIDKey)

log.Info("got sync completed, gen: ", newgeneration)

app.hub.NotifySync(uid, deviceID)
}

c.JSON(http.StatusOK, messages.SyncRootV3Response{
Generation: newgeneration,
Hash: rootv3.Hash,
SchemaVersion: SchemaVersion,
})
}

const SchemaVersion = 3

func (app *App) syncGetRootV3(c *gin.Context) {
uid := c.GetString(userIDKey)

Expand All @@ -777,9 +788,10 @@ func (app *App) syncGetRootV3(c *gin.Context) {
return
}

c.JSON(http.StatusOK, messages.SyncRootV3{
Generation: generation,
Hash: string(roothash),
c.JSON(http.StatusOK, messages.SyncRootV3Response{
Generation: generation,
Hash: string(roothash),
SchemaVersion: SchemaVersion,
})
}

Expand Down Expand Up @@ -838,9 +850,10 @@ func (app *App) blobStorageWrite(c *gin.Context) {
return
}

c.JSON(http.StatusOK, messages.SyncRootV3{
Generation: newgeneration,
Hash: string(blobID),
c.JSON(http.StatusOK, messages.SyncRootV3Response{
Generation: newgeneration,
Hash: string(blobID),
SchemaVersion: SchemaVersion,
})
}

Expand Down
24 changes: 12 additions & 12 deletions internal/app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ func (app *App) registerRoutes(router *gin.Engine) {
"webapp": endpoint,
})
})
router.GET("/discovery/v1/webapp", func(c *gin.Context) {
endpoint, err := app.MyEndpoint()
if err != nil {
log.Warn("endpoint error:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"Status": "OK",
"Host": endpoint,
})
})
router.GET("/discovery/v1/webapp", func(c *gin.Context) {
endpoint, err := app.MyEndpoint()
if err != nil {
log.Warn("endpoint error:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"Status": "OK",
"Host": endpoint,
})
})

router.GET("/health", func(c *gin.Context) {
count := app.hub.ClientCount()
Expand Down
12 changes: 10 additions & 2 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ type SyncCompletedRequestV2 struct {
Generation int64 `json:"generation"`
}

// SyncRootV3
type SyncRootV3 struct {
// SyncRootV3Request
type SyncRootV3Request struct {
Generation int64 `json:"generation"`
Hash string `json:"hash,omitempty"`
Broadcast bool `json:"broadcast"`
}

// SyncRootV3Response
type SyncRootV3Response struct {
Generation int64 `json:"generation"`
Hash string `json:"hash,omitempty"`
SchemaVersion int64 `json:"schemaVersion"`
}

type CheckFiles struct {
Expand Down

0 comments on commit 4af294b

Please sign in to comment.