Mark-Lasfar commited on
Commit ·
f5287d7
1
Parent(s): 459ac93
Add GraphQL
Browse files- graphql/resolvers.js +61 -32
graphql/resolvers.js
CHANGED
|
@@ -1662,38 +1662,67 @@ const Query = {
|
|
| 1662 |
},
|
| 1663 |
|
| 1664 |
post: async (parent, { id }, context) => {
|
| 1665 |
-
|
| 1666 |
-
|
| 1667 |
-
|
| 1668 |
-
|
| 1669 |
-
|
| 1670 |
-
|
| 1671 |
-
|
| 1672 |
-
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
| 1680 |
-
|
| 1681 |
-
|
| 1682 |
-
|
| 1683 |
-
|
| 1684 |
-
|
| 1685 |
-
|
| 1686 |
-
|
| 1687 |
-
|
| 1688 |
-
|
| 1689 |
-
sharedFrom
|
| 1690 |
-
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1697 |
},
|
| 1698 |
|
| 1699 |
userPosts: async (parent, { userId, page = 1, limit = 20 }, context) => {
|
|
|
|
| 1662 |
},
|
| 1663 |
|
| 1664 |
post: async (parent, { id }, context) => {
|
| 1665 |
+
const post = await Post.findById(id)
|
| 1666 |
+
.populate('userId', 'username profile.nickname profile.avatar profile.jobTitle profile.bio')
|
| 1667 |
+
.lean();
|
| 1668 |
+
|
| 1669 |
+
if (!post) throw new GraphQLError('Post not found');
|
| 1670 |
+
|
| 1671 |
+
// التحقق من الخصوصية
|
| 1672 |
+
const isOwner = context.user && context.user.userId === post.userId._id.toString();
|
| 1673 |
+
const isFollowing = context.user ? await Follow.findOne({
|
| 1674 |
+
followerId: context.user.userId,
|
| 1675 |
+
followingId: post.userId._id,
|
| 1676 |
+
status: 'accepted'
|
| 1677 |
+
}) : false;
|
| 1678 |
+
|
| 1679 |
+
if (!isOwner && post.visibility === 'only-me') {
|
| 1680 |
+
throw new GraphQLError('This post is private');
|
| 1681 |
+
}
|
| 1682 |
+
if (post.visibility === 'followers' && !isOwner && !isFollowing) {
|
| 1683 |
+
throw new GraphQLError('This post is only visible to followers');
|
| 1684 |
+
}
|
| 1685 |
+
|
| 1686 |
+
// ============================================================
|
| 1687 |
+
// ✅ جلب البوست الأصلي إذا كان هذا البوست مشاركة
|
| 1688 |
+
// ============================================================
|
| 1689 |
+
let sharedFrom = post.sharedFrom || null;
|
| 1690 |
+
|
| 1691 |
+
if (sharedFrom && sharedFrom.originalPostId) {
|
| 1692 |
+
const originalPost = await Post.findById(sharedFrom.originalPostId)
|
| 1693 |
+
.populate('userId', 'username profile.nickname profile.avatar profile.jobTitle')
|
| 1694 |
+
.lean();
|
| 1695 |
+
|
| 1696 |
+
if (originalPost) {
|
| 1697 |
+
sharedFrom = {
|
| 1698 |
+
originalPostId: originalPost._id,
|
| 1699 |
+
originalAuthorId: originalPost.userId?._id || null,
|
| 1700 |
+
originalAuthorName: originalPost.userId?.profile?.nickname || originalPost.userId?.username || 'Unknown',
|
| 1701 |
+
originalAuthorAvatar: originalPost.userId?.profile?.avatar || null,
|
| 1702 |
+
originalAuthorJobTitle: originalPost.userId?.profile?.jobTitle || null,
|
| 1703 |
+
originalContent: originalPost.content || '',
|
| 1704 |
+
originalImages: originalPost.images || [],
|
| 1705 |
+
originalVideo: originalPost.video || null,
|
| 1706 |
+
originalCreatedAt: originalPost.createdAt || null,
|
| 1707 |
+
originalLikesCount: originalPost.likes?.length || 0,
|
| 1708 |
+
originalCommentsCount: originalPost.comments?.length || 0,
|
| 1709 |
+
originalSharesCount: originalPost.shares?.length || 0,
|
| 1710 |
+
sharedAt: post.sharedFrom?.sharedAt || null
|
| 1711 |
+
};
|
| 1712 |
+
}
|
| 1713 |
+
}
|
| 1714 |
+
|
| 1715 |
+
// ✅ حساب القيم مباشرة
|
| 1716 |
+
return {
|
| 1717 |
+
...post,
|
| 1718 |
+
sharedFrom, // ✅ الآن فيها كل البيانات
|
| 1719 |
+
likesCount: post.likes?.length || 0,
|
| 1720 |
+
commentsCount: post.comments?.length || 0,
|
| 1721 |
+
sharesCount: post.shares?.length || 0,
|
| 1722 |
+
savesCount: post.saves?.length || 0,
|
| 1723 |
+
isLiked: context.user ? post.likes?.some(l => l.userId?.toString() === context.user.userId) : false,
|
| 1724 |
+
isSaved: context.user ? post.saves?.some(s => s.userId?.toString() === context.user.userId) : false
|
| 1725 |
+
};
|
| 1726 |
},
|
| 1727 |
|
| 1728 |
userPosts: async (parent, { userId, page = 1, limit = 20 }, context) => {
|