Mark-Lasfar commited on
Commit ·
8b6931a
1
Parent(s): b58a845
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -18412,7 +18412,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18412 |
.limit(20);
|
| 18413 |
|
| 18414 |
// ============================
|
| 18415 |
-
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة
|
| 18416 |
// ============================
|
| 18417 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18418 |
let historicalViews = [];
|
|
@@ -18422,17 +18422,14 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18422 |
const viewerMap = new Map();
|
| 18423 |
|
| 18424 |
views.forEach(view => {
|
| 18425 |
-
// ✅ دعم المشاهدات القديمة (بدون viewerIp)
|
| 18426 |
let key = null;
|
| 18427 |
-
|
| 18428 |
if (view.viewerId) {
|
| 18429 |
-
// ✅ مستخدم مسجل
|
| 18430 |
key = view.viewerId.toString();
|
| 18431 |
} else if (view.viewerIp) {
|
| 18432 |
-
// ✅ زائر مع IP
|
| 18433 |
key = view.viewerIp;
|
| 18434 |
} else {
|
| 18435 |
-
// ✅ مشاهدات قديمة جداً (بدون viewerId ولا viewerIp)
|
| 18436 |
// نستخدم معرف فريد مؤقت
|
| 18437 |
key = `legacy_${view._id || Math.random().toString(36).substring(7)}`;
|
| 18438 |
}
|
|
@@ -18460,12 +18457,12 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18460 |
}
|
| 18461 |
|
| 18462 |
// ============================
|
| 18463 |
-
// ✅ 3. دمج المشاهدات
|
| 18464 |
// ============================
|
| 18465 |
const allViewers = [];
|
| 18466 |
const processedIds = new Set();
|
| 18467 |
|
| 18468 |
-
// ✅ أضف المشاهدات من AlsoViewed أولاً
|
| 18469 |
for (const item of alsoViewed) {
|
| 18470 |
let key = null;
|
| 18471 |
if (item.viewerId) {
|
|
@@ -18487,7 +18484,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18487 |
}
|
| 18488 |
}
|
| 18489 |
|
| 18490 |
-
// ✅ أضف المشاهدات التاريخية
|
| 18491 |
for (const item of historicalViews) {
|
| 18492 |
let key = null;
|
| 18493 |
if (item.viewerId) {
|
|
@@ -18504,8 +18501,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18504 |
viewerId: item.viewerId || null,
|
| 18505 |
viewerIp: item.viewerIp || null,
|
| 18506 |
viewedAt: item.viewedAt,
|
| 18507 |
-
source: 'historical'
|
| 18508 |
-
isLegacy: item.isLegacy || false
|
| 18509 |
});
|
| 18510 |
}
|
| 18511 |
}
|
|
@@ -18514,7 +18510,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18514 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18515 |
|
| 18516 |
// ============================
|
| 18517 |
-
// ✅ 4. جلب بيانات المستخدمين
|
| 18518 |
// ============================
|
| 18519 |
const userIds = allViewers
|
| 18520 |
.filter(v => v.viewerId)
|
|
@@ -18550,14 +18546,14 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18550 |
};
|
| 18551 |
}
|
| 18552 |
|
| 18553 |
-
// ✅ زائر (غير مسجل)
|
| 18554 |
let displayName = 'Guest 🌐';
|
| 18555 |
let viewerIp = item.viewerIp || 'Unknown';
|
| 18556 |
|
| 18557 |
-
// ✅ إذا كان
|
| 18558 |
-
if (
|
| 18559 |
displayName = 'Guest (Legacy)';
|
| 18560 |
-
viewerIp = '
|
| 18561 |
}
|
| 18562 |
|
| 18563 |
return {
|
|
@@ -18570,13 +18566,12 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18570 |
viewedAt: item.viewedAt,
|
| 18571 |
isGuest: true,
|
| 18572 |
viewerIp: viewerIp,
|
| 18573 |
-
source: item.source
|
| 18574 |
-
isLegacy: item.isLegacy || false
|
| 18575 |
};
|
| 18576 |
});
|
| 18577 |
|
| 18578 |
// ============================
|
| 18579 |
-
// ✅ 6. التصفية حسب الصلاحية
|
| 18580 |
// ============================
|
| 18581 |
if (!isOwner) {
|
| 18582 |
// ✅ الزائر: يرى فقط ��لمستخدمين المسجلين (يزيل الزوار)
|
|
@@ -18605,7 +18600,7 @@ app.get('/api/analytics/also-viewed', async (req, res) => {
|
|
| 18605 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18606 |
|
| 18607 |
// ============================
|
| 18608 |
-
// ✅ 9. إرجاع النتيجة
|
| 18609 |
// ============================
|
| 18610 |
res.json({
|
| 18611 |
data: shuffledViewers.slice(0, 5),
|
|
|
|
| 18412 |
.limit(20);
|
| 18413 |
|
| 18414 |
// ============================
|
| 18415 |
+
// ✅ 2. جلب المشاهدات من ProfileAnalytics (للمشاهدات القديمة)
|
| 18416 |
// ============================
|
| 18417 |
const analytics = await ProfileAnalytics.findOne({ userId });
|
| 18418 |
let historicalViews = [];
|
|
|
|
| 18422 |
const viewerMap = new Map();
|
| 18423 |
|
| 18424 |
views.forEach(view => {
|
| 18425 |
+
// ✅ دعم المشاهدات القديمة (بدون viewerIp)
|
| 18426 |
let key = null;
|
|
|
|
| 18427 |
if (view.viewerId) {
|
|
|
|
| 18428 |
key = view.viewerId.toString();
|
| 18429 |
} else if (view.viewerIp) {
|
|
|
|
| 18430 |
key = view.viewerIp;
|
| 18431 |
} else {
|
| 18432 |
+
// ✅ للمشاهدات القديمة جداً (بدون viewerId ولا viewerIp)
|
| 18433 |
// نستخدم معرف فريد مؤقت
|
| 18434 |
key = `legacy_${view._id || Math.random().toString(36).substring(7)}`;
|
| 18435 |
}
|
|
|
|
| 18457 |
}
|
| 18458 |
|
| 18459 |
// ============================
|
| 18460 |
+
// ✅ 3. دمج المشاهدات
|
| 18461 |
// ============================
|
| 18462 |
const allViewers = [];
|
| 18463 |
const processedIds = new Set();
|
| 18464 |
|
| 18465 |
+
// ✅ أضف المشاهدات من AlsoViewed أولاً
|
| 18466 |
for (const item of alsoViewed) {
|
| 18467 |
let key = null;
|
| 18468 |
if (item.viewerId) {
|
|
|
|
| 18484 |
}
|
| 18485 |
}
|
| 18486 |
|
| 18487 |
+
// ✅ أضف المشاهدات التاريخية
|
| 18488 |
for (const item of historicalViews) {
|
| 18489 |
let key = null;
|
| 18490 |
if (item.viewerId) {
|
|
|
|
| 18501 |
viewerId: item.viewerId || null,
|
| 18502 |
viewerIp: item.viewerIp || null,
|
| 18503 |
viewedAt: item.viewedAt,
|
| 18504 |
+
source: 'historical'
|
|
|
|
| 18505 |
});
|
| 18506 |
}
|
| 18507 |
}
|
|
|
|
| 18510 |
allViewers.sort((a, b) => new Date(b.viewedAt) - new Date(a.viewedAt));
|
| 18511 |
|
| 18512 |
// ============================
|
| 18513 |
+
// ✅ 4. جلب بيانات المستخدمين
|
| 18514 |
// ============================
|
| 18515 |
const userIds = allViewers
|
| 18516 |
.filter(v => v.viewerId)
|
|
|
|
| 18546 |
};
|
| 18547 |
}
|
| 18548 |
|
| 18549 |
+
// ✅ زائر (غير مسجل)
|
| 18550 |
let displayName = 'Guest 🌐';
|
| 18551 |
let viewerIp = item.viewerIp || 'Unknown';
|
| 18552 |
|
| 18553 |
+
// ✅ إذا كان من المشاهدات القديمة جداً
|
| 18554 |
+
if (!item.viewerId && !item.viewerIp) {
|
| 18555 |
displayName = 'Guest (Legacy)';
|
| 18556 |
+
viewerIp = 'legacy';
|
| 18557 |
}
|
| 18558 |
|
| 18559 |
return {
|
|
|
|
| 18566 |
viewedAt: item.viewedAt,
|
| 18567 |
isGuest: true,
|
| 18568 |
viewerIp: viewerIp,
|
| 18569 |
+
source: item.source
|
|
|
|
| 18570 |
};
|
| 18571 |
});
|
| 18572 |
|
| 18573 |
// ============================
|
| 18574 |
+
// ✅ 6. التصفية حسب الصلاحية
|
| 18575 |
// ============================
|
| 18576 |
if (!isOwner) {
|
| 18577 |
// ✅ الزائر: يرى فقط ��لمستخدمين المسجلين (يزيل الزوار)
|
|
|
|
| 18600 |
const shuffledViewers = shuffleArray(formattedViewers);
|
| 18601 |
|
| 18602 |
// ============================
|
| 18603 |
+
// ✅ 9. إرجاع النتيجة
|
| 18604 |
// ============================
|
| 18605 |
res.json({
|
| 18606 |
data: shuffledViewers.slice(0, 5),
|