Skip to content

Commit

Permalink
change isFollowing to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
docimin committed Aug 27, 2024
1 parent 47b4180 commit 0f03714
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 45 deletions.
10 changes: 1 addition & 9 deletions src/app/[locale]/(user)/user/[profileUrl]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
import { Link } from '@/navigation'
import { Button } from '@/components/ui/button'
import ContextMenuProfile from '@/components/user/contextMenuProfile'
import { getUser } from '@/utils/server-api/account/user'
import {
getAvatarImageUrlView,
getBannerImageUrlPreview,
Expand Down Expand Up @@ -70,12 +69,6 @@ export async function generateMetadata({

export default async function UserProfile({ params: { profileUrl } }) {
const { databases } = await createSessionServerClient()
let account = null
try {
account = await getUser()
} catch (error) {
// User is not logged in
}

const userDataResponse: UserData.UserDataType = await databases.listDocuments(
'hp_db',
Expand All @@ -85,8 +78,7 @@ export default async function UserProfile({ params: { profileUrl } }) {
const userData = userDataResponse.documents[0]
const followers = await getFollowers(userData.$id, 0, 5000000)
const following = await getFollowing(userData.$id, 0, 5000000)
const isFollowingResponse = await getIsFollowing(account?.$id, userData?.$id)
const isFollowing = isFollowingResponse?.documents?.length > 0 || false
const isFollowing = await getIsFollowing(userData?.$id)

const totalFollowers: number = followers?.length
const totalFollowing: number = following?.length
Expand Down
49 changes: 13 additions & 36 deletions src/utils/server-api/followers/getFollowing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { createSessionServerClient } from '@/app/appwrite-session'
import { Followers } from '@/utils/types/models'
import { ExecutionMethod, Query } from 'node-appwrite'
import { unstable_noStore } from 'next/cache'
import { ExecutionMethod } from 'node-appwrite'
import { createSessionServerClient } from '@/app/appwrite-session'

/**
* This function is used to get the followers of a user.
* @example
* const following = await getFollowing('user_id', 0, 20)
*/
export async function getFollowing(
userId: string,
offset: number = 0,
Expand All @@ -24,32 +18,15 @@ export async function getFollowing(
return JSON.parse(data.responseBody)
}

/**
* This function is used to check if a user is following another user.
* @param {string} userId The user id.
* @param {string} followerId The follower id.
* @returns {Promise<Followers.FollowerType>} The follower document.
* @example
* const following = await isFollowing('user_id', 'follower_id')
* if (following.documents.length > 0) {
* console.log('User is following')
* return
* }
*/
export async function getIsFollowing(
userId: string,
followerId: string
): Promise<Followers.FollowerType> {
unstable_noStore()
const { databases } = await createSessionServerClient()
return await databases
.listDocuments('hp_db', 'followers', [
Query.and([
Query.equal('userId', userId),
Query.equal('followerId', followerId),
]),
])
.catch((error) => {
return error
})
export async function getIsFollowing(followerId: string) {
const { functions } = await createSessionServerClient()
const data = await functions.createExecution(
'user-endpoints',
'',
false,
`/user/isFollowing?followerId=${followerId}`,
ExecutionMethod.POST
)

return JSON.parse(data.responseBody)
}

0 comments on commit 0f03714

Please sign in to comment.