Skip to content

Commit

Permalink
#165: first attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
basmasking committed May 9, 2024
1 parent 4833ec7 commit 7de075a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/domain/post/data/retrieveByCreators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import database, { RecordQuery, RecordSort, SortDirections } from '^/integrations/database/module';
import database, { RecordQuery } from '^/integrations/database/module';

import { RECORD_TYPE } from '../definitions/constants';
import type PostData from './PostData';
Expand All @@ -8,9 +8,9 @@ import createData from './mapRecord';
export default async function retrieveByCreators(creatorIds: string[]): Promise<PostData[]>
{
const query: RecordQuery = { creatorId: { 'IN': creatorIds } };
const sort: RecordSort = { createdAt: SortDirections.DESCENDING };
//const sort: RecordSort = { createdAt: SortDirections.DESCENDING };

const records = await database.searchRecords(RECORD_TYPE, query, undefined, sort);
const records = await database.searchRecords(RECORD_TYPE, query, undefined, undefined);

const dataList = records.map(createData);

Expand Down
27 changes: 26 additions & 1 deletion src/domain/post/getTimeline.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@

import type Requester from '../authentication/Requester';
import retrieveFollowerData from '../relation/data/retrieveByFollower';
import SortOptions from '../relation/definitions/SortOptions';
import PostData from './data/PostData';
import retrieveByCreators from './data/retrieveByCreators';
import type PostView from './view/PostView';
import createView from './view/createView';

export default async function getTimeline(requester: Requester): Promise<PostView[]>
export default async function getTimeline(requester: Requester, sortOption: SortOptions): Promise<PostView[]>
{
const followerData = await retrieveFollowerData(requester.id);
const followingIds = followerData.map(data => data.followingId);
const postData = await retrieveByCreators(followingIds);

sortOption === SortOptions.RECENT
? postData.sort(sortRecent)
: postData.sort(sortPopular);

const views = postData.map(item => createView(requester, item));

return Promise.all(views);
}

function sortRecent(a: PostData, b: PostData)
{
if (a.createdAt > b.createdAt) return 1;
if (a.createdAt < b.createdAt) return -1;

return 0;
}

function sortPopular(a: PostData, b: PostData)
{
const popularityA = a.reactionCount * 2 + a.ratingCount;
const popularityB = b.reactionCount * 2 + b.ratingCount;

if (popularityA > popularityB) return -1;
if (popularityA < popularityB) return 1;

return 0;
}

0 comments on commit 7de075a

Please sign in to comment.