Browse Source

Merge pull request #79 from stakwork/botcommands

Botcommands
push-params
Evan Feenstra 4 years ago
committed by GitHub
parent
commit
a08a827f30
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      api/controllers/chats.ts
  2. 65
      api/utils/tribes.ts
  3. 2
      dist/api/controllers/chats.js
  4. 2
      dist/api/controllers/chats.js.map
  5. 55
      dist/api/utils/tribes.js
  6. 2
      dist/api/utils/tribes.js.map

2
api/controllers/chats.ts

@ -375,6 +375,7 @@ export async function receiveGroupJoin(payload) {
})
replayChatHistory(chat, theSender)
tribes.putstats({
chatId: chat.id,
uuid: chat.uuid,
host: chat.host,
member_count: contactIds.length,
@ -431,6 +432,7 @@ export async function receiveGroupLeave(payload) {
await models.ChatMember.destroy({where:{chatId: chat.id, contactId: sender.id}})
} catch(e) {}
tribes.putstats({
chatId: chat.id,
uuid: chat.uuid,
host: chat.host,
member_count: contactIds.length,

65
api/utils/tribes.ts

@ -50,14 +50,16 @@ export async function connect(onMessage) {
}
async function updateTribeStats(myPubkey) {
const myTribes = await models.Chat.findAll({where:{
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})
await putstats({ uuid: tribe.uuid, host: tribe.host, member_count, chatId: tribe.id })
} catch (e) { }
})
console.log(`[tribes] updated stats for ${myTribes.length} tribes`)
@ -176,12 +178,65 @@ export async function putActivity( uuid:string, host:string ) {
}
}
export async function putstats({ uuid, host, member_count }) {
async function makeBotsJSON(tribeID) {
const bots = await models.ChatBot.findAll({
where: {
chatId: tribeID
}
})
if (!bots) return []
if (!bots.length) return []
return bots.map(b => {
const bot = b.dataValues
if (bot.botPrefix === '/loopout') {
return loopoutBotJSON()
}
return <BotJSON>{
prefix: bot.botPrefix,
price: bot.pricePerUse||0,
commands: null,
}
})
}
interface BotJSON {
prefix: string,
price: number,
commands: BotCommand[] | null,
}
interface BotCommand {
command: string,
price: number,
min_price: number,
max_price: number,
price_index: number,
admin_only: boolean,
}
function loopoutBotJSON(): BotJSON {
return <BotJSON>{
prefix: '/loopout',
price: 0,
commands: [{
command: '*',
price: 0,
min_price: 250000,
max_price: 16777215,
price_index: 2,
admin_only: false
}]
}
}
export async function putstats({ uuid, host, member_count, chatId }) {
if (!uuid) return
const bots = await makeBotsJSON(chatId)
try {
const token = await genSignedTimestamp()
await fetch('https://' + host + '/tribestats?token=' + token, {
method: 'PUT',
body: JSON.stringify({uuid, member_count}),
body: JSON.stringify({
uuid, member_count, bots: JSON.stringify(bots || [])
}),
headers: { 'Content-Type': 'application/json' }
})
} catch (e) {

2
dist/api/controllers/chats.js

@ -367,6 +367,7 @@ function receiveGroupJoin(payload) {
});
chatTribes_1.replayChatHistory(chat, theSender);
tribes.putstats({
chatId: chat.id,
uuid: chat.uuid,
host: chat.host,
member_count: contactIds.length,
@ -421,6 +422,7 @@ function receiveGroupLeave(payload) {
}
catch (e) { }
tribes.putstats({
chatId: chat.id,
uuid: chat.uuid,
host: chat.host,
member_count: contactIds.length,

2
dist/api/controllers/chats.js.map

File diff suppressed because one or more lines are too long

55
dist/api/utils/tribes.js

@ -64,14 +64,16 @@ 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: {
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 });
yield putstats({ uuid: tribe.uuid, host: tribe.host, member_count, chatId: tribe.id });
}
catch (e) { }
}));
@ -209,13 +211,56 @@ function putActivity(uuid, host) {
});
}
exports.putActivity = putActivity;
function putstats({ uuid, host, member_count }) {
function makeBotsJSON(tribeID) {
return __awaiter(this, void 0, void 0, function* () {
const bots = yield models_1.models.ChatBot.findAll({
where: {
chatId: tribeID
}
});
if (!bots)
return [];
if (!bots.length)
return [];
return bots.map(b => {
const bot = b.dataValues;
if (bot.botPrefix === '/loopout') {
return loopoutBotJSON();
}
return {
prefix: bot.botPrefix,
price: bot.pricePerUse || 0,
commands: null,
};
});
});
}
function loopoutBotJSON() {
return {
prefix: '/loopout',
price: 0,
commands: [{
command: '*',
price: 0,
min_price: 250000,
max_price: 16777215,
price_index: 2,
admin_only: false
}]
};
}
function putstats({ uuid, host, member_count, chatId }) {
return __awaiter(this, void 0, void 0, function* () {
if (!uuid)
return;
const bots = yield makeBotsJSON(chatId);
try {
const token = yield genSignedTimestamp();
yield node_fetch_1.default('https://' + host + '/tribestats?token=' + token, {
method: 'PUT',
body: JSON.stringify({ uuid, member_count }),
body: JSON.stringify({
uuid, member_count, bots: JSON.stringify(bots || [])
}),
headers: { 'Content-Type': 'application/json' }
});
}

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

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