From 5672edc715c7396fe0241400e0f8359ff05f522a Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 3 Aug 2023 15:15:18 +0200 Subject: [PATCH 1/2] Add function for delete User --- .../justMUSIC/lib/services/AuthService.dart | 24 +++++++++++++++++++ .../justMUSIC/lib/services/UserService.dart | 2 +- .../lib/view_model/UserViewModel.dart | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 39ba6c2..6e77d10 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -88,4 +88,28 @@ class AuthService { void signOut() async { await FirebaseAuth.instance.signOut(); } + + Future delete() async { + try { + User? currentUser = FirebaseAuth.instance.currentUser; + await MyApp.db + .collection("users") + .doc(currentUser?.uid) + .delete() + .then((value) => print("Firestore deleted user")) + .catchError( + (error) => print("Error deleting user from Firestore: $error")); + + await currentUser?.delete(); + await FirebaseAuth.instance.signOut(); + + } on FirebaseAuthException catch (e) { + if (e.code == 'requires-recent-login') { + throw ('Please log in again to delete your account'); + } + rethrow; + } catch (error) { + rethrow; + } + } } diff --git a/Sources/justMUSIC/lib/services/UserService.dart b/Sources/justMUSIC/lib/services/UserService.dart index b299f3d..acd3f72 100644 --- a/Sources/justMUSIC/lib/services/UserService.dart +++ b/Sources/justMUSIC/lib/services/UserService.dart @@ -1,4 +1,4 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; + import 'package:cloud_firestore/cloud_firestore.dart'; import '../main.dart'; diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index b3cf791..178b522 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -112,6 +112,10 @@ class UserViewModel { authService.signOut(); } + delete() { + authService.delete(); + } + bool isFriend(String id) { return _userCurrent.followed.contains(id); } From 120735e0c6d3ddb2ece171b97a5e908f9d468e02 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 3 Aug 2023 17:12:08 +0200 Subject: [PATCH 2/2] Add functions recapSevenDays and sendNotifyComment :white_check_mark: --- .../lib/services/NotificationService.dart | 10 ++++++- .../justMUSIC/lib/services/PostService.dart | 29 +++++++++++++++++++ .../lib/view_model/PostViewModel.dart | 9 ++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Sources/justMUSIC/lib/services/NotificationService.dart b/Sources/justMUSIC/lib/services/NotificationService.dart index 512c03b..ac02322 100644 --- a/Sources/justMUSIC/lib/services/NotificationService.dart +++ b/Sources/justMUSIC/lib/services/NotificationService.dart @@ -1,6 +1,8 @@ import 'dart:convert'; import 'package:http/http.dart' as http; +import '../main.dart'; + class NotificationService { sendPushMessage(String token, String title, String body) async { try { @@ -25,7 +27,13 @@ class NotificationService { "to": token, })); } catch (e) { - print("error push notification"); + print("error push notification: ${e.toString()}"); } } + + sendNotifyComment(String token, String text) async { + var pseudo = MyApp.userViewModel.userCurrent.pseudo; + await sendPushMessage( + token, "Nouveau message", "$pseudo à réagi à votre post,\"$text\"."); + } } diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 4f86827..18719ab 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -99,4 +99,33 @@ class PostService { return !isTodayAvailable; } + + Future> recapSevenDays(String id) async { + List recapList = []; + + DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 7)); + + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("posts") + .where("user_id", isEqualTo: id) + .get(); + + List?> postList = response.docs + .map((DocumentSnapshot> doc) => doc.data()) + .toList(); + + for (int i = 0; i < 7; i++) { + DateTime date = sevenDaysAgo.add(Duration(days: i)); + bool postExists = postList.any((post) => + post?["date"] != null && + post?["date"].toDate().year == date.year && + post?["date"].toDate().month == date.month && + post?["date"].toDate().day == date.day); + + recapList.add(postExists); + } + + return recapList; + } } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index a89abb2..c70fbbb 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -80,6 +80,15 @@ class PostViewModel { throw new Error(); } + Future> recapSevenDays(String id) async { + try { + return await _postService.recapSevenDays(id); + } catch (e) { + print(e); + rethrow; + } + } + Future getAvailable() async { try { return await _postService