-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4833ec7
commit 7de075a
Showing
2 changed files
with
29 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |