Mark-Lasfar commited on
Commit ·
8cf3d33
1
Parent(s): dd97a3d
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18410,7 +18410,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18410 |
// ============================
|
| 18411 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18412 |
.sort({ viewedAt: -1 })
|
| 18413 |
-
.limit(20);
|
| 18414 |
|
| 18415 |
// ============================
|
| 18416 |
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
|
@@ -18498,7 +18498,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18498 |
}
|
| 18499 |
|
| 18500 |
// ============================
|
| 18501 |
-
// ✅ 5. تنسيق النتيجة النهائية
|
| 18502 |
// ============================
|
| 18503 |
let formattedViewers = allViewers.map(item => {
|
| 18504 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
|
@@ -18518,7 +18518,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18518 |
// ✅ زائر (غير مسجل)
|
| 18519 |
return {
|
| 18520 |
_id: null,
|
| 18521 |
-
name:
|
| 18522 |
nickname: null,
|
| 18523 |
username: null,
|
| 18524 |
avatar: '/assets/img/default-avatar.png',
|
|
@@ -18540,14 +18540,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18540 |
}
|
| 18541 |
|
| 18542 |
// ============================
|
| 18543 |
-
// ✅ 7.
|
| 18544 |
-
// ============================
|
| 18545 |
-
const totalViewers = formattedViewers.length;
|
| 18546 |
-
const registeredCount = formattedViewers.filter(v => !v.isGuest).length;
|
| 18547 |
-
const guestCount = formattedViewers.filter(v => v.isGuest).length;
|
| 18548 |
-
|
| 18549 |
-
// ============================
|
| 18550 |
-
// ✅ 8. خلط عشوائي (يتغير مع كل ريلود)
|
| 18551 |
// ============================
|
| 18552 |
function shuffleArray(array) {
|
| 18553 |
const shuffled = [...array];
|
|
@@ -18558,18 +18551,11 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18558 |
return shuffled;
|
| 18559 |
}
|
| 18560 |
|
|
|
|
| 18561 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18562 |
|
| 18563 |
-
//
|
| 18564 |
-
|
| 18565 |
-
// ============================
|
| 18566 |
-
res.json({
|
| 18567 |
-
data: shuffledViewers.slice(0, 5),
|
| 18568 |
-
total: totalViewers,
|
| 18569 |
-
registered: registeredCount,
|
| 18570 |
-
guests: guestCount,
|
| 18571 |
-
lastUpdated: new Date().toISOString()
|
| 18572 |
-
});
|
| 18573 |
|
| 18574 |
} catch (error) {
|
| 18575 |
logger.error(`Error fetching also viewed: ${error.message}`);
|
|
|
|
| 18410 |
// ============================
|
| 18411 |
const alsoViewed = await AlsoViewed.find({ targetUserId: userId })
|
| 18412 |
.sort({ viewedAt: -1 })
|
| 18413 |
+
.limit(20); // ✅ زودنا الـ limit عشان نجيب أكثر
|
| 18414 |
|
| 18415 |
// ============================
|
| 18416 |
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
|
|
|
| 18498 |
}
|
| 18499 |
|
| 18500 |
// ============================
|
| 18501 |
+
// ✅ 5. تنسيق النتيجة النهائية (مع التفرقة بين المالك والزائر)
|
| 18502 |
// ============================
|
| 18503 |
let formattedViewers = allViewers.map(item => {
|
| 18504 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
|
|
|
| 18518 |
// ✅ زائر (غير مسجل)
|
| 18519 |
return {
|
| 18520 |
_id: null,
|
| 18521 |
+
name: `Guest 🌐`,
|
| 18522 |
nickname: null,
|
| 18523 |
username: null,
|
| 18524 |
avatar: '/assets/img/default-avatar.png',
|
|
|
|
| 18540 |
}
|
| 18541 |
|
| 18542 |
// ============================
|
| 18543 |
+
// ✅ 7. خلط عشوائي (يتغير مع كل ريلود)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18544 |
// ============================
|
| 18545 |
function shuffleArray(array) {
|
| 18546 |
const shuffled = [...array];
|
|
|
|
| 18551 |
return shuffled;
|
| 18552 |
}
|
| 18553 |
|
| 18554 |
+
// ✅ خلط القائمة عشوائياً
|
| 18555 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18556 |
|
| 18557 |
+
// ✅ خذ أول 5 فقط
|
| 18558 |
+
res.json(shuffledViewers.slice(0, 5));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18559 |
|
| 18560 |
} catch (error) {
|
| 18561 |
logger.error(`Error fetching also viewed: ${error.message}`);
|