Remove old chat code
This commit is contained in:
parent
43e4db91db
commit
148b42c5ed
2 changed files with 0 additions and 471 deletions
|
|
@ -238,78 +238,6 @@ const pubToEpub = async pub => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Should only be called with a recipient pub that has already been contacted.
|
|
||||||
* If returns null, a disconnect happened.
|
|
||||||
* @param {string} recipientPub
|
|
||||||
* @returns {Promise<string|null>}
|
|
||||||
*/
|
|
||||||
const recipientPubToLastReqSentID = async recipientPub => {
|
|
||||||
const maybeLastReqSentID = await tryAndWait(
|
|
||||||
(_, user) => {
|
|
||||||
const userToLastReqSent = user.get(Key.USER_TO_LAST_REQUEST_SENT)
|
|
||||||
return userToLastReqSent.get(recipientPub).then()
|
|
||||||
},
|
|
||||||
// retry on undefined, in case it is a false negative
|
|
||||||
v => typeof v === 'undefined'
|
|
||||||
)
|
|
||||||
|
|
||||||
if (typeof maybeLastReqSentID !== 'string') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return maybeLastReqSentID
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} recipientPub
|
|
||||||
* @returns {Promise<boolean>}
|
|
||||||
*/
|
|
||||||
const successfulHandshakeAlreadyExists = async recipientPub => {
|
|
||||||
const maybeIncomingID = await tryAndWait((_, user) => {
|
|
||||||
const userToIncoming = user.get(Key.USER_TO_INCOMING)
|
|
||||||
|
|
||||||
return userToIncoming.get(recipientPub).then()
|
|
||||||
})
|
|
||||||
|
|
||||||
const maybeOutgoingID = await tryAndWait((_, user) => {
|
|
||||||
const recipientToOutgoing = user.get(Key.RECIPIENT_TO_OUTGOING)
|
|
||||||
|
|
||||||
return recipientToOutgoing.get(recipientPub).then()
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
|
||||||
typeof maybeIncomingID === 'string' && typeof maybeOutgoingID === 'string'
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {string} recipientPub
|
|
||||||
* @returns {Promise<string|null>}
|
|
||||||
*/
|
|
||||||
const recipientToOutgoingID = async recipientPub => {
|
|
||||||
const maybeEncryptedOutgoingID = await tryAndWait(
|
|
||||||
(_, user) =>
|
|
||||||
user
|
|
||||||
.get(Key.RECIPIENT_TO_OUTGOING)
|
|
||||||
.get(recipientPub)
|
|
||||||
.then(),
|
|
||||||
// force retry in case undefined is a false negative
|
|
||||||
v => typeof v === 'undefined'
|
|
||||||
)
|
|
||||||
|
|
||||||
if (typeof maybeEncryptedOutgoingID === 'string') {
|
|
||||||
const outgoingID = await require('../../Mediator/index').mySEA.decrypt(
|
|
||||||
maybeEncryptedOutgoingID,
|
|
||||||
await mySecret()
|
|
||||||
)
|
|
||||||
|
|
||||||
return outgoingID || null
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {import('../SimpleGUN').ListenerData} listenerData
|
* @param {import('../SimpleGUN').ListenerData} listenerData
|
||||||
* @returns {listenerData is import('../SimpleGUN').ListenerObj}
|
* @returns {listenerData is import('../SimpleGUN').ListenerObj}
|
||||||
|
|
@ -350,9 +278,6 @@ module.exports = {
|
||||||
dataHasSoul,
|
dataHasSoul,
|
||||||
delay,
|
delay,
|
||||||
pubToEpub,
|
pubToEpub,
|
||||||
recipientPubToLastReqSentID,
|
|
||||||
successfulHandshakeAlreadyExists,
|
|
||||||
recipientToOutgoingID,
|
|
||||||
tryAndWait,
|
tryAndWait,
|
||||||
mySecret,
|
mySecret,
|
||||||
promisifyGunNode: require('./promisifygun'),
|
promisifyGunNode: require('./promisifygun'),
|
||||||
|
|
|
||||||
396
src/routes.js
396
src/routes.js
|
|
@ -2132,28 +2132,6 @@ module.exports = async (
|
||||||
|
|
||||||
const Events = require('../services/gunDB/contact-api/events')
|
const Events = require('../services/gunDB/contact-api/events')
|
||||||
|
|
||||||
app.get(`/api/gun/${GunEvent.ON_CHATS}`, (_, res) => {
|
|
||||||
try {
|
|
||||||
const data = Events.getChats()
|
|
||||||
const noAvatar = data.map(mex => {
|
|
||||||
return { ...mex, recipientAvatar: null }
|
|
||||||
})
|
|
||||||
res.json({
|
|
||||||
data: noAvatar
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.info('Error in Chats poll:')
|
|
||||||
logger.error(err)
|
|
||||||
res
|
|
||||||
.status(
|
|
||||||
err.message === Common.Constants.ErrorCode.NOT_AUTH ? 401 : 500
|
|
||||||
)
|
|
||||||
.json({
|
|
||||||
errorMessage: typeof err === 'string' ? err : err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.get(`/api/gun/${GunEvent.ON_DISPLAY_NAME}`, async (_, res) => {
|
app.get(`/api/gun/${GunEvent.ON_DISPLAY_NAME}`, async (_, res) => {
|
||||||
try {
|
try {
|
||||||
const user = require('../services/gunDB/Mediator').getUser()
|
const user = require('../services/gunDB/Mediator').getUser()
|
||||||
|
|
@ -2614,319 +2592,6 @@ module.exports = async (
|
||||||
ap.get(`/api/gun/me`, apiGunMeGet)
|
ap.get(`/api/gun/me`, apiGunMeGet)
|
||||||
ap.put(`/api/gun/me`, apiGunMePut)
|
ap.put(`/api/gun/me`, apiGunMePut)
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} ChatsRouteParams
|
|
||||||
* @prop {(string|undefined)=} publicKey
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<ChatsRouteParams>}
|
|
||||||
*/
|
|
||||||
const apiGunChatsPost = async (req, res) => {
|
|
||||||
const { publicKey } = req.params
|
|
||||||
const { body } = req.body
|
|
||||||
|
|
||||||
if (!publicKey) {
|
|
||||||
return res.status(400).json({
|
|
||||||
errorMessage: `Must specify a publicKey route param for POSTing a message`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const user = GunDB.getUser()
|
|
||||||
const SEA = GunDB.mySEA
|
|
||||||
|
|
||||||
return res
|
|
||||||
.status(200)
|
|
||||||
.json(await GunActions.sendMessageNew(publicKey, body, user, SEA))
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<ChatsRouteParams>}
|
|
||||||
*/
|
|
||||||
const apiGunChatsDelete = async (req, res) => {
|
|
||||||
const { publicKey } = req.params
|
|
||||||
|
|
||||||
if (!publicKey) {
|
|
||||||
return res.status(400).json({
|
|
||||||
errorMessage: `Must specify a publicKey route param for DELETING a chat`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await GunActions.disconnect(publicKey)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
ok: true
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ap.post(`/api/gun/chats/:publicKey?`, apiGunChatsPost)
|
|
||||||
ap.delete(`/api/gun/chats/:publicKey?`, apiGunChatsDelete)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} RequestsRouteParams
|
|
||||||
* @prop {(string|undefined)=} requestID
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<{}>}
|
|
||||||
*/
|
|
||||||
const apiGunRequestsReceivedGet = (_, res) => {
|
|
||||||
try {
|
|
||||||
const data = Events.getCurrentReceivedReqs()
|
|
||||||
const noAvatar = data.map(req => {
|
|
||||||
return { ...req, recipientAvatar: null }
|
|
||||||
})
|
|
||||||
res.json({
|
|
||||||
data: noAvatar
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<{}>}
|
|
||||||
*/
|
|
||||||
const apiGunRequestsSentGet = (_, res) => {
|
|
||||||
try {
|
|
||||||
const data = Events.getCurrentSentReqs()
|
|
||||||
const noAvatar = data.map(req => {
|
|
||||||
return { ...req, recipientAvatar: null }
|
|
||||||
})
|
|
||||||
res.json({
|
|
||||||
data: noAvatar
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} RequestsRoutePOSTBody
|
|
||||||
* @prop {string=} initialMsg
|
|
||||||
* @prop {string} publicKey
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<{}>}
|
|
||||||
*/
|
|
||||||
const apiGunRequestsPost = async (req, res) => {
|
|
||||||
const {
|
|
||||||
initialMsg,
|
|
||||||
publicKey
|
|
||||||
} = /** @type {RequestsRoutePOSTBody} */ (req.body)
|
|
||||||
|
|
||||||
if (!publicKey) {
|
|
||||||
return res.status(400).json({
|
|
||||||
errorMessage: `Must specify a publicKey route param for POSTing a message`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const gun = require('../services/gunDB/Mediator').getGun()
|
|
||||||
const user = require('../services/gunDB/Mediator').getUser()
|
|
||||||
const SEA = require('../services/gunDB/Mediator').mySEA
|
|
||||||
|
|
||||||
if (initialMsg) {
|
|
||||||
await GunActions.sendHRWithInitialMsg(
|
|
||||||
initialMsg,
|
|
||||||
publicKey,
|
|
||||||
gun,
|
|
||||||
user,
|
|
||||||
SEA
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
await GunActions.sendHandshakeRequest(publicKey, gun, user, SEA)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
ok: true
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {object} RequestsRoutePUTBody
|
|
||||||
* @prop {boolean=} accept
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {RequestHandler<RequestsRouteParams>}
|
|
||||||
*/
|
|
||||||
const apiGunRequestsPut = async (req, res) => {
|
|
||||||
const { requestID } = req.params
|
|
||||||
const { accept } = /** @type {RequestsRoutePUTBody} */ (req.body)
|
|
||||||
|
|
||||||
if (!requestID) {
|
|
||||||
return res.status(400).json({
|
|
||||||
errorMessage: `Must specify a requestID route param for accepting a request`
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!accept) {
|
|
||||||
return res.status(200).json({
|
|
||||||
ok: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const gun = require('../services/gunDB/Mediator').getGun()
|
|
||||||
const user = require('../services/gunDB/Mediator').getUser()
|
|
||||||
const SEA = require('../services/gunDB/Mediator').mySEA
|
|
||||||
|
|
||||||
await GunActions.acceptRequest(requestID, gun, user, SEA)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
ok: true
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
logger.error(err)
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ap.get(
|
|
||||||
`/api/gun/${GunEvent.ON_RECEIVED_REQUESTS}`,
|
|
||||||
apiGunRequestsReceivedGet
|
|
||||||
)
|
|
||||||
ap.get(`/api/gun/${GunEvent.ON_SENT_REQUESTS}`, apiGunRequestsSentGet)
|
|
||||||
ap.get(`/api/gun/requests/received`, apiGunRequestsReceivedGet)
|
|
||||||
ap.get(`/api/gun/requests/sent`, apiGunRequestsSentGet)
|
|
||||||
ap.post('/api/gun/requests/', apiGunRequestsPost)
|
|
||||||
ap.put(`/api/gun/requests/:requestID?`, apiGunRequestsPut)
|
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/userToIncoming`, async (_, res) => {
|
|
||||||
try {
|
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
|
||||||
|
|
||||||
const data = await tryAndWait(
|
|
||||||
(_, u) =>
|
|
||||||
new Promise(res => {
|
|
||||||
u.get(GunKey.USER_TO_INCOMING).load(data => {
|
|
||||||
res(data)
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
v => {
|
|
||||||
if (typeof v !== 'object') {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (v === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// load sometimes returns an empty set on the first try
|
|
||||||
return size(v) === 0
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
data
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/recipientToOutgoing`, async (_, res) => {
|
|
||||||
try {
|
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
|
||||||
|
|
||||||
const data = await tryAndWait(
|
|
||||||
(_, u) =>
|
|
||||||
new Promise(res => {
|
|
||||||
u.get(GunKey.RECIPIENT_TO_OUTGOING).load(data => {
|
|
||||||
res(data)
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
v => {
|
|
||||||
if (typeof v !== 'object') {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (v === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// load sometimes returns an empty set on the first try
|
|
||||||
return size(v) === 0
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
data
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/outgoings`, async (_, res) => {
|
|
||||||
try {
|
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
|
||||||
|
|
||||||
const data = await tryAndWait(
|
|
||||||
(_, u) =>
|
|
||||||
new Promise(res => {
|
|
||||||
u.get(GunKey.OUTGOINGS).load(data => {
|
|
||||||
res(data)
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
v => {
|
|
||||||
if (typeof v !== 'object') {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (v === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// load sometimes returns an empty set on the first try
|
|
||||||
return size(v) === 0
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
data
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/currentHandshakeAddress`, async (_, res) => {
|
ap.get(`/api/gun/dev/currentHandshakeAddress`, async (_, res) => {
|
||||||
try {
|
try {
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
||||||
|
|
@ -3020,67 +2685,6 @@ module.exports = async (
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/storedReqs`, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
|
||||||
|
|
||||||
const data = await tryAndWait(
|
|
||||||
(_, u) => new Promise(res => u.get(Key.STORED_REQS).load(res)),
|
|
||||||
v => {
|
|
||||||
if (typeof v !== 'object') {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (v === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// load sometimes returns an empty set on the first try
|
|
||||||
return size(v) === 0
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
data
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ap.get(`/api/gun/dev/userToLastReqSent`, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const { tryAndWait } = require('../services/gunDB/contact-api/utils')
|
|
||||||
|
|
||||||
const data = await tryAndWait(
|
|
||||||
(_, u) =>
|
|
||||||
new Promise(res => u.get(Key.USER_TO_LAST_REQUEST_SENT).load(res)),
|
|
||||||
v => {
|
|
||||||
if (typeof v !== 'object') {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (v === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// load sometimes returns an empty set on the first try
|
|
||||||
return size(v) === 0
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
data
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return res.status(500).json({
|
|
||||||
errorMessage: err.message
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
ap.get(`/api/gun/auth`, (_, res) => {
|
ap.get(`/api/gun/auth`, (_, res) => {
|
||||||
const { isAuthenticated } = require('../services/gunDB/Mediator')
|
const { isAuthenticated } = require('../services/gunDB/Mediator')
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue