i need to calculate mutual friends between users, right now it is working fine, but i don’t think it will work for users who have a large follower base, it will take a long to load and compare. please suggest me good db design/query(if needed).
I’ve created tables and have a query like this:
table:
export enum FollowStatus {
REQUESTED = "REQUESTED",
APPROVED = "APPROVED",
DECLINED = "DECLINED"
}
@Entity("followers")
export class Followers {
@PrimaryGeneratedColumn()
@Property()
id: number;
@Property()
@Column()
followerId: string;
@ManyToOne(() => Users, { eager: true })
@JoinColumn({ name: "followerId" })
follower: Users;
@Property()
@Column()
followedId: string;
@ManyToOne(() => Users, { eager: true })
@JoinColumn({ name: "followedId" })
followed: Users;
@Column({
type: "enum",
enum: FollowStatus,
default: FollowStatus.REQUESTED
})
@Property()
status: FollowStatus;
}
query:
async getFollowers(userId: string, page: number, limit: number) {
const builder: SelectQueryBuilder<Followers> = this.createQueryBuilder("followers")
.where("followers.followedId = :userId", { userId })
.andWhere("followers.status = :status", { status: "APPROVED" })
.leftJoinAndSelect("followers.follower", "follower")
.leftJoinAndSelect("follower.followers", "followerFollowers")
.take(limit)
.skip((page - 1) * limit);
const followers = await builder.getManyAndCount();
return followers;
}
main function:
async getFollowers(userId: string, page: number, limit: number) {
page = page || DEFAULT_PAGE_NUMBER;
limit = limit || DEFAULT_PAGE_SIZE;
const followers = await this.followersRepository.getFollowers(userId, page, limit);
const myFollowers = followers[0].map((follower) => follower.followerId);
const response: SearchResponse<FollowerUsersInterface> = {
data: await Promise.all(
followers[0].map(async (user) => {
const isFollowing = user.follower.followers.findIndex((follower) => follower.followerId == userId);
const userFollowers = user.follower.followers;
// count mutual followers, exluding the user itself
const mutualFollowersCount = userFollowers.filter((follower) => {
if (follower.followerId !== user.followerId) {
return myFollowers.some((myFollower) => myFollower === follower.followerId && follower.status === FollowStatus.APPROVED);
}
}).length;
return {
isFollowing: isFollowing > -1,
userId: user.follower.id,
userName: user.follower.userName,
profilePic: await this.s3.getSignedUrl(user.follower.profilePic),
mutualFollowers: mutualFollowersCount
} as FollowerUsersInterface;
})
),
totalCount: followers[1]
};
return response;
}