@@ -36,6 +36,8 @@ const avatars = new Avatars(client);
36
36
const databases = new Databases ( client ) ;
37
37
const storage = new Storage ( client ) ;
38
38
39
+
40
+ //Creating Sessions For User
39
41
export const createUser = async ( email , password , username ) => {
40
42
try {
41
43
// Create a new account
@@ -71,7 +73,6 @@ export const createUser = async (email, password, username) => {
71
73
}
72
74
} ;
73
75
74
-
75
76
export const signIn = async ( email , password ) => {
76
77
try {
77
78
// Try to get the current session
@@ -91,7 +92,7 @@ export const signIn = async (email, password) => {
91
92
} ;
92
93
93
94
94
- // Ensure this function is properly defined
95
+ // Getiing Current Logged User
95
96
export const getCurrentUser = async ( ) => {
96
97
try {
97
98
const currentAccount = await account . get ( ) ;
@@ -108,52 +109,53 @@ export const getCurrentUser = async () => {
108
109
}
109
110
} ;
110
111
112
+ //User Feed
111
113
export const getAllPosts = async ( ) => {
112
114
try {
113
115
const posts = await databases . listDocuments (
114
116
databaseId ,
115
117
videoCollectionId ,
118
+ [ Query . orderDesc ( '$createdAt' ) ]
116
119
) ;
117
120
return posts . documents ;
118
121
} catch ( error ) {
119
122
throw new Error ( error . message || 'Error fetching posts' ) ;
120
123
}
121
124
}
122
125
123
-
124
- export const getLatestPosts = async ( ) => {
126
+ export const getUserPosts = async ( userID ) => {
125
127
try {
126
128
const posts = await databases . listDocuments (
127
129
databaseId ,
128
130
videoCollectionId ,
129
- [ Query . orderDesc ( '$createdAt ', Query . limit ( 7 ) ) ]
131
+ [ Query . equal ( 'creator ', userID ) , Query . orderDesc ( '$createdAt' ) ]
130
132
) ;
131
133
return posts . documents ;
132
134
} catch ( error ) {
133
135
throw new Error ( error . message || 'Error fetching posts' ) ;
134
136
}
135
137
}
136
138
137
-
138
- export const searchPosts = async ( query ) => {
139
+ export const getLatestPosts = async ( ) => {
139
140
try {
140
141
const posts = await databases . listDocuments (
141
142
databaseId ,
142
143
videoCollectionId ,
143
- [ Query . search ( 'title ', query ) ]
144
+ [ Query . orderDesc ( '$createdAt ', Query . limit ( 7 ) ) ]
144
145
) ;
145
146
return posts . documents ;
146
147
} catch ( error ) {
147
148
throw new Error ( error . message || 'Error fetching posts' ) ;
148
149
}
149
150
}
150
151
151
- export const getUserPosts = async ( userID ) => {
152
+ //User Action
153
+ export const searchPosts = async ( query ) => {
152
154
try {
153
155
const posts = await databases . listDocuments (
154
156
databaseId ,
155
157
videoCollectionId ,
156
- [ Query . equal ( 'creator ', userID ) ]
158
+ [ Query . search ( 'title ', query ) ]
157
159
) ;
158
160
return posts . documents ;
159
161
} catch ( error ) {
@@ -162,6 +164,7 @@ export const getUserPosts = async (userID) => {
162
164
}
163
165
164
166
167
+ //Clearing Sessions Created
165
168
export const signOut = async ( ) => {
166
169
try {
167
170
const session = await account . deleteSession ( 'current' ) ;
@@ -171,7 +174,7 @@ export const signOut = async () => {
171
174
}
172
175
}
173
176
174
-
177
+ //Creating a Post and Submitting to DB
175
178
export const getFilePreview = async ( fileId , type ) => {
176
179
let fileUrl ;
177
180
try {
@@ -238,4 +241,131 @@ export const CreateVideo = async (form) => {
238
241
console . log ( error ) ;
239
242
throw new Error ( error . message || 'Error creating video' ) ;
240
243
}
241
- }
244
+ }
245
+
246
+ //Bookmarks Logics
247
+ const getUserDocument = async ( userId , collectionId ) => {
248
+ try {
249
+ const userDocument = await databases . getDocument ( databaseId , collectionId , userId ) ;
250
+ console . log ( 'userDocument:' , userDocument ) ;
251
+
252
+ // Set default roleID if it's null or undefined
253
+ userDocument . roleID = userDocument . roleID || 'user' ;
254
+
255
+ return userDocument ;
256
+ } catch ( error ) {
257
+ console . error ( 'Error getting user document:' , error ) ;
258
+ return null ;
259
+ }
260
+ } ;
261
+ export const likePost = async ( videoUrl , userId , unlike = false ) => {
262
+ try {
263
+ // Step 1: Get the current user
264
+ const user = await getCurrentUser ( ) ;
265
+
266
+ // Step 2: Get the video document
267
+ const videoDocument = await getVideoDocument ( videoUrl ) ;
268
+
269
+ // Step 3: Get the user document
270
+ const userDocument = await getUserDocument ( user . $id , user . $collectionId ) ;
271
+
272
+ // Step 4: Check if the user has already liked/unliked the video
273
+ if ( unlike && ( ! userDocument . LikedVideos || ! userDocument . LikedVideos . includes ( videoDocument . $id ) ) ) {
274
+ console . log ( 'User has not liked the video' ) ;
275
+ return ;
276
+ }
277
+
278
+ if ( ! unlike && userDocument . LikedVideos && userDocument . LikedVideos . includes ( videoDocument . $id ) ) {
279
+ console . log ( 'User has already liked the video' ) ;
280
+ return ;
281
+ }
282
+
283
+ // Step 5: Update the user document with LikedVideos
284
+ await updateUserDocument ( userDocument , videoDocument . $id , unlike ) ;
285
+ } catch ( error ) {
286
+ console . error ( 'Error liking post:' , error ) ;
287
+ } finally {
288
+ console . log ( 'Like post function completed' ) ;
289
+ }
290
+ } ;
291
+
292
+ // Helper function to get the video document
293
+ const getVideoDocument = async ( videoUrl ) => {
294
+ const videoDocument = await databases . listDocuments (
295
+ databaseId ,
296
+ videoCollectionId ,
297
+ [ Query . equal ( 'video' , videoUrl ) ] ,
298
+ ) ;
299
+
300
+ if ( videoDocument . documents . length > 0 ) {
301
+ return videoDocument . documents [ 0 ] ;
302
+ } else {
303
+ throw new Error ( 'Video document not found' ) ;
304
+ }
305
+ } ;
306
+
307
+ // Helper function to get the user document
308
+ const updateUserDocument = async ( userDocument , videoDocumentId , unlike = false ) => {
309
+ if ( ! userDocument ) {
310
+ console . error ( 'User document not found' ) ;
311
+ return ;
312
+ }
313
+
314
+ let likedVideos = userDocument . LikedVideos || [ ] ;
315
+
316
+ if ( unlike ) {
317
+ likedVideos = likedVideos . filter ( ( id ) => id !== videoDocumentId ) ;
318
+ } else {
319
+ likedVideos . push ( videoDocumentId ) ;
320
+ }
321
+
322
+ const newUserDocument = {
323
+ accountid : userDocument . accountid ,
324
+ avatar : userDocument . avatar ,
325
+ email : userDocument . email ,
326
+ roleID : userDocument . roleID || 'user' , // Set roleID to 'user' if it's null or undefined
327
+ username : userDocument . username ,
328
+ LikedVideos : likedVideos ,
329
+ } ;
330
+
331
+ console . log ( 'newUserDocument:' , newUserDocument ) ; // Add this line
332
+
333
+ try {
334
+ await databases . updateDocument (
335
+ databaseId ,
336
+ userDocument . $collectionId ,
337
+ userDocument . $id ,
338
+ { data : newUserDocument } ,
339
+ ) ;
340
+ } catch ( error ) {
341
+ console . error ( 'Error updating user document:' , error ) ;
342
+ }
343
+ } ;
344
+
345
+
346
+ export const getLikedPosts = async ( ) => {
347
+ try {
348
+ // Get the current user
349
+ const user = await getCurrentUser ( ) ;
350
+
351
+ if ( ! user || ! user . LikedVideos ) {
352
+ return [ ] ;
353
+ }
354
+
355
+ // Fetch the liked posts using the LikedVideos attribute
356
+ const likedPosts = await Promise . all (
357
+ user . LikedVideos . map ( async ( id ) => {
358
+ const post = await databases . getDocument (
359
+ databaseId ,
360
+ videoCollectionId ,
361
+ id ,
362
+ ) ;
363
+ return post ;
364
+ } ) ,
365
+ ) ;
366
+
367
+ return likedPosts ;
368
+ } catch ( error ) {
369
+ throw new Error ( error . message || 'Error fetching liked posts' ) ;
370
+ }
371
+ } ;
0 commit comments