databag/net/server/internal/api_getOpenMessage.go

67 lines
1.8 KiB
Go
Raw Normal View History

2022-01-21 01:01:02 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
2022-01-21 01:01:02 +00:00
)
2022-07-29 21:39:39 +00:00
//GetOpenMessage retrieve message to deliver to contact for connection
2022-01-21 01:01:02 +00:00
func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
account, code, res := ParamAgentToken(r, true)
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-07-22 19:28:14 +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 {
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
}
2022-01-21 01:01:02 +00:00
2022-07-22 19:28:14 +00:00
if slot.Card.Status != APPCardConnecting && slot.Card.Status != APPCardConnected {
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
return
}
2022-01-21 01:01:02 +00:00
2022-07-22 19:28:14 +00:00
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,
Seal: detail.SealPublic,
2022-07-22 19:28:14 +00:00
Location: detail.Location,
Image: detail.Image,
Version: APPVersion,
Node: getStrConfigValue(CNFDomain, ""),
}
2022-01-21 01:01:02 +00:00
2022-07-22 19:28:14 +00:00
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
APPSignPKCS1V15, account.GUID, APPMsgConnect, &connect)
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-01-21 01:01:02 +00:00
2022-07-22 19:28:14 +00:00
WriteResponse(w, msg)
2022-01-21 01:01:02 +00:00
}