mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package databag
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"gorm.io/gorm"
|
|
"github.com/gorilla/mux"
|
|
"databag/internal/store"
|
|
)
|
|
|
|
func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
account, code, res := ParamAgentToken(r, true);
|
|
if res != nil {
|
|
ErrResponse(w, code, res)
|
|
return
|
|
}
|
|
detail := account.AccountDetail
|
|
cardID := mux.Vars(r)["cardID"]
|
|
|
|
var slot store.CardSlot
|
|
if err := store.DB.Preload("Card").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&slot).Error; err != nil {
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
} else {
|
|
ErrResponse(w, http.StatusNotFound, err)
|
|
}
|
|
return
|
|
}
|
|
if slot.Card == nil {
|
|
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
|
|
return
|
|
}
|
|
|
|
if slot.Card.Status != APPCardConnecting && slot.Card.Status != APPCardConnected {
|
|
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
|
|
return
|
|
}
|
|
|
|
connect := &Connect{
|
|
Contact: slot.Card.GUID,
|
|
Token: slot.Card.InToken,
|
|
ArticleRevision: account.ArticleRevision,
|
|
ProfileRevision: account.ProfileRevision,
|
|
ViewRevision: slot.Card.ViewRevision,
|
|
ChannelRevision: account.ChannelRevision,
|
|
Handle: account.Username,
|
|
Name: detail.Name,
|
|
Description: detail.Description,
|
|
Location: detail.Location,
|
|
Image: detail.Image,
|
|
Version: APPVersion,
|
|
Node: getStrConfigValue(CNFDomain, ""),
|
|
}
|
|
|
|
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
|
|
APPSignPKCS1V15, account.GUID, APPMsgConnect, &connect)
|
|
if err != nil {
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
WriteResponse(w, msg)
|
|
}
|
|
|
|
|