Mark-Lasfar commited on
Commit
286c407
·
1 Parent(s): f96a5a2

Add GraphQL

Browse files
Files changed (3) hide show
  1. graphql/resolvers.js +199 -0
  2. graphql/schema.js +41 -0
  3. server.js +1 -0
graphql/resolvers.js CHANGED
@@ -1678,6 +1678,50 @@ const Query = {
1678
  return reportedComments;
1679
  },
1680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1681
  // ============================================
1682
  // 🔹 STORIES RESOLVERS - النسخة النهائية
1683
  // ============================================
@@ -2012,6 +2056,161 @@ const Query = {
2012
  userId: story.userId
2013
  };
2014
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015
  // ===== Jobs =====
2016
  jobs: async (parent, { page = 1, limit = 20, search, category, jobType, location }) => {
2017
  const skip = (page - 1) * limit;
 
1678
  return reportedComments;
1679
  },
1680
 
1681
+
1682
+ // ✅ getPostComments - جلب تعليقات منشور
1683
+ getPostComments: async (parent, { postId, page = 1, limit = 50 }, context) => {
1684
+ const post = await Post.findById(postId);
1685
+ if (!post) throw new GraphQLError('Post not found');
1686
+
1687
+ const skip = (page - 1) * limit;
1688
+ const comments = post.comments || [];
1689
+
1690
+ // جلب تعليقات الصفحة
1691
+ const paginatedComments = comments.slice(skip, skip + limit);
1692
+
1693
+ // جلب بيانات المستخدمين
1694
+ const commentData = await Promise.all(paginatedComments.map(async (comment) => {
1695
+ const user = await User.findById(comment.userId).select('username profile.nickname profile.avatar');
1696
+ const likesCount = comment.likes?.length || 0;
1697
+ const isLiked = comment.likes?.some(id => id.toString() === context.user?.userId) || false;
1698
+
1699
+ return {
1700
+ _id: comment._id,
1701
+ userId: user || { _id: comment.userId, username: 'Unknown', profile: {} },
1702
+ text: comment.text,
1703
+ createdAt: comment.createdAt,
1704
+ likesCount,
1705
+ isLiked,
1706
+ edited: comment.edited || false,
1707
+ editedAt: comment.editedAt || null,
1708
+ hidden: comment.hidden || false
1709
+ };
1710
+ }));
1711
+
1712
+ return {
1713
+ data: commentData,
1714
+ pagination: {
1715
+ page,
1716
+ limit,
1717
+ total: comments.length,
1718
+ pages: Math.ceil(comments.length / limit),
1719
+ hasNext: skip + limit < comments.length,
1720
+ hasPrev: page > 1
1721
+ }
1722
+ };
1723
+ },
1724
+
1725
  // ============================================
1726
  // 🔹 STORIES RESOLVERS - النسخة النهائية
1727
  // ============================================
 
2056
  userId: story.userId
2057
  };
2058
  },
2059
+
2060
+ // ============================================
2061
+ // 🔹 VIDEOS QUERIES
2062
+ // ============================================
2063
+
2064
+ // ✅ videosFeed - جلب كل الفيديوهات
2065
+ videosFeed: async (parent, { page = 1, limit = 20, type = 'latest' }, context) => {
2066
+ if (!context.user) throw new GraphQLError('Not authenticated');
2067
+
2068
+ const skip = (page - 1) * limit;
2069
+
2070
+ // ✅ شرط وجود فيديو
2071
+ let query = {
2072
+ 'video.url': { $exists: true, $ne: null },
2073
+ visibility: { $in: ['public', 'followers'] }
2074
+ };
2075
+
2076
+ let sort = { createdAt: -1 };
2077
+
2078
+ if (type === 'trending') {
2079
+ // ✅ الترند: الأكثر مشاهدة + إعجابات
2080
+ sort = { viewsCount: -1, likesCount: -1 };
2081
+ } else if (type === 'following') {
2082
+ // ✅ فقط من المتابعين
2083
+ const following = await Follow.find({
2084
+ followerId: context.user.userId,
2085
+ status: 'accepted'
2086
+ }).select('followingId');
2087
+ const followingIds = following.map(f => f.followingId);
2088
+ followingIds.push(context.user.userId);
2089
+ query.userId = { $in: followingIds };
2090
+ }
2091
+
2092
+ // ✅ جلب الفيديوهات
2093
+ const videos = await Post.find(query)
2094
+ .sort(sort)
2095
+ .skip(skip)
2096
+ .limit(limit)
2097
+ .populate('userId', 'username profile.nickname profile.avatar profile.jobTitle profile.bio')
2098
+ .lean();
2099
+
2100
+ // ✅ حساب الإحصائيات لكل فيديو
2101
+ const videosWithStats = await Promise.all(videos.map(async (video) => {
2102
+ const viewsCount = video.views?.length || 0;
2103
+ const likesCount = video.likes?.length || 0;
2104
+ const commentsCount = video.comments?.length || 0;
2105
+ const sharesCount = video.shares?.length || 0;
2106
+ const savesCount = video.saves?.length || 0;
2107
+
2108
+ const isLiked = video.likes?.some(l => l.userId?.toString() === context.user.userId) || false;
2109
+ const isSaved = video.saves?.some(s => s.userId?.toString() === context.user.userId) || false;
2110
+
2111
+ // ✅ التحقق من المتابعة
2112
+ let isFollowing = false;
2113
+ if (video.userId && context.user) {
2114
+ isFollowing = await Follow.exists({
2115
+ followerId: context.user.userId,
2116
+ followingId: video.userId._id,
2117
+ status: 'accepted'
2118
+ });
2119
+ }
2120
+
2121
+ return {
2122
+ ...video,
2123
+ viewsCount,
2124
+ likesCount,
2125
+ commentsCount,
2126
+ sharesCount,
2127
+ savesCount,
2128
+ isLiked,
2129
+ isSaved,
2130
+ isFollowing: !!isFollowing
2131
+ };
2132
+ }));
2133
+
2134
+ // ✅ جلب الفيديوهات الترند (للسايدبار)
2135
+ const trending = await Post.find({
2136
+ 'video.url': { $exists: true, $ne: null },
2137
+ visibility: 'public'
2138
+ })
2139
+ .sort({ viewsCount: -1, likesCount: -1 })
2140
+ .limit(5)
2141
+ .populate('userId', 'username profile.nickname profile.avatar')
2142
+ .lean()
2143
+ .then(videos => videos.map(v => ({
2144
+ ...v,
2145
+ viewsCount: v.views?.length || 0,
2146
+ likesCount: v.likes?.length || 0
2147
+ })));
2148
+
2149
+ const total = await Post.countDocuments(query);
2150
+
2151
+ return {
2152
+ data: videosWithStats,
2153
+ pagination: {
2154
+ page,
2155
+ limit,
2156
+ total,
2157
+ pages: Math.ceil(total / limit),
2158
+ hasNext: skip + limit < total,
2159
+ hasPrev: page > 1
2160
+ },
2161
+ trending
2162
+ };
2163
+ },
2164
+
2165
+ // ✅ video - فيديو واحد
2166
+ video: async (parent, { id }, context) => {
2167
+ const video = await Post.findOne({
2168
+ _id: id,
2169
+ 'video.url': { $exists: true, $ne: null }
2170
+ })
2171
+ .populate('userId', 'username profile.nickname profile.avatar profile.jobTitle profile.bio')
2172
+ .lean();
2173
+
2174
+ if (!video) throw new GraphQLError('Video not found');
2175
+
2176
+ // ✅ زيادة المشاهدات (غير متزامن)
2177
+ if (context.user) {
2178
+ Post.findByIdAndUpdate(id, {
2179
+ $push: { views: { userId: context.user.userId, viewedAt: new Date() } }
2180
+ }).catch(() => {});
2181
+ }
2182
+
2183
+ const viewsCount = video.views?.length || 0;
2184
+ const likesCount = video.likes?.length || 0;
2185
+ const commentsCount = video.comments?.length || 0;
2186
+ const sharesCount = video.shares?.length || 0;
2187
+ const savesCount = video.saves?.length || 0;
2188
+
2189
+ const isLiked = video.likes?.some(l => l.userId?.toString() === context.user?.userId) || false;
2190
+ const isSaved = video.saves?.some(s => s.userId?.toString() === context.user?.userId) || false;
2191
+
2192
+ let isFollowing = false;
2193
+ if (video.userId && context.user) {
2194
+ isFollowing = await Follow.exists({
2195
+ followerId: context.user.userId,
2196
+ followingId: video.userId._id,
2197
+ status: 'accepted'
2198
+ });
2199
+ }
2200
+
2201
+ return {
2202
+ ...video,
2203
+ viewsCount,
2204
+ likesCount,
2205
+ commentsCount,
2206
+ sharesCount,
2207
+ savesCount,
2208
+ isLiked,
2209
+ isSaved,
2210
+ isFollowing: !!isFollowing
2211
+ };
2212
+ },
2213
+
2214
  // ===== Jobs =====
2215
  jobs: async (parent, { page = 1, limit = 20, search, category, jobType, location }) => {
2216
  const skip = (page - 1) * limit;
graphql/schema.js CHANGED
@@ -1034,6 +1034,41 @@ const typeDefs = gql`
1034
  timestamp: Date!
1035
  }
1036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1037
  # ============================================
1038
  # 🔹 THEME TYPES
1039
  # ============================================
@@ -1561,6 +1596,8 @@ const typeDefs = gql`
1561
  userComments(userId: ID!, page: Int, limit: Int): CommentsResult!
1562
  reportedComments: [Comment!]! # للمشرف فقط
1563
 
 
 
1564
  # ===== Stories =====
1565
  storiesFeed(limit: Int): [StoryGroup!]!
1566
  storiesAll(page: Int, limit: Int): StoriesResult!
@@ -1595,6 +1632,10 @@ const typeDefs = gql`
1595
  storeSettings: StoreSettingsResult!
1596
  storeAnalytics(username: String!): StoreAnalytics!
1597
 
 
 
 
 
1598
  # ===== Cart =====
1599
  cart: Cart!
1600
  cartSummary: CartSummary!
 
1034
  timestamp: Date!
1035
  }
1036
 
1037
+
1038
+ # ============================================
1039
+ # 🔹 VIDEO TYPES
1040
+ # ============================================
1041
+
1042
+ type VideoPost {
1043
+ _id: ID!
1044
+ userId: User!
1045
+ content: String!
1046
+ video: PostVideo!
1047
+ images: [PostImage!]!
1048
+ likes: [Like!]!
1049
+ comments: [Comment!]!
1050
+ shares: [Share!]!
1051
+ saves: [Save!]!
1052
+ visibility: String!
1053
+ tags: [String!]!
1054
+ createdAt: Date!
1055
+ updatedAt: Date!
1056
+ # حسابية
1057
+ likesCount: Int!
1058
+ commentsCount: Int!
1059
+ sharesCount: Int!
1060
+ savesCount: Int!
1061
+ viewsCount: Int!
1062
+ isLiked: Boolean
1063
+ isSaved: Boolean
1064
+ isFollowing: Boolean
1065
+ }
1066
+
1067
+ type VideosResult {
1068
+ data: [VideoPost!]!
1069
+ pagination: Pagination!
1070
+ trending: [VideoPost!]!
1071
+ }
1072
  # ============================================
1073
  # 🔹 THEME TYPES
1074
  # ============================================
 
1596
  userComments(userId: ID!, page: Int, limit: Int): CommentsResult!
1597
  reportedComments: [Comment!]! # للمشرف فقط
1598
 
1599
+ getPostComments(postId: ID!, page: Int, limit: Int): CommentsResult!
1600
+
1601
  # ===== Stories =====
1602
  storiesFeed(limit: Int): [StoryGroup!]!
1603
  storiesAll(page: Int, limit: Int): StoriesResult!
 
1632
  storeSettings: StoreSettingsResult!
1633
  storeAnalytics(username: String!): StoreAnalytics!
1634
 
1635
+ # ===== Videos =====
1636
+ videosFeed(page: Int, limit: Int, type: String): VideosResult!
1637
+ video(id: ID!): VideoPost!
1638
+
1639
  # ===== Cart =====
1640
  cart: Cart!
1641
  cartSummary: CartSummary!
server.js CHANGED
@@ -34109,6 +34109,7 @@ function generateStaticSitemap() {
34109
  const staticPages = [
34110
  { loc: '/', changefreq: 'daily', priority: 1.0 },
34111
  { loc: '/root.html', changefreq: 'daily', priority: 1.0 },
 
34112
  { loc: '/feed.html', changefreq: 'daily', priority: 0.9 },
34113
  { loc: '/template-store.html', changefreq: 'daily', priority: 0.9 },
34114
  { loc: '/explore.html', changefreq: 'daily', priority: 0.9 },
 
34109
  const staticPages = [
34110
  { loc: '/', changefreq: 'daily', priority: 1.0 },
34111
  { loc: '/root.html', changefreq: 'daily', priority: 1.0 },
34112
+ { loc: '/discover.html', changefreq: 'daily', priority: 1.0 },
34113
  { loc: '/feed.html', changefreq: 'daily', priority: 0.9 },
34114
  { loc: '/template-store.html', changefreq: 'daily', priority: 0.9 },
34115
  { loc: '/explore.html', changefreq: 'daily', priority: 0.9 },