mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 01:02:00 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f91395766 | ||
|
|
dda6cb2aa4 | ||
|
|
578aade39b | ||
|
|
b09890a527 | ||
|
|
a32bd840b9 | ||
|
|
0bdc232938 | ||
|
|
f543cfce73 | ||
|
|
0f5e694682 | ||
|
|
615638d705 | ||
|
|
9b46bbd7bb |
Vendored
+3
-1
@@ -47,7 +47,8 @@ export default {
|
||||
useFullStackCash: process.env.USE_FULLSTACKCASH ? true : false,
|
||||
consumerUrl: process.env.CONSUMER_URL
|
||||
? process.env.CONSUMER_URL
|
||||
: 'https://free-bch.fullstack.cash',
|
||||
// : 'https://free-bch.fullstack.cash',
|
||||
: 'https://dev-consumer.psfoundation.info',
|
||||
// : 'https://wa-usa-bch-consumer.fullstackcash.nl',
|
||||
|
||||
// P2WDB URL that will accept API calls from the p2wdb npm library.
|
||||
@@ -184,6 +185,7 @@ export default {
|
||||
// Nostr
|
||||
nostrRelay: process.env.NOSTR_RELAY ? process.env.NOSTR_RELAY : 'wss://nostr-relay.psfoundation.info',
|
||||
nostrTopic: process.env.NOSTR_TOPIC ? process.env.NOSTR_TOPIC : 'bch-dex-test-topic-02',
|
||||
nostrGlobalFeed: process.env.NOSTR_GLOBAL_FEED ? process.env.NOSTR_GLOBAL_FEED : 'slpdex-socialmedia',
|
||||
|
||||
// Account Configuration
|
||||
disableNewAccounts: process.env.DISABLE_NEW_ACCOUNTS ? true : false,
|
||||
|
||||
@@ -8,6 +8,7 @@ import Entries from './models/entry.js'
|
||||
import Order from './models/order.js'
|
||||
import Offer from './models/offer.js'
|
||||
import Usage from './models/usage.js'
|
||||
import SmAccount from './models/smAccount.js'
|
||||
|
||||
class LocalDB {
|
||||
constructor () {
|
||||
@@ -17,6 +18,7 @@ class LocalDB {
|
||||
this.Order = Order
|
||||
this.Offer = Offer
|
||||
this.Usage = Usage
|
||||
this.SmAccount = SmAccount
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
Social Media Account Model.
|
||||
|
||||
This model is used to track content creators using the DEX
|
||||
Nostr social media channel.
|
||||
*/
|
||||
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
const SmAccount = new mongoose.Schema({
|
||||
npub: { type: String, required: true },
|
||||
bchAddr: { type: String, required: true, default: '' },
|
||||
pubkey: { type: String, required: true },
|
||||
pfp: { type: String, default: '' },
|
||||
createdAt: { type: Date, default: Date.now },
|
||||
updatedAt: { type: Date, default: Date.now },
|
||||
followerCnt: { type: Number, default: 0 },
|
||||
followingCnt: { type: Number, default: 0 },
|
||||
posts: { type: Number, default: 0 },
|
||||
likes: { type: Number, default: 0 },
|
||||
comments: { type: Number, default: 0 },
|
||||
reposts: { type: Number, default: 0 }
|
||||
})
|
||||
|
||||
export default mongoose.model('smAccount', SmAccount)
|
||||
@@ -12,6 +12,7 @@ class NostrAdapter {
|
||||
constructor (localConfig = { nostrRelay: '', nostrTopic: '' }) {
|
||||
this.relayWs = localConfig.nostrRelay //
|
||||
this.topic = localConfig.nostrTopic
|
||||
this.globalFeed = localConfig.nostrGlobalFeed
|
||||
|
||||
if (!this.relayWs) {
|
||||
throw new Error(
|
||||
@@ -39,6 +40,10 @@ class NostrAdapter {
|
||||
this.post = this.post.bind(this)
|
||||
this.read = this.read.bind(this)
|
||||
this.eventId2note = this.eventId2note.bind(this)
|
||||
this.pubkey2npub = this.pubkey2npub.bind(this)
|
||||
this.npub2pubkey = this.npub2pubkey.bind(this)
|
||||
this.readGlobalFeed = this.readGlobalFeed.bind(this)
|
||||
this.getFollowers = this.getFollowers.bind(this)
|
||||
}
|
||||
|
||||
// Create nostr keys.
|
||||
@@ -131,6 +136,117 @@ class NostrAdapter {
|
||||
eventId2note (eventId) {
|
||||
return nip19.noteEncode(eventId)
|
||||
}
|
||||
|
||||
// Convert a pubkey into a `npubabc..` syntax that Astral expects.
|
||||
pubkey2npub (pubkey) {
|
||||
return nip19.npubEncode(pubkey)
|
||||
}
|
||||
|
||||
npub2pubkey (npub) {
|
||||
return nip19.decode(npub)
|
||||
}
|
||||
|
||||
// Read the global feed.
|
||||
async readGlobalFeed (inObj = {}) {
|
||||
try {
|
||||
const { limit = 10 } = inObj
|
||||
|
||||
if (typeof limit !== 'number' || limit < 1) {
|
||||
throw new Error('Limit must be greater than 0')
|
||||
}
|
||||
|
||||
const relays = [this.relayWs]
|
||||
const pool = this.RelayPool(relays)
|
||||
|
||||
const nostrData = new Promise((resolve, reject) => {
|
||||
const messages = []
|
||||
|
||||
pool.on('open', (relay) => {
|
||||
// relay.subscribe('REQ', { ids: [eventId] })
|
||||
relay.subscribe('REQ', { limit, kinds: [1], '#t': [this.globalFeed] })
|
||||
})
|
||||
|
||||
pool.on('eose', (relay) => {
|
||||
relay.close()
|
||||
resolve(messages)
|
||||
})
|
||||
|
||||
pool.on('event', (relay, subId, ev) => {
|
||||
// console.log('ev: ', ev)
|
||||
|
||||
const { content, id, tags, pubkey } = ev
|
||||
|
||||
const msg = {
|
||||
content,
|
||||
id,
|
||||
tags,
|
||||
npub: this.pubkey2npub(pubkey),
|
||||
pubkey
|
||||
}
|
||||
|
||||
messages.push(msg)
|
||||
})
|
||||
})
|
||||
|
||||
const messages = await nostrData
|
||||
|
||||
return messages
|
||||
} catch (error) {
|
||||
console.log(`Error in nostr.js/readGlobalFeed() ${error.message} `)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async getFollowers (inObj = {}) {
|
||||
try {
|
||||
const { pubkey } = inObj
|
||||
|
||||
if (!pubkey || typeof pubkey !== 'string') {
|
||||
throw new Error('pubkey must be a string!')
|
||||
}
|
||||
|
||||
const relays = [this.relayWs]
|
||||
const pool = this.RelayPool(relays)
|
||||
|
||||
const nostrData = new Promise((resolve, reject) => {
|
||||
const followers = []
|
||||
|
||||
pool.on('open', (relay) => {
|
||||
// Query for kind 3 events where the target pubkey appears in tags
|
||||
// This finds all contact lists that include the target pubkey
|
||||
relay.subscribe('subid', {
|
||||
limit: 100,
|
||||
kinds: [3],
|
||||
'#p': [pubkey] // Filter by tag 'p' containing the target pubkey
|
||||
})
|
||||
})
|
||||
|
||||
pool.on('eose', relay => {
|
||||
relay.close()
|
||||
resolve(followers)
|
||||
})
|
||||
|
||||
pool.on('event', (relay, subId, ev) => {
|
||||
// Each event represents a contact list from someone who follows the target
|
||||
// The author of this event is a follower
|
||||
const follower = {
|
||||
pubkey: ev.pubkey,
|
||||
npub: this.pubkey2npub(ev.pubkey),
|
||||
contactList: ev.tags,
|
||||
createdAt: ev.created_at
|
||||
}
|
||||
|
||||
followers.push(follower)
|
||||
})
|
||||
})
|
||||
|
||||
const followers = await nostrData
|
||||
return followers
|
||||
} catch (error) {
|
||||
console.log(`Error in nostr.js/getFollowers() ${error.message} `)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default NostrAdapter
|
||||
|
||||
@@ -18,6 +18,7 @@ import OfferRouter from './offer/index.js'
|
||||
import OrderRouter from './order/index.js'
|
||||
import P2WDBRouter from './p2wdb/index.js'
|
||||
import UsageRESTController from './usage/index.js'
|
||||
import SmAccountRouter from './smAccount/index.js'
|
||||
|
||||
class RESTControllers {
|
||||
constructor (localConfig = {}) {
|
||||
@@ -86,6 +87,9 @@ class RESTControllers {
|
||||
// Attach the REST API Controllers associated with the /usage route
|
||||
const usageRESTController = new UsageRESTController(dependencies)
|
||||
usageRESTController.attach(app)
|
||||
|
||||
const smAccountRouter = new SmAccountRouter(dependencies)
|
||||
smAccountRouter.attach(app)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
/*
|
||||
REST API Controller library for the /offer route
|
||||
|
||||
TODO:
|
||||
- Add api-doc documentation for all endpoints in this file.
|
||||
*/
|
||||
|
||||
import wlogger from '../../../adapters/wlogger.js'
|
||||
@@ -30,6 +33,7 @@ class OfferRESTControllerLib {
|
||||
this.listNftOffers = this.listNftOffers.bind(this)
|
||||
this.listFungibleOffers = this.listFungibleOffers.bind(this)
|
||||
this.takeOffer = this.takeOffer.bind(this)
|
||||
this.listOffersByAddress = this.listOffersByAddress.bind(this)
|
||||
this.handleError = this.handleError.bind(this)
|
||||
}
|
||||
|
||||
@@ -115,6 +119,21 @@ class OfferRESTControllerLib {
|
||||
}
|
||||
}
|
||||
|
||||
// List all offers being made by a given address.
|
||||
// curl -X GET http://localhost:5700/offer/list/addr/bitcoincash:qrpxtnrlhfz9wsuuse7z5k2mxmvw0r3pu5qepyhmq2
|
||||
async listOffersByAddress (ctx) {
|
||||
try {
|
||||
const addr = ctx.params.addr
|
||||
|
||||
const offers = await this.useCases.offer.listOffersByAddress(addr)
|
||||
|
||||
ctx.body = offers
|
||||
} catch (err) {
|
||||
console.log('Error in listOffersByAddress REST API handler: ', err)
|
||||
this.handleError(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
// DRY error handler
|
||||
handleError (ctx, err) {
|
||||
console.log('err', err.message)
|
||||
|
||||
@@ -8,8 +8,6 @@ import Router from 'koa-router'
|
||||
// Local libraries.
|
||||
import OfferRESTControllerLib from './controller.js'
|
||||
|
||||
let _this
|
||||
|
||||
class OfferRouter {
|
||||
constructor (localConfig = {}) {
|
||||
// Dependency Injection.
|
||||
@@ -38,7 +36,8 @@ class OfferRouter {
|
||||
const baseUrl = '/offer'
|
||||
this.router = new Router({ prefix: baseUrl })
|
||||
|
||||
_this = this
|
||||
// Bind 'this' object to all subfunctions.
|
||||
this.attach = this.attach.bind(this)
|
||||
}
|
||||
|
||||
attach (app) {
|
||||
@@ -50,19 +49,20 @@ class OfferRouter {
|
||||
|
||||
// 12/3/24 CT:
|
||||
// Note: The createOffer() path was used by a P2WDB webhook to generate an
|
||||
// Offer from and Order. This has been deprecated and Offers are now created
|
||||
// Offer from an Order. This has been deprecated and Offers are now created
|
||||
// by a Timer Controller monitoring a Nostr topic.
|
||||
|
||||
// Define the routes and attach the controller.
|
||||
// this.router.post('/', _this.offerRESTController.createOffer) // Deprecated.
|
||||
this.router.post('/take', _this.offerRESTController.takeOffer)
|
||||
this.router.get('/list/all/:page', _this.offerRESTController.listOffers)
|
||||
this.router.get('/list/nft/:page', _this.offerRESTController.listNftOffers)
|
||||
this.router.get('/list/fungible/:page', _this.offerRESTController.listFungibleOffers)
|
||||
this.router.post('/take', this.offerRESTController.takeOffer)
|
||||
this.router.get('/list/all/:page', this.offerRESTController.listOffers)
|
||||
this.router.get('/list/nft/:page', this.offerRESTController.listNftOffers)
|
||||
this.router.get('/list/fungible/:page', this.offerRESTController.listFungibleOffers)
|
||||
this.router.get('/list/addr/:addr', this.offerRESTController.listOffersByAddress)
|
||||
|
||||
// Attach the Controller routes to the Koa app.
|
||||
app.use(_this.router.routes())
|
||||
app.use(_this.router.allowedMethods())
|
||||
app.use(this.router.routes())
|
||||
app.use(this.router.allowedMethods())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
REST API Controller library for the /order route
|
||||
*/
|
||||
|
||||
// const { wlogger } = require('../../../adapters/wlogger')
|
||||
|
||||
let _this
|
||||
|
||||
class SmAccountRESTControllerLib {
|
||||
constructor (localConfig = {}) {
|
||||
// Dependency Injection.
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
throw new Error(
|
||||
'Instance of Adapters library required when instantiating /order REST Controller.'
|
||||
)
|
||||
}
|
||||
this.useCases = localConfig.useCases
|
||||
if (!this.useCases) {
|
||||
throw new Error(
|
||||
'Instance of Use Cases library required when instantiating /order REST Controller.'
|
||||
)
|
||||
}
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.SmAccountModel = this.adapters.localdb.SmAccount
|
||||
// this.userUseCases = this.useCases.user
|
||||
|
||||
_this = this
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /sm/list/all/{page} List all Social Media Accounts
|
||||
* @apiPermission public
|
||||
* @apiName ListAccounts
|
||||
* @apiGroup REST Social Media Accounts
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -H "Content-Type: application/json" -X GET localhost:5700/sm/list/all/0
|
||||
*
|
||||
*/
|
||||
// curl -X GET http://localhost:5700/sm/list/all/0
|
||||
async listAccounts (ctx) {
|
||||
try {
|
||||
let page = ctx.params.page
|
||||
if (!page) page = 0
|
||||
|
||||
const offers = await _this.useCases.smAccount.listAccounts(page)
|
||||
|
||||
ctx.body = offers
|
||||
} catch (err) {
|
||||
console.log('Error in listAccounts REST API handler: ', err)
|
||||
_this.handleError(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /sm/npub/{npub} Get Social Media Account by Npub
|
||||
* @apiPermission public
|
||||
* @apiName GetAccountByNpub
|
||||
* @apiGroup REST Social Media Accounts
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -H "Content-Type: application/json" -X GET localhost:5700/sm/npub/npub1y3xq402pu9aqms3khetnt5gzm54t63pzcla56wwfmwshde0ws77qkff5gc
|
||||
*
|
||||
*/
|
||||
// curl -X GET http://localhost:5700/sm/npub/npub1y3xq402pu9aqms3khetnt5gzm54t63pzcla56wwfmwshde0ws77qkff5gc
|
||||
async getAccountByNpub (ctx) {
|
||||
try {
|
||||
const npub = ctx.params.npub
|
||||
if (!npub) {
|
||||
ctx.throw(400, 'Npub is required')
|
||||
}
|
||||
|
||||
const account = await _this.useCases.smAccount.getAccountByNpub(npub)
|
||||
|
||||
if (!account) {
|
||||
ctx.throw(404, 'Account not found')
|
||||
}
|
||||
|
||||
ctx.body = account
|
||||
} catch (err) {
|
||||
// console.log('Error in getAccountByNpub REST API handler: ', err)
|
||||
_this.handleError(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /sm/bchAddr/{bchAddr} Get Social Media Account by BCH address
|
||||
* @apiPermission public
|
||||
* @apiName GetAccountByBchAddr
|
||||
* @apiGroup REST Social Media Accounts
|
||||
*
|
||||
* @apiExample Example usage:
|
||||
* curl -H "Content-Type: application/json" -X GET localhost:5700/sm/bchAddr/bitcoincash:qz6lf6gpmn3secx73g7ucfcgy8mrh3sz2y2ylk643x
|
||||
*
|
||||
*/
|
||||
// curl -X GET http://localhost:5700/sm/bchAddr/bitcoincash:qz6lf6gpmn3secx73g7ucfcgy8mrh3sz2y2ylk643x
|
||||
async getAccountByBchAddr (ctx) {
|
||||
try {
|
||||
const bchAddr = ctx.params.bchAddr
|
||||
if (!bchAddr) {
|
||||
ctx.throw(400, 'BCH address is required')
|
||||
}
|
||||
|
||||
const account = await _this.useCases.smAccount.getAccountByBchAddr(bchAddr)
|
||||
|
||||
if (!account) {
|
||||
ctx.throw(404, 'Account not found')
|
||||
}
|
||||
|
||||
ctx.body = account
|
||||
} catch (err) {
|
||||
// console.log('Error in getAccountByBchAddr REST API handler: ', err)
|
||||
_this.handleError(ctx, err)
|
||||
}
|
||||
}
|
||||
|
||||
// DRY error handler
|
||||
handleError (ctx, err) {
|
||||
console.log('err', err)
|
||||
|
||||
// If an HTTP status is specified by the buisiness logic, use that.
|
||||
if (err.status) {
|
||||
if (err.message) {
|
||||
ctx.throw(err.status, err.message)
|
||||
} else {
|
||||
ctx.throw(err.status)
|
||||
}
|
||||
} else {
|
||||
// By default use a 422 error if the HTTP status is not specified.
|
||||
ctx.throw(422, err.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default SmAccountRESTControllerLib
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
REST API library for /order route.
|
||||
*/
|
||||
|
||||
// Public npm libraries.
|
||||
import Router from 'koa-router'
|
||||
|
||||
// Local libraries.
|
||||
import SmAccountRESTControllerLib from './controller.js'
|
||||
import Validators from '../middleware/validators.js'
|
||||
|
||||
let _this
|
||||
|
||||
class SmAccountRouter {
|
||||
constructor (localConfig = {}) {
|
||||
// Dependency Injection.
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
throw new Error(
|
||||
'Instance of Adapters library required when instantiating /sm REST Controller.'
|
||||
)
|
||||
}
|
||||
this.useCases = localConfig.useCases
|
||||
if (!this.useCases) {
|
||||
throw new Error(
|
||||
'Instance of Use Cases library required when instantiating /sm REST Controller.'
|
||||
)
|
||||
}
|
||||
|
||||
const dependencies = {
|
||||
adapters: this.adapters,
|
||||
useCases: this.useCases
|
||||
}
|
||||
|
||||
// Encapsulate dependencies.
|
||||
this.validators = new Validators()
|
||||
this.smAccountRESTController = new SmAccountRESTControllerLib(dependencies)
|
||||
|
||||
// Instantiate the router and set the base route.
|
||||
const baseUrl = '/sm'
|
||||
this.router = new Router({ prefix: baseUrl })
|
||||
|
||||
this.listAccounts = this.listAccounts.bind(this)
|
||||
_this = this
|
||||
}
|
||||
|
||||
attach (app) {
|
||||
if (!app) {
|
||||
throw new Error(
|
||||
'Must pass app object when attached REST API controllers.'
|
||||
)
|
||||
}
|
||||
|
||||
// Define the routes and attach the controller.
|
||||
this.router.get('/list/all/:page', _this.smAccountRESTController.listAccounts)
|
||||
this.router.get('/npub/:npub', _this.smAccountRESTController.getAccountByNpub)
|
||||
this.router.get('/bchAddr/:bchAddr', _this.smAccountRESTController.getAccountByBchAddr)
|
||||
|
||||
// Attach the Controller routes to the Koa app.
|
||||
app.use(_this.router.routes())
|
||||
app.use(_this.router.allowedMethods())
|
||||
}
|
||||
|
||||
async listAccounts (ctx, next) {
|
||||
// await _this.validators.ensureUser(ctx, next)
|
||||
await _this.smAccountRESTController.listAccounts(ctx, next)
|
||||
}
|
||||
}
|
||||
|
||||
export default SmAccountRouter
|
||||
@@ -26,6 +26,8 @@ class TimerControllers {
|
||||
// Constants
|
||||
this.cleanUsageInterval = 60000 * 60 // 1 hour
|
||||
this.backupUsageInterval = 60000 * 10 // 10 minutes
|
||||
this.newSmAccountsInterval = 60000 * 60 // 60 minutes
|
||||
this.updateSmAccountsInterval = 60000 * 10 // 10 minutes
|
||||
|
||||
// Encapsulate dependencies
|
||||
this.config = config
|
||||
@@ -36,17 +38,18 @@ class TimerControllers {
|
||||
this.gcOffers = this.gcOffers.bind(this)
|
||||
this.checkDupOffers = this.checkDupOffers.bind(this)
|
||||
this.loadOffers = this.loadOffers.bind(this)
|
||||
// Bind 'this' object to all subfunctions.
|
||||
// this.exampleTimerFunc = this.exampleTimerFunc.bind(this)
|
||||
this.cleanUsage = this.cleanUsage.bind(this)
|
||||
this.backupUsage = this.backupUsage.bind(this)
|
||||
this.newSmAccounts = this.newSmAccounts.bind(this)
|
||||
this.updateSmAccounts = this.updateSmAccounts.bind(this)
|
||||
|
||||
// State
|
||||
this.gcOrdersInt = null
|
||||
this.gcOffersInt = null
|
||||
this.checkDupOffersInt = null
|
||||
this.loadOffersInt = null
|
||||
this.cleanUsage = this.cleanUsage.bind(this)
|
||||
this.backupUsage = this.backupUsage.bind(this)
|
||||
this.newSmAccountsInt = null
|
||||
this.updateSmAccountsInt = null
|
||||
}
|
||||
|
||||
// Start all the time-based controllers.
|
||||
@@ -55,11 +58,10 @@ class TimerControllers {
|
||||
this.gcOffersInt = setInterval(this.gcOffers, 60000 * 5)
|
||||
// this.checkDupOffersInt = setInterval(this.checkDupOffers, 60000 * 4.5)
|
||||
this.loadOffersInt = setInterval(this.loadOffers, 60000 * 2)
|
||||
// Any new timer control functions can be added here. They will be started
|
||||
// when the server starts.
|
||||
this.cleanUsageHandle = setInterval(this.cleanUsage, this.cleanUsageInterval)
|
||||
this.backupUsageHandle = setInterval(this.backupUsage, this.backupUsageInterval)
|
||||
|
||||
this.newSmAccountsInt = setInterval(this.newSmAccounts, this.newSmAccountsInterval)
|
||||
this.updateSmAccountsInt = setInterval(this.updateSmAccounts, this.updateSmAccountsInterval)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -70,6 +72,8 @@ class TimerControllers {
|
||||
clearInterval(this.optimizeWalletHandle)
|
||||
clearInterval(this.cleanUsageHandle)
|
||||
clearInterval(this.backupUsageHandle)
|
||||
clearInterval(this.newSmAccountsInt)
|
||||
clearInterval(this.updateSmAccountsInt)
|
||||
}
|
||||
|
||||
// Garbage Collect the Orders.
|
||||
@@ -165,6 +169,30 @@ class TimerControllers {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Check for new Social Media Accounts.
|
||||
async newSmAccounts () {
|
||||
try {
|
||||
await this.useCases.smAccount.checkForNewSmAccounts()
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Error in time-controller.js/newSmAccounts(): ', err)
|
||||
|
||||
// Note: Do not throw an error. This is a top-level function.
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Update the Social Media Accounts.
|
||||
async updateSmAccounts () {
|
||||
try {
|
||||
await this.useCases.smAccount.updateSmAccounts()
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Error in time-controller.js/updateSmAccounts(): ', err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default TimerControllers
|
||||
|
||||
@@ -10,6 +10,7 @@ import EntryUseCases from './entry.js'
|
||||
import OfferUseCases from './offer/index.js'
|
||||
import OrderUseCases from './order.js'
|
||||
import { UsageUseCases } from './usage-use-cases.js'
|
||||
import SmAccountUseCases from './smAccount-use-cases.js'
|
||||
|
||||
class UseCases {
|
||||
constructor (localConfig = {}) {
|
||||
@@ -27,6 +28,7 @@ class UseCases {
|
||||
localConfig.order = this.order
|
||||
this.offer = new OfferUseCases(localConfig)
|
||||
this.usage = new UsageUseCases(localConfig)
|
||||
this.smAccount = new SmAccountUseCases(localConfig)
|
||||
}
|
||||
|
||||
// Run any startup Use Cases at the start of the app.
|
||||
|
||||
@@ -64,6 +64,7 @@ class OfferUseCases {
|
||||
this.removeStaleOffers = this.removeStaleOffers.bind(this)
|
||||
this.flagOffer = this.flagOffer.bind(this)
|
||||
this.loadOffers = this.loadOffers.bind(this)
|
||||
this.listOffersByAddress = this.listOffersByAddress.bind(this)
|
||||
|
||||
// State
|
||||
this.seenOffers = []
|
||||
@@ -808,6 +809,17 @@ class OfferUseCases {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// List all offers being made by a given address.
|
||||
async listOffersByAddress (addr) {
|
||||
try {
|
||||
const offers = await this.OfferModel.find({ makerAddr: addr })
|
||||
return offers
|
||||
} catch (err) {
|
||||
console.error('Error in listOffersByAddress(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default OfferUseCases
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
Social Media Account Use Cases.
|
||||
*/
|
||||
|
||||
class smAccountUseCases {
|
||||
constructor (localConfig = {}) {
|
||||
// console.log('User localConfig: ', localConfig)
|
||||
|
||||
// Dependency injection.
|
||||
this.adapters = localConfig.adapters
|
||||
if (!this.adapters) {
|
||||
throw new Error(
|
||||
'Instance of adapters must be passed in when instantiating Order Use Cases library.'
|
||||
)
|
||||
}
|
||||
|
||||
// Bind 'this' object to all methods
|
||||
this.checkForNewSmAccounts = this.checkForNewSmAccounts.bind(this)
|
||||
this.listAccounts = this.listAccounts.bind(this)
|
||||
this.getAccountByNpub = this.getAccountByNpub.bind(this)
|
||||
this.getAccountByBchAddr = this.getAccountByBchAddr.bind(this)
|
||||
this.updateSmAccounts = this.updateSmAccounts.bind(this)
|
||||
}
|
||||
|
||||
// Check for new Social Media Accounts.
|
||||
async checkForNewSmAccounts () {
|
||||
try {
|
||||
console.log('Checking for new Social Media Accounts...')
|
||||
|
||||
const messages = await this.adapters.nostr.readGlobalFeed()
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
const msg = messages[i]
|
||||
// const { content, id, tags, npub, pubkey } = msg
|
||||
const { tags, npub, pubkey } = msg
|
||||
let bchAddr = null
|
||||
// console.log('msg: ', JSON.stringify(msg, null, 2))
|
||||
|
||||
// Find the 'u' tag if it exists
|
||||
const uTag = tags.find(tag => tag[0] === 'u')
|
||||
|
||||
// If 'u' tag exists and contains a BCH address
|
||||
if (uTag && uTag[1].includes('bitcoincash:')) {
|
||||
bchAddr = uTag[1]
|
||||
// console.log('BCH address found:', bchAddr)
|
||||
}
|
||||
|
||||
const SmAccount = await this.adapters.localdb.SmAccount
|
||||
|
||||
if (bchAddr) {
|
||||
const smAccount = await SmAccount.findOne({ npub })
|
||||
if (!smAccount) {
|
||||
console.log(`Creating new Social Media Account with npub: ${npub} and bchAddr: ${bchAddr}`)
|
||||
const newSmAccount = new SmAccount({ npub, bchAddr, pubkey })
|
||||
await newSmAccount.save()
|
||||
} else {
|
||||
console.log(`Social Media Account with npub: ${npub} already exists`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error('Error in smAccount-use-cases.js/checkForNewSmAccounts(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// List all Social Media Accounts.
|
||||
async listAccounts (page = 0) {
|
||||
try {
|
||||
const SmAccount = await this.adapters.localdb.SmAccount
|
||||
const accounts = await SmAccount.find({}).skip(page * 10).limit(10)
|
||||
return accounts
|
||||
} catch (err) {
|
||||
console.error('Error in smAccount-use-cases.js/listAccounts(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Get a Social Media Account by Npub.
|
||||
async getAccountByNpub (npub) {
|
||||
try {
|
||||
const SmAccount = await this.adapters.localdb.SmAccount
|
||||
const account = await SmAccount.findOne({ npub })
|
||||
return account
|
||||
} catch (err) {
|
||||
console.error('Error in smAccount-use-cases.js/getAccountByNpub(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Get a Social Media Account by BCH address.
|
||||
async getAccountByBchAddr (bchAddr) {
|
||||
try {
|
||||
const SmAccount = await this.adapters.localdb.SmAccount
|
||||
const account = await SmAccount.findOne({ bchAddr })
|
||||
console.log('Account found: ', account)
|
||||
|
||||
return account
|
||||
} catch (err) {
|
||||
console.error('Error in smAccount-use-cases.js/getAccountByBchAddr(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Update metadata about social media accounts being tracked.
|
||||
async updateSmAccounts () {
|
||||
try {
|
||||
console.log('Updating Social Media Accounts...')
|
||||
const SmAccount = await this.adapters.localdb.SmAccount
|
||||
const accounts = await SmAccount.find({})
|
||||
for (let i = 0; i < accounts.length; i++) {
|
||||
const account = accounts[i]
|
||||
// console.log(`Account ${i}: `, account)
|
||||
|
||||
// Get the number of accounts following this account
|
||||
const pubkey = account.pubkey
|
||||
const followList = await this.adapters.nostr.getFollowers({ pubkey })
|
||||
// console.log(`Follow list ${i}: `, JSON.stringify(followList, null, 2))
|
||||
|
||||
// Update the follower count for this account.
|
||||
const followerCnt = followList.length
|
||||
await SmAccount.updateOne({ _id: account._id }, { $set: { followerCnt } })
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error in smAccount-use-cases.js/updateSmAccounts(): ', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default smAccountUseCases
|
||||
Reference in New Issue
Block a user