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.Node = identity.Node
card.ProfileRevision = identity.Revision card.ProfileRevision = identity.Revision
} }
if card.Status == APP_CARDPENDING {
card.Status = APP_CARDCONFIRMED
}
card.DetailRevision = account.CardRevision + 1
// save contact card // save contact card
err = store.DB.Transaction(func(tx *gorm.DB) error { err = store.DB.Transaction(func(tx *gorm.DB) error {

View File

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

View File

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

View File

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

View File

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

View File

@ -521,22 +521,20 @@ func OpenTestCard(account string, cardId string) (err error) {
var contactStatus ContactStatus var contactStatus ContactStatus
// set to connecting state // 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 return
} }
r = mux.SetURLVars(r, vars) r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
SetCardStatus(w, r) SetCardStatus(w, r)
if err = ReadResponse(w, &card); err != nil { if err = ReadResponse(w, &card); err != nil {
return return
} }
// get open message // 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 return
} }
r = mux.SetURLVars(r, vars) r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
GetOpenMessage(w, r) GetOpenMessage(w, r)
if err = ReadResponse(w, &msg); err != nil { if err = ReadResponse(w, &msg); err != nil {
return return
@ -557,11 +555,10 @@ func OpenTestCard(account string, cardId string) (err error) {
article := "articleRevision=" + strconv.FormatInt(contactStatus.ArticleRevision, 10) article := "articleRevision=" + strconv.FormatInt(contactStatus.ArticleRevision, 10)
channel := "channelRevision=" + strconv.FormatInt(contactStatus.ChannelRevision, 10) channel := "channelRevision=" + strconv.FormatInt(contactStatus.ChannelRevision, 10)
profile := "profileRevision=" + strconv.FormatInt(contactStatus.ProfileRevision, 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 return
} }
r = mux.SetURLVars(r, vars) r = mux.SetURLVars(r, vars)
SetBearerAuth(r, account)
SetCardStatus(w, r) SetCardStatus(w, r)
if err = ReadResponse(w, &card); err != nil { if err = ReadResponse(w, &card); err != nil {
return return
@ -605,7 +602,7 @@ func AddTestAccount(username string) (guid string, token string, err error) {
app := AppData{ app := AppData{
Name: "Appy", Name: "Appy",
Description: "A test app", Description: "A test app",
Url: "http://app.example.com", Url: "http://app.coredb.org",
}; };
var claim Claim var claim Claim
var msg DataMessage 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 <></> return <></>
} }
const Confirm = () => {
if (state.showButtons.confirm) {
return <ProfileButton ghost onClick={() => actions.confirm()}>Save Contact</ProfileButton>
}
return <></>
}
const SaveRequest = () => { const SaveRequest = () => {
if (state.showButtons.saveRequest) { if (state.showButtons.saveRequest) {
return <ProfileButton ghost>Save & Request</ProfileButton> return <ProfileButton ghost>Save & Request</ProfileButton>
@ -83,7 +90,7 @@ export function Contact() {
const Connect = () => { const Connect = () => {
if (state.showButtons.connect) { if (state.showButtons.connect) {
return <ProfileButton ghost>Connect</ProfileButton> return <ProfileButton ghost onClick={() => actions.connect()}>Connect</ProfileButton>
} }
return <></> return <></>
} }
@ -110,6 +117,7 @@ export function Contact() {
<ContactSpin size="large" spinning={state.busy} /> <ContactSpin size="large" spinning={state.busy} />
<Disconnect /> <Disconnect />
<Remove /> <Remove />
<Confirm />
<Cancel /> <Cancel />
<Ignore /> <Ignore />
<Save /> <Save />

View File

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

View File

@ -4,6 +4,9 @@ import { useNavigate, useLocation, useParams } from "react-router-dom";
import { getListingMessage } from '../../Api/getListingMessage'; import { getListingMessage } from '../../Api/getListingMessage';
import { addCard } from '../../Api/addCard'; import { addCard } from '../../Api/addCard';
import { removeCard } from '../../Api/removeCard'; 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() { export function useContact() {
@ -46,7 +49,34 @@ export function useContact() {
updateState({ busy: false }); 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: () => { disconnect: () => {
}, },
@ -100,11 +130,11 @@ export function useContact() {
updateState({ showButtons: { cancel: true, remove: true }}); updateState({ showButtons: { cancel: true, remove: true }});
} }
if (status === 'pending') { if (status === 'pending') {
updateState({ status: 'requested' }); updateState({ status: 'pending' });
updateState({ showButtons: { ignore: true, save: true, saveAccept: true }}); updateState({ showButtons: { ignore: true, confirm: true, saveAccept: true }});
} }
if (status === 'confirmed') { if (status === 'confirmed') {
updateState({ status: 'disconnected' }); updateState({ status: 'confirmed' });
updateState({ showButtons: { remove: true, connect: true }}); updateState({ showButtons: { remove: true, connect: true }});
} }
if (status === 'requested') { if (status === 'requested') {

View File

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