Mark-Lasfar commited on
Commit ·
9e2daf5
1
Parent(s): c65884c
Add GraphQL
Browse files10184 unmodified lines1018510186101871018810189101901019113 unmodified lines1020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461 unmodified line10248102491025010251102521025310254102551025619 unmodified lines102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102949589 unmodified lines198841988519886198871988819889157 unmodified lines2004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200975 unmodified lines201032010420105201062010720108201092011020111201122011320114201157 unmodified lines20123201242012520126201272012820129201302013120132201336 unmodified lines2014020141201422014320144201452014620147201482014913 unmodified lines20163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022510184 unmodified lines});// ============================================// GET /api/users/:userId/badges - جلب البرونزيات// ============================================app.get('/api/users/:userId/badges', async (req, res) => {try {13 unmodified lineslet progress = 100;let nextBadgeType = null;// ✅ برونزيات المشاهداتif (!badges.views?.silver && viewCount < VIEW_BADGE_THRESHOLDS.SILVER) {nextBadge = 'Views Silver';nextThreshold = VIEW_BADGE_THRESHOLDS.SILVER;progress = (viewCount / VIEW_BADGE_THRESHOLDS.SILVER) * 100;nextBadgeType = 'views';} else if (!badges.views?.gold && viewCount < VIEW_BADGE_THRESHOLDS.GOLD) {nextBadge = 'Views Gold';nextThreshold = VIEW_BADGE_THRESHOLDS.GOLD;progress = (viewCount / VIEW_BADGE_THRESHOLDS.GOLD) * 100;nextBadgeType = 'views';} else if (!badges.views?.red && viewCount < VIEW_BADGE_THRESHOLDS.RED) {nextBadge = 'Views Red';nextThreshold = VIEW_BADGE_THRESHOLDS.RED;progress = (viewCount / VIEW_BADGE_THRESHOLDS.RED) * 100;nextBadgeType = 'views';}// ✅ برونزيات الإعجابات (إذا لم يكن هناك برونزية مشاهدات تالية)if (!nextBadge) {if (!badges.likes?.bronze && likeCount < LIKE_BADGE_THRESHOLDS.BRONZE) {nextBadge = 'Likes Bronze';nextThreshold = LIKE_BADGE_THRESHOLDS.BRONZE;progress = (likeCount / LIKE_BADGE_THRESHOLDS.BRONZE) * 100;nextBadgeType = 'likes';} else if (!badges.likes?.red && likeCount < LIKE_BADGE_THRESHOLDS.RED) {nextBadge = 'Likes Red';nextThreshold = LIKE_BADGE_THRESHOLDS.RED;progress = (likeCount / LIKE_BADGE_THRESHOLDS.RED) * 100;nextBadgeType = 'likes';} else if (!badges.likes?.silver && likeCount < LIKE_BADGE_THRESHOLDS.SILVER) {nextBadge = 'Likes Silver';nextThreshold = LIKE_BADGE_THRESHOLDS.SILVER;progress = (likeCount / LIKE_BADGE_THRESHOLDS.SILVER) * 100;nextBadgeType = 'likes';} else if (!badges.likes?.gold && likeCount < LIKE_BADGE_THRESHOLDS.GOLD) {nextBadge = 'Likes Gold';nextThreshold = LIKE_BADGE_THRESHOLDS.GOLD;progress = (likeCount / LIKE_BADGE_THRESHOLDS.GOLD) * 100;1 unmodified line}}// ✅ برونزيات التفاعل (إذا لم يكن هناك برونزية تالية)if (!nextBadge) {if (!badges.interaction?.bronze && replyCount < INTERACTION_BADGE_THRESHOLDS.BRONZE) {nextBadge = 'Interaction Bronze';nextThreshold = INTERACTION_BADGE_THRESHOLDS.BRONZE;progress = (replyCount / INTERACTION_BADGE_THRESHOLDS.BRONZE) * 100;19 unmodified linesviewCount,likeCount,replyCount,badges: {views: {...badges.views,count: badges.views?.count || 0},likes: {...badges.likes,count: badges.likes?.count || 0},interaction: {...badges.interaction,count: badges.interaction?.count || 0},totalCount: totalBadges},thresholds: {9589 unmodified linesbronze: '/assets/img/QuickDraw_SkinTone1.png'};/*** حساب عدد المشاهدات الفريدة للمستخدم*/157 unmodified lineslet newBadgeUnlocked = false;let unlockedBadges = [];// ✅ تحقق من برونزيات المشاهداتif (badges.views.silver && !user.profile.badges.views.silver) {user.profile.badges.views.silver = true;newBadgeUnlocked = true;unlockedBadges.push('🥈 Views Silver');}if (badges.views.gold && !user.profile.badges.views.gold) {user.profile.badges.views.gold = true;newBadgeUnlocked = true;unlockedBadges.push('🥇 Views Gold');}if (badges.views.red && !user.profile.badges.views.red) {user.profile.badges.views.red = true;newBadgeUnlocked = true;unlockedBadges.push('🔥 Views Red');}// ✅ تحقق من برونزيات الإعجاباتif (badges.likes.bronze && !user.profile.badges.likes.bronze) {user.profile.badges.likes.bronze = true;newBadgeUnlocked = true;unlockedBadges.push('🟤 Likes Bronze');}if (badges.likes.red && !user.profile.badges.likes.red) {user.profile.badges.likes.red = true;newBadgeUnlocked = true;unlockedBadges.push('🔴 Likes Red');}if (badges.likes.silver && !user.profile.badges.likes.silver) {user.profile.badges.likes.silver = true;newBadgeUnlocked = true;unlockedBadges.push('🥈 Likes Silver');}if (badges.likes.gold && !user.profile.badges.likes.gold) {user.profile.badges.likes.gold = true;newBadgeUnlocked = true;unlockedBadges.push('🥇 Likes Gold');}// ✅ تحقق من برونزيات التفاعلif (badges.interaction.bronze && !user.profile.badges.interaction.bronze) {user.profile.badges.interaction.bronze = true;newBadgeUnlocked = true;unlockedBadges.push('💬 Interaction Bronze');}user.profile.badges.updatedAt = Date.now();5 unmodified lines// ✅ 1. إشعار في User.notifications (جوه الـ User)user.notifications = user.notifications || [];user.notifications.push({message: `🎉 Congratulations! You unlocked new badges: ${badgeList}!`,type: 'success',link: '/profile/me',read: false,createdAt: new Date(),metadata: {badges: unlockedBadges,viewCount,likeCount,replyCount7 unmodified linestype: 'system',actorId: userId,actorName: 'System',content: `🎉 You unlocked ${unlockedBadges.length} new badge(s): ${badgeList}!`,read: false,createdAt: new Date(),metadata: {badges: unlockedBadges,viewCount,likeCount,replyCount6 unmodified linesconsole.log(`🏆 Badges unlocked for user ${userId}: ${badgeList}`);}// ✅ ✅ ✅ أهم جزء: حفظ المستخدمawait user.save();// ✅ ✅ ✅ تأكد من الحفظconst savedUser = await User.findById(userId).select('profile.badges').lean();console.log('✅ Saved badges:', savedUser?.profile?.badges);13 unmodified lines}}/*** الحصول على أيقونة البرونزية المناسبة (للمشاهدات)*/function getBadgeIcon(badges) {if (badges.red) return VIEW_BADGE_ICONS.red;if (badges.gold) return VIEW_BADGE_ICONS.gold;if (badges.silver) return VIEW_BADGE_ICONS.silver;return null;}/*** جلب حالة البرونزيات للمستخدم*/async function getUserBadges(userId) {try {const user = await User.findById(userId).select('profile.badges').lean();if (!user || !user.profile?.badges) {return {views: { silver: false, gold: false, red: false, count: 0 },likes: { bronze: false, silver: false, gold: false, red: false, count: 0 },interaction: { bronze: false, count: 0 },totalCount: 0};}const badges = user.profile.badges;let viewCount = 0;if (badges.views?.silver) viewCount++;if (badges.views?.gold) viewCount++;if (badges.views?.red) viewCount++;let likeCount = 0;if (badges.likes?.bronze) likeCount++;if (badges.likes?.red) likeCount++;if (badges.likes?.silver) likeCount++;if (badges.likes?.gold) likeCount++;let interactionCount = 0;if (badges.interaction?.bronze) interactionCount++;const totalCount = viewCount + likeCount + interactionCount;return {views: { ...badges.views, count: viewCount },likes: { ...badges.likes, count: likeCount },interaction: { ...badges.interaction, count: interactionCount },totalCount,unlockedAt: badges.unlockedAt || null};} catch (error) {console.error('Error getting badges:', error);return {views: { silver: false, gold: false, red: false, count: 0 },likes: { bronze: false, silver: false, gold: false, red: false, count: 0 },interaction: { bronze: false, count: 0 },totalCount: 0};}}10184 unmodified lines1018510186101871018810189101901019113 unmodified lines10205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571 unmodified line10259102601026110262102631026410265102661026710268102691027019 unmodified lines1029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103109589 unmodified lines1990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937157 unmodified lines2009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201545 unmodified lines20160201612016220163201642016520166201672016820169201702017120172201737 unmodified lines2018120182201832018420185201862018720188201892019020191201926 unmodified lines2019920200202012020220203202042020520206202072020813 unmodified lines20222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035010184 unmodified lines});// ============================================// GET /api/users/:userId/badges - جلب البرونزيات المفتوحة فقط// ============================================app.get('/api/users/:userId/badges', async (req, res) => {try {13 unmodified lineslet progress = 100;let nextBadgeType = null;// ✅ برونزيات المشاهدات (باستخدام البيانات المخزنة)const viewsBadges = badges.views || { unlocked: [], count: 0 };const hasSilver = viewsBadges.unlocked.some(b => b.key === 'silver');const hasGold = viewsBadges.unlocked.some(b => b.key === 'gold');const hasRed = viewsBadges.unlocked.some(b => b.key === 'red');if (!hasSilver && viewCount < VIEW_BADGE_THRESHOLDS.SILVER) {nextBadge = 'Views Silver';nextThreshold = VIEW_BADGE_THRESHOLDS.SILVER;progress = (viewCount / VIEW_BADGE_THRESHOLDS.SILVER) * 100;nextBadgeType = 'views';} else if (!hasGold && viewCount < VIEW_BADGE_THRESHOLDS.GOLD) {nextBadge = 'Views Gold';nextThreshold = VIEW_BADGE_THRESHOLDS.GOLD;progress = (viewCount / VIEW_BADGE_THRESHOLDS.GOLD) * 100;nextBadgeType = 'views';} else if (!hasRed && viewCount < VIEW_BADGE_THRESHOLDS.RED) {nextBadge = 'Views Red';nextThreshold = VIEW_BADGE_THRESHOLDS.RED;progress = (viewCount / VIEW_BADGE_THRESHOLDS.RED) * 100;nextBadgeType = 'views';}// ✅ برونزيات الإعجاباتconst likesBadges = badges.likes || { unlocked: [], count: 0 };const hasLikesBronze = likesBadges.unlocked.some(b => b.key === 'bronze');const hasLikesRed = likesBadges.unlocked.some(b => b.key === 'red');const hasLikesSilver = likesBadges.unlocked.some(b => b.key === 'silver');const hasLikesGold = likesBadges.unlocked.some(b => b.key === 'gold');if (!nextBadge) {if (!hasLikesBronze && likeCount < LIKE_BADGE_THRESHOLDS.BRONZE) {nextBadge = 'Likes Bronze';nextThreshold = LIKE_BADGE_THRESHOLDS.BRONZE;progress = (likeCount / LIKE_BADGE_THRESHOLDS.BRONZE) * 100;nextBadgeType = 'likes';} else if (!hasLikesRed && likeCount < LIKE_BADGE_THRESHOLDS.RED) {nextBadge = 'Likes Red';nextThreshold = LIKE_BADGE_THRESHOLDS.RED;progress = (likeCount / LIKE_BADGE_THRESHOLDS.RED) * 100;nextBadgeType = 'likes';} else if (!hasLikesSilver && likeCount < LIKE_BADGE_THRESHOLDS.SILVER) {nextBadge = 'Likes Silver';nextThreshold = LIKE_BADGE_THRESHOLDS.SILVER;progress = (likeCount / LIKE_BADGE_THRESHOLDS.SILVER) * 100;nextBadgeType = 'likes';} else if (!hasLikesGold && likeCount < LIKE_BADGE_THRESHOLDS.GOLD) {nextBadge = 'Likes Gold';nextThreshold = LIKE_BADGE_THRESHOLDS.GOLD;progress = (likeCount / LIKE_BADGE_THRESHOLDS.GOLD) * 100;1 unmodified line}}// ✅ برونزيات التفاعلconst interactionBadges = badges.interaction || { unlocked: [], count: 0 };const hasInteractionBronze = interactionBadges.unlocked.some(b => b.key === 'bronze');if (!nextBadge) {if (!hasInteractionBronze && replyCount < INTERACTION_BADGE_THRESHOLDS.BRONZE) {nextBadge = 'Interaction Bronze';nextThreshold = INTERACTION_BADGE_THRESHOLDS.BRONZE;progress = (replyCount / INTERACTION_BADGE_THRESHOLDS.BRONZE) * 100;19 unmodified linesviewCount,likeCount,replyCount,// ✅ البرونزيات المفتوحة فقطbadges: {views: {unlocked: badges.views?.unlocked || [],count: badges.views?.count || 0},likes: {unlocked: badges.likes?.unlocked || [],count: badges.likes?.count || 0},interaction: {unlocked: badges.interaction?.unlocked || [],count: badges.interaction?.count || 0},unlocked: badges.unlocked || [],totalCount: totalBadges},thresholds: {9589 unmodified linesbronze: '/assets/img/QuickDraw_SkinTone1.png'};/*** الحصول على أيقونة البرونزية المناسبة حسب النوع والمستوى* @param {string} type - نوع البرونزية: 'views', 'likes', 'interaction'* @param {string} level - المستوى: 'silver', 'gold', 'red', 'bronze'* @returns {string} - مسار الأيقونة*/function getBadgeIcon(type, level) {// ✅ برونزيات المشاهداتif (type === 'views') {if (level === 'silver') return VIEW_BADGE_ICONS.silver;if (level === 'gold') return VIEW_BADGE_ICONS.gold;if (level === 'red') return VIEW_BADGE_ICONS.red;}// ✅ برونزيات الإعجاباتif (type === 'likes') {if (level === 'bronze') return LIKE_BADGE_ICONS.bronze;if (level === 'red') return LIKE_BADGE_ICONS.red;if (level === 'silver') return LIKE_BADGE_ICONS.silver;if (level === 'gold') return LIKE_BADGE_ICONS.gold;}// ✅ برونزيات التفاعلif (type === 'interaction') {if (level === 'bronze') return INTERACTION_BADGE_ICONS.bronze;}// ✅ أيقونة افتراضيةreturn '/assets/img/default-badge.png';}/*** حساب عدد المشاهدات الفريدة للمستخدم*/157 unmodified lineslet newBadgeUnlocked = false;let unlockedBadges = [];let unlockedBadgeIcons = [];// ✅ تحقق من برونزيات المشاهدات - استخدم getBadgeIconif (badges.views.silver && !user.profile.badges.views.silver) {user.profile.badges.views.silver = true;newBadgeUnlocked = true;unlockedBadges.push('Views Silver');unlockedBadgeIcons.push(getBadgeIcon('views', 'silver'));}if (badges.views.gold && !user.profile.badges.views.gold) {user.profile.badges.views.gold = true;newBadgeUnlocked = true;unlockedBadges.push('Views Gold');unlockedBadgeIcons.push(getBadgeIcon('views', 'gold'));}if (badges.views.red && !user.profile.badges.views.red) {user.profile.badges.views.red = true;newBadgeUnlocked = true;unlockedBadges.push('Views Red');unlockedBadgeIcons.push(getBadgeIcon('views', 'red'));}// ✅ تحقق من برونزيات الإعجابات - استخدم getBadgeIconif (badges.likes.bronze && !user.profile.badges.likes.bronze) {user.profile.badges.likes.bronze = true;newBadgeUnlocked = true;unlockedBadges.push('Likes Bronze');unlockedBadgeIcons.push(getBadgeIcon('likes', 'bronze'));}if (badges.likes.red && !user.profile.badges.likes.red) {user.profile.badges.likes.red = true;newBadgeUnlocked = true;unlockedBadges.push('Likes Red');unlockedBadgeIcons.push(getBadgeIcon('likes', 'red'));}if (badges.likes.silver && !user.profile.badges.likes.silver) {user.profile.badges.likes.silver = true;newBadgeUnlocked = true;unlockedBadges.push('Likes Silver');unlockedBadgeIcons.push(getBadgeIcon('likes', 'silver'));}if (badges.likes.gold && !user.profile.badges.likes.gold) {user.profile.badges.likes.gold = true;newBadgeUnlocked = true;unlockedBadges.push('Likes Gold');unlockedBadgeIcons.push(getBadgeIcon('likes', 'gold'));}// ✅ تحقق من برونزيات التفاعل - استخدم getBadgeIconif (badges.interaction.bronze && !user.profile.badges.interaction.bronze) {user.profile.badges.interaction.bronze = true;newBadgeUnlocked = true;unlockedBadges.push('Interaction Bronze');unlockedBadgeIcons.push(getBadgeIcon('interaction', 'bronze'));}user.profile.badges.updatedAt = Date.now();5 unmodified lines// ✅ 1. إشعار في User.notifications (جوه الـ User)user.notifications = user.notifications || [];user.notifications.push({message: `🎉 Congratulations! You unlocked new badges: ${badgeList}! ${unlockedBadgeIcons.join(' ')}`,type: 'success',link: '/profile/me',read: false,createdAt: new Date(),metadata: {badges: unlockedBadges,badgeIcons: unlockedBadgeIcons,viewCount,likeCount,replyCount7 unmodified linestype: 'system',actorId: userId,actorName: 'System',content: `🎉 You unlocked ${unlockedBadges.length} new badge(s): ${badgeList}! ${unlockedBadgeIcons.join(' ')}`,read: false,createdAt: new Date(),metadata: {badges: unlockedBadges,badgeIcons: unlockedBadgeIcons,viewCount,likeCount,replyCount6 unmodified linesconsole.log(`🏆 Badges unlocked for user ${userId}: ${badgeList}`);}// ✅ حفظ المستخدمawait user.save();// ✅ تأكد من الحفظconst savedUser = await User.findById(userId).select('profile.badges').lean();console.log('✅ Saved badges:', savedUser?.profile?.badges);13 unmodified lines}}/*** جلب حالة البرونزيات للمستخدم (المفتوحة فقط + الأيقونات)*/async function getUserBadges(userId) {try {const user = await User.findById(userId).select('profile.badges').lean();if (!user || !user.profile?.badges) {return {unlocked: [],totalCount: 0,views: { unlocked: [], count: 0 },likes: { unlocked: [], count: 0 },interaction: { unlocked: [], count: 0 }};}const badges = user.profile.badges;const result = {views: { unlocked: [], count: 0 },likes: { unlocked: [], count: 0 },interaction: { unlocked: [], count: 0 },unlocked: [],totalCount: 0};// ✅ برونزيات المشاهدات - استخدم getBadgeIconif (badges.views?.silver) {result.views.unlocked.push({name: 'Views Silver',icon: getBadgeIcon('views', 'silver'),key: 'silver',type: 'views'});result.views.count++;}if (badges.views?.gold) {result.views.unlocked.push({name: 'Views Gold',icon: getBadgeIcon('views', 'gold'),key: 'gold',type: 'views'});result.views.count++;}if (badges.views?.red) {result.views.unlocked.push({name: 'Views Red',icon: getBadgeIcon('views', 'red'),key: 'red',type: 'views'});result.views.count++;}// ✅ برونزيات الإعجابات - استخدم getBadgeIconif (badges.likes?.bronze) {result.likes.unlocked.push({name: 'Likes Bronze',icon: getBadgeIcon('likes', 'bronze'),key: 'bronze',type: 'likes'});result.likes.count++;}if (badges.likes?.red) {result.likes.unlocked.push({name: 'Likes Red',icon: getBadgeIcon('likes', 'red'),key: 'red',type: 'likes'});result.likes.count++;}if (badges.likes?.silver) {result.likes.unlocked.push({name: 'Likes Silver',icon: getBadgeIcon('likes', 'silver'),key: 'silver',type: 'likes'});result.likes.count++;}if (badges.likes?.gold) {result.likes.unlocked.push({name: 'Likes Gold',icon: getBadgeIcon('likes', 'gold'),key: 'gold',type: 'likes'});result.likes.count++;}// ✅ برونزيات التفاعل - استخدم getBadgeIconif (badges.interaction?.bronze) {result.interaction.unlocked.push({name: 'Interaction Bronze',icon: getBadgeIcon('interaction', 'bronze'),key: 'bronze',type: 'interaction'});result.interaction.count++;}// ✅ تجميع الكلresult.unlocked = [...result.views.unlocked,...result.likes.unlocked,...result.interaction.unlocked];result.totalCount = result.unlocked.length;return result;} catch (error) {console.error('Error getting badges:', error);return {unlocked: [],totalCount: 0,views: { unlocked: [], count: 0 },likes: { unlocked: [], count: 0 },interaction: { unlocked: [], count: 0 }};}}
server.js+192-67