mirror of
https://github.com/balzack/databag.git
synced 2025-02-11 19:19:16 +00:00
adding open message
This commit is contained in:
parent
787e46ecc9
commit
69d9736152
@ -1111,6 +1111,8 @@ paths:
|
||||
description: permission denied
|
||||
'404':
|
||||
description: card not found
|
||||
'405':
|
||||
description: invalid card state
|
||||
'410':
|
||||
description: account disabled
|
||||
'500':
|
||||
|
@ -74,6 +74,9 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO UPDATE CONTACT REVISION
|
||||
// TODO SET STATUS
|
||||
|
||||
WriteResponse(w, getCardModel(&card))
|
||||
}
|
||||
|
||||
|
@ -53,11 +53,6 @@ func GetCloseMessage(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func RemoveCard(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
60
net/server/internal/api_getOpenMessage.go
Normal file
60
net/server/internal/api_getOpenMessage.go
Normal file
@ -0,0 +1,60 @@
|
||||
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 := BearerAppToken(r, true);
|
||||
if res != nil {
|
||||
ErrResponse(w, code, res)
|
||||
return
|
||||
}
|
||||
detail := account.AccountDetail
|
||||
cardId := mux.Vars(r)["cardId"]
|
||||
|
||||
var card store.Card
|
||||
if err := store.DB.Where("account_id = ? AND card_id = ?", account.ID, cardId).First(&card).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if card.Status != APP_CARDCONNECTING && card.Status != APP_CARDCONNECTED {
|
||||
ErrResponse(w, http.StatusMethodNotAllowed, errors.New("invalid card state"))
|
||||
return
|
||||
}
|
||||
|
||||
connect := &Connect{
|
||||
Contact: card.Guid,
|
||||
Token: card.InToken,
|
||||
ContentRevision: account.ViewRevision,
|
||||
ProfileRevision: account.ProfileRevision,
|
||||
Handle: account.Username,
|
||||
Name: detail.Name,
|
||||
Description: detail.Description,
|
||||
Location: detail.Location,
|
||||
Image: detail.Image,
|
||||
Version: APP_VERSION,
|
||||
Node: "https://" + getStrConfigValue(CONFIG_DOMAIN, "") + "/",
|
||||
}
|
||||
|
||||
msg, err := WriteDataMessage(detail.PrivateKey, detail.PublicKey, detail.KeyType,
|
||||
APP_SIGNPKCS1V15, account.Guid, APP_MSGCONNECT, &connect)
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(w, msg)
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,11 @@ package databag
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"encoding/hex"
|
||||
"gorm.io/gorm"
|
||||
"github.com/gorilla/mux"
|
||||
"databag/internal/store"
|
||||
"github.com/theckman/go-securerandom"
|
||||
)
|
||||
|
||||
func SetCardStatus(w http.ResponseWriter, r *http.Request) {
|
||||
@ -38,18 +40,31 @@ func SetCardStatus(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// update card
|
||||
card.Status = status
|
||||
if token != "" {
|
||||
card.Token = token
|
||||
card.OutToken = token
|
||||
}
|
||||
if status == APP_CARDCONNECTING {
|
||||
data, err := securerandom.Bytes(32)
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
card.InToken = hex.EncodeToString(data)
|
||||
}
|
||||
|
||||
if err := store.DB.Save(&card).Error; err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO UPDATE CARD REVISION, CONTACT REVISION
|
||||
// TODO SET STATUS
|
||||
|
||||
WriteResponse(w, getCardModel(&card));
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,8 @@ type Card struct {
|
||||
Node string `gorm:"not null"`
|
||||
ProfileRevision int64 `gorm:"not null"`
|
||||
Status string `gorm:"not null"`
|
||||
Token string
|
||||
InToken string
|
||||
OutToken string
|
||||
Notes string
|
||||
DataRevision int64 `gorm:"not null"`
|
||||
Created int64 `gorm:"autoCreateTime"`
|
||||
|
@ -8,32 +8,41 @@ import (
|
||||
|
||||
func TestConnectContact(t *testing.T) {
|
||||
var card Card
|
||||
var msg DataMessage
|
||||
var vars map[string]string
|
||||
|
||||
// create some contacts for this test
|
||||
access := AddTestContacts(t, "connect", 2)
|
||||
|
||||
// get B identity message
|
||||
// get A identity message
|
||||
r, w, _ := NewRequest("GET", "/profile/message", nil)
|
||||
SetBearerAuth(r, access[0])
|
||||
GetProfileMessage(w, r)
|
||||
var msg DataMessage
|
||||
assert.NoError(t, ReadResponse(w, &msg))
|
||||
|
||||
// add B card in A
|
||||
// add A card in B
|
||||
r, w, _ = NewRequest("POST", "/contact/cards", &msg)
|
||||
SetBearerAuth(r, access[1])
|
||||
AddCard(w, r)
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
// update status to connecting
|
||||
// update A status to connecting
|
||||
r, w, _ = NewRequest("PUT", "/contact/cards/{cardId}/status", APP_CARDCONNECTING)
|
||||
vars := map[string]string{ "cardId": card.CardId }
|
||||
vars = map[string]string{ "cardId": card.CardId }
|
||||
r = mux.SetURLVars(r, vars)
|
||||
SetBearerAuth(r, access[1])
|
||||
SetCardStatus(w, r)
|
||||
assert.NoError(t, ReadResponse(w, &card))
|
||||
|
||||
PrintMsg(card)
|
||||
// get open message to A
|
||||
r, w, _ = NewRequest("GET", "/contact/cards/{cardId}/openMessage", nil)
|
||||
vars = map[string]string{ "cardId": card.CardId }
|
||||
r = mux.SetURLVars(r, vars)
|
||||
SetBearerAuth(r, access[1])
|
||||
GetOpenMessage(w, r)
|
||||
assert.NoError(t, ReadResponse(w, &msg))
|
||||
|
||||
PrintMsg(msg)
|
||||
|
||||
// A request B
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user