Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Chat/backend/modules/whatsapp/data/groupStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ export const groupStore = {
const db = await load();
if (db[groupId]) {
db[groupId] = { ...db[groupId], ...updates };
await save(db);
}
},

async updateStats(groupId, type) {
await init();
let db = await load();
if (!db[groupId]) {
await this.initGroup(groupId);
db = await load();
}

const group = db[groupId];
if (group) {
if (!group.stats) group.stats = { msg: 0, image: 0, sticker: 0, cmd: 0 };

if (type === 'message') group.stats.msg++;
else if (type === 'image') group.stats.image++;
else if (type === 'sticker') group.stats.sticker++;
else if (type === 'command') group.stats.cmd++;

await save(db);
}
}
Expand Down
7 changes: 3 additions & 4 deletions Chat/backend/modules/whatsapp/data/simpleStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ export const simpleStore = {

async updateStats(userId, type) {
await init();
const db = await load();
let db = await load();
if (!db.users[userId]) {
// Create phantom user if not exists
this.createUser(userId, 'Unknown');
await this.createUser(userId, 'Unknown');
// Reload to get the ref
const reloaded = await load();
if (reloaded.users[userId]) db.users[userId] = reloaded.users[userId];
db = await load();
}

if (db.users[userId]) {
Expand Down
13 changes: 10 additions & 3 deletions Chat/backend/modules/whatsapp/features/activityTracker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { simpleStore } from '../data/simpleStore.js';
import { groupStore } from '../data/groupStore.js';

export async function trackActivity(event) {
try {
const { userId, type, message } = event;
const { userId, groupId, type, message } = event;

let statType = 'message';
if (message.media) statType = 'image'; // Generic media
if (message.media) {
statType = 'image'; // Default media
if (message.media.type === 'sticker') statType = 'sticker';
}
if (message.text && message.text.startsWith('/')) statType = 'command';

// Update User Stats
await simpleStore.updateStats(userId, statType);

// TODO: Update Group Stats (if we add groupStore later)
// Update Group Stats
if (groupId) {
await groupStore.updateStats(groupId, statType);
}

} catch (e) {
console.error('[WA Tracker] Failed to track:', e);
Expand Down