diff --git a/services/gunDB/Mediator/index.js b/services/gunDB/Mediator/index.js index 9e8545e2..9464b6a5 100644 --- a/services/gunDB/Mediator/index.js +++ b/services/gunDB/Mediator/index.js @@ -385,12 +385,8 @@ const authenticate = async (alias, pass, __user) => { API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.lastSeenNode(_user) - API.Events.onAvatar(() => {}, user)() - API.Events.onBio(() => {}, user) - API.Events.onBlacklist(() => {}, user) API.Events.onChats(() => {})() API.Events.onCurrentHandshakeAddress(() => {}, user)() - API.Events.onDisplayName(() => {}, user)() API.Events.onOutgoing(() => {})() API.Events.onSeedBackup(() => {}, user, mySEA) API.Events.onSimplerReceivedRequests(() => {})() @@ -443,12 +439,9 @@ const authenticate = async (alias, pass, __user) => { API.Jobs.onOrders(_user, gun, mySEA) API.Jobs.lastSeenNode(_user) - API.Events.onAvatar(() => {}, user)() - API.Events.onBio(() => {}, user) - API.Events.onBlacklist(() => {}, user) API.Events.onChats(() => {})() API.Events.onCurrentHandshakeAddress(() => {}, user)() - API.Events.onDisplayName(() => {}, user)() + API.Events.onOutgoing(() => {})() API.Events.onSeedBackup(() => {}, user, mySEA) API.Events.onSimplerReceivedRequests(() => {})() @@ -546,7 +539,6 @@ class Mediator { this.socket.on('disconnect', this.onDisconnect) this.socket.on(Action.ACCEPT_REQUEST, this.acceptRequest) - this.socket.on(Action.BLACKLIST, this.blacklist) this.socket.on( Action.GENERATE_NEW_HANDSHAKE_NODE, this.generateHandshakeNode @@ -559,26 +551,21 @@ class Mediator { this.sendHRWithInitialMsg ) this.socket.on(Action.SEND_MESSAGE, this.sendMessage) - this.socket.on(Action.SET_AVATAR, this.setAvatar) - this.socket.on(Action.SET_DISPLAY_NAME, this.setDisplayName) + this.socket.on(Action.SEND_PAYMENT, this.sendPayment) - this.socket.on(Action.SET_BIO, this.setBio) + this.socket.on(Action.DISCONNECT, this.disconnect) - this.socket.on(Event.ON_AVATAR, this.onAvatar) - this.socket.on(Event.ON_BLACKLIST, this.onBlacklist) this.socket.on(Event.ON_CHATS, this.onChats) - this.socket.on(Event.ON_DISPLAY_NAME, this.onDisplayName) + this.socket.on(Event.ON_HANDSHAKE_ADDRESS, this.onHandshakeAddress) this.socket.on(Event.ON_RECEIVED_REQUESTS, this.onReceivedRequests) this.socket.on(Event.ON_SENT_REQUESTS, this.onSentRequests) - this.socket.on(Event.ON_BIO, this.onBio) + this.socket.on(Event.ON_SEED_BACKUP, this.onSeedBackup) this.socket.on(Constants.Misc.IS_GUN_AUTH, this.isGunAuth) - this.socket.on(Action.SET_LAST_SEEN_APP, this.setLastSeenApp) - Object.values(Action).forEach(actionConstant => this.socket.on(actionConstant, this.setLastSeenApp) ) @@ -665,32 +652,6 @@ class Mediator { } } - /** - * @param {Readonly<{ publicKey: string , token: string }>} body - */ - blacklist = async body => { - try { - const { publicKey, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.blacklist(publicKey, user) - - this.socket.emit(Action.BLACKLIST, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.BLACKLIST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - onDisconnect = () => { this.connected = false } @@ -919,124 +880,8 @@ class Mediator { } } - /** - * @param {Readonly<{ avatar: string|null , token: string }>} body - */ - setAvatar = async body => { - try { - const { avatar, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setAvatar(avatar, user) - - this.socket.emit(Action.SET_AVATAR, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_AVATAR, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ displayName: string , token: string }>} body - */ - setDisplayName = async body => { - try { - const { displayName, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setDisplayName(displayName, user) - - this.socket.emit(Action.SET_DISPLAY_NAME, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_DISPLAY_NAME, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - ////////////////////////////////////////////////////////////////////////////// - /** - * @param {Readonly<{ token: string }>} body - */ - onAvatar = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onAvatar(avatar => { - if (Config.SHOW_LOG) { - logger.info('---avatar---') - logger.info(avatar || 'null') - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_AVATAR, { - msg: avatar, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_AVATAR, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ token: string }>} body - */ - onBlacklist = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onBlacklist(blacklist => { - if (Config.SHOW_LOG) { - logger.info('---blacklist---') - logger.info(blacklist.join(',')) - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_BLACKLIST, { - msg: blacklist, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_BLACKLIST, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ @@ -1071,38 +916,6 @@ class Mediator { } } - /** - * @param {Readonly<{ token: string }>} body - */ - onDisplayName = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onDisplayName(displayName => { - if (Config.SHOW_LOG) { - logger.info('---displayName---') - logger.info(displayName || 'null or empty string') - logger.info('-----------------------') - } - - this.socket.emit(Event.ON_DISPLAY_NAME, { - msg: displayName, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_DISPLAY_NAME, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ @@ -1198,58 +1011,6 @@ class Mediator { } } - /** - * @param {Readonly<{ token: string }>} body - */ - onBio = async body => { - try { - const { token } = body - - await throwOnInvalidToken(token) - - API.Events.onBio(bio => { - this.socket.emit(Event.ON_BIO, { - msg: bio, - ok: true, - origBody: body - }) - }, user) - } catch (err) { - logger.info(err) - this.socket.emit(Event.ON_BIO, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - - /** - * @param {Readonly<{ bio: string|null , token: string }>} body - */ - setBio = async body => { - try { - const { bio, token } = body - - await throwOnInvalidToken(token) - - await API.Actions.setBio(bio, user) - - this.socket.emit(Action.SET_BIO, { - ok: true, - msg: null, - origBody: body - }) - } catch (err) { - logger.info(err) - this.socket.emit(Action.SET_BIO, { - ok: false, - msg: err.message, - origBody: body - }) - } - } - /** * @param {Readonly<{ token: string }>} body */ diff --git a/services/gunDB/contact-api/events/index.js b/services/gunDB/contact-api/events/index.js index c7d3ba73..54e6725e 100644 --- a/services/gunDB/contact-api/events/index.js +++ b/services/gunDB/contact-api/events/index.js @@ -80,85 +80,6 @@ const __onUserToIncoming = (cb, user, SEA) => { }) } -/** @type {Set<(av: string|null) => void>} */ -const avatarListeners = new Set() - -/** @type {string|null} */ -let currentAvatar = null - -const getAvatar = () => currentAvatar - -/** @param {string|null} av */ -const setAvatar = av => { - currentAvatar = av - avatarListeners.forEach(l => l(currentAvatar)) -} - -let avatarSubbed = false - -/** - * @param {(avatar: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {() => void} - */ -const onAvatar = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - avatarListeners.add(cb) - - cb(currentAvatar) - - if (!avatarSubbed) { - avatarSubbed = true - user - .get(Key.PROFILE_BINARY) - .get(Key.AVATAR) - .on(avatar => { - if (typeof avatar === 'string' || avatar === null) { - setAvatar(avatar) - } - }) - } - - return () => { - avatarListeners.delete(cb) - } -} - -/** - * @param {(blacklist: string[]) => void} cb - * @param {UserGUNNode} user - * @returns {void} - */ -const onBlacklist = (cb, user) => { - /** @type {string[]} */ - const blacklist = [] - - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - const callb = debounce(cb, DEBOUNCE_WAIT_TIME) - - // Initial value if no items are in blacklist in gun - callb(blacklist) - - user - .get(Key.BLACKLIST) - .map() - .on(publicKey => { - if (typeof publicKey === 'string' && publicKey.length > 0) { - blacklist.push(publicKey) - callb(blacklist) - } else { - logger.warn('Invalid public key received for blacklist') - } - }) -} - /** @type {Set<(addr: string|null) => void>} */ const addressListeners = new Set() @@ -210,54 +131,6 @@ const onCurrentHandshakeAddress = (cb, user) => { } } -/** @type {Set<(dn: string|null) => void>} */ -const dnListeners = new Set() - -/** @type {string|null} */ -let currentDn = null - -const getDisplayName = () => currentDn - -/** @param {string|null} dn */ -const setDn = dn => { - currentDn = dn - dnListeners.forEach(l => l(currentDn)) -} - -let dnSubbed = false - -/** - * @param {(displayName: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {() => void} - */ -const onDisplayName = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - cb(currentDn) - - dnListeners.add(cb) - - if (!dnSubbed) { - dnSubbed = true - user - .get(Key.PROFILE) - .get(Key.DISPLAY_NAME) - .on(displayName => { - if (typeof displayName === 'string' || displayName === null) { - setDn(displayName) - } - }) - } - - return () => { - dnListeners.delete(cb) - } -} - /** * @param {(messages: Record) => void} cb * @param {string} userPK Public key of the user from whom the incoming @@ -543,32 +416,6 @@ const onChats = cb => { } } -/** @type {string|null} */ -let currentBio = null - -/** - * @param {(bio: string|null) => void} cb - * @param {UserGUNNode} user Pass only for testing purposes. - * @throws {Error} If user hasn't been auth. - * @returns {void}outgoingsListeners.forEach() - */ -const onBio = (cb, user) => { - if (!user.is) { - throw new Error(ErrorCode.NOT_AUTH) - } - - const callb = debounce(cb, DEBOUNCE_WAIT_TIME) - // Initial value if avvatar is undefined in gun - callb(currentBio) - - user.get(Key.BIO).on(bio => { - if (typeof bio === 'string' || bio === null) { - currentBio = bio - callb(bio) - } - }) -} - /** @type {string|null} */ let currentSeedBackup = null @@ -599,10 +446,7 @@ const onSeedBackup = (cb, user, SEA) => { module.exports = { __onUserToIncoming, - onAvatar, - onBlacklist, onCurrentHandshakeAddress, - onDisplayName, onIncomingMessages, onOutgoing, getCurrentOutgoings, @@ -610,11 +454,8 @@ module.exports = { onSimplerSentRequests: require('./onSentReqs').onSentReqs, getCurrentSentReqs: require('./onSentReqs').getCurrentSentReqs, getCurrentReceivedReqs: require('./onReceivedReqs').getReceivedReqs, - onBio, onSeedBackup, onChats, - getAvatar, - getDisplayName, getHandshakeAddress, getChats } diff --git a/services/gunDB/contact-api/events/onSentReqs.js b/services/gunDB/contact-api/events/onSentReqs.js index f06518e1..3aefb6ef 100644 --- a/services/gunDB/contact-api/events/onSentReqs.js +++ b/services/gunDB/contact-api/events/onSentReqs.js @@ -113,8 +113,6 @@ const onSentReqs = cb => { Streams.onStoredReqs(react) Streams.onLastSentReqIDs(react) Streams.onPubToFeed(react) - Streams.onAvatar(react) - Streams.onDisplayName(react) subbed = true } diff --git a/services/gunDB/contact-api/streams/index.js b/services/gunDB/contact-api/streams/index.js index 0b211bcd..3f586954 100644 --- a/services/gunDB/contact-api/streams/index.js +++ b/services/gunDB/contact-api/streams/index.js @@ -3,99 +3,6 @@ const { Schema, Utils: CommonUtils } = require('shock-common') const Key = require('../key') const Utils = require('../utils') -/** - * @typedef {Record} Avatars - * @typedef {(avatars: Avatars) => void} AvatarListener - */ - -/** @type {Avatars} */ -const pubToAvatar = {} - -const getPubToAvatar = () => pubToAvatar - -/** @type {Set} */ -const avatarListeners = new Set() - -const notifyAvatarListeners = () => { - avatarListeners.forEach(l => l(pubToAvatar)) -} - -/** @type {Set} */ -const pubsWithAvatarListeners = new Set() - -/** - * @param {AvatarListener} cb - * @param {string=} pub - */ -const onAvatar = (cb, pub) => { - avatarListeners.add(cb) - cb(pubToAvatar) - if (pub && pubsWithAvatarListeners.add(pub)) { - require('../../Mediator') - .getGun() - .user(pub) - .get(Key.PROFILE_BINARY) - .get(Key.AVATAR) - .on(av => { - if (typeof av === 'string' || av === null) { - pubToAvatar[pub] = av || null - } else { - pubToAvatar[pub] = null - } - notifyAvatarListeners() - }) - } - return () => { - avatarListeners.delete(cb) - } -} - -/** - * @typedef {Record} DisplayNames - * @typedef {(avatars: Avatars) => void} DisplayNameListener - */ - -/** @type {DisplayNames} */ -const pubToDisplayName = {} - -const getPubToDn = () => pubToDisplayName - -/** @type {Set} */ -const displayNameListeners = new Set() - -const notifyDisplayNameListeners = () => { - displayNameListeners.forEach(l => l(pubToDisplayName)) -} - -/** @type {Set} */ -const pubsWithDisplayNameListeners = new Set() - -/** - * @param {DisplayNameListener} cb - * @param {string=} pub - */ -const onDisplayName = (cb, pub) => { - displayNameListeners.add(cb) - cb(pubToDisplayName) - if (pub && pubsWithDisplayNameListeners.add(pub)) { - require('../../Mediator') - .getGun() - .user(pub) - .get(Key.PROFILE) - .get(Key.DISPLAY_NAME) - .on(dn => { - if (typeof dn === 'string' || dn === null) { - pubToDisplayName[pub] = dn || null - } else { - pubToDisplayName[pub] = null - } - notifyDisplayNameListeners() - }) - } - return () => { - displayNameListeners.delete(cb) - } -} /** * @typedef {import('shock-common').Schema.StoredRequest} StoredRequest @@ -171,11 +78,6 @@ const onStoredReqs = cb => { } module.exports = { - onAvatar, - getPubToAvatar, - onDisplayName, - getPubToDn, - onPubToIncoming: require('./pubToIncoming').onPubToIncoming, getPubToIncoming: require('./pubToIncoming').getPubToIncoming, setPubToIncoming: require('./pubToIncoming').setPubToIncoming,