2022-01-21 00:26:59 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-02-04 08:20:57 +00:00
|
|
|
"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) {
|
2022-02-04 08:20:57 +00:00
|
|
|
var res error
|
2022-01-21 00:26:59 +00:00
|
|
|
|
2022-04-05 20:52:52 +00:00
|
|
|
account, code, err := ParamAgentToken(r, false);
|
2022-01-21 00:26:59 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan parameters
|
|
|
|
params := mux.Vars(r)
|
2022-07-22 17:15:44 +00:00
|
|
|
cardID := params["cardID"]
|
2022-01-21 00:26:59 +00:00
|
|
|
token := r.FormValue("token")
|
2022-01-29 06:21:54 +00:00
|
|
|
|
2022-02-04 08:20:57 +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 {
|
2022-02-04 08:20:57 +00:00
|
|
|
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 {
|
2022-02-04 08:20:57 +00:00
|
|
|
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
|
|
|
|
}
|
2022-07-22 17:52:13 +00:00
|
|
|
if status == APPCardConnected && token == "" {
|
2022-01-21 18:34:08 +00:00
|
|
|
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
|
2022-07-22 17:15:44 +00:00
|
|
|
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
|
|
|
}
|
2022-07-22 17:52:13 +00:00
|
|
|
if status == APPCardConnecting {
|
|
|
|
if slot.Card.Status != APPCardConnecting && slot.Card.Status != APPCardConnected {
|
|
|
|
data, err := securerandom.Bytes(APPTokenSize)
|
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
|
2022-02-04 08:20:57 +00:00
|
|
|
slot.Card.NotifiedView = viewRevision
|
2022-02-08 20:24:42 +00:00
|
|
|
slot.Card.NotifiedArticle = articleRevision
|
|
|
|
slot.Card.NotifiedChannel = channelRevision
|
2022-02-04 08:20:57 +00:00
|
|
|
slot.Card.NotifiedProfile = profileRevision
|
2022-07-22 18:01:29 +00:00
|
|
|
slot.Card.DetailRevision++
|
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
|
|
|
|
}
|
2022-02-02 21:44:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|