Browse Source

tribe stats

feature/dockerfile-arm
Evan Feenstra 4 years ago
parent
commit
b2734de792
  1. 2
      api/network/receive.ts
  2. 36
      api/utils/tribes.ts
  3. 2
      dist/api/network/receive.js
  4. 2
      dist/api/network/receive.js.map
  5. 42
      dist/api/utils/tribes.js
  6. 2
      dist/api/utils/tribes.js.map

2
api/network/receive.ts

@ -149,7 +149,7 @@ export async function initTribesSubscriptions(){
tribes.connect(async(topic, message)=>{ // onMessage callback
try{
const msg = message.toString()
console.log("=====> msg received! TOPIC", topic, "MESSAGE", msg)
// console.log("=====> msg received! TOPIC", topic, "MESSAGE", msg)
// check topic is signed by sender?
const payload = await parseAndVerifyPayload(msg)
onReceive(payload)

36
api/utils/tribes.ts

@ -5,6 +5,7 @@ import * as LND from './lightning'
import * as path from 'path'
import * as mqtt from 'mqtt'
import * as fetch from 'node-fetch'
import { models } from '../models'
const env = process.env.NODE_ENV || 'development'
const config = require(path.join(__dirname, '../../config/app.json'))[env]
@ -27,6 +28,7 @@ export async function connect(onMessage) {
client.on('connect', function () {
console.log("[tribes] connected!")
client.subscribe(`${info.identity_pubkey}/#`)
updateTribeStats(info.identity_pubkey)
})
client.on('close', function (e) {
setTimeout(() => reconnect(), 2000)
@ -45,6 +47,20 @@ export async function connect(onMessage) {
}
}
async function updateTribeStats(myPubkey){
const myTribes = await models.Chat.findAll({where:{
ownerPubkey:myPubkey
}})
await asyncForEach(myTribes, async(tribe)=>{
try {
const contactIds = JSON.parse(tribe.contactIds)
const member_count = (contactIds&&contactIds.length)||0
await putstats({uuid:tribe.uuid, host:tribe.host, member_count})
} catch(e) {}
})
console.log(`[tribes] updated stats for ${myTribes.length} tribes`)
}
export function subscribe(topic) {
if (client) client.subscribe(topic)
}
@ -94,6 +110,20 @@ export async function edit({ uuid, host, name, description, tags, img, price_per
}
}
export async function putstats({ uuid, host, member_count }) {
try {
const token = await genSignedTimestamp()
await fetch('https://' + host + '/tribestats?token=' + token, {
method: 'PUT',
body: JSON.stringify({uuid, member_count}),
headers: { 'Content-Type': 'application/json' }
})
} catch(e) {
console.log('[tribes] unauthorized to putstats')
throw e
}
}
export async function genSignedTimestamp() {
const now = moment().unix()
const tsBytes = Buffer.from(now.toString(16), 'hex')
@ -122,4 +152,10 @@ export function getHost() {
function urlBase64(buf) {
return buf.toString('base64').replace(/\//g, '_').replace(/\+/g, '-')
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}

2
dist/api/network/receive.js

@ -170,7 +170,7 @@ function initTribesSubscriptions() {
tribes.connect((topic, message) => __awaiter(this, void 0, void 0, function* () {
try {
const msg = message.toString();
console.log("=====> msg received! TOPIC", topic, "MESSAGE", msg);
// console.log("=====> msg received! TOPIC", topic, "MESSAGE", msg)
// check topic is signed by sender?
const payload = yield parseAndVerifyPayload(msg);
onReceive(payload);

2
dist/api/network/receive.js.map

File diff suppressed because one or more lines are too long

42
dist/api/utils/tribes.js

@ -15,6 +15,7 @@ const LND = require("./lightning");
const path = require("path");
const mqtt = require("mqtt");
const fetch = require("node-fetch");
const models_1 = require("../models");
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env];
let client;
@ -35,6 +36,7 @@ function connect(onMessage) {
client.on('connect', function () {
console.log("[tribes] connected!");
client.subscribe(`${info.identity_pubkey}/#`);
updateTribeStats(info.identity_pubkey);
});
client.on('close', function (e) {
setTimeout(() => reconnect(), 2000);
@ -56,6 +58,22 @@ function connect(onMessage) {
});
}
exports.connect = connect;
function updateTribeStats(myPubkey) {
return __awaiter(this, void 0, void 0, function* () {
const myTribes = yield models_1.models.Chat.findAll({ where: {
ownerPubkey: myPubkey
} });
yield asyncForEach(myTribes, (tribe) => __awaiter(this, void 0, void 0, function* () {
try {
const contactIds = JSON.parse(tribe.contactIds);
const member_count = (contactIds && contactIds.length) || 0;
yield putstats({ uuid: tribe.uuid, host: tribe.host, member_count });
}
catch (e) { }
}));
console.log(`[tribes] updated stats for ${myTribes.length} tribes`);
});
}
function subscribe(topic) {
if (client)
client.subscribe(topic);
@ -113,6 +131,23 @@ function edit({ uuid, host, name, description, tags, img, price_per_message, pri
});
}
exports.edit = edit;
function putstats({ uuid, host, member_count }) {
return __awaiter(this, void 0, void 0, function* () {
try {
const token = yield genSignedTimestamp();
yield fetch('https://' + host + '/tribestats?token=' + token, {
method: 'PUT',
body: JSON.stringify({ uuid, member_count }),
headers: { 'Content-Type': 'application/json' }
});
}
catch (e) {
console.log('[tribes] unauthorized to putstats');
throw e;
}
});
}
exports.putstats = putstats;
function genSignedTimestamp() {
return __awaiter(this, void 0, void 0, function* () {
const now = moment().unix();
@ -147,4 +182,11 @@ exports.getHost = getHost;
function urlBase64(buf) {
return buf.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
}
function asyncForEach(array, callback) {
return __awaiter(this, void 0, void 0, function* () {
for (let index = 0; index < array.length; index++) {
yield callback(array[index], index, array);
}
});
}
//# sourceMappingURL=tribes.js.map

2
dist/api/utils/tribes.js.map

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save