File size: 9,387 Bytes
dab771e
dd7713b
dab771e
 
 
 
 
 
 
 
dd7713b
dab771e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65e041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355ccc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eedf20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab771e
 
 
 
 
 
932a984
 
 
 
 
 
 
 
 
 
 
 
 
 
6de0095
 
 
 
932a984
 
 
 
 
 
 
6de0095
932a984
 
6de0095
 
 
 
 
 
 
 
 
 
 
 
932a984
 
 
 
 
 
 
 
 
 
 
 
6de0095
 
932a984
 
 
 
 
 
 
 
 
 
638d329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0ea521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5f928e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab771e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const express = require('express');
const cors = require("cors");
const { MongoClient } = require('mongodb');
require('dotenv').config(); // Load .env variables

const MONGO_URI = process.env.MONGO_URI;
const DB_NAME = 'finral';
const COLLECTION_NAME = 'json';

const app = express();
app.use(cors());
const PORT = 7860;

// GET /group/:id/users β†’ returns group users
app.get('/group/:id/users', async (req, res) => {
  const groupId = req.params.id;
  const filename = `data/groups/${groupId}.json`;

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    const doc = await collection.findOne({ filename });

    if (!doc) {
      return res.status(404).json({ status: false, message: 'Group not found in MongoDB' });
    }

    const groupData = doc.data;
    return res.json({
      status: true,
      groupId,
      totalUsers: Object.keys(groupData.users || {}).length,
      users: groupData.users || {},
      lastReset: groupData.lastReset || null
    });
  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

// GET /user/:id/pfp β†’ fetch user PFP info from MongoDB (data/user_pfps.json)
app.get('/user/:id/pfp', async (req, res) => {
  const userId = req.params.id;
  const filename = 'data/user_pfps.json';

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    // Find the document for user_pfps
    const doc = await collection.findOne({ filename });

    if (!doc || !doc.data || !doc.data[userId]) {
      return res.status(404).json({ status: false, message: 'User PFP not found' });
    }

    const { shortCode, lastUpdated } = doc.data[userId];

    // Convert shortCode: "00vpom-jpg" β†’ "00vpom.jpg"
    const catboxUrl = `https://files.catbox.moe/${shortCode.replace(/-(\w+)$/, '.$1')}`;

    return res.json({
      status: true,
      userId,
      shortCode,
      lastUpdated,
      catboxUrl
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

app.get('/user/:id/banner', async (req, res) => {
  const userId = req.params.id;
  const filename = 'data/user_banners.json';

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    // Find the document for user_pfps
    const doc = await collection.findOne({ filename });

    if (!doc || !doc.data || !doc.data[userId]) {
      return res.status(404).json({ status: false, message: 'User PFP not found' });
    }

    const { shortCode, lastUpdated } = doc.data[userId];

    // Convert shortCode: "00vpom-jpg" β†’ "00vpom.jpg"
    const catboxUrl = `https://files.catbox.moe/${shortCode.replace(/-(\w+)$/, '.$1')}`;

    return res.json({
      status: true,
      userId,
      shortCode,
      lastUpdated,
      catboxUrl
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

// GET /user/:chatid/nfts β†’ Return all NFTs owned by a user, with holders count
app.get('/user/:chatid/nfts', async (req, res) => {
  const userId = req.params.chatid;
  const filename = 'data/nfts.json';

  const client = new MongoClient(MONGO_URI);

  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    const doc = await collection.findOne({ filename });

    if (!doc || !doc.data) {
      return res.status(404).json({ status: false, message: 'NFT data not found' });
    }

    const nftData = doc.data;
    const userNFTs = nftData[userId];

    if (!userNFTs || userNFTs.length === 0) {
      return res.status(404).json({ status: false, message: 'No NFTs found for this user' });
    }

    // Calculate how many users hold each nftid
    const holdersCount = {};
    for (const uid in nftData) {
      const ownedNFTs = nftData[uid];
      for (const nft of ownedNFTs) {
        holdersCount[nft.nftid] = (holdersCount[nft.nftid] || 0) + 1;
      }
    }

    // Append holders count to each NFT the user owns
    const enrichedNFTs = userNFTs.map(nft => ({
      ...nft,
      holders: holdersCount[nft.nftid] || 1
    }));

    return res.json({
      status: true,
      userId,
      totalNFTs: enrichedNFTs.length,
      nfts: enrichedNFTs
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

// βœ… NEW: GET /user/:id/info β†’ Get user's global info across all groups
app.get('/user/:id/info', async (req, res) => {
  const userId = req.params.id;

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    const groupDocs = await collection.find({ filename: { $regex: '^data/groups/' } }).toArray();

    let totalXP = 0;
    let totalLevel = 0;
    let totalRoyalties = 0;
    let totalDailyMessages = 0;

    const groups = [];

    for (const doc of groupDocs) {
      const groupId = doc.filename.replace('data/groups/', '').replace('.json', '');
      const groupUsers = doc.data.users || {};

      if (groupUsers[userId]) {
        const userInfo = groupUsers[userId];

        totalXP += userInfo.xp || 0;
        totalLevel += userInfo.level || 0;
        totalRoyalties += userInfo.royalties || 0;
        totalDailyMessages += userInfo.dailyMessages || 0;

        groups.push({
          groupId,
          fullName: userInfo.fullName || null,
          username: userInfo.username || null,
          level: userInfo.level || 0,
          xp: userInfo.xp || 0,
          royalties: userInfo.royalties || 0,
          dailyMessages: userInfo.dailyMessages || 0
        });
      }
    }

    if (groups.length === 0) {
      return res.status(404).json({ status: false, message: 'User not found in any group' });
    }

    return res.json({
      status: true,
      userId,
      totalXP,
      totalLevel,
      totalRoyalties,
      totalDailyMessages,
      groups
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

app.get('/user/:id/theme', async (req, res) => {
  const userId = req.params.id;
  const filename = 'data/themes.json';

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    // Find the document for themes.json
    const doc = await collection.findOne({ filename });

    if (!doc || !doc.data || !doc.data[userId]) {
      return res.status(404).json({ status: false, message: 'User theme not found' });
    }

    const { theme, lastUpdated } = doc.data[userId];

    return res.json({
      status: true,
      userId,
      theme,
      lastUpdated
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

app.get('/user/:id/mark', async (req, res) => {
  const userId = req.params.id;
  const filename = 'data/marks.json';

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    // Find the document for marks.json
    const doc = await collection.findOne({ filename });

    if (!doc || !doc.data || !doc.data[userId]) {
      return res.status(404).json({ status: false, message: 'User mark not found' });
    }

    const { mark, url, lastUpdated } = doc.data[userId];

    return res.json({
      status: true,
      userId,
      mark,
      url,
      lastUpdated
    });

  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

// GET /group/:id/meta β†’ returns group metadata from MongoDB (names.json)
app.get('/group/:id/meta', async (req, res) => {
  const groupId = req.params.id;
  if (!groupId) {
    return res.status(400).json({ status: false, message: 'Group ID required' });
  }

  const client = new MongoClient(MONGO_URI);
  try {
    await client.connect();
    const db = client.db(DB_NAME);
    const collection = db.collection(COLLECTION_NAME);

    const doc = await collection.findOne({ filename: 'data/names.json' });
    if (!doc || !doc.data || !doc.data[groupId]) {
      return res.status(404).json({ status: false, message: 'Group metadata not found' });
    }

    const group = doc.data[groupId];
    return res.json({
      status: true,
      groupId,
      groupName: group.groupName,
      groupPic: group.groupPic || null,
      inviteLink: group.inviteLink || null,
      memberCount: group.memberCount || null,
      admins: group.admins || []
    });
  } catch (err) {
    return res.status(500).json({ status: false, error: err.message });
  } finally {
    await client.close();
  }
});

app.listen(PORT, () => {
  console.log(`Express server running at http://localhost:${PORT}`);
});