Mark-Lasfar commited on
Commit ·
dd97a3d
1
Parent(s): 2ca78f6
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18413,7 +18413,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18413 |
.limit(20);
|
| 18414 |
|
| 18415 |
// ============================
|
| 18416 |
-
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة
|
| 18417 |
// ============================
|
| 18418 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18419 |
let historicalViews = [];
|
|
@@ -18423,28 +18423,13 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18423 |
const viewerMap = new Map();
|
| 18424 |
|
| 18425 |
views.forEach(view => {
|
| 18426 |
-
|
| 18427 |
-
let key = null;
|
| 18428 |
-
|
| 18429 |
-
if (view.viewerId) {
|
| 18430 |
-
// ✅ مستخدم مسجل
|
| 18431 |
-
key = view.viewerId.toString();
|
| 18432 |
-
} else if (view.viewerIp) {
|
| 18433 |
-
// ✅ زائر مع IP
|
| 18434 |
-
key = view.viewerIp;
|
| 18435 |
-
} else {
|
| 18436 |
-
// ✅ مشاهدات قديمة جداً (بدون viewerId ولا viewerIp)
|
| 18437 |
-
// نستخدم معرف فريد مؤقت
|
| 18438 |
-
key = `legacy_${view._id || Math.random().toString(36).substring(7)}`;
|
| 18439 |
-
}
|
| 18440 |
-
|
| 18441 |
if (!viewerMap.has(key)) {
|
| 18442 |
viewerMap.set(key, {
|
| 18443 |
viewerId: view.viewerId || null,
|
| 18444 |
viewerIp: view.viewerIp || null,
|
| 18445 |
viewedAt: view.viewedAt,
|
| 18446 |
-
count: 1
|
| 18447 |
-
isLegacy: !view.viewerId && !view.viewerIp
|
| 18448 |
});
|
| 18449 |
} else {
|
| 18450 |
const existing = viewerMap.get(key);
|
|
@@ -18461,22 +18446,13 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18461 |
}
|
| 18462 |
|
| 18463 |
// ============================
|
| 18464 |
-
// ✅ 3. دمج المشاهدات
|
| 18465 |
// ============================
|
| 18466 |
const allViewers = [];
|
| 18467 |
const processedIds = new Set();
|
| 18468 |
|
| 18469 |
-
// ✅ أضف المشاهدات من AlsoViewed أولاً (الأحدث)
|
| 18470 |
for (const item of alsoViewed) {
|
| 18471 |
-
|
| 18472 |
-
if (item.viewerId) {
|
| 18473 |
-
key = item.viewerId.toString();
|
| 18474 |
-
} else if (item.viewerIp) {
|
| 18475 |
-
key = item.viewerIp;
|
| 18476 |
-
} else {
|
| 18477 |
-
key = `also_${item._id}`;
|
| 18478 |
-
}
|
| 18479 |
-
|
| 18480 |
if (!processedIds.has(key)) {
|
| 18481 |
processedIds.add(key);
|
| 18482 |
allViewers.push({
|
|
@@ -18488,25 +18464,15 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18488 |
}
|
| 18489 |
}
|
| 18490 |
|
| 18491 |
-
// ✅ أضف المشاهدات التاريخية (اللي مش موجودة في AlsoViewed)
|
| 18492 |
for (const item of historicalViews) {
|
| 18493 |
-
|
| 18494 |
-
if (item.viewerId) {
|
| 18495 |
-
key = item.viewerId.toString();
|
| 18496 |
-
} else if (item.viewerIp) {
|
| 18497 |
-
key = item.viewerIp;
|
| 18498 |
-
} else {
|
| 18499 |
-
key = `legacy_${Math.random().toString(36).substring(7)}`;
|
| 18500 |
-
}
|
| 18501 |
-
|
| 18502 |
if (!processedIds.has(key)) {
|
| 18503 |
processedIds.add(key);
|
| 18504 |
allViewers.push({
|
| 18505 |
viewerId: item.viewerId || null,
|
| 18506 |
viewerIp: item.viewerIp || null,
|
| 18507 |
viewedAt: item.viewedAt,
|
| 18508 |
-
source: 'historical'
|
| 18509 |
-
isLegacy: item.isLegacy || false
|
| 18510 |
});
|
| 18511 |
}
|
| 18512 |
}
|
|
@@ -18515,7 +18481,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18515 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18516 |
|
| 18517 |
// ============================
|
| 18518 |
-
// ✅ 4. جلب بيانات المستخدمين
|
| 18519 |
// ============================
|
| 18520 |
const userIds = allViewers
|
| 18521 |
.filter(v => v.viewerId)
|
|
@@ -18535,7 +18501,6 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18535 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18536 |
// ============================
|
| 18537 |
let formattedViewers = allViewers.map(item => {
|
| 18538 |
-
// ✅ مستخدم مسجل
|
| 18539 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18540 |
const user = userMap[item.viewerId.toString()];
|
| 18541 |
return {
|
|
@@ -18549,35 +18514,25 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18549 |
isGuest: false,
|
| 18550 |
source: item.source
|
| 18551 |
};
|
| 18552 |
-
}
|
| 18553 |
-
|
| 18554 |
-
|
| 18555 |
-
|
| 18556 |
-
|
| 18557 |
-
|
| 18558 |
-
|
| 18559 |
-
|
| 18560 |
-
|
| 18561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18562 |
}
|
| 18563 |
-
|
| 18564 |
-
return {
|
| 18565 |
-
_id: null,
|
| 18566 |
-
name: displayName,
|
| 18567 |
-
nickname: null,
|
| 18568 |
-
username: null,
|
| 18569 |
-
avatar: '/assets/img/default-avatar.png',
|
| 18570 |
-
title: 'Guest Visitor',
|
| 18571 |
-
viewedAt: item.viewedAt,
|
| 18572 |
-
isGuest: true,
|
| 18573 |
-
viewerIp: viewerIp,
|
| 18574 |
-
source: item.source,
|
| 18575 |
-
isLegacy: item.isLegacy || false
|
| 18576 |
-
};
|
| 18577 |
});
|
| 18578 |
|
| 18579 |
// ============================
|
| 18580 |
-
// ✅ 6. التصفية حسب الصلاحية
|
| 18581 |
// ============================
|
| 18582 |
if (!isOwner) {
|
| 18583 |
// ✅ الزائر: يرى فقط المستخدمين المسجلين (يزيل الزوار)
|
|
@@ -18606,7 +18561,7 @@ app.get('/api/analytics/also-viewed', authenticateToken, async (req, res) => {
|
|
| 18606 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18607 |
|
| 18608 |
// ============================
|
| 18609 |
-
// ✅ 9. إرجاع النتيجة
|
| 18610 |
// ============================
|
| 18611 |
res.json({
|
| 18612 |
data: shuffledViewers.slice(0, 5),
|
|
|
|
| 18413 |
.limit(20);
|
| 18414 |
|
| 18415 |
// ============================
|
| 18416 |
+
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
| 18417 |
// ============================
|
| 18418 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18419 |
let historicalViews = [];
|
|
|
|
| 18423 |
const viewerMap = new Map();
|
| 18424 |
|
| 18425 |
views.forEach(view => {
|
| 18426 |
+
const key = view.viewerId ? view.viewerId.toString() : view.viewerIp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18427 |
if (!viewerMap.has(key)) {
|
| 18428 |
viewerMap.set(key, {
|
| 18429 |
viewerId: view.viewerId || null,
|
| 18430 |
viewerIp: view.viewerIp || null,
|
| 18431 |
viewedAt: view.viewedAt,
|
| 18432 |
+
count: 1
|
|
|
|
| 18433 |
});
|
| 18434 |
} else {
|
| 18435 |
const existing = viewerMap.get(key);
|
|
|
|
| 18446 |
}
|
| 18447 |
|
| 18448 |
// ============================
|
| 18449 |
+
// ✅ 3. دمج المشاهدات
|
| 18450 |
// ============================
|
| 18451 |
const allViewers = [];
|
| 18452 |
const processedIds = new Set();
|
| 18453 |
|
|
|
|
| 18454 |
for (const item of alsoViewed) {
|
| 18455 |
+
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18456 |
if (!processedIds.has(key)) {
|
| 18457 |
processedIds.add(key);
|
| 18458 |
allViewers.push({
|
|
|
|
| 18464 |
}
|
| 18465 |
}
|
| 18466 |
|
|
|
|
| 18467 |
for (const item of historicalViews) {
|
| 18468 |
+
const key = item.viewerId ? item.viewerId.toString() : item.viewerIp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18469 |
if (!processedIds.has(key)) {
|
| 18470 |
processedIds.add(key);
|
| 18471 |
allViewers.push({
|
| 18472 |
viewerId: item.viewerId || null,
|
| 18473 |
viewerIp: item.viewerIp || null,
|
| 18474 |
viewedAt: item.viewedAt,
|
| 18475 |
+
source: 'historical'
|
|
|
|
| 18476 |
});
|
| 18477 |
}
|
| 18478 |
}
|
|
|
|
| 18481 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18482 |
|
| 18483 |
// ============================
|
| 18484 |
+
// ✅ 4. جلب بيانات المستخدمين
|
| 18485 |
// ============================
|
| 18486 |
const userIds = allViewers
|
| 18487 |
.filter(v => v.viewerId)
|
|
|
|
| 18501 |
// ✅ 5. تنسيق النتيجة النهائية
|
| 18502 |
// ============================
|
| 18503 |
let formattedViewers = allViewers.map(item => {
|
|
|
|
| 18504 |
if (item.viewerId && userMap[item.viewerId.toString()]) {
|
| 18505 |
const user = userMap[item.viewerId.toString()];
|
| 18506 |
return {
|
|
|
|
| 18514 |
isGuest: false,
|
| 18515 |
source: item.source
|
| 18516 |
};
|
| 18517 |
+
} else {
|
| 18518 |
+
// ✅ زائر (غير مسجل)
|
| 18519 |
+
return {
|
| 18520 |
+
_id: null,
|
| 18521 |
+
name: 'Guest 🌐',
|
| 18522 |
+
nickname: null,
|
| 18523 |
+
username: null,
|
| 18524 |
+
avatar: '/assets/img/default-avatar.png',
|
| 18525 |
+
title: 'Guest Visitor',
|
| 18526 |
+
viewedAt: item.viewedAt,
|
| 18527 |
+
isGuest: true,
|
| 18528 |
+
viewerIp: item.viewerIp || 'Unknown',
|
| 18529 |
+
source: item.source
|
| 18530 |
+
};
|
| 18531 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18532 |
});
|
| 18533 |
|
| 18534 |
// ============================
|
| 18535 |
+
// ✅ 6. التصفية حسب الصلاحية
|
| 18536 |
// ============================
|
| 18537 |
if (!isOwner) {
|
| 18538 |
// ✅ الزائر: يرى فقط المستخدمين المسجلين (يزيل الزوار)
|
|
|
|
| 18561 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18562 |
|
| 18563 |
// ============================
|
| 18564 |
+
// ✅ 9. إرجاع النتيجة
|
| 18565 |
// ============================
|
| 18566 |
res.json({
|
| 18567 |
data: shuffledViewers.slice(0, 5),
|