databag/net/server/internal/api_addCard.go

145 lines
3.6 KiB
Go
Raw Normal View History

2022-01-20 23:19:26 +00:00
package databag
import (
2022-08-13 14:01:13 +00:00
"time"
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"encoding/hex"
"errors"
"github.com/google/uuid"
"github.com/theckman/go-securerandom"
"gorm.io/gorm"
"net/http"
2022-01-20 23:19:26 +00:00
)
2022-07-25 18:44:33 +00:00
//AddCard adds contact to account specified by agent query param
2022-01-20 23:19:26 +00:00
func AddCard(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
account, code, err := ParamAgentToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
2022-01-20 23:19:26 +00:00
2022-07-22 19:28:14 +00:00
var message DataMessage
if err := ParseRequest(r, w, &message); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
2022-01-20 23:19:26 +00:00
2022-07-22 19:28:14 +00:00
var identity Identity
guid, messageType, _, err := ReadDataMessage(&message, &identity)
if messageType != APPMsgIdentity || err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
2022-01-20 23:19:26 +00:00
2022-07-22 19:28:14 +00:00
slot := &store.CardSlot{}
var card store.Card
if err := store.DB.Preload("CardSlot").Preload("Groups").Where("account_id = ? AND guid = ?", account.GUID, guid).First(&card).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-21 07:41:08 +00:00
2022-07-22 19:28:14 +00:00
// create new card data
data, res := securerandom.Bytes(APPTokenSize)
if res != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
card := &store.Card{
GUID: guid,
Username: identity.Handle,
Name: identity.Name,
Description: identity.Description,
Location: identity.Location,
Image: identity.Image,
Version: identity.Version,
Node: identity.Node,
Seal: identity.Seal,
2022-07-22 19:28:14 +00:00
ProfileRevision: identity.Revision,
Status: APPCardConfirmed,
2022-08-13 14:01:13 +00:00
StatusUpdated: time.Now().Unix(),
2022-07-22 19:28:14 +00:00
ViewRevision: 0,
InToken: hex.EncodeToString(data),
AccountID: account.GUID,
}
2022-01-21 07:41:08 +00:00
2022-07-22 19:28:14 +00:00
// save new card
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(card).Error; res != nil {
return res
}
slot.CardSlotID = uuid.New().String()
slot.AccountID = account.ID
slot.Revision = account.CardRevision + 1
slot.CardID = card.ID
slot.Card = card
if res := tx.Save(slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("card_revision", account.CardRevision+1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
} else {
2022-02-09 07:54:09 +00:00
2022-07-22 19:28:14 +00:00
if identity.Revision > card.ProfileRevision {
2022-01-20 23:19:26 +00:00
2022-07-22 19:28:14 +00:00
// update card data
card.Username = identity.Handle
card.Name = identity.Name
card.Description = identity.Description
card.Location = identity.Location
card.Image = identity.Image
card.Version = identity.Version
card.Node = identity.Node
card.ProfileRevision = identity.Revision
}
if card.Status == APPCardPending {
card.Status = APPCardConfirmed
}
card.DetailRevision = account.CardRevision + 1
2022-01-21 05:48:24 +00:00
2022-07-22 19:28:14 +00:00
// save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(&card).Error; res != nil {
return res
}
slot = &card.CardSlot
if slot == nil {
slot = &store.CardSlot{
CardSlotID: uuid.New().String(),
AccountID: account.ID,
Revision: account.CardRevision + 1,
CardID: card.ID,
Card: &card,
}
} else {
slot.Revision = account.CardRevision + 1
}
if res := tx.Preload("Card").Save(slot).Error; res != nil {
return res
}
if res := tx.Model(&account).Update("card_revision", account.CardRevision+1).Error; res != nil {
return res
}
slot.Card = &card
return nil
})
2022-02-03 06:13:46 +00:00
2022-07-22 21:42:58 +00:00
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-07-22 19:28:14 +00:00
}
2022-01-20 23:19:26 +00:00
2022-07-22 19:28:14 +00:00
SetStatus(account)
WriteResponse(w, getCardModel(slot))
2022-01-20 23:19:26 +00:00
}