databag/net/server/internal/api_getOpenMessage.go

67 lines
1.7 KiB
Go
Raw Normal View History

2022-01-21 01:01:02 +00:00
package databag
import (
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
2022-04-05 20:52:52 +00:00
account, code, res := ParamAgentToken(r, true);
2022-01-21 01:01:02 +00:00
if res != nil {
ErrResponse(w, code, res)
return
}
detail := account.AccountDetail
cardID := mux.Vars(r)["cardID"]
2022-01-21 01:01:02 +00:00
2022-02-03 06:13:46 +00:00
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 {
2022-01-21 01:01:02 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
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 01:01:02 +00:00
2022-07-22 17:52:13 +00:00
if slot.Card.Status != APPCardConnecting && slot.Card.Status != APPCardConnected {
2022-01-21 01:01:02 +00:00
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
return
}
connect := &Connect{
Contact: slot.Card.GUID,
2022-02-03 06:13:46 +00:00
Token: slot.Card.InToken,
2022-02-08 20:24:42 +00:00
ArticleRevision: account.ArticleRevision,
2022-01-21 01:01:02 +00:00
ProfileRevision: account.ProfileRevision,
2022-02-06 04:41:52 +00:00
ViewRevision: slot.Card.ViewRevision,
2022-02-08 20:24:42 +00:00
ChannelRevision: account.ChannelRevision,
2022-01-21 01:01:02 +00:00
Handle: account.Username,
Name: detail.Name,
Description: detail.Description,
Location: detail.Location,
Image: detail.Image,
2022-07-22 17:52:13 +00:00
Version: APPVersion,
2022-07-22 18:01:29 +00:00
Node: getStrConfigValue(CNFDomain, ""),
2022-01-21 01:01:02 +00:00
}
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
2022-07-22 17:52:13 +00:00
APPSignPKCS1V15, account.GUID, APPMsgConnect, &connect)
2022-01-21 01:01:02 +00:00
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
WriteResponse(w, msg)
}