Mark-Lasfar commited on
Commit ·
9fc4d6e
1
Parent(s): 33913a2
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18418,42 +18418,25 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18418 |
|
| 18419 |
if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
|
| 18420 |
const views = analytics.profileViews;
|
| 18421 |
-
const viewerMap = new Map();
|
| 18422 |
|
| 18423 |
-
|
| 18424 |
-
|
| 18425 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18426 |
|
| 18427 |
-
|
| 18428 |
-
|
| 18429 |
-
|
| 18430 |
-
|
| 18431 |
-
|
| 18432 |
-
|
| 18433 |
-
//
|
| 18434 |
-
|
| 18435 |
-
}
|
| 18436 |
-
|
| 18437 |
-
if (!viewerMap.has(key)) {
|
| 18438 |
-
viewerMap.set(key, {
|
| 18439 |
-
viewerId: view.viewerId || null,
|
| 18440 |
-
viewerIp: view.viewerIp || null,
|
| 18441 |
-
viewedAt: view.viewedAt,
|
| 18442 |
-
count: 1,
|
| 18443 |
-
isLegacy: !view.viewerId && !view.viewerIp
|
| 18444 |
-
});
|
| 18445 |
-
} else {
|
| 18446 |
-
const existing = viewerMap.get(key);
|
| 18447 |
-
if (view.viewedAt > existing.viewedAt) {
|
| 18448 |
-
existing.viewedAt = view.viewedAt;
|
| 18449 |
-
}
|
| 18450 |
-
existing.count++;
|
| 18451 |
-
}
|
| 18452 |
-
});
|
| 18453 |
-
|
| 18454 |
-
historicalViews = Array.from(viewerMap.values())
|
| 18455 |
-
.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt))
|
| 18456 |
-
.slice(0, 20);
|
| 18457 |
}
|
| 18458 |
|
| 18459 |
// ============================
|
|
@@ -18462,6 +18445,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18462 |
const allViewers = [];
|
| 18463 |
const processedIds = new Set();
|
| 18464 |
|
|
|
|
| 18465 |
for (const item of alsoViewed) {
|
| 18466 |
let key = null;
|
| 18467 |
if (item.viewerId) {
|
|
@@ -18478,32 +18462,30 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18478 |
viewerId: item.viewerId || null,
|
| 18479 |
viewerIp: item.viewerIp || null,
|
| 18480 |
viewedAt: item.viewedAt,
|
| 18481 |
-
source: 'alsoViewed'
|
|
|
|
| 18482 |
});
|
| 18483 |
}
|
| 18484 |
}
|
| 18485 |
|
|
|
|
| 18486 |
for (const item of historicalViews) {
|
| 18487 |
-
|
| 18488 |
-
|
| 18489 |
-
key = item.viewerId.toString();
|
| 18490 |
-
} else if (item.viewerIp) {
|
| 18491 |
-
key = item.viewerIp;
|
| 18492 |
-
} else {
|
| 18493 |
-
key = `legacy_${Math.random().toString(36).substring(7)}`;
|
| 18494 |
-
}
|
| 18495 |
|
| 18496 |
if (!processedIds.has(key)) {
|
| 18497 |
processedIds.add(key);
|
| 18498 |
allViewers.push({
|
| 18499 |
-
viewerId:
|
| 18500 |
-
viewerIp:
|
| 18501 |
viewedAt: item.viewedAt,
|
| 18502 |
-
source: 'historical'
|
|
|
|
| 18503 |
});
|
| 18504 |
}
|
| 18505 |
}
|
| 18506 |
|
|
|
|
| 18507 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18508 |
|
| 18509 |
// ============================
|
|
@@ -18527,7 +18509,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18527 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18528 |
// ============================
|
| 18529 |
let formattedViewers = allViewers.map(item => {
|
| 18530 |
-
// ✅
|
| 18531 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18532 |
const user = userMap[item.viewerId.toString()];
|
| 18533 |
return {
|
|
@@ -18543,7 +18525,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18543 |
};
|
| 18544 |
}
|
| 18545 |
|
| 18546 |
-
// ✅
|
| 18547 |
if (item.viewerId) {
|
| 18548 |
return {
|
| 18549 |
_id: item.viewerId,
|
|
@@ -18558,25 +18540,17 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18558 |
};
|
| 18559 |
}
|
| 18560 |
|
| 18561 |
-
// ✅ زائر (
|
| 18562 |
-
let displayName = 'Guest 🌐';
|
| 18563 |
-
let viewerIp = item.viewerIp || 'Unknown';
|
| 18564 |
-
|
| 18565 |
-
if (!item.viewerId && !item.viewerIp) {
|
| 18566 |
-
displayName = 'Guest (Legacy)';
|
| 18567 |
-
viewerIp = 'legacy';
|
| 18568 |
-
}
|
| 18569 |
-
|
| 18570 |
return {
|
| 18571 |
_id: null,
|
| 18572 |
-
name:
|
| 18573 |
nickname: null,
|
| 18574 |
username: null,
|
| 18575 |
avatar: '/assets/img/default-avatar.png',
|
| 18576 |
title: 'Guest Visitor',
|
| 18577 |
viewedAt: item.viewedAt,
|
| 18578 |
isGuest: true,
|
| 18579 |
-
viewerIp: viewerIp,
|
| 18580 |
source: item.source
|
| 18581 |
};
|
| 18582 |
});
|
|
|
|
| 18418 |
|
| 18419 |
if (analytics && analytics.profileViews && analytics.profileViews.length > 0) {
|
| 18420 |
const views = analytics.profileViews;
|
|
|
|
| 18421 |
|
| 18422 |
+
// ✅ إذا كانت المشاهدات بدون viewerId ولا viewerIp
|
| 18423 |
+
// نعتبر كل مشاهدة فريدة (لأننا لا نستطيع تمييزها)
|
| 18424 |
+
if (views.length > 0) {
|
| 18425 |
+
// ✅ نأخذ آخر 20 مشاهدة
|
| 18426 |
+
const sortedViews = [...views].sort((a, b) =>
|
| 18427 |
+
new Date(b.viewedAt) - new Date(a.viewedAt)
|
| 18428 |
+
).slice(0, 20);
|
| 18429 |
|
| 18430 |
+
historicalViews = sortedViews.map((view, index) => ({
|
| 18431 |
+
viewerId: null,
|
| 18432 |
+
viewerIp: null,
|
| 18433 |
+
viewedAt: view.viewedAt || new Date(),
|
| 18434 |
+
count: 1,
|
| 18435 |
+
isLegacy: true,
|
| 18436 |
+
// ✅ مفتاح فريد لكل مشاهدة
|
| 18437 |
+
uniqueKey: `legacy_${index}_${view._id || Date.now()}_${Math.random()}`
|
| 18438 |
+
}));
|
| 18439 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18440 |
}
|
| 18441 |
|
| 18442 |
// ============================
|
|
|
|
| 18445 |
const allViewers = [];
|
| 18446 |
const processedIds = new Set();
|
| 18447 |
|
| 18448 |
+
// ✅ أضف المشاهدات من AlsoViewed
|
| 18449 |
for (const item of alsoViewed) {
|
| 18450 |
let key = null;
|
| 18451 |
if (item.viewerId) {
|
|
|
|
| 18462 |
viewerId: item.viewerId || null,
|
| 18463 |
viewerIp: item.viewerIp || null,
|
| 18464 |
viewedAt: item.viewedAt,
|
| 18465 |
+
source: 'alsoViewed',
|
| 18466 |
+
isLegacy: false
|
| 18467 |
});
|
| 18468 |
}
|
| 18469 |
}
|
| 18470 |
|
| 18471 |
+
// ✅ أضف المشاهدات التاريخية
|
| 18472 |
for (const item of historicalViews) {
|
| 18473 |
+
// ✅ استخدام uniqueKey كمفتاح فريد
|
| 18474 |
+
const key = item.uniqueKey || `legacy_${Math.random().toString(36).substring(7)}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18475 |
|
| 18476 |
if (!processedIds.has(key)) {
|
| 18477 |
processedIds.add(key);
|
| 18478 |
allViewers.push({
|
| 18479 |
+
viewerId: null,
|
| 18480 |
+
viewerIp: null,
|
| 18481 |
viewedAt: item.viewedAt,
|
| 18482 |
+
source: 'historical',
|
| 18483 |
+
isLegacy: true
|
| 18484 |
});
|
| 18485 |
}
|
| 18486 |
}
|
| 18487 |
|
| 18488 |
+
// ✅ ترتيب حسب الأحدث
|
| 18489 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18490 |
|
| 18491 |
// ============================
|
|
|
|
| 18509 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18510 |
// ============================
|
| 18511 |
let formattedViewers = allViewers.map(item => {
|
| 18512 |
+
// ✅ مستخدم مسجل
|
| 18513 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18514 |
const user = userMap[item.viewerId.toString()];
|
| 18515 |
return {
|
|
|
|
| 18525 |
};
|
| 18526 |
}
|
| 18527 |
|
| 18528 |
+
// ✅ مستخدم مسجل ولكن لم يتم جلب بياناته
|
| 18529 |
if (item.viewerId) {
|
| 18530 |
return {
|
| 18531 |
_id: item.viewerId,
|
|
|
|
| 18540 |
};
|
| 18541 |
}
|
| 18542 |
|
| 18543 |
+
// ✅ زائر (ليجاسي أو عادي)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18544 |
return {
|
| 18545 |
_id: null,
|
| 18546 |
+
name: item.isLegacy ? 'Guest (Legacy)' : 'Guest 🌐',
|
| 18547 |
nickname: null,
|
| 18548 |
username: null,
|
| 18549 |
avatar: '/assets/img/default-avatar.png',
|
| 18550 |
title: 'Guest Visitor',
|
| 18551 |
viewedAt: item.viewedAt,
|
| 18552 |
isGuest: true,
|
| 18553 |
+
viewerIp: item.viewerIp || 'legacy',
|
| 18554 |
source: item.source
|
| 18555 |
};
|
| 18556 |
});
|