tavern/web/handler_server_inbox.go

67 lines
1.6 KiB
Go

package web
import (
"io"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"github.com/ngerakines/tavern/storage"
)
func (h handler) serverInbox(c *gin.Context) {
// TODO: verify signature
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 1*1024*1024))
if err != nil {
h.logger.Error("unable to slurp request body into payload", zap.Error(err))
c.AbortWithStatus(http.StatusInternalServerError)
return
}
payload, err := storage.PayloadFromBytes(body)
if err != nil {
h.logger.Error("unable to marshall request body into payload", zap.Error(err))
c.AbortWithStatus(http.StatusInternalServerError)
return
}
payloadID, err := storage.ValidateActivity(payload)
if err != nil {
h.hardFail(c, err)
return
}
activityID := storage.NewV4()
err = h.storage.RecordActivity(c.Request.Context(), activityID, payloadID, string(body))
if err != nil {
h.logger.Error("unable to record activity", zap.Error(err))
c.AbortWithStatus(http.StatusInternalServerError)
return
}
payloadType, _ := storage.JSONString(payload, "type")
switch payloadType {
case "Follow":
h.logger.Debug("User received ping payload")
h.serverInboxFollow(c, payload)
case "Undo":
h.logger.Debug("User received pong payload")
h.serverInboxUndo(c, payload)
default:
h.logger.Debug("Server received unexpected payload type", zap.String("type", payloadType))
c.Status(http.StatusOK)
}
}
func (h handler) serverInboxFollow(c *gin.Context, payload storage.Payload) {
c.Status(http.StatusOK)
}
func (h handler) serverInboxUndo(c *gin.Context, payload storage.Payload) {
c.Status(http.StatusOK)
}