nodebb-plugin-niki-loyalty 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/library.js +168 -54
  2. package/package.json +1 -1
  3. package/plugin.json +8 -28
package/library.js CHANGED
@@ -1,11 +1,10 @@
1
- 'use strict';
2
-
3
1
  const db = require.main.require('./src/database');
4
2
  const user = require.main.require('./src/user');
5
3
  const posts = require.main.require('./src/posts');
6
4
  const routeHelpers = require.main.require('./src/controllers/helpers');
7
5
  const nconf = require.main.require('nconf');
8
6
  const socketHelpers = require.main.require('./src/socket.io/index');
7
+ const SocketPlugins = require.main.require('./src/socket.io/plugins');
9
8
  const Plugin = {};
10
9
 
11
10
  // =========================
@@ -17,12 +16,12 @@ const SETTINGS = {
17
16
 
18
17
  // Puan Tablosu ve Limitleri
19
18
  const ACTIONS = {
20
- login: { points: 5, limit: 1, name: 'Günlük Giriş 👋' }, // 5 puan
21
- new_topic: { points: 5, limit: 1, name: 'Yeni Konu 📝' }, // 5 puan
22
- reply: { points: 3, limit: 2, name: 'Yorum Yazma 💬' }, // 3 x 2 = 6 puan
23
- read: { points: 1, limit: 8, name: 'Konu Okuma 👀' }, // Heartbeat ile çalışır
24
- like_given: { points: 4, limit: 2, name: 'Beğeni Atma ❤️' }, // 4 x 2 = 8 puan
25
- like_taken: { points: 5, limit: 2, name: 'Beğeni Alma 🌟' } // 5 x 2 = 10 puan
19
+ login: { points: 2, limit: 1, name: 'Günlük Giriş 👋' },
20
+ new_topic: { points: 7, limit: 1, name: 'Yeni Konu 📝' },
21
+ reply: { points: 3.5, limit: 2, name: 'Yorum Yazma 💬' },
22
+ read: { points: 1, limit: 8, name: 'Konu Okuma 👀' }, // Heartbeat ile çalışır
23
+ like_given: { points: 4, limit: 2, name: 'Beğeni Atma ❤️' }, // 4 puan x 2 = max 8
24
+ like_taken: { points: 5, limit: 2, name: 'Beğeni Alma 🌟' } // 5 puan x 2 = max 10
26
25
  };
27
26
 
28
27
  // Ödüller
@@ -356,51 +355,6 @@ Plugin.init = async function (params) {
356
355
  if (!isStaff) return res.render('403', {});
357
356
  return res.render('niki-kasa', { title: 'Niki Kasa' });
358
357
  });
359
-
360
- // 7) (niki-admin artık Custom Page ile yönetiliyor, route kaldırıldı)
361
-
362
- // 8) ADMIN API - TÜM KULLANICILARIN PUANLARI
363
- router.get('/api/niki-loyalty/admin/users', middleware.ensureLoggedIn, async (req, res) => {
364
- try {
365
- console.log('[Niki-Loyalty] Admin API called, uid:', req.uid);
366
-
367
- const isAdmin = await user.isAdministrator(req.uid);
368
- const isMod = await user.isGlobalModerator(req.uid);
369
-
370
- console.log('[Niki-Loyalty] isAdmin:', isAdmin, 'isMod:', isMod);
371
-
372
- if (!isAdmin && !isMod) {
373
- console.log('[Niki-Loyalty] Access denied for uid:', req.uid);
374
- return res.status(403).json({ error: 'Yetkisiz' });
375
- }
376
-
377
- // Tüm kullanıcıları al (limit 500)
378
- const uids = await db.getSortedSetRange('users:joindate', 0, 499);
379
- if (!uids || uids.length === 0) return res.json([]);
380
-
381
- // Kullanıcı bilgilerini al
382
- const usersData = await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'niki_points', 'icon:bgColor']);
383
-
384
- // Puanları olan kullanıcıları filtrele ve sırala
385
- const result = usersData
386
- .map(u => ({
387
- uid: u.uid,
388
- username: u.username,
389
- userslug: u.userslug,
390
- picture: u.picture || '',
391
- iconBg: u['icon:bgColor'] || '#4b5563',
392
- points: parseFloat(u.niki_points || 0)
393
- }))
394
- .filter(u => u.points > 0) // Sadece puanı olanlar
395
- .sort((a, b) => b.points - a.points); // Yüksekten düşüğe sırala
396
-
397
- console.log('[Niki-Loyalty] Returning', result.length, 'users');
398
- return res.json(result);
399
- } catch (err) {
400
- console.error('[Niki-Loyalty] Admin users error:', err);
401
- return res.status(500).json({ error: 'Sunucu hatası' });
402
- }
403
- });
404
358
  };
405
359
 
406
360
  Plugin.addScripts = async function (scripts) {
@@ -419,5 +373,165 @@ Plugin.addNavigation = async function (nav) {
419
373
  return nav;
420
374
  };
421
375
 
422
- // Widget fonksiyonları kaldırıldı
376
+ // =========================
377
+ // 🔌 SOCKET IO FONKSİYONLARI
378
+ // =========================
379
+ Plugin.adminGetUsers = async function (socket, data) {
380
+ // Yetki Kontrolü
381
+ const uid = socket.uid;
382
+ if (!uid) throw new Error('Giriş yapmalısınız.');
383
+
384
+ const isAdmin = await user.isAdministrator(uid);
385
+ const isMod = await user.isGlobalModerator(uid);
386
+
387
+ if (!isAdmin && !isMod) throw new Error('Yetkisiz Erişim');
388
+
389
+ // Tüm kullanıcıları al (limit 500)
390
+ const uids = await db.getSortedSetRange('users:joindate', 0, 499);
391
+ if (!uids || uids.length === 0) return [];
392
+
393
+ // Kullanıcı bilgilerini al
394
+ const usersData = await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'niki_points', 'icon:bgColor']);
395
+
396
+ // Puanları olan kullanıcıları filtrele ve sırala
397
+ const result = usersData
398
+ .map(u => ({
399
+ uid: u.uid,
400
+ username: u.username,
401
+ userslug: u.userslug,
402
+ picture: u.picture || '',
403
+ iconBg: u['icon:bgColor'] || '#4b5563',
404
+ points: parseFloat(u.niki_points || 0)
405
+ }))
406
+ .filter(u => u.points > 0) // Sadece puanı olanlar
407
+ .sort((a, b) => b.points - a.points); // Yüksekten düşüğe sırala
408
+
409
+ return result;
410
+ };
411
+
412
+ // Soket'e kaydet (Client: socket.emit('plugins.niki.getUsers', ...))
413
+ SocketPlugins.niki = {
414
+ getUsers: Plugin.adminGetUsers,
415
+ scanQR: Plugin.socketScanQR,
416
+ getKasaHistory: Plugin.socketKasaHistory,
417
+ managePoints: Plugin.adminManagePoints
418
+ };
419
+
420
+ // =========================
421
+ // 🔌 YENİ SOCKET FONKSİYONLARI (POS & ADMIN)
422
+ // =========================
423
+
424
+ // 1) QR SCAN (Socket Versiyonu)
425
+ Plugin.socketScanQR = async function (socket, data) {
426
+ const uid = socket.uid;
427
+ if (!uid) throw new Error('Giriş yapmalısınız.');
428
+
429
+ const isAdmin = await user.isAdministrator(uid);
430
+ const isMod = await user.isGlobalModerator(uid);
431
+ if (!isAdmin && !isMod) throw new Error('Yetkisiz Erişim');
432
+
433
+ const token = data.token;
434
+ if (!token) throw new Error('Geçersiz Token');
435
+
436
+ const custUid = await db.get(`niki:qr:${token}`);
437
+ if (!custUid) throw new Error('QR Kod Geçersiz veya Süresi Dolmuş');
438
+
439
+ const pts = parseFloat((await user.getUserField(custUid, 'niki_points')) || 0);
440
+
441
+ let selectedReward = null;
442
+ if (!TEST_MODE_UNLIMITED) {
443
+ for (const r of REWARDS) {
444
+ if (pts >= r.cost) { selectedReward = r; break; }
445
+ }
446
+ if (!selectedReward) throw new Error('Puan Yetersiz');
447
+ } else { selectedReward = REWARDS[0]; }
448
+
449
+ if (!TEST_MODE_UNLIMITED) {
450
+ await user.decrementUserFieldBy(custUid, 'niki_points', selectedReward.cost);
451
+ }
452
+ await db.delete(`niki:qr:${token}`);
453
+
454
+ const cData = await user.getUserFields(custUid, ['username', 'picture', 'userslug']);
455
+ await addUserLog(custUid, 'spend', selectedReward.cost, selectedReward.name);
456
+ await addKasaLog(uid, cData.username, custUid, selectedReward.name, selectedReward.cost);
457
+
458
+ return { success: true, customer: cData, rewardName: selectedReward.name, cost: selectedReward.cost };
459
+ };
460
+
461
+ // 2) KASA HISTORY (Socket Versiyonu)
462
+ Plugin.socketKasaHistory = async function (socket, data) {
463
+ const uid = socket.uid;
464
+ if (!uid) throw new Error('Giriş yapmalısınız.');
465
+
466
+ const isAdmin = await user.isAdministrator(uid);
467
+ const isMod = await user.isGlobalModerator(uid);
468
+ if (!isAdmin && !isMod) throw new Error('Yetkisiz Erişim');
469
+
470
+ const raw = await db.getListRange('niki:kasa:history', 0, -1);
471
+ const rows = (raw || []).map(safeParseMaybeJson).filter(Boolean).reverse();
472
+
473
+ const uids = rows.map(r => parseInt(r.cuid, 10)).filter(n => Number.isFinite(n) && n > 0);
474
+ const users = await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'icon:bgColor']);
475
+ const userMap = {};
476
+ (users || []).forEach(u => userMap[u.uid] = u);
477
+
478
+ const rp = nconf.get('relative_path') || '';
479
+ return rows.map(r => {
480
+ const u = userMap[r.cuid] || {};
481
+ return {
482
+ ...r,
483
+ cust: u.username || r.cust || 'Bilinmeyen',
484
+ picture: u.picture || '',
485
+ iconBg: u['icon:bgColor'] || '#4b5563',
486
+ profileUrl: u.userslug ? `${rp}/user/${u.userslug}` : '',
487
+ reward: r.reward || 'İşlem'
488
+ };
489
+ });
490
+ };
491
+
492
+ // 3) MANUEL PUAN YÖNETİMİ (Güvenli)
493
+ Plugin.adminManagePoints = async function (socket, data) {
494
+ // data = { targetUid, action: 'add'|'remove', amount, reason }
495
+ const uid = socket.uid;
496
+ if (!uid) throw new Error('Giriş yapmalısınız.');
497
+
498
+ // KESİN YETKİ KONTROLÜ (Sadece Administrator)
499
+ const isAdmin = await user.isAdministrator(uid);
500
+ if (!isAdmin) {
501
+ console.warn(`[NIKI SECURITY] Yetkisiz puan değiştirme denemesi! Actor: ${uid}`);
502
+ throw new Error('BU İŞLEM İÇİN YETKİNİZ YOK! (Olay Loglandı)');
503
+ }
504
+
505
+ const targetUid = data.targetUid;
506
+ const amount = Math.abs(parseFloat(data.amount));
507
+ const action = data.action;
508
+ const reason = data.reason || 'Manuel Düzenleme';
509
+
510
+ if (!targetUid || !amount || amount <= 0) throw new Error('Geçersiz veri.');
511
+
512
+ const exists = await user.exists(targetUid);
513
+ if (!exists) throw new Error('Kullanıcı bulunamadı.');
514
+
515
+ if (action === 'add') {
516
+ await user.incrementUserFieldBy(targetUid, 'niki_points', amount);
517
+ } else if (action === 'remove') {
518
+ await user.decrementUserFieldBy(targetUid, 'niki_points', amount);
519
+ } else {
520
+ throw new Error('Geçersiz işlem türü.');
521
+ }
522
+
523
+ // GÜVENLİK LOGU
524
+ const adminUserData = await user.getUserFields(uid, ['username']);
525
+ const logMsg = `Admin (${adminUserData.username}) tarafından ${action === 'add' ? '+' : '-'}${amount} puan. Sebep: ${reason}`;
526
+
527
+ await addUserLog(targetUid, 'admin_adjust', amount, logMsg);
528
+
529
+ // Denetim Logu
530
+ const auditLog = { ts: Date.now(), adminUid: uid, adminName: adminUserData.username, targetUid: targetUid, action: action, amount: amount, reason: reason };
531
+ await db.listAppend('niki:audit:admin_points', JSON.stringify(auditLog));
532
+
533
+ const newPoints = await user.getUserField(targetUid, 'niki_points');
534
+ return { success: true, newPoints: parseFloat(newPoints) };
535
+ };
536
+
423
537
  module.exports = Plugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-niki-loyalty",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "Niki The Cat Coffee Loyalty System - Earn points while studying on IEU Forum.",
5
5
  "main": "library.js",
6
6
  "nbbpm": {
package/plugin.json CHANGED
@@ -5,34 +5,14 @@
5
5
  "url": "https://forum.ieu.app",
6
6
  "library": "./library.js",
7
7
  "hooks": [
8
- {
9
- "hook": "static:app.load",
10
- "method": "init"
11
- },
12
- {
13
- "hook": "filter:navigation.available",
14
- "method": "addNavigation"
15
- },
16
- {
17
- "hook": "filter:scripts.get",
18
- "method": "addScripts"
19
- },
20
- {
21
- "hook": "action:user.loggedIn",
22
- "method": "onLogin"
23
- },
24
- {
25
- "hook": "action:topic.save",
26
- "method": "onTopicCreate"
27
- },
28
- {
29
- "hook": "action:post.save",
30
- "method": "onPostCreate"
31
- },
32
- {
33
- "hook": "action:post.upvote",
34
- "method": "onUpvote"
35
- }
8
+ { "hook": "static:app.load", "method": "init" },
9
+ { "hook": "filter:navigation.available", "method": "addNavigation" },
10
+ { "hook": "filter:scripts.get", "method": "addScripts" },
11
+
12
+ { "hook": "action:user.loggedIn", "method": "onLogin" },
13
+ { "hook": "action:topic.save", "method": "onTopicCreate" },
14
+ { "hook": "action:post.save", "method": "onPostCreate" },
15
+ { "hook": "action:post.upvote", "method": "onUpvote" }
36
16
  ],
37
17
  "staticDirs": {
38
18
  "static": "./static"