Forum Discussion
AI- based Post Sorting
I have developed a Post and Comment module inside SPFx. where i have 3 sharepoint list:-
- Posts
- Comments
- Reaction
here is the SPFX, with 2 posts and the first post has 2 comments:-
Currently the Posts and their Comments will be shown sorted by the created date , here is the method to show the posts:-
async getPostsPage(pageSize: number = 10, afterId?: number) {
let url = `${this.siteUrl}/_api/web/lists/getByTitle('SocialPosts')/items`
+ `?$select=Id,Title,Body,Created,Author/Id,Author/Title,LikeCount,CommentCount`
+ `&$expand=Author&$orderby=Id desc&$top=${pageSize}`;
if (afterId) url += `&$filter=Id lt ${afterId}`;
const data = await this.getJson<{ value: any[] }>(url);
return data.value.map(v => ({
Id: v.Id,
Title: v.Title,
Body: v.Body,
Created: v.Created,
AuthorId: v.Author?.Id,
AuthorTitle: v.Author?.Title,
LikeCount: v.LikeCount || 0,
CommentCount: v.CommentCount || 0
})) as import('../Models/Models').Post[];
}
and the comments:-
// ---------- Comments ----------
async getComments(postId: number): Promise<Comment[]> {
const url =
this.commentsUrl() +
`?$select=Id,Body,Created,Author/Id,Author/Title,LikeCount,Post/Id,PostId` +
`&$expand=Author,Post&$filter=PostId eq ${postId}&$orderby=Created asc`;
const data = await this.getJson<{ value: any[] }>(url);
return data.value.map((v) => ({
Id: v.Id,
Body: v.Body,
Created: v.Created,
AuthorId: v.Author?.Id,
AuthorTitle: v.Author?.Title,
LikeCount: v.LikeCount || 0,
PostId: v.PostId ?? v.Post?.Id,
}));
}
but the client is asking us if there is a way to order the items using AI algorithm so it show the most relevant posts and comments first? which approach i can follow? to allow order items inside SPFx using AI algorithm instead of Created date?? any advice?