databag/net/server/internal/api_setCardStatus.go

135 lines
3.5 KiB
Go
Raw Normal View History

2022-01-21 00:26:59 +00:00
package databag
import (
"errors"
"strconv"
2022-01-21 00:26:59 +00:00
"net/http"
2022-01-21 01:01:02 +00:00
"encoding/hex"
2022-01-21 00:26:59 +00:00
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
2022-01-21 01:01:02 +00:00
"github.com/theckman/go-securerandom"
2022-01-21 00:26:59 +00:00
)
func SetCardStatus(w http.ResponseWriter, r *http.Request) {
var res error
2022-01-21 00:26:59 +00:00
account, code, err := BearerAppToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return
}
// scan parameters
params := mux.Vars(r)
cardId := params["cardId"]
token := r.FormValue("token")
2022-01-29 06:21:54 +00:00
// scan revisions
var viewRevision int64
view := r.FormValue("viewRevision")
if view != "" {
if viewRevision, res = strconv.ParseInt(view, 10, 64); res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
}
2022-02-08 20:24:42 +00:00
var articleRevision int64
article := r.FormValue("articleRevision")
if article != "" {
if articleRevision, res = strconv.ParseInt(article, 10, 64); res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
}
2022-02-08 20:24:42 +00:00
var channelRevision int64
channel := r.FormValue("channelRevision")
if channel != "" {
if channelRevision, res = strconv.ParseInt(channel, 10, 64); res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
}
var profileRevision int64
profile := r.FormValue("profileRevision")
if profile != "" {
if profileRevision, res = strconv.ParseInt(profile, 10, 64); res != nil {
ErrResponse(w, http.StatusBadRequest, res)
return
}
}
2022-01-21 00:26:59 +00:00
var status string
if err := ParseRequest(r, w, &status); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
if !AppCardStatus(status) {
2022-01-21 18:34:08 +00:00
ErrResponse(w, http.StatusBadRequest, errors.New("unknown status"))
return
}
if status == APP_CARDCONNECTED && token == "" {
ErrResponse(w, http.StatusBadRequest, errors.New("connected token not set"))
2022-01-21 00:26:59 +00:00
return
}
// load referenced card
2022-02-03 06:13:46 +00:00
var slot store.CardSlot
if err := store.DB.Preload("Card.Groups").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
2022-01-21 00:26:59 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
2022-01-21 01:01:02 +00:00
return
2022-01-21 00:26:59 +00:00
}
2022-02-03 06:13:46 +00:00
if slot.Card == nil {
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
return
}
2022-01-21 00:26:59 +00:00
// update card
2022-02-03 06:13:46 +00:00
slot.Revision = account.CardRevision + 1
2022-01-21 00:26:59 +00:00
if token != "" {
2022-02-03 06:13:46 +00:00
slot.Card.OutToken = token
2022-01-21 01:01:02 +00:00
}
if status == APP_CARDCONNECTING {
2022-02-03 06:13:46 +00:00
if slot.Card.Status != APP_CARDCONNECTING && slot.Card.Status != APP_CARDCONNECTED {
2022-01-22 19:40:20 +00:00
data, err := securerandom.Bytes(APP_TOKENSIZE)
2022-01-22 07:00:47 +00:00
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-02-03 06:13:46 +00:00
slot.Card.InToken = hex.EncodeToString(data)
2022-01-21 01:01:02 +00:00
}
2022-01-21 00:26:59 +00:00
}
2022-02-03 06:13:46 +00:00
slot.Card.Status = status
slot.Card.NotifiedView = viewRevision
2022-02-08 20:24:42 +00:00
slot.Card.NotifiedArticle = articleRevision
slot.Card.NotifiedChannel = channelRevision
slot.Card.NotifiedProfile = profileRevision
2022-02-24 07:18:23 +00:00
slot.Card.DetailRevision += 1
2022-01-21 01:01:02 +00:00
2022-01-21 05:48:24 +00:00
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-03 06:13:46 +00:00
if res := tx.Save(&slot.Card).Error; res != nil {
return res
}
if res := tx.Preload("Card").Save(&slot).Error; res != nil {
2022-01-21 05:48:24 +00:00
return res
}
if res := tx.Model(&account).Update("card_revision", account.CardRevision + 1).Error; res != nil {
2022-01-21 05:48:24 +00:00
return res
}
return nil
})
if err != nil {
2022-01-21 00:26:59 +00:00
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-21 05:48:24 +00:00
SetStatus(account)
2022-02-03 06:13:46 +00:00
WriteResponse(w, getCardModel(&slot));
2022-01-21 00:26:59 +00:00
}