diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index f4a1fdd..85a9f67 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -27,6 +27,7 @@ class AuthService { "followers": [], "token_notify": token, "musics_likes": [], + "creation_date": DateTime.now(), "picture": "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc" }; @@ -132,6 +133,7 @@ class AuthService { Future delete() async { try { User? currentUser = FirebaseAuth.instance.currentUser; + String? id = currentUser?.uid; await MyApp.db .collection("users") .doc(currentUser?.uid) @@ -140,6 +142,35 @@ class AuthService { .catchError( (error) => print("Error deleting user from Firestore: $error")); + QuerySnapshot usersSnapshot = await MyApp.db.collection("users").get(); + + // Delete all posts + QuerySnapshot postsSnapshot = await MyApp.db.collection("posts").where("user_id", isEqualTo: id).get(); + for (var postDoc in postsSnapshot.docs) { + await postDoc.reference.delete(); + } + + // Delete all comments + QuerySnapshot commentsSnapshot = await MyApp.db.collection("comments").where("user_id", isEqualTo: id).get(); + for (var commentDoc in commentsSnapshot.docs) { + await commentDoc.reference.delete(); + } + + for (var userDoc in usersSnapshot.docs) { + if (userDoc.exists) { + var userData = userDoc.data() as Map; + List followers = List.from(userData['followers'] ?? []); + List followed = List.from(userData['followed'] ?? []); + if (followers.contains(id)) { + followers.remove(id); + } + if (followed.contains(id)) { + followed.remove(id); + } + await userDoc.reference.update({'followers': followers, 'followed': followed}); + } + } + await currentUser?.delete(); await FirebaseAuth.instance.signOut(); } on FirebaseAuthException catch (e) { diff --git a/Sources/justMUSIC/lib/services/CommentService.dart b/Sources/justMUSIC/lib/services/CommentService.dart index 0808b25..76e5341 100644 --- a/Sources/justMUSIC/lib/services/CommentService.dart +++ b/Sources/justMUSIC/lib/services/CommentService.dart @@ -15,6 +15,16 @@ class CommentService { await MyApp.db.collection("comments").add(comment); } + deleteComment(String id) async { + await MyApp.db + .collection("comments") + .doc(id) + .delete() + .then((value) => print("Firestore deleted comment")) + .catchError( + (error) => print("Error deleting comment from Firestore: $error")); + } + Future>>> getCommentsByPostId( String id) async { var response = await FirebaseFirestore.instance diff --git a/Sources/justMUSIC/lib/services/NotificationService.dart b/Sources/justMUSIC/lib/services/NotificationService.dart index 859d4b1..5ca8670 100644 --- a/Sources/justMUSIC/lib/services/NotificationService.dart +++ b/Sources/justMUSIC/lib/services/NotificationService.dart @@ -34,6 +34,16 @@ class NotificationService { sendNotifyComment(String token, String text) async { var pseudo = MyApp.userViewModel.userCurrent.pseudo; - await sendPushMessage(token, "Nouveau message de $pseudo", "$text\"."); + await sendPushMessage(token, "$pseudo a commenté votre capsule", "$text"); + } + + sendNotifyLike(String token, int nbLikes) async { + var pseudo = MyApp.userViewModel.userCurrent.pseudo; + await sendPushMessage(token, "$pseudo a aimé votre capsule", "Votre capsule a été aimé $nbLikes fois."); + } + + sendNotifyFriend(String token) async { + var pseudo = MyApp.userViewModel.userCurrent.pseudo; + await sendPushMessage(token, "$pseudo vous suit.", "Il pourra à présent voir votre activité dans son Feed \"Mes Amis\"."); } }