integrated connecting contacts

This commit is contained in:
Roland Osborne 2022-04-05 13:52:52 -07:00
parent 2d98ac4807
commit 9d7220486f
13 changed files with 118 additions and 80 deletions

View File

@ -97,6 +97,10 @@ func AddCard(w http.ResponseWriter, r *http.Request) {
card.Node = identity.Node
card.ProfileRevision = identity.Revision
}
if card.Status == APP_CARDPENDING {
card.Status = APP_CARDCONFIRMED
}
card.DetailRevision = account.CardRevision + 1
// save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error {

View File

@ -10,7 +10,7 @@ import (
func GetOpenMessage(w http.ResponseWriter, r *http.Request) {
account, code, res := BearerAppToken(r, true);
account, code, res := ParamAgentToken(r, true);
if res != nil {
ErrResponse(w, code, res)
return

View File

@ -14,7 +14,7 @@ import (
func SetCardStatus(w http.ResponseWriter, r *http.Request) {
var res error
account, code, err := BearerAppToken(r, false);
account, code, err := ParamAgentToken(r, false);
if err != nil {
ErrResponse(w, code, err)
return

View File

@ -44,7 +44,7 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
// see if card exists
slot := &store.CardSlot{}
card := &store.Card{}
if err := store.DB.Where("account_id = ? AND guid = ?", account.Guid, guid).First(card).Error; err != nil {
if err := store.DB.Preload("CardSlot").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
@ -74,54 +74,28 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
card.InToken = hex.EncodeToString(data)
card.AccountID = account.Guid
// create new card or update existing
if err = store.DB.Preload("Card").Where("account_id = ? AND card_id = 0", account.ID).First(slot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
return
// create slot
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(card).Error; res != nil {
return res
}
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
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
}
} else {
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Save(card).Error; res != nil {
return res
}
slot.Revision = account.CardRevision + 1
slot.CardID = card.ID
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
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 {
// update profile if revision changed
@ -160,20 +134,8 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
if res := tx.Save(&card).Error; res != nil {
return res
}
if res := tx.Preload("Card").Where("account_id = ? AND card_id = ?", account.ID, card.ID).First(&slot).Error; res != nil {
if !errors.Is(res, gorm.ErrRecordNotFound) {
return 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
}
slot = &card.CardSlot
slot.Revision = account.CardRevision + 1
if res := tx.Preload("Card").Save(slot).Error; res != nil {
return res
}
@ -189,9 +151,9 @@ func SetOpenMessage(w http.ResponseWriter, r *http.Request) {
}
status := &ContactStatus{
Token: slot.Card.InToken,
Status: slot.Card.Status,
ViewRevision: slot.Card.ViewRevision,
Token: card.InToken,
Status: card.Status,
ViewRevision: card.ViewRevision,
ChannelRevision: account.ChannelRevision,
ProfileRevision: account.ProfileRevision,
ArticleRevision: account.ArticleRevision,

View File

@ -54,7 +54,7 @@ func TestMain(m *testing.M) {
}
// config server
config := NodeConfig{Domain: "example.com", AccountLimit: 1024, OpenAccess: true, AccountStorage: 4096}
config := NodeConfig{Domain: "databag.coredb.org", AccountLimit: 1024, OpenAccess: true, AccountStorage: 4096}
r, w, _ = NewRequest("PUT", "/admin/config", &config)
SetBasicAuth(r, "admin:pass")
SetNodeConfig(w, r)
@ -70,7 +70,7 @@ func TestMain(m *testing.M) {
if ReadResponse(w, &check) != nil {
panic("failed to get node config")
}
if check.Domain != "example.com" {
if check.Domain != "databag.coredb.org" {
panic("failed to set config domain");
}
if check.AccountLimit != 1024 {

View File

@ -521,22 +521,20 @@ func OpenTestCard(account string, cardId string) (err error) {
var contactStatus ContactStatus
// set to connecting state
if r, w, err = NewRequest("PUT", "/contact/cards/{cardId}/status", APP_CARDCONNECTING); err != nil {
if r, w, err = NewRequest("PUT", "/contact/cards/{cardId}/status?agent=" + account, APP_CARDCONNECTING); err != nil {
return
}
r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
SetCardStatus(w, r)
if err = ReadResponse(w, &card); err != nil {
return
}
// get open message
if r, w, err = NewRequest("GET", "/contact/cards/{cardId}/openMessage", nil); err != nil {
if r, w, err = NewRequest("GET", "/contact/cards/{cardId}/openMessage?agent=" + account, nil); err != nil {
return
}
r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
GetOpenMessage(w, r)
if err = ReadResponse(w, &msg); err != nil {
return
@ -557,11 +555,10 @@ func OpenTestCard(account string, cardId string) (err error) {
article := "articleRevision=" + strconv.FormatInt(contactStatus.ArticleRevision, 10)
channel := "channelRevision=" + strconv.FormatInt(contactStatus.ChannelRevision, 10)
profile := "profileRevision=" + strconv.FormatInt(contactStatus.ProfileRevision, 10)
if r, w, err = NewRequest("PUT", "/contact/cards/{cardId}/status?token=" + contactStatus.Token + "&" + view + "&" + article + "&" + channel + "&" + profile, APP_CARDCONNECTED); err != nil {
if r, w, err = NewRequest("PUT", "/contact/cards/{cardId}/status?agent=" + account + "&token=" + contactStatus.Token + "&" + view + "&" + article + "&" + channel + "&" + profile, APP_CARDCONNECTED); err != nil {
return
}
r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
SetCardStatus(w, r)
if err = ReadResponse(w, &card); err != nil {
return
@ -605,7 +602,7 @@ func AddTestAccount(username string) (guid string, token string, err error) {
app := AppData{
Name: "Appy",
Description: "A test app",
Url: "http://app.example.com",
Url: "http://app.coredb.org",
};
var claim Claim
var msg DataMessage

View File

@ -0,0 +1,8 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getCardOpenMessage(token, cardId) {
let message = await fetchWithTimeout(`/contact/cards/${cardId}/openMessage?agent=${token}`, { method: 'GET' });
checkResponse(message);
return await message.json();
}

View File

@ -0,0 +1,8 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function setCardOpenMessage(server, message) {
let status = await fetchWithTimeout(`https://${server}/contact/openMessage`, { method: 'PUT', body: JSON.stringify(message) });
checkResponse(status);
return await status.json();
}

View File

@ -0,0 +1,20 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function setCardConnecting(token, cardId) {
let card = await fetchWithTimeout(`/contact/cards/${cardId}/status?agent=${token}`, { method: 'PUT', body: JSON.stringify('connecting') } );
checkResponse(card);
return await card.json();
}
export async function setCardConnected(token, cardId, access, view, article, channel, profile) {
let card = await fetchWithTimeout(`/contact/cards/${cardId}/status?agent=${token}&token=${access}&viewRevision=${view}&articleRevision=${article}&channelRevision=${channel}&profileRevision=${profile}`, { method: 'PUT', body: JSON.stringify('connected') } );
checkResponse(card);
return await card.json();
}
export async function setCardConfirmed(token, cardId) {
let card = await fetchWithTimeout(`/contact/cards/${cardId}/status?agent=${token}`, { method: 'PUT', body: JSON.stringify('confirmed') } );
checkResponse(card);
return await card.json();
}

View File

@ -74,6 +74,13 @@ export function Contact() {
return <></>
}
const Confirm = () => {
if (state.showButtons.confirm) {
return <ProfileButton ghost onClick={() => actions.confirm()}>Save Contact</ProfileButton>
}
return <></>
}
const SaveRequest = () => {
if (state.showButtons.saveRequest) {
return <ProfileButton ghost>Save & Request</ProfileButton>
@ -83,7 +90,7 @@ export function Contact() {
const Connect = () => {
if (state.showButtons.connect) {
return <ProfileButton ghost>Connect</ProfileButton>
return <ProfileButton ghost onClick={() => actions.connect()}>Connect</ProfileButton>
}
return <></>
}
@ -110,6 +117,7 @@ export function Contact() {
<ContactSpin size="large" spinning={state.busy} />
<Disconnect />
<Remove />
<Confirm />
<Cancel />
<Ignore />
<Save />

View File

@ -64,6 +64,7 @@ export const ContactWrapper = styled.div`
display: flex;
flex-direction: row;
margin-right: 32px;
align-items: center;
}
.profile {

View File

@ -4,6 +4,9 @@ import { useNavigate, useLocation, useParams } from "react-router-dom";
import { getListingMessage } from '../../Api/getListingMessage';
import { addCard } from '../../Api/addCard';
import { removeCard } from '../../Api/removeCard';
import { setCardConnecting, setCardConnected, setCardConfirmed } from '../../Api/setCardStatus';
import { getCardOpenMessage } from '../../Api/getCardOpenMessage';
import { setCardOpenMessage } from '../../Api/setCardOpenMessage';
export function useContact() {
@ -46,7 +49,34 @@ export function useContact() {
updateState({ busy: false });
}
},
connect: () => {
confirm: async () => {
if (!state.busy) {
updateState({ busy: true });
try {
await setCardConfirmed(app.state.token, state.cardId);
}
catch (err) {
window.alert(err);
}
updateState({ busy: false });
}
},
connect: async () => {
if (!state.busy) {
updateState({ busy: true });
try {
await setCardConnecting(app.state.token, state.cardId);
let message = await getCardOpenMessage(app.state.token, state.cardId);
let contact = await setCardOpenMessage(state.node, message);
if (contact.status === 'connected') {
await setCardConnected(app.state.token, state.cardId, contact.token, contact.viewRevision, contact.articleRevision, contact.channelRevision, contact.profileRevision);
}
}
catch (err) {
window.alert(err);
}
updateState({ busy: false });
}
},
disconnect: () => {
},
@ -100,11 +130,11 @@ export function useContact() {
updateState({ showButtons: { cancel: true, remove: true }});
}
if (status === 'pending') {
updateState({ status: 'requested' });
updateState({ showButtons: { ignore: true, save: true, saveAccept: true }});
updateState({ status: 'pending' });
updateState({ showButtons: { ignore: true, confirm: true, saveAccept: true }});
}
if (status === 'confirmed') {
updateState({ status: 'disconnected' });
updateState({ status: 'confirmed' });
updateState({ showButtons: { remove: true, connect: true }});
}
if (status === 'requested') {

View File

@ -1,10 +1,10 @@
import styled from 'styled-components';
export const SideBarWrapper = styled.div`
width: 30%;
width: 20%;
height: 100%;
max-width: 300px;
min-width: 200px;
min-width: 260px;
border-right: 1px solid #8fbea7;
background-color: #8fbea7;
`;