Mark-Lasfar commited on
Commit ·
f96a5a2
1
Parent(s): 965981a
Add GraphQL
Browse files- graphql/resolvers.js +321 -322
graphql/resolvers.js
CHANGED
|
@@ -1684,335 +1684,334 @@ const Query = {
|
|
| 1684 |
|
| 1685 |
// ===== Stories =====
|
| 1686 |
|
| 1687 |
-
|
| 1688 |
-
storiesFeed: async (parent, { limit = 20 }, context) => {
|
| 1689 |
-
|
| 1690 |
-
|
| 1691 |
-
|
| 1692 |
-
|
| 1693 |
-
|
| 1694 |
-
|
| 1695 |
-
|
| 1696 |
-
|
| 1697 |
-
|
| 1698 |
-
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
|
| 1702 |
-
|
| 1703 |
-
|
| 1704 |
-
|
| 1705 |
-
|
| 1706 |
-
|
| 1707 |
-
|
| 1708 |
-
|
| 1709 |
-
|
| 1710 |
-
|
| 1711 |
-
|
| 1712 |
-
|
| 1713 |
-
|
| 1714 |
-
|
| 1715 |
-
|
| 1716 |
-
|
| 1717 |
-
|
| 1718 |
-
|
| 1719 |
-
|
| 1720 |
-
|
| 1721 |
-
|
| 1722 |
-
|
| 1723 |
-
|
| 1724 |
-
|
| 1725 |
-
|
| 1726 |
-
|
| 1727 |
-
|
| 1728 |
-
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
|
| 1732 |
-
},
|
| 1733 |
-
|
| 1734 |
|
| 1735 |
-
|
| 1736 |
-
storiesAll: async (parent, { page = 1, limit = 20 }, context) => {
|
| 1737 |
-
|
| 1738 |
-
|
| 1739 |
-
|
| 1740 |
-
|
| 1741 |
-
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
| 1745 |
-
|
| 1746 |
-
|
| 1747 |
-
|
| 1748 |
-
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
-
|
| 1761 |
-
|
| 1762 |
-
|
| 1763 |
-
|
| 1764 |
-
|
| 1765 |
-
|
| 1766 |
-
|
| 1767 |
-
|
| 1768 |
-
|
| 1769 |
-
|
| 1770 |
-
|
| 1771 |
-
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
|
| 1775 |
-
|
| 1776 |
-
|
| 1777 |
-
|
| 1778 |
-
|
| 1779 |
-
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
-
|
| 1784 |
-
|
| 1785 |
-
|
| 1786 |
-
|
| 1787 |
-
|
| 1788 |
-
|
| 1789 |
-
|
| 1790 |
-
|
| 1791 |
-
|
| 1792 |
-
|
| 1793 |
-
|
| 1794 |
-
|
| 1795 |
-
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
| 1805 |
-
|
| 1806 |
-
|
| 1807 |
-
|
| 1808 |
-
|
| 1809 |
-
|
| 1810 |
-
|
| 1811 |
-
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
},
|
| 1820 |
|
| 1821 |
-
|
| 1822 |
-
storiesDiscover: async (parent, { limit = 10 }, context) => {
|
| 1823 |
-
|
| 1824 |
-
|
| 1825 |
-
|
| 1826 |
-
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
| 1831 |
-
|
| 1832 |
-
|
| 1833 |
-
|
| 1834 |
-
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
-
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
|
| 1845 |
-
|
| 1846 |
-
|
| 1847 |
-
|
| 1848 |
-
|
| 1849 |
-
|
| 1850 |
-
|
| 1851 |
-
|
| 1852 |
-
|
| 1853 |
-
|
| 1854 |
-
|
| 1855 |
-
|
| 1856 |
-
|
| 1857 |
-
|
| 1858 |
-
|
| 1859 |
-
|
| 1860 |
-
|
| 1861 |
-
|
| 1862 |
-
|
| 1863 |
-
},
|
| 1864 |
|
| 1865 |
-
|
| 1866 |
-
storyStats: async (parent, args, context) => {
|
| 1867 |
-
|
| 1868 |
-
|
| 1869 |
-
|
| 1870 |
-
|
| 1871 |
-
|
| 1872 |
-
|
| 1873 |
-
|
| 1874 |
-
|
| 1875 |
-
|
| 1876 |
-
|
| 1877 |
-
|
| 1878 |
-
|
| 1879 |
-
|
| 1880 |
-
|
| 1881 |
-
|
| 1882 |
-
|
| 1883 |
-
|
| 1884 |
-
|
| 1885 |
-
|
| 1886 |
-
|
| 1887 |
-
|
| 1888 |
-
|
| 1889 |
-
|
| 1890 |
-
|
| 1891 |
-
|
| 1892 |
-
|
| 1893 |
-
|
| 1894 |
-
|
| 1895 |
-
|
| 1896 |
-
|
| 1897 |
-
|
| 1898 |
-
},
|
| 1899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1900 |
|
| 1901 |
-
|
| 1902 |
-
|
| 1903 |
-
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
|
| 1908 |
-
|
| 1909 |
-
|
| 1910 |
-
|
| 1911 |
-
|
| 1912 |
-
|
| 1913 |
-
|
| 1914 |
-
|
| 1915 |
-
|
| 1916 |
-
|
| 1917 |
-
|
| 1918 |
-
|
| 1919 |
-
|
| 1920 |
-
|
| 1921 |
-
|
| 1922 |
-
|
| 1923 |
-
|
| 1924 |
-
|
| 1925 |
-
|
| 1926 |
-
|
| 1927 |
-
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
|
| 1932 |
-
|
| 1933 |
-
|
| 1934 |
-
|
| 1935 |
-
|
| 1936 |
-
|
| 1937 |
-
|
| 1938 |
-
|
| 1939 |
-
|
| 1940 |
-
|
| 1941 |
-
|
| 1942 |
-
|
| 1943 |
-
|
| 1944 |
-
|
| 1945 |
-
throw new GraphQLError('You are not authorized to view viewers of this story');
|
| 1946 |
-
}
|
| 1947 |
-
|
| 1948 |
-
const skip = (page - 1) * limit;
|
| 1949 |
-
const views = story.views || [];
|
| 1950 |
-
const total = views.length;
|
| 1951 |
-
|
| 1952 |
-
const viewerIds = views.map(v => v.userId);
|
| 1953 |
-
const paginatedIds = viewerIds.slice(skip, skip + limit);
|
| 1954 |
-
|
| 1955 |
-
const viewers = await User.find({
|
| 1956 |
-
_id: { $in: paginatedIds }
|
| 1957 |
-
}); // ✅ بدون select عشان يجيب كل البيانات
|
| 1958 |
-
|
| 1959 |
-
const viewersWithTime = paginatedIds.map((id) => {
|
| 1960 |
-
const user = viewers.find(u => u._id.toString() === id.toString());
|
| 1961 |
-
const view = views.find(v => v.userId.toString() === id.toString());
|
| 1962 |
-
return {
|
| 1963 |
-
userId: user || { _id: id, username: 'Unknown', profile: {} },
|
| 1964 |
-
viewedAt: view?.viewedAt || new Date()
|
| 1965 |
-
};
|
| 1966 |
-
});
|
| 1967 |
-
|
| 1968 |
-
return {
|
| 1969 |
-
data: viewersWithTime,
|
| 1970 |
-
total,
|
| 1971 |
-
pagination: {
|
| 1972 |
-
page,
|
| 1973 |
-
limit,
|
| 1974 |
-
total,
|
| 1975 |
-
pages: Math.ceil(total / limit),
|
| 1976 |
-
hasNext: skip + limit < total,
|
| 1977 |
-
hasPrev: page > 1
|
| 1978 |
-
}
|
| 1979 |
-
};
|
| 1980 |
-
},
|
| 1981 |
|
| 1982 |
// ✅ story - قصة واحدة
|
| 1983 |
-
story: async (parent, { id }, context) => {
|
| 1984 |
-
|
| 1985 |
-
|
| 1986 |
-
|
| 1987 |
-
|
| 1988 |
-
|
| 1989 |
-
|
| 1990 |
-
|
| 1991 |
-
|
| 1992 |
-
|
| 1993 |
-
|
| 1994 |
-
|
| 1995 |
-
|
| 1996 |
-
|
| 1997 |
-
|
| 1998 |
-
|
| 1999 |
-
|
| 2000 |
-
|
| 2001 |
-
|
| 2002 |
-
|
| 2003 |
-
|
| 2004 |
-
|
| 2005 |
-
|
| 2006 |
-
|
| 2007 |
-
|
| 2008 |
-
|
| 2009 |
-
|
| 2010 |
-
|
| 2011 |
-
|
| 2012 |
-
|
| 2013 |
-
|
| 2014 |
-
|
| 2015 |
-
},
|
| 2016 |
// ===== Jobs =====
|
| 2017 |
jobs: async (parent, { page = 1, limit = 20, search, category, jobType, location }) => {
|
| 2018 |
const skip = (page - 1) * limit;
|
|
|
|
| 1684 |
|
| 1685 |
// ===== Stories =====
|
| 1686 |
|
| 1687 |
+
// ✅ storiesFeed - قصص المتابعين فقط
|
| 1688 |
+
storiesFeed: async (parent, { limit = 20 }, context) => {
|
| 1689 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1690 |
+
|
| 1691 |
+
const following = await Follow.find({
|
| 1692 |
+
followerId: context.user.userId,
|
| 1693 |
+
status: 'accepted'
|
| 1694 |
+
}).select('followingId');
|
| 1695 |
+
|
| 1696 |
+
const followingIds = following.map(f => f.followingId);
|
| 1697 |
+
followingIds.push(context.user.userId);
|
| 1698 |
+
|
| 1699 |
+
const stories = await Story.find({
|
| 1700 |
+
userId: { $in: followingIds },
|
| 1701 |
+
expiresAt: { $gt: new Date() },
|
| 1702 |
+
$or: [
|
| 1703 |
+
{ visibility: 'public' },
|
| 1704 |
+
{ visibility: 'followers' },
|
| 1705 |
+
{ visibility: 'only-me', userId: context.user.userId }
|
| 1706 |
+
]
|
| 1707 |
+
})
|
| 1708 |
+
.sort({ createdAt: -1 })
|
| 1709 |
+
.limit(limit * 2)
|
| 1710 |
+
.populate('userId')
|
| 1711 |
+
.lean();
|
| 1712 |
+
|
| 1713 |
+
const storiesByUser = {};
|
| 1714 |
+
stories.forEach(story => {
|
| 1715 |
+
const userId = story.userId._id.toString();
|
| 1716 |
+
if (!storiesByUser[userId]) {
|
| 1717 |
+
storiesByUser[userId] = {
|
| 1718 |
+
user: story.userId,
|
| 1719 |
+
stories: []
|
| 1720 |
+
};
|
| 1721 |
+
}
|
| 1722 |
+
const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
|
| 1723 |
+
storiesByUser[userId].stories.push({
|
| 1724 |
+
...story,
|
| 1725 |
+
viewsCount: (story.views || []).length,
|
| 1726 |
+
reactionsCount: (story.reactions || []).length,
|
| 1727 |
+
hasViewed: hasViewed
|
| 1728 |
+
});
|
| 1729 |
+
});
|
| 1730 |
+
|
| 1731 |
+
return Object.values(storiesByUser);
|
| 1732 |
+
},
|
|
|
|
| 1733 |
|
| 1734 |
+
// ✅ storiesAll - كل القصص (للاكتشاف + المتابعين)
|
| 1735 |
+
storiesAll: async (parent, { page = 1, limit = 20 }, context) => {
|
| 1736 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1737 |
+
|
| 1738 |
+
const skip = (page - 1) * limit;
|
| 1739 |
+
|
| 1740 |
+
const following = await Follow.find({
|
| 1741 |
+
followerId: context.user.userId,
|
| 1742 |
+
status: 'accepted'
|
| 1743 |
+
}).select('followingId');
|
| 1744 |
+
|
| 1745 |
+
const followingIds = following.map(f => f.followingId.toString());
|
| 1746 |
+
|
| 1747 |
+
const allStories = await Story.find({
|
| 1748 |
+
expiresAt: { $gt: new Date() },
|
| 1749 |
+
$or: [
|
| 1750 |
+
{ visibility: 'public' },
|
| 1751 |
+
{ userId: { $in: followingIds }, visibility: { $in: ['public', 'followers'] } },
|
| 1752 |
+
{ userId: context.user.userId }
|
| 1753 |
+
]
|
| 1754 |
+
})
|
| 1755 |
+
.sort({ createdAt: -1 })
|
| 1756 |
+
.skip(skip)
|
| 1757 |
+
.limit(limit)
|
| 1758 |
+
.populate('userId')
|
| 1759 |
+
.lean();
|
| 1760 |
+
|
| 1761 |
+
const storiesMap = {};
|
| 1762 |
+
const userIds = new Set();
|
| 1763 |
+
|
| 1764 |
+
allStories.forEach(story => {
|
| 1765 |
+
const userId = story.userId._id.toString();
|
| 1766 |
+
userIds.add(userId);
|
| 1767 |
+
|
| 1768 |
+
if (!storiesMap[userId]) {
|
| 1769 |
+
storiesMap[userId] = {
|
| 1770 |
+
user: story.userId,
|
| 1771 |
+
stories: [],
|
| 1772 |
+
isFollowing: followingIds.includes(userId),
|
| 1773 |
+
isCurrentUser: userId === context.user.userId
|
| 1774 |
+
};
|
| 1775 |
+
}
|
| 1776 |
+
const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
|
| 1777 |
+
storiesMap[userId].stories.push({
|
| 1778 |
+
...story,
|
| 1779 |
+
viewsCount: (story.views || []).length,
|
| 1780 |
+
reactionsCount: (story.reactions || []).length,
|
| 1781 |
+
hasViewed: hasViewed
|
| 1782 |
+
});
|
| 1783 |
+
});
|
| 1784 |
+
|
| 1785 |
+
const sortedGroups = Object.values(storiesMap).sort((a, b) => {
|
| 1786 |
+
if (a.isFollowing && !b.isFollowing) return -1;
|
| 1787 |
+
if (!a.isFollowing && b.isFollowing) return 1;
|
| 1788 |
+
if (a.isCurrentUser && !b.isCurrentUser) return -1;
|
| 1789 |
+
if (!a.isCurrentUser && b.isCurrentUser) return 1;
|
| 1790 |
+
return b.stories.length - a.stories.length;
|
| 1791 |
+
});
|
| 1792 |
+
|
| 1793 |
+
const paginatedData = sortedGroups.slice(0, limit);
|
| 1794 |
+
const total = sortedGroups.length;
|
| 1795 |
+
const totalStories = allStories.length;
|
| 1796 |
+
const followingStories = allStories.filter(s =>
|
| 1797 |
+
followingIds.includes(s.userId._id.toString())
|
| 1798 |
+
).length;
|
| 1799 |
+
|
| 1800 |
+
return {
|
| 1801 |
+
data: paginatedData,
|
| 1802 |
+
pagination: {
|
| 1803 |
+
page,
|
| 1804 |
+
limit,
|
| 1805 |
+
total,
|
| 1806 |
+
pages: Math.ceil(total / limit),
|
| 1807 |
+
hasNext: skip + limit < total,
|
| 1808 |
+
hasPrev: page > 1
|
| 1809 |
+
},
|
| 1810 |
+
stats: {
|
| 1811 |
+
totalStories,
|
| 1812 |
+
myStories: allStories.filter(s => s.userId._id.toString() === context.user.userId).length,
|
| 1813 |
+
followingStories,
|
| 1814 |
+
discoverStories: totalStories - followingStories,
|
| 1815 |
+
followingCount: followingIds.length
|
| 1816 |
+
}
|
| 1817 |
+
};
|
| 1818 |
+
},
|
| 1819 |
|
| 1820 |
+
// ✅ storiesDiscover - قصص جديدة للاكتشاف
|
| 1821 |
+
storiesDiscover: async (parent, { limit = 10 }, context) => {
|
| 1822 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1823 |
+
|
| 1824 |
+
const following = await Follow.find({
|
| 1825 |
+
followerId: context.user.userId,
|
| 1826 |
+
status: 'accepted'
|
| 1827 |
+
}).select('followingId');
|
| 1828 |
+
|
| 1829 |
+
const followingIds = following.map(f => f.followingId);
|
| 1830 |
+
followingIds.push(context.user.userId);
|
| 1831 |
+
|
| 1832 |
+
const discoverStories = await Story.find({
|
| 1833 |
+
userId: { $nin: followingIds },
|
| 1834 |
+
visibility: 'public',
|
| 1835 |
+
expiresAt: { $gt: new Date() }
|
| 1836 |
+
})
|
| 1837 |
+
.sort({ createdAt: -1 })
|
| 1838 |
+
.limit(parseInt(limit))
|
| 1839 |
+
.populate('userId')
|
| 1840 |
+
.lean();
|
| 1841 |
+
|
| 1842 |
+
const storiesByUser = {};
|
| 1843 |
+
discoverStories.forEach(story => {
|
| 1844 |
+
const userId = story.userId._id.toString();
|
| 1845 |
+
if (!storiesByUser[userId]) {
|
| 1846 |
+
storiesByUser[userId] = {
|
| 1847 |
+
user: story.userId,
|
| 1848 |
+
stories: [],
|
| 1849 |
+
isFollowing: false
|
| 1850 |
+
};
|
| 1851 |
+
}
|
| 1852 |
+
const hasViewed = story.views?.some(v => v.userId?.toString() === context.user.userId) || false;
|
| 1853 |
+
storiesByUser[userId].stories.push({
|
| 1854 |
+
...story,
|
| 1855 |
+
viewsCount: (story.views || []).length,
|
| 1856 |
+
reactionsCount: (story.reactions || []).length,
|
| 1857 |
+
hasViewed: hasViewed
|
| 1858 |
+
});
|
| 1859 |
+
});
|
| 1860 |
+
|
| 1861 |
+
return Object.values(storiesByUser);
|
| 1862 |
+
},
|
| 1863 |
|
| 1864 |
+
// ✅ storyStats - إحصائيات القصص
|
| 1865 |
+
storyStats: async (parent, args, context) => {
|
| 1866 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1867 |
+
|
| 1868 |
+
const following = await Follow.find({
|
| 1869 |
+
followerId: context.user.userId,
|
| 1870 |
+
status: 'accepted'
|
| 1871 |
+
}).select('followingId');
|
| 1872 |
+
|
| 1873 |
+
const followingIds = following.map(f => f.followingId);
|
| 1874 |
+
|
| 1875 |
+
const [totalStories, myStories, followingStories, discoverStories] = await Promise.all([
|
| 1876 |
+
Story.countDocuments({ expiresAt: { $gt: new Date() } }),
|
| 1877 |
+
Story.countDocuments({ userId: context.user.userId, expiresAt: { $gt: new Date() } }),
|
| 1878 |
+
Story.countDocuments({
|
| 1879 |
+
userId: { $in: followingIds },
|
| 1880 |
+
expiresAt: { $gt: new Date() },
|
| 1881 |
+
visibility: { $in: ['public', 'followers'] }
|
| 1882 |
+
}),
|
| 1883 |
+
Story.countDocuments({
|
| 1884 |
+
userId: { $nin: [...followingIds, context.user.userId] },
|
| 1885 |
+
expiresAt: { $gt: new Date() },
|
| 1886 |
+
visibility: 'public'
|
| 1887 |
+
})
|
| 1888 |
+
]);
|
| 1889 |
+
|
| 1890 |
+
return {
|
| 1891 |
+
totalStories,
|
| 1892 |
+
myStories,
|
| 1893 |
+
followingStories,
|
| 1894 |
+
discoverStories,
|
| 1895 |
+
followingCount: followingIds.length
|
| 1896 |
+
};
|
| 1897 |
+
},
|
| 1898 |
|
| 1899 |
+
// ✅ userStories - قصص مستخدم معين
|
| 1900 |
+
userStories: async (parent, { userId }, context) => {
|
| 1901 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1902 |
+
|
| 1903 |
+
const isOwner = userId === context.user.userId;
|
| 1904 |
+
const isFollowing = await Follow.exists({
|
| 1905 |
+
followerId: context.user.userId,
|
| 1906 |
+
followingId: userId,
|
| 1907 |
+
status: 'accepted'
|
| 1908 |
+
});
|
| 1909 |
+
|
| 1910 |
+
let visibilityQuery = { userId, expiresAt: { $gt: new Date() } };
|
| 1911 |
+
|
| 1912 |
+
if (!isOwner) {
|
| 1913 |
+
visibilityQuery.$or = [
|
| 1914 |
+
{ visibility: 'public' },
|
| 1915 |
+
{ visibility: 'followers' }
|
| 1916 |
+
];
|
| 1917 |
+
if (!isFollowing) {
|
| 1918 |
+
visibilityQuery.visibility = 'public';
|
| 1919 |
+
}
|
| 1920 |
+
}
|
| 1921 |
+
|
| 1922 |
+
const stories = await Story.find(visibilityQuery)
|
| 1923 |
+
.sort({ createdAt: -1 })
|
| 1924 |
+
.populate('userId')
|
| 1925 |
+
.lean();
|
| 1926 |
+
|
| 1927 |
+
return stories.map(story => ({
|
| 1928 |
+
...story,
|
| 1929 |
+
viewsCount: (story.views || []).length,
|
| 1930 |
+
reactionsCount: (story.reactions || []).length,
|
| 1931 |
+
hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
|
| 1932 |
+
userId: story.userId
|
| 1933 |
+
}));
|
| 1934 |
+
},
|
| 1935 |
|
| 1936 |
+
// ✅ storyViewers - جلب مشاهدين القصة (للمالك فقط)
|
| 1937 |
+
storyViewers: async (parent, { storyId, page = 1, limit = 20 }, context) => {
|
| 1938 |
+
if (!context.user) throw new GraphQLError('Not authenticated');
|
| 1939 |
+
|
| 1940 |
+
const story = await Story.findById(storyId);
|
| 1941 |
+
if (!story) throw new GraphQLError('Story not found');
|
| 1942 |
+
|
| 1943 |
+
if (story.userId.toString() !== context.user.userId) {
|
| 1944 |
+
throw new GraphQLError('You are not authorized to view viewers of this story');
|
| 1945 |
+
}
|
| 1946 |
+
|
| 1947 |
+
const skip = (page - 1) * limit;
|
| 1948 |
+
const views = story.views || [];
|
| 1949 |
+
const total = views.length;
|
| 1950 |
+
|
| 1951 |
+
const viewerIds = views.map(v => v.userId);
|
| 1952 |
+
const paginatedIds = viewerIds.slice(skip, skip + limit);
|
| 1953 |
+
|
| 1954 |
+
const viewers = await User.find({
|
| 1955 |
+
_id: { $in: paginatedIds }
|
| 1956 |
+
});
|
| 1957 |
+
|
| 1958 |
+
const viewersWithTime = paginatedIds.map((id) => {
|
| 1959 |
+
const user = viewers.find(u => u._id.toString() === id.toString());
|
| 1960 |
+
const view = views.find(v => v.userId.toString() === id.toString());
|
| 1961 |
+
return {
|
| 1962 |
+
userId: user || { _id: id, username: 'Unknown', profile: {} },
|
| 1963 |
+
viewedAt: view?.viewedAt || new Date()
|
| 1964 |
+
};
|
| 1965 |
+
});
|
| 1966 |
+
|
| 1967 |
+
return {
|
| 1968 |
+
data: viewersWithTime,
|
| 1969 |
+
total,
|
| 1970 |
+
pagination: {
|
| 1971 |
+
page,
|
| 1972 |
+
limit,
|
| 1973 |
+
total,
|
| 1974 |
+
pages: Math.ceil(total / limit),
|
| 1975 |
+
hasNext: skip + limit < total,
|
| 1976 |
+
hasPrev: page > 1
|
| 1977 |
+
}
|
| 1978 |
+
};
|
| 1979 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1980 |
|
| 1981 |
// ✅ story - قصة واحدة
|
| 1982 |
+
story: async (parent, { id }, context) => {
|
| 1983 |
+
const story = await Story.findById(id)
|
| 1984 |
+
.populate('userId')
|
| 1985 |
+
.lean();
|
| 1986 |
+
|
| 1987 |
+
if (!story) throw new GraphQLError('Story not found');
|
| 1988 |
+
|
| 1989 |
+
if (story.expiresAt && new Date(story.expiresAt) < new Date()) {
|
| 1990 |
+
throw new GraphQLError('Story has expired');
|
| 1991 |
+
}
|
| 1992 |
+
|
| 1993 |
+
const isOwner = context.user && context.user.userId === story.userId._id.toString();
|
| 1994 |
+
const isFollowing = context.user ? await Follow.exists({
|
| 1995 |
+
followerId: context.user.userId,
|
| 1996 |
+
followingId: story.userId._id,
|
| 1997 |
+
status: 'accepted'
|
| 1998 |
+
}) : false;
|
| 1999 |
+
|
| 2000 |
+
if (!isOwner && story.visibility === 'only-me') {
|
| 2001 |
+
throw new GraphQLError('This story is private');
|
| 2002 |
+
}
|
| 2003 |
+
if (story.visibility === 'followers' && !isOwner && !isFollowing) {
|
| 2004 |
+
throw new GraphQLError('This story is only visible to followers');
|
| 2005 |
+
}
|
| 2006 |
+
|
| 2007 |
+
return {
|
| 2008 |
+
...story,
|
| 2009 |
+
viewsCount: (story.views || []).length,
|
| 2010 |
+
reactionsCount: (story.reactions || []).length,
|
| 2011 |
+
hasViewed: story.views?.some(v => v.userId?.toString() === context.user.userId) || false,
|
| 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;
|