Untitled
unknown
plain_text
a year ago
13 kB
9
Indexable
const processedUsers = await Promise.all(
users.map(async (u) => {
try {
const user = await UserHelper.findByEmail(u.email);
if (!user) {
return {};
}
const communities = await UserHelper.getUserCommunities(u.id);
const communityIds = communities.filter((c) => !c.blocked).map((c) => c.communityId);
const [userFollowingNonProfits, companyFollowingNonProfits] = await Promise.all([
db.userNonProfits.findAll({ where: { user_id: u.id }, attributes: ['nonprofit_id'], raw: true }),
db.companyNonProfits.findAll({
where: { company_id: { [db.Sequelize.Op.in]: communityIds } },
attributes: ['nonprofit_id'],
raw: true,
}),
]);
const nonProfitIds = new Set([
...userFollowingNonProfits.map((userNpo) => userNpo.nonprofit_id),
...companyFollowingNonProfits.map((companyNpo) => companyNpo.nonprofit_id),
]);
// const postQuery = await UserFeedService.createBasicPostQuery(user, communities, null, 'all', null);
// const nonProfitPosts = await UserFeedService.queryPosts(
// postQuery.query,
// postQuery.allowedCampaignTypes,
// 10,
// postQuery.volunteerOpportunity
// );
// const getFilteredPosts = (post) => {
// if (!post.sourcePostData) return false;
// if (nonProfitIds.has(post.nonProfitId)) return true;
// if (communityIds.includes(post.companyId)) return true;
// if (post?.likingUsers?.find((l) => communityIds.includes(l.companyId))) return true;
// if (post?.commentingUsers?.find((l) => communityIds.includes(l.companyId))) return true;
// return false;
// };
// const nonProfitPosts = posts
// .filter(
// (post) =>
// !!post.sourcePostData && (nonProfitIds.has(post.nonProfitId) || communityIds.includes(post.companyId))
// )
// .slice(0, 20);
// const getFilteredPosts = (post) => {
// if (!post?.sourcePostData) return false;
// const { nonProfitId, companyId, likingUsers, commentingUsers } = post;
// // Check if post is related to a known nonprofit or community
// if (nonProfitIds.has(nonProfitId) || communityIds.includes(companyId)) return true;
// // Check if any liking or commenting user is related to a known community
// const isFromCommunity = (user) => communityIds.includes(user.companyId);
// if (likingUsers?.some(isFromCommunity) || commentingUsers?.some(isFromCommunity)) return true;
// return false;
// };
// const nonProfitPosts = posts.filter(getFilteredPosts).slice(0, 100); // Limit to first 20 posts
const MAX_NEWS_FEEDS = 10;
const MAX_USER_POSTS = 10;
const MAX_VOLUNTEERING = 10;
const MAX_FUNRDRAISER = 10;
let NewsFeedsCount = 0;
let UserPostsCount = 0;
let VolunteeringCount = 0;
let FundraiserCount = 0;
const getFilteredPosts = (post) => {
if (!post?.sourcePostData) return false;
const { nonProfitId, companyId, likingUsers, commentingUsers } = post;
// Check if post is related to a known nonprofit or community
if (nonProfitIds.has(nonProfitId) || communityIds.includes(companyId)) return true;
// Check if any liking or commenting user is related to a known community
const isFromCommunity = (user) => communityIds.includes(user.companyId);
if (likingUsers?.some(isFromCommunity) || commentingUsers?.some(isFromCommunity)) return true;
return false;
};
const nonProfitPosts = posts.filter((post) => {
if (!getFilteredPosts(post)) return false;
const { sourcePostType, sourcePostData } = post;
if (sourcePostType === PostType.NewsFeeds) {
if (NewsFeedsCount >= MAX_NEWS_FEEDS) return false; // Skip if max NewsFeeds reached
NewsFeedsCount++;
} else if (sourcePostType === PostType.UserPost) {
if (UserPostsCount >= MAX_USER_POSTS) return false; // Skip if max UserPosts reached
UserPostsCount++;
} else if (sourcePostType === PostType.Campaign && sourcePostData.campaignType === CampaignType.FundRaise) {
if (FundraiserCount >= MAX_FUNRDRAISER) return false; // Skip if max UserPosts reached
FundraiserCount++;
} else if (sourcePostType === PostType.Volunteering) {
if (VolunteeringCount >= MAX_VOLUNTEERING) return false; // Skip if max UserPosts reached
VolunteeringCount++;
}
return true;
});
// .slice(0, 100); // Limit to first 100 posts
// TODO: check
// const postsWithMetadata = await UserFeedService.attachPostsMetadata(nonProfitPosts, user);
let homeTwitterFeeds = [];
let externalTwitterFeeds = [];
// let filteredPosts = await Promise.all(
// nonProfitPosts.filter(async (post) => {
// if (!post.sourcePostData) return false;
// if (post.sourcePostType === PostType.NewsFeeds) {
// if (post.homeFeedVisibility) {
// homeTwitterFeeds.push(post);
// } else {
// externalTwitterFeeds.push(post);
// }
// return homeTwitterFeeds.length <= 10 && post.homeFeedVisibility;
// }
// if (post.sourcePostType === PostType.UserPost) return true;
// if (post.sourcePostType === PostType.Volunteering) {
// // const opportunity = post.sourcePostData?.volunteerOpportunity;
// // const diffInDays = differenceInDays(new Date(opportunity.endsOn), new Date());
// // return diffInDays > 0;
// const volunteeringId = post.sourcePostData?.volunteeringId;
// const opportunity = await db.volunteerOpportunities.findByPk(volunteeringId);
// if (!opportunity) {
// return false;
// }
// const diffInDays = differenceInDays(new Date(opportunity?.endsOn), new Date());
// return diffInDays >= 0;
// }
// if (
// post.sourcePostType === PostType.Campaign &&
// post.sourcePostData?.campaignType === CampaignType.FundRaise
// ) {
// const diffInDays = differenceInDays(new Date(post.sourcePostData.endDate), new Date());
// return diffInDays >= 0;
// }
// return false;
// })
// );
let filteredPosts = await Promise.all(
nonProfitPosts.map(async (post) => {
if (post.sourcePostType === PostType.Volunteering) {
const volunteeringId = post.sourcePostData?.volunteeringId;
const opportunity = await db.volunteerOpportunities.findByPk(volunteeringId);
if (!opportunity) return null;
const diffInDays = differenceInDays(new Date(opportunity.endsOn), new Date());
return diffInDays >= 0 ? post : null;
} else if (
post.sourcePostType === PostType.Campaign &&
post.sourcePostData?.campaignType === CampaignType.FundRaise
) {
const diffInDays = differenceInDays(new Date(post.sourcePostData.endDate), new Date());
return diffInDays >= 0 ? post : null;
} else if (post.sourcePostType === PostType.UserPost) {
const diffInDays = differenceInDays(new Date(), new Date(post.sourcePostData.createdOn));
return diffInDays <= 7 ? post : null;
} else if (post.sourcePostType === PostType.NewsFeeds) {
return post;
}
return null;
})
);
filteredPosts = filteredPosts.filter(Boolean); // Remove null entries
if (homeTwitterFeeds.length < 5) {
filteredPosts = [...filteredPosts, ...externalTwitterFeeds.slice(0, 5 - homeTwitterFeeds.length)];
}
let userPostsCount = filteredPosts.length;
if (userPostsCount < 5) {
// const [userFollowingNonProfits] = await Promise.all([
// db.userNonProfits.findAll({ where: { user_id: u.id }, attributes: ['nonprofit_id'], raw: true }),
// ]);
const userFollowingNonProfitIds = userFollowingNonProfits.map((userNpo) => userNpo.nonprofit_id);
const userSupportedNonprofitDetails = await db.nonprofits.findAll({
where: {
id: {
[db.Sequelize.Op.in]: userFollowingNonProfitIds,
},
blocked: {
[db.Sequelize.Op.or]: [false, null],
},
},
attributes: ['name'],
});
return {
...user,
communities,
supportedNonprofits: userSupportedNonprofitDetails,
allPosts: {
// filteredPosts,
// [CampaignType.FundRaise]: filteredPosts.filter(
// (post) =>
// post.sourcePostType === PostType.Campaign &&
// post.sourcePostData?.campaignType === CampaignType.FundRaise
// ),
// [PostType.Volunteering]: filteredPosts.filter((post) => post.sourcePostType === PostType.Volunteering),
// [PostType.NewsFeeds]: filteredPosts.filter((post) => post.sourcePostType === PostType.NewsFeeds),
// [PostType.UserPost]: filteredPosts.filter((post) => post.sourcePostType === PostType.UserPost),
totalPostCount: userPostsCount,
},
};
}
// const [fundraisers, volunteerings, newsfeeds, userposts] = await Promise.all([
// this.attachWeeklyDigestPostsMetadata(
// filteredPosts
// .filter(
// (post) =>
// post.sourcePostType === PostType.Campaign &&
// post.sourcePostData?.campaignType === CampaignType.FundRaise
// )
// .slice(0, 5),
// user
// ),
// this.attachWeeklyDigestPostsMetadata(
// filteredPosts.filter((post) => post.sourcePostType === PostType.Volunteering).slice(0, 5),
// user
// ),
// this.attachWeeklyDigestPostsMetadata(
// filteredPosts.filter((post) => post.sourcePostType === PostType.NewsFeeds).slice(0, 5),
// user
// ),
// this.attachWeeklyDigestPostsMetadata(
// filteredPosts.filter((post) => post.sourcePostType === PostType.UserPost).slice(0, 5),
// user
// ),
// ]);
// let userObject = {
// ...user,
// communities,
// // supportedNonprofits: userSupportedNonprofitDetails,
// allPosts: {
// // filteredPosts,
// [CampaignType.FundRaise]: fundraisers,
// [PostType.Volunteering]: volunteerings,
// [PostType.NewsFeeds]: newsfeeds,
// [PostType.UserPost]: userposts,
// totalPostCount: userPostsCount,
// },
// };
return {
...user,
communities,
// supportedNonprofits: userSupportedNonprofitDetails,
allPosts: {
// filteredPosts,
[CampaignType.FundRaise]: filteredPosts.filter(
(post) =>
post.sourcePostType === PostType.Campaign &&
post.sourcePostData?.campaignType === CampaignType.FundRaise
),
[PostType.Volunteering]: filteredPosts.filter((post) => post.sourcePostType === PostType.Volunteering),
[PostType.NewsFeeds]: filteredPosts.filter((post) => post.sourcePostType === PostType.NewsFeeds),
[PostType.UserPost]: filteredPosts.filter((post) => post.sourcePostType === PostType.UserPost),
totalPostCount: userPostsCount,
},
};
} catch (e) {
console.log(e);
return null;
}
})
);Editor is loading...
Leave a Comment