From 00d0f6b8cd8c3151db32c4e2062cc539319829ed Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 4 Apr 2022 14:56:57 -0700 Subject: [PATCH] syncing profile --- doc/api.oa3 | 2 ++ net/server/internal/api_getCards.go | 2 -- net/server/internal/api_setCardProfile.go | 2 +- net/server/internal/store/schema.go | 1 + net/web/src/AppContext/fetchUtil.js | 15 +++++++++++++-- net/web/src/AppContext/useAppContext.hook.js | 12 +++++++++--- net/web/src/User/Contact/Contact.styled.js | 12 ++++++++++-- net/web/src/User/Profile/useProfile.hook.js | 9 ++++++--- 8 files changed, 42 insertions(+), 13 deletions(-) diff --git a/doc/api.oa3 b/doc/api.oa3 index 808e5dc2..e2e431cb 100644 --- a/doc/api.oa3 +++ b/doc/api.oa3 @@ -3343,6 +3343,8 @@ components: type: string node: type: string + revision: + type: int64 CardDetail: type: object diff --git a/net/server/internal/api_getCards.go b/net/server/internal/api_getCards.go index dd065d46..f9fff2c6 100644 --- a/net/server/internal/api_getCards.go +++ b/net/server/internal/api_getCards.go @@ -46,8 +46,6 @@ func GetCards(w http.ResponseWriter, r *http.Request) { } } -PrintMsg(response); - w.Header().Set("Card-Revision", strconv.FormatInt(account.CardRevision, 10)) WriteResponse(w, response) } diff --git a/net/server/internal/api_setCardProfile.go b/net/server/internal/api_setCardProfile.go index e2a21fa3..6c2da9e4 100644 --- a/net/server/internal/api_setCardProfile.go +++ b/net/server/internal/api_setCardProfile.go @@ -10,7 +10,7 @@ import ( func SetCardProfile(w http.ResponseWriter, r *http.Request) { - account, code, err := BearerAppToken(r, false); + account, code, err := ParamAgentToken(r, false); if err != nil { ErrResponse(w, code, err) return diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index a60b3195..c1d17719 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -148,6 +148,7 @@ type Card struct { Location string Image string Version string `gorm:"not null"` + Revision string `gorm:"not null"` Node string `gorm:"not null"` ProfileRevision int64 `gorm:"not null"` DetailRevision int64 `gorm:"not null;default:1"` diff --git a/net/web/src/AppContext/fetchUtil.js b/net/web/src/AppContext/fetchUtil.js index 2128baae..b341266a 100644 --- a/net/web/src/AppContext/fetchUtil.js +++ b/net/web/src/AppContext/fetchUtil.js @@ -113,8 +113,13 @@ export async function getCards(token, revision) { } export async function getCardProfile(token, cardId) { - let param = "?agent=" + token - let profile = await fetchWithTimeout(`/contact/cards/${cardId}/profile${param}`, { method: 'GET', timeout: FETCH_TIMEOUT }); + let profile = await fetchWithTimeout(`/contact/cards/${cardId}/profile?agent=${token}`, { method: 'GET', timeout: FETCH_TIMEOUT }); + checkResponse(profile); + return await profile.json() +} + +export async function setCardProfile(token, cardId, message) { + let profile = await fetchWithTimeout(`/contact/cards/${cardId}/profile?agent=${token}`, { method: 'PUT', body: JSON.stringify(message), timeout: FETCH_TIMEOUT }); checkResponse(profile); return await profile.json() } @@ -126,3 +131,9 @@ export async function getCardDetail(token, cardId) { return await detail.json() } +export async function getContactProfile(server, guid, token) { + let profile = await fetchWithTimeout(`https://${server}/profile/message?contact=${guid}.${token}`, { method: 'GET', timeout: FETCH_TIMEOUT }); + checkResponse(profile); + return await profile.json() +} + diff --git a/net/web/src/AppContext/useAppContext.hook.js b/net/web/src/AppContext/useAppContext.hook.js index 76b96e22..af372a50 100644 --- a/net/web/src/AppContext/useAppContext.hook.js +++ b/net/web/src/AppContext/useAppContext.hook.js @@ -1,5 +1,5 @@ import { useEffect, useState, useRef } from 'react'; -import { getCards, getCardImageUrl, getCardProfile, getCardDetail, getListingImageUrl, getListing, setProfileImage, setProfileData, getProfileImageUrl, getAccountStatus, setAccountSearchable, getProfile, getGroups, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; +import { getContactProfile, setCardProfile, getCards, getCardImageUrl, getCardProfile, getCardDetail, getListingImageUrl, getListing, setProfileImage, setProfileData, getProfileImageUrl, getAccountStatus, setAccountSearchable, getProfile, getGroups, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; async function updateAccount(token, updateData) { let status = await getAccountStatus(token); @@ -25,6 +25,7 @@ async function updateGroups(token, revision, groupMap, updateData) { } async function updateCards(token, revision, cardMap, updateData) { + let cards = await getCards(token, revision); for (let card of cards) { if (card.data) { @@ -42,7 +43,7 @@ async function updateCards(token, revision, cardMap, updateData) { cur.data.detailRevision = card.data.detailRevision; } if (cur.data.profileRevision != card.data.profileRevision) { - if (cur.data.cardProfile != null) { + if (card.data.cardProfile != null) { cur.data.cardProfile = card.data.cardProfile; } else { @@ -50,7 +51,12 @@ async function updateCards(token, revision, cardMap, updateData) { } cur.data.profileRevision = card.data.profileRevision; } - if (cur.data.notifiedProfile != card.data.notifiedProfile) { + if (cur.data.profileRevision != card.data.notifiedProfile) { + const { cardDetail, cardProfile } = cur.data; + if (cardDetail.status === 'connected') { + let message = await getContactProfile(cardProfile.node, cardProfile.guid, cardDetail.token); + await setCardProfile(token, card.id, message); + } // update remote profile cur.data.notifiedProfile = card.data.notifiedProfile; } diff --git a/net/web/src/User/Contact/Contact.styled.js b/net/web/src/User/Contact/Contact.styled.js index bda271e7..7c220246 100644 --- a/net/web/src/User/Contact/Contact.styled.js +++ b/net/web/src/User/Contact/Contact.styled.js @@ -66,8 +66,17 @@ export const ContactWrapper = styled.div` width: 192px; min-width: 192px; font-size: 8em; + border-radius: 8px; + overflow: hidden; + border: 1px solid #888888; + } + + .logo { + width: 192px; + height 192px; display: flex; - justify-content: flex-end; + align-items: center; + justify-content: center; } .unset { @@ -117,7 +126,6 @@ export const ContactWrapper = styled.div` align-items: center; justify-content: flex-end; width: 50%; - cursor: pointer; } `; diff --git a/net/web/src/User/Profile/useProfile.hook.js b/net/web/src/User/Profile/useProfile.hook.js index 56fb9bd9..0a1222c9 100644 --- a/net/web/src/User/Profile/useProfile.hook.js +++ b/net/web/src/User/Profile/useProfile.hook.js @@ -2,6 +2,8 @@ import { useContext, useState, useEffect } from 'react'; import { AppContext } from '../../AppContext/AppContext'; import { useNavigate } from "react-router-dom"; +const IMAGE_DIM = 256; + export function useProfile() { const [state, setState] = useState({ @@ -79,10 +81,11 @@ export function useProfile() { img.onload = () => { var canvas = document.createElement("canvas"); var context = canvas.getContext('2d'); - canvas.width = 128; - canvas.height = 128; + canvas.width = IMAGE_DIM; + canvas.height = IMAGE_DIM; + context.imageSmoothingQuality = "medium"; context.drawImage(img, state.crop.x, state.crop.y, state.crop.w, state.crop.h, - 0, 0, 128, 128); + 0, 0, IMAGE_DIM, IMAGE_DIM); resolve(canvas.toDataURL()); } img.onerror = reject;