Mark-Lasfar commited on
Commit ·
9e2daf5
1
Parent(s): c65884c
Add GraphQL
Browse files
server.js
CHANGED
|
@@ -10185,7 +10185,7 @@ app.get('/api/profile/:nickname', async (req, res) => {
|
|
| 10185 |
});
|
| 10186 |
|
| 10187 |
// ============================================
|
| 10188 |
-
// GET /api/users/:userId/badges - جلب البرونزيات
|
| 10189 |
// ============================================
|
| 10190 |
app.get('/api/users/:userId/badges', async (req, res) => {
|
| 10191 |
try {
|
|
@@ -10205,42 +10205,53 @@ app.get('/api/users/:userId/badges', async (req, res) => {
|
|
| 10205 |
let progress = 100;
|
| 10206 |
let nextBadgeType = null;
|
| 10207 |
|
| 10208 |
-
// ✅ برونزيات المشاهدات
|
| 10209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10210 |
nextBadge = 'Views Silver';
|
| 10211 |
nextThreshold = VIEW_BADGE_THRESHOLDS.SILVER;
|
| 10212 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.SILVER) * 100;
|
| 10213 |
nextBadgeType = 'views';
|
| 10214 |
-
} else if (!
|
| 10215 |
nextBadge = 'Views Gold';
|
| 10216 |
nextThreshold = VIEW_BADGE_THRESHOLDS.GOLD;
|
| 10217 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.GOLD) * 100;
|
| 10218 |
nextBadgeType = 'views';
|
| 10219 |
-
} else if (!
|
| 10220 |
nextBadge = 'Views Red';
|
| 10221 |
nextThreshold = VIEW_BADGE_THRESHOLDS.RED;
|
| 10222 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.RED) * 100;
|
| 10223 |
nextBadgeType = 'views';
|
| 10224 |
}
|
| 10225 |
|
| 10226 |
-
// ✅ برونزيات الإعجابات
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10227 |
if (!nextBadge) {
|
| 10228 |
-
if (!
|
| 10229 |
nextBadge = 'Likes Bronze';
|
| 10230 |
nextThreshold = LIKE_BADGE_THRESHOLDS.BRONZE;
|
| 10231 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.BRONZE) * 100;
|
| 10232 |
nextBadgeType = 'likes';
|
| 10233 |
-
} else if (!
|
| 10234 |
nextBadge = 'Likes Red';
|
| 10235 |
nextThreshold = LIKE_BADGE_THRESHOLDS.RED;
|
| 10236 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.RED) * 100;
|
| 10237 |
nextBadgeType = 'likes';
|
| 10238 |
-
} else if (!
|
| 10239 |
nextBadge = 'Likes Silver';
|
| 10240 |
nextThreshold = LIKE_BADGE_THRESHOLDS.SILVER;
|
| 10241 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.SILVER) * 100;
|
| 10242 |
nextBadgeType = 'likes';
|
| 10243 |
-
} else if (!
|
| 10244 |
nextBadge = 'Likes Gold';
|
| 10245 |
nextThreshold = LIKE_BADGE_THRESHOLDS.GOLD;
|
| 10246 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.GOLD) * 100;
|
|
@@ -10248,9 +10259,12 @@ app.get('/api/users/:userId/badges', async (req, res) => {
|
|
| 10248 |
}
|
| 10249 |
}
|
| 10250 |
|
| 10251 |
-
// ✅ برونزيات التفاعل
|
|
|
|
|
|
|
|
|
|
| 10252 |
if (!nextBadge) {
|
| 10253 |
-
if (!
|
| 10254 |
nextBadge = 'Interaction Bronze';
|
| 10255 |
nextThreshold = INTERACTION_BADGE_THRESHOLDS.BRONZE;
|
| 10256 |
progress = (replyCount / INTERACTION_BADGE_THRESHOLDS.BRONZE) * 100;
|
|
@@ -10276,19 +10290,21 @@ app.get('/api/users/:userId/badges', async (req, res) => {
|
|
| 10276 |
viewCount,
|
| 10277 |
likeCount,
|
| 10278 |
replyCount,
|
|
|
|
| 10279 |
badges: {
|
| 10280 |
views: {
|
| 10281 |
-
|
| 10282 |
count: badges.views?.count || 0
|
| 10283 |
},
|
| 10284 |
likes: {
|
| 10285 |
-
|
| 10286 |
count: badges.likes?.count || 0
|
| 10287 |
},
|
| 10288 |
interaction: {
|
| 10289 |
-
|
| 10290 |
count: badges.interaction?.count || 0
|
| 10291 |
},
|
|
|
|
| 10292 |
totalCount: totalBadges
|
| 10293 |
},
|
| 10294 |
thresholds: {
|
|
@@ -19884,6 +19900,38 @@ const INTERACTION_BADGE_ICONS = {
|
|
| 19884 |
bronze: '/assets/img/QuickDraw_SkinTone1.png'
|
| 19885 |
};
|
| 19886 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19887 |
/**
|
| 19888 |
* حساب عدد المشاهدات الفريدة للمستخدم
|
| 19889 |
*/
|
|
@@ -20047,51 +20095,60 @@ async function updateUserBadges(userId) {
|
|
| 20047 |
|
| 20048 |
let newBadgeUnlocked = false;
|
| 20049 |
let unlockedBadges = [];
|
|
|
|
| 20050 |
|
| 20051 |
-
// ✅ تحقق من برونزيات المشاهدات
|
| 20052 |
if (badges.views.silver && !user.profile.badges.views.silver) {
|
| 20053 |
user.profile.badges.views.silver = true;
|
| 20054 |
newBadgeUnlocked = true;
|
| 20055 |
-
unlockedBadges.push('
|
|
|
|
| 20056 |
}
|
| 20057 |
if (badges.views.gold && !user.profile.badges.views.gold) {
|
| 20058 |
user.profile.badges.views.gold = true;
|
| 20059 |
newBadgeUnlocked = true;
|
| 20060 |
-
unlockedBadges.push('
|
|
|
|
| 20061 |
}
|
| 20062 |
if (badges.views.red && !user.profile.badges.views.red) {
|
| 20063 |
user.profile.badges.views.red = true;
|
| 20064 |
newBadgeUnlocked = true;
|
| 20065 |
-
unlockedBadges.push('
|
|
|
|
| 20066 |
}
|
| 20067 |
|
| 20068 |
-
// ✅ تحقق من برونزيات الإعجابات
|
| 20069 |
if (badges.likes.bronze && !user.profile.badges.likes.bronze) {
|
| 20070 |
user.profile.badges.likes.bronze = true;
|
| 20071 |
newBadgeUnlocked = true;
|
| 20072 |
-
unlockedBadges.push('
|
|
|
|
| 20073 |
}
|
| 20074 |
if (badges.likes.red && !user.profile.badges.likes.red) {
|
| 20075 |
user.profile.badges.likes.red = true;
|
| 20076 |
newBadgeUnlocked = true;
|
| 20077 |
-
unlockedBadges.push('
|
|
|
|
| 20078 |
}
|
| 20079 |
if (badges.likes.silver && !user.profile.badges.likes.silver) {
|
| 20080 |
user.profile.badges.likes.silver = true;
|
| 20081 |
newBadgeUnlocked = true;
|
| 20082 |
-
unlockedBadges.push('
|
|
|
|
| 20083 |
}
|
| 20084 |
if (badges.likes.gold && !user.profile.badges.likes.gold) {
|
| 20085 |
user.profile.badges.likes.gold = true;
|
| 20086 |
newBadgeUnlocked = true;
|
| 20087 |
-
unlockedBadges.push('
|
|
|
|
| 20088 |
}
|
| 20089 |
|
| 20090 |
-
// ✅ تحقق من برونزيات التفاعل
|
| 20091 |
if (badges.interaction.bronze && !user.profile.badges.interaction.bronze) {
|
| 20092 |
user.profile.badges.interaction.bronze = true;
|
| 20093 |
newBadgeUnlocked = true;
|
| 20094 |
-
unlockedBadges.push('
|
|
|
|
| 20095 |
}
|
| 20096 |
|
| 20097 |
user.profile.badges.updatedAt = Date.now();
|
|
@@ -20103,13 +20160,14 @@ async function updateUserBadges(userId) {
|
|
| 20103 |
// ✅ 1. إشعار في User.notifications (جوه الـ User)
|
| 20104 |
user.notifications = user.notifications || [];
|
| 20105 |
user.notifications.push({
|
| 20106 |
-
message: `🎉 Congratulations! You unlocked new badges: ${badgeList}!`,
|
| 20107 |
type: 'success',
|
| 20108 |
link: '/profile/me',
|
| 20109 |
read: false,
|
| 20110 |
createdAt: new Date(),
|
| 20111 |
metadata: {
|
| 20112 |
badges: unlockedBadges,
|
|
|
|
| 20113 |
viewCount,
|
| 20114 |
likeCount,
|
| 20115 |
replyCount
|
|
@@ -20123,11 +20181,12 @@ async function updateUserBadges(userId) {
|
|
| 20123 |
type: 'system',
|
| 20124 |
actorId: userId,
|
| 20125 |
actorName: 'System',
|
| 20126 |
-
content: `🎉 You unlocked ${unlockedBadges.length} new badge(s): ${badgeList}!`,
|
| 20127 |
read: false,
|
| 20128 |
createdAt: new Date(),
|
| 20129 |
metadata: {
|
| 20130 |
badges: unlockedBadges,
|
|
|
|
| 20131 |
viewCount,
|
| 20132 |
likeCount,
|
| 20133 |
replyCount
|
|
@@ -20140,10 +20199,10 @@ async function updateUserBadges(userId) {
|
|
| 20140 |
console.log(`🏆 Badges unlocked for user ${userId}: ${badgeList}`);
|
| 20141 |
}
|
| 20142 |
|
| 20143 |
-
// ✅
|
| 20144 |
await user.save();
|
| 20145 |
|
| 20146 |
-
// ✅
|
| 20147 |
const savedUser = await User.findById(userId).select('profile.badges').lean();
|
| 20148 |
console.log('✅ Saved badges:', savedUser?.profile?.badges);
|
| 20149 |
|
|
@@ -20163,63 +20222,129 @@ async function updateUserBadges(userId) {
|
|
| 20163 |
}
|
| 20164 |
}
|
| 20165 |
|
| 20166 |
-
|
| 20167 |
-
* الحصول على أيقونة البرونزية المناسبة (للمشاهدات)
|
| 20168 |
-
*/
|
| 20169 |
-
function getBadgeIcon(badges) {
|
| 20170 |
-
if (badges.red) return VIEW_BADGE_ICONS.red;
|
| 20171 |
-
if (badges.gold) return VIEW_BADGE_ICONS.gold;
|
| 20172 |
-
if (badges.silver) return VIEW_BADGE_ICONS.silver;
|
| 20173 |
-
return null;
|
| 20174 |
-
}
|
| 20175 |
|
| 20176 |
/**
|
| 20177 |
-
* جلب حالة البرونزيات للمستخدم
|
| 20178 |
*/
|
| 20179 |
async function getUserBadges(userId) {
|
| 20180 |
try {
|
| 20181 |
const user = await User.findById(userId).select('profile.badges').lean();
|
| 20182 |
if (!user || !user.profile?.badges) {
|
| 20183 |
return {
|
| 20184 |
-
|
| 20185 |
-
|
| 20186 |
-
|
| 20187 |
-
|
|
|
|
| 20188 |
};
|
| 20189 |
}
|
| 20190 |
|
| 20191 |
const badges = user.profile.badges;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20192 |
|
| 20193 |
-
|
| 20194 |
-
if (badges.
|
| 20195 |
-
|
| 20196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20197 |
|
| 20198 |
-
|
| 20199 |
-
if (badges.
|
| 20200 |
-
|
| 20201 |
-
|
| 20202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20203 |
|
| 20204 |
-
|
| 20205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20206 |
|
| 20207 |
-
|
| 20208 |
|
| 20209 |
-
return {
|
| 20210 |
-
views: { ...badges.views, count: viewCount },
|
| 20211 |
-
likes: { ...badges.likes, count: likeCount },
|
| 20212 |
-
interaction: { ...badges.interaction, count: interactionCount },
|
| 20213 |
-
totalCount,
|
| 20214 |
-
unlockedAt: badges.unlockedAt || null
|
| 20215 |
-
};
|
| 20216 |
} catch (error) {
|
| 20217 |
console.error('Error getting badges:', error);
|
| 20218 |
return {
|
| 20219 |
-
|
| 20220 |
-
|
| 20221 |
-
|
| 20222 |
-
|
|
|
|
| 20223 |
};
|
| 20224 |
}
|
| 20225 |
}
|
|
|
|
| 10185 |
});
|
| 10186 |
|
| 10187 |
// ============================================
|
| 10188 |
+
// GET /api/users/:userId/badges - جلب البرونزيات المفتوحة فقط
|
| 10189 |
// ============================================
|
| 10190 |
app.get('/api/users/:userId/badges', async (req, res) => {
|
| 10191 |
try {
|
|
|
|
| 10205 |
let progress = 100;
|
| 10206 |
let nextBadgeType = null;
|
| 10207 |
|
| 10208 |
+
// ✅ برونزيات المشاهدات (باستخدام البيانات المخزنة)
|
| 10209 |
+
const viewsBadges = badges.views || { unlocked: [], count: 0 };
|
| 10210 |
+
const hasSilver = viewsBadges.unlocked.some(b => b.key === 'silver');
|
| 10211 |
+
const hasGold = viewsBadges.unlocked.some(b => b.key === 'gold');
|
| 10212 |
+
const hasRed = viewsBadges.unlocked.some(b => b.key === 'red');
|
| 10213 |
+
|
| 10214 |
+
if (!hasSilver && viewCount < VIEW_BADGE_THRESHOLDS.SILVER) {
|
| 10215 |
nextBadge = 'Views Silver';
|
| 10216 |
nextThreshold = VIEW_BADGE_THRESHOLDS.SILVER;
|
| 10217 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.SILVER) * 100;
|
| 10218 |
nextBadgeType = 'views';
|
| 10219 |
+
} else if (!hasGold && viewCount < VIEW_BADGE_THRESHOLDS.GOLD) {
|
| 10220 |
nextBadge = 'Views Gold';
|
| 10221 |
nextThreshold = VIEW_BADGE_THRESHOLDS.GOLD;
|
| 10222 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.GOLD) * 100;
|
| 10223 |
nextBadgeType = 'views';
|
| 10224 |
+
} else if (!hasRed && viewCount < VIEW_BADGE_THRESHOLDS.RED) {
|
| 10225 |
nextBadge = 'Views Red';
|
| 10226 |
nextThreshold = VIEW_BADGE_THRESHOLDS.RED;
|
| 10227 |
progress = (viewCount / VIEW_BADGE_THRESHOLDS.RED) * 100;
|
| 10228 |
nextBadgeType = 'views';
|
| 10229 |
}
|
| 10230 |
|
| 10231 |
+
// ✅ برونزيات الإعجابات
|
| 10232 |
+
const likesBadges = badges.likes || { unlocked: [], count: 0 };
|
| 10233 |
+
const hasLikesBronze = likesBadges.unlocked.some(b => b.key === 'bronze');
|
| 10234 |
+
const hasLikesRed = likesBadges.unlocked.some(b => b.key === 'red');
|
| 10235 |
+
const hasLikesSilver = likesBadges.unlocked.some(b => b.key === 'silver');
|
| 10236 |
+
const hasLikesGold = likesBadges.unlocked.some(b => b.key === 'gold');
|
| 10237 |
+
|
| 10238 |
if (!nextBadge) {
|
| 10239 |
+
if (!hasLikesBronze && likeCount < LIKE_BADGE_THRESHOLDS.BRONZE) {
|
| 10240 |
nextBadge = 'Likes Bronze';
|
| 10241 |
nextThreshold = LIKE_BADGE_THRESHOLDS.BRONZE;
|
| 10242 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.BRONZE) * 100;
|
| 10243 |
nextBadgeType = 'likes';
|
| 10244 |
+
} else if (!hasLikesRed && likeCount < LIKE_BADGE_THRESHOLDS.RED) {
|
| 10245 |
nextBadge = 'Likes Red';
|
| 10246 |
nextThreshold = LIKE_BADGE_THRESHOLDS.RED;
|
| 10247 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.RED) * 100;
|
| 10248 |
nextBadgeType = 'likes';
|
| 10249 |
+
} else if (!hasLikesSilver && likeCount < LIKE_BADGE_THRESHOLDS.SILVER) {
|
| 10250 |
nextBadge = 'Likes Silver';
|
| 10251 |
nextThreshold = LIKE_BADGE_THRESHOLDS.SILVER;
|
| 10252 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.SILVER) * 100;
|
| 10253 |
nextBadgeType = 'likes';
|
| 10254 |
+
} else if (!hasLikesGold && likeCount < LIKE_BADGE_THRESHOLDS.GOLD) {
|
| 10255 |
nextBadge = 'Likes Gold';
|
| 10256 |
nextThreshold = LIKE_BADGE_THRESHOLDS.GOLD;
|
| 10257 |
progress = (likeCount / LIKE_BADGE_THRESHOLDS.GOLD) * 100;
|
|
|
|
| 10259 |
}
|
| 10260 |
}
|
| 10261 |
|
| 10262 |
+
// ✅ برونزيات التفاعل
|
| 10263 |
+
const interactionBadges = badges.interaction || { unlocked: [], count: 0 };
|
| 10264 |
+
const hasInteractionBronze = interactionBadges.unlocked.some(b => b.key === 'bronze');
|
| 10265 |
+
|
| 10266 |
if (!nextBadge) {
|
| 10267 |
+
if (!hasInteractionBronze && replyCount < INTERACTION_BADGE_THRESHOLDS.BRONZE) {
|
| 10268 |
nextBadge = 'Interaction Bronze';
|
| 10269 |
nextThreshold = INTERACTION_BADGE_THRESHOLDS.BRONZE;
|
| 10270 |
progress = (replyCount / INTERACTION_BADGE_THRESHOLDS.BRONZE) * 100;
|
|
|
|
| 10290 |
viewCount,
|
| 10291 |
likeCount,
|
| 10292 |
replyCount,
|
| 10293 |
+
// ✅ البرونزيات المفتوحة فقط
|
| 10294 |
badges: {
|
| 10295 |
views: {
|
| 10296 |
+
unlocked: badges.views?.unlocked || [],
|
| 10297 |
count: badges.views?.count || 0
|
| 10298 |
},
|
| 10299 |
likes: {
|
| 10300 |
+
unlocked: badges.likes?.unlocked || [],
|
| 10301 |
count: badges.likes?.count || 0
|
| 10302 |
},
|
| 10303 |
interaction: {
|
| 10304 |
+
unlocked: badges.interaction?.unlocked || [],
|
| 10305 |
count: badges.interaction?.count || 0
|
| 10306 |
},
|
| 10307 |
+
unlocked: badges.unlocked || [],
|
| 10308 |
totalCount: totalBadges
|
| 10309 |
},
|
| 10310 |
thresholds: {
|
|
|
|
| 19900 |
bronze: '/assets/img/QuickDraw_SkinTone1.png'
|
| 19901 |
};
|
| 19902 |
|
| 19903 |
+
|
| 19904 |
+
/**
|
| 19905 |
+
* الحصول على أيقونة البرونزية المناسبة حسب النوع والمستوى
|
| 19906 |
+
* @param {string} type - نوع البرونزية: 'views', 'likes', 'interaction'
|
| 19907 |
+
* @param {string} level - المستوى: 'silver', 'gold', 'red', 'bronze'
|
| 19908 |
+
* @returns {string} - مسار الأيقونة
|
| 19909 |
+
*/
|
| 19910 |
+
function getBadgeIcon(type, level) {
|
| 19911 |
+
// ✅ برونزيات المشاهدات
|
| 19912 |
+
if (type === 'views') {
|
| 19913 |
+
if (level === 'silver') return VIEW_BADGE_ICONS.silver;
|
| 19914 |
+
if (level === 'gold') return VIEW_BADGE_ICONS.gold;
|
| 19915 |
+
if (level === 'red') return VIEW_BADGE_ICONS.red;
|
| 19916 |
+
}
|
| 19917 |
+
|
| 19918 |
+
// ✅ برونزيات الإعجابات
|
| 19919 |
+
if (type === 'likes') {
|
| 19920 |
+
if (level === 'bronze') return LIKE_BADGE_ICONS.bronze;
|
| 19921 |
+
if (level === 'red') return LIKE_BADGE_ICONS.red;
|
| 19922 |
+
if (level === 'silver') return LIKE_BADGE_ICONS.silver;
|
| 19923 |
+
if (level === 'gold') return LIKE_BADGE_ICONS.gold;
|
| 19924 |
+
}
|
| 19925 |
+
|
| 19926 |
+
// ✅ برونزيات التفاعل
|
| 19927 |
+
if (type === 'interaction') {
|
| 19928 |
+
if (level === 'bronze') return INTERACTION_BADGE_ICONS.bronze;
|
| 19929 |
+
}
|
| 19930 |
+
|
| 19931 |
+
// ✅ أيقونة افتراضية
|
| 19932 |
+
return '/assets/img/default-badge.png';
|
| 19933 |
+
}
|
| 19934 |
+
|
| 19935 |
/**
|
| 19936 |
* حساب عدد المشاهدات الفريدة للمستخدم
|
| 19937 |
*/
|
|
|
|
| 20095 |
|
| 20096 |
let newBadgeUnlocked = false;
|
| 20097 |
let unlockedBadges = [];
|
| 20098 |
+
let unlockedBadgeIcons = [];
|
| 20099 |
|
| 20100 |
+
// ✅ تحقق من برونزيات المشاهدات - استخدم getBadgeIcon
|
| 20101 |
if (badges.views.silver && !user.profile.badges.views.silver) {
|
| 20102 |
user.profile.badges.views.silver = true;
|
| 20103 |
newBadgeUnlocked = true;
|
| 20104 |
+
unlockedBadges.push('Views Silver');
|
| 20105 |
+
unlockedBadgeIcons.push(getBadgeIcon('views', 'silver'));
|
| 20106 |
}
|
| 20107 |
if (badges.views.gold && !user.profile.badges.views.gold) {
|
| 20108 |
user.profile.badges.views.gold = true;
|
| 20109 |
newBadgeUnlocked = true;
|
| 20110 |
+
unlockedBadges.push('Views Gold');
|
| 20111 |
+
unlockedBadgeIcons.push(getBadgeIcon('views', 'gold'));
|
| 20112 |
}
|
| 20113 |
if (badges.views.red && !user.profile.badges.views.red) {
|
| 20114 |
user.profile.badges.views.red = true;
|
| 20115 |
newBadgeUnlocked = true;
|
| 20116 |
+
unlockedBadges.push('Views Red');
|
| 20117 |
+
unlockedBadgeIcons.push(getBadgeIcon('views', 'red'));
|
| 20118 |
}
|
| 20119 |
|
| 20120 |
+
// ✅ تحقق من برونزيات الإعجابات - استخدم getBadgeIcon
|
| 20121 |
if (badges.likes.bronze && !user.profile.badges.likes.bronze) {
|
| 20122 |
user.profile.badges.likes.bronze = true;
|
| 20123 |
newBadgeUnlocked = true;
|
| 20124 |
+
unlockedBadges.push('Likes Bronze');
|
| 20125 |
+
unlockedBadgeIcons.push(getBadgeIcon('likes', 'bronze'));
|
| 20126 |
}
|
| 20127 |
if (badges.likes.red && !user.profile.badges.likes.red) {
|
| 20128 |
user.profile.badges.likes.red = true;
|
| 20129 |
newBadgeUnlocked = true;
|
| 20130 |
+
unlockedBadges.push('Likes Red');
|
| 20131 |
+
unlockedBadgeIcons.push(getBadgeIcon('likes', 'red'));
|
| 20132 |
}
|
| 20133 |
if (badges.likes.silver && !user.profile.badges.likes.silver) {
|
| 20134 |
user.profile.badges.likes.silver = true;
|
| 20135 |
newBadgeUnlocked = true;
|
| 20136 |
+
unlockedBadges.push('Likes Silver');
|
| 20137 |
+
unlockedBadgeIcons.push(getBadgeIcon('likes', 'silver'));
|
| 20138 |
}
|
| 20139 |
if (badges.likes.gold && !user.profile.badges.likes.gold) {
|
| 20140 |
user.profile.badges.likes.gold = true;
|
| 20141 |
newBadgeUnlocked = true;
|
| 20142 |
+
unlockedBadges.push('Likes Gold');
|
| 20143 |
+
unlockedBadgeIcons.push(getBadgeIcon('likes', 'gold'));
|
| 20144 |
}
|
| 20145 |
|
| 20146 |
+
// ✅ تحقق من برونزيات التفاعل - استخدم getBadgeIcon
|
| 20147 |
if (badges.interaction.bronze && !user.profile.badges.interaction.bronze) {
|
| 20148 |
user.profile.badges.interaction.bronze = true;
|
| 20149 |
newBadgeUnlocked = true;
|
| 20150 |
+
unlockedBadges.push('Interaction Bronze');
|
| 20151 |
+
unlockedBadgeIcons.push(getBadgeIcon('interaction', 'bronze'));
|
| 20152 |
}
|
| 20153 |
|
| 20154 |
user.profile.badges.updatedAt = Date.now();
|
|
|
|
| 20160 |
// ✅ 1. إشعار في User.notifications (جوه الـ User)
|
| 20161 |
user.notifications = user.notifications || [];
|
| 20162 |
user.notifications.push({
|
| 20163 |
+
message: `🎉 Congratulations! You unlocked new badges: ${badgeList}! ${unlockedBadgeIcons.join(' ')}`,
|
| 20164 |
type: 'success',
|
| 20165 |
link: '/profile/me',
|
| 20166 |
read: false,
|
| 20167 |
createdAt: new Date(),
|
| 20168 |
metadata: {
|
| 20169 |
badges: unlockedBadges,
|
| 20170 |
+
badgeIcons: unlockedBadgeIcons,
|
| 20171 |
viewCount,
|
| 20172 |
likeCount,
|
| 20173 |
replyCount
|
|
|
|
| 20181 |
type: 'system',
|
| 20182 |
actorId: userId,
|
| 20183 |
actorName: 'System',
|
| 20184 |
+
content: `🎉 You unlocked ${unlockedBadges.length} new badge(s): ${badgeList}! ${unlockedBadgeIcons.join(' ')}`,
|
| 20185 |
read: false,
|
| 20186 |
createdAt: new Date(),
|
| 20187 |
metadata: {
|
| 20188 |
badges: unlockedBadges,
|
| 20189 |
+
badgeIcons: unlockedBadgeIcons,
|
| 20190 |
viewCount,
|
| 20191 |
likeCount,
|
| 20192 |
replyCount
|
|
|
|
| 20199 |
console.log(`🏆 Badges unlocked for user ${userId}: ${badgeList}`);
|
| 20200 |
}
|
| 20201 |
|
| 20202 |
+
// ✅ حفظ المستخدم
|
| 20203 |
await user.save();
|
| 20204 |
|
| 20205 |
+
// ✅ تأكد من الحفظ
|
| 20206 |
const savedUser = await User.findById(userId).select('profile.badges').lean();
|
| 20207 |
console.log('✅ Saved badges:', savedUser?.profile?.badges);
|
| 20208 |
|
|
|
|
| 20222 |
}
|
| 20223 |
}
|
| 20224 |
|
| 20225 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20226 |
|
| 20227 |
/**
|
| 20228 |
+
* جلب حالة البرونزيات للمستخدم (المفتوحة فقط + الأيقونات)
|
| 20229 |
*/
|
| 20230 |
async function getUserBadges(userId) {
|
| 20231 |
try {
|
| 20232 |
const user = await User.findById(userId).select('profile.badges').lean();
|
| 20233 |
if (!user || !user.profile?.badges) {
|
| 20234 |
return {
|
| 20235 |
+
unlocked: [],
|
| 20236 |
+
totalCount: 0,
|
| 20237 |
+
views: { unlocked: [], count: 0 },
|
| 20238 |
+
likes: { unlocked: [], count: 0 },
|
| 20239 |
+
interaction: { unlocked: [], count: 0 }
|
| 20240 |
};
|
| 20241 |
}
|
| 20242 |
|
| 20243 |
const badges = user.profile.badges;
|
| 20244 |
+
const result = {
|
| 20245 |
+
views: { unlocked: [], count: 0 },
|
| 20246 |
+
likes: { unlocked: [], count: 0 },
|
| 20247 |
+
interaction: { unlocked: [], count: 0 },
|
| 20248 |
+
unlocked: [],
|
| 20249 |
+
totalCount: 0
|
| 20250 |
+
};
|
| 20251 |
+
|
| 20252 |
+
// ✅ برونزيات المشاهدات - استخدم getBadgeIcon
|
| 20253 |
+
if (badges.views?.silver) {
|
| 20254 |
+
result.views.unlocked.push({
|
| 20255 |
+
name: 'Views Silver',
|
| 20256 |
+
icon: getBadgeIcon('views', 'silver'),
|
| 20257 |
+
key: 'silver',
|
| 20258 |
+
type: 'views'
|
| 20259 |
+
});
|
| 20260 |
+
result.views.count++;
|
| 20261 |
+
}
|
| 20262 |
+
if (badges.views?.gold) {
|
| 20263 |
+
result.views.unlocked.push({
|
| 20264 |
+
name: 'Views Gold',
|
| 20265 |
+
icon: getBadgeIcon('views', 'gold'),
|
| 20266 |
+
key: 'gold',
|
| 20267 |
+
type: 'views'
|
| 20268 |
+
});
|
| 20269 |
+
result.views.count++;
|
| 20270 |
+
}
|
| 20271 |
+
if (badges.views?.red) {
|
| 20272 |
+
result.views.unlocked.push({
|
| 20273 |
+
name: 'Views Red',
|
| 20274 |
+
icon: getBadgeIcon('views', 'red'),
|
| 20275 |
+
key: 'red',
|
| 20276 |
+
type: 'views'
|
| 20277 |
+
});
|
| 20278 |
+
result.views.count++;
|
| 20279 |
+
}
|
| 20280 |
|
| 20281 |
+
// ✅ برونزيات الإعجابات - استخدم getBadgeIcon
|
| 20282 |
+
if (badges.likes?.bronze) {
|
| 20283 |
+
result.likes.unlocked.push({
|
| 20284 |
+
name: 'Likes Bronze',
|
| 20285 |
+
icon: getBadgeIcon('likes', 'bronze'),
|
| 20286 |
+
key: 'bronze',
|
| 20287 |
+
type: 'likes'
|
| 20288 |
+
});
|
| 20289 |
+
result.likes.count++;
|
| 20290 |
+
}
|
| 20291 |
+
if (badges.likes?.red) {
|
| 20292 |
+
result.likes.unlocked.push({
|
| 20293 |
+
name: 'Likes Red',
|
| 20294 |
+
icon: getBadgeIcon('likes', 'red'),
|
| 20295 |
+
key: 'red',
|
| 20296 |
+
type: 'likes'
|
| 20297 |
+
});
|
| 20298 |
+
result.likes.count++;
|
| 20299 |
+
}
|
| 20300 |
+
if (badges.likes?.silver) {
|
| 20301 |
+
result.likes.unlocked.push({
|
| 20302 |
+
name: 'Likes Silver',
|
| 20303 |
+
icon: getBadgeIcon('likes', 'silver'),
|
| 20304 |
+
key: 'silver',
|
| 20305 |
+
type: 'likes'
|
| 20306 |
+
});
|
| 20307 |
+
result.likes.count++;
|
| 20308 |
+
}
|
| 20309 |
+
if (badges.likes?.gold) {
|
| 20310 |
+
result.likes.unlocked.push({
|
| 20311 |
+
name: 'Likes Gold',
|
| 20312 |
+
icon: getBadgeIcon('likes', 'gold'),
|
| 20313 |
+
key: 'gold',
|
| 20314 |
+
type: 'likes'
|
| 20315 |
+
});
|
| 20316 |
+
result.likes.count++;
|
| 20317 |
+
}
|
| 20318 |
|
| 20319 |
+
// ✅ برونزيات التفاعل - استخدم getBadgeIcon
|
| 20320 |
+
if (badges.interaction?.bronze) {
|
| 20321 |
+
result.interaction.unlocked.push({
|
| 20322 |
+
name: 'Interaction Bronze',
|
| 20323 |
+
icon: getBadgeIcon('interaction', 'bronze'),
|
| 20324 |
+
key: 'bronze',
|
| 20325 |
+
type: 'interaction'
|
| 20326 |
+
});
|
| 20327 |
+
result.interaction.count++;
|
| 20328 |
+
}
|
| 20329 |
|
| 20330 |
+
// ✅ تجميع الكل
|
| 20331 |
+
result.unlocked = [
|
| 20332 |
+
...result.views.unlocked,
|
| 20333 |
+
...result.likes.unlocked,
|
| 20334 |
+
...result.interaction.unlocked
|
| 20335 |
+
];
|
| 20336 |
+
result.totalCount = result.unlocked.length;
|
| 20337 |
|
| 20338 |
+
return result;
|
| 20339 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20340 |
} catch (error) {
|
| 20341 |
console.error('Error getting badges:', error);
|
| 20342 |
return {
|
| 20343 |
+
unlocked: [],
|
| 20344 |
+
totalCount: 0,
|
| 20345 |
+
views: { unlocked: [], count: 0 },
|
| 20346 |
+
likes: { unlocked: [], count: 0 },
|
| 20347 |
+
interaction: { unlocked: [], count: 0 }
|
| 20348 |
};
|
| 20349 |
}
|
| 20350 |
}
|