From 71b242eaa064043cb9d0050dbf505a0317d092bb Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Thu, 17 Nov 2022 22:34:42 +1030 Subject: [PATCH] Begin adding pagination to messages, conversations, and friends Friends needs some attention, due to accepted / non accepted requests --- Backend/Database/FriendRequests.go | 4 ++++ Backend/Database/Seeder/FriendSeeder.go | 4 ++-- Backend/Database/Seeder/UserSeeder.go | 24 ++++++++++++++++++- Backend/Database/UserConversations.go | 2 +- README.md | 1 + .../lib/services/conversations_service.dart | 23 ++++++++++++++---- mobile/lib/services/friends_service.dart | 9 ++++++- mobile/lib/services/messages_service.dart | 20 ++++++++++++++-- .../lib/views/main/conversation/detail.dart | 19 +++++++++++++++ mobile/lib/views/main/conversation/list.dart | 19 +++++++++++++++ .../views/main/conversation/list_item.dart | 8 +++++-- mobile/lib/views/main/friend/list.dart | 20 ++++++++++++++++ 12 files changed, 140 insertions(+), 13 deletions(-) diff --git a/Backend/Database/FriendRequests.go b/Backend/Database/FriendRequests.go index 3726204..bd2f38f 100644 --- a/Backend/Database/FriendRequests.go +++ b/Backend/Database/FriendRequests.go @@ -63,6 +63,10 @@ func GetFriendRequestsByUserID(userID string, page int, acceptedAt time.Time) ([ query = query. Offset(offset). Limit(PageSize) + + if page > 0 { + query = query.Where("accepted_at IS NULL") + } } err = query. diff --git a/Backend/Database/Seeder/FriendSeeder.go b/Backend/Database/Seeder/FriendSeeder.go index 5d3c97d..7adcb10 100644 --- a/Backend/Database/Seeder/FriendSeeder.go +++ b/Backend/Database/Seeder/FriendSeeder.go @@ -116,13 +116,13 @@ func SeedFriends() { accepted = false - for i = 0; i <= 5; i++ { + for i = 0; i < 25; i++ { secondaryUser, err = Database.GetUserByUsername(userNames[i]) if err != nil { panic(err) } - if i > 3 { + if i > 7 { accepted = true } diff --git a/Backend/Database/Seeder/UserSeeder.go b/Backend/Database/Seeder/UserSeeder.go index 2b9c9d9..1d2d332 100644 --- a/Backend/Database/Seeder/UserSeeder.go +++ b/Backend/Database/Seeder/UserSeeder.go @@ -19,6 +19,28 @@ var userNames = []string{ "nodesanymore", "sacredpolitical", "pajamasenergy", + "Multiscience", + "Exequies", + "Nebulae", + "Synartesis", + "LinklingKaput", + "Gumption", + "Festooned", + "Ratatouille", + "VersusBoing", + "Jiggermast", + "Ambsase", + "Ard3pic", + "Smegma", + "Vaporiform", + "Agromania", + "GleetrAlgor", + "Zymurgy", + "Nipperkin", + "Thespian", + "Mewling", + "Scram", + "Fizgig", } func createUser(username string) (Database.User, error) { @@ -70,7 +92,7 @@ func SeedUsers() { panic(err) } - for i = 0; i <= 10; i++ { + for i = 0; i < len(userNames); i++ { _, err = createUser(userNames[i]) if err != nil { panic(err) diff --git a/Backend/Database/UserConversations.go b/Backend/Database/UserConversations.go index ee72c3f..4e8defd 100644 --- a/Backend/Database/UserConversations.go +++ b/Backend/Database/UserConversations.go @@ -45,8 +45,8 @@ func GetUserConversationsByUserId(id string, page int) ([]UserConversation, erro err = DB.Offset(offset). Limit(PageSize). - Order("created_at DESC"). Find(&conversations, "user_id = ?", id). + Order("created_at DESC"). Error return conversations, err diff --git a/README.md b/README.md index 711935b..06b22df 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,4 @@ Encrypted messaging app - Sort conversation based on latest message - Fix admin bool on conversation object frontend - Fix image picker being patchy on iOS +- Fix being able to add friends > 1 times diff --git a/mobile/lib/services/conversations_service.dart b/mobile/lib/services/conversations_service.dart index 32ed2fc..a820518 100644 --- a/mobile/lib/services/conversations_service.dart +++ b/mobile/lib/services/conversations_service.dart @@ -111,11 +111,23 @@ class ConversationsService { } } - static Future<_BaseConversationsResult> _getBaseConversations() async { + static Future<_BaseConversationsResult> _getBaseConversations({ + int page = 0 + }) async { + RSAPrivateKey privKey = await MyProfile.getPrivateKey(); + Map params = {}; + + if (page != 0) { + params['page'] = page.toString(); + } + + var uri = await MyProfile.getServerUrl('api/v1/auth/conversations'); + uri = uri.replace(queryParameters: params); + http.Response resp = await http.get( - await MyProfile.getServerUrl('api/v1/auth/conversations'), + uri, headers: { 'cookie': await getSessionCookie(), } @@ -225,8 +237,11 @@ class ConversationsService { } } - static Future updateConversations({DateTime? updatedAt}) async { - _BaseConversationsResult baseConvs = await _getBaseConversations(); + static Future updateConversations({ + int page = 0, + DateTime? updatedAt + }) async { + _BaseConversationsResult baseConvs = await _getBaseConversations(page: page); if (baseConvs.detailIds.isEmpty) { return; diff --git a/mobile/lib/services/friends_service.dart b/mobile/lib/services/friends_service.dart index facb5ad..4475b32 100644 --- a/mobile/lib/services/friends_service.dart +++ b/mobile/lib/services/friends_service.dart @@ -11,11 +11,18 @@ import '/utils/storage/database.dart'; import '/utils/storage/session_cookie.dart'; class FriendsService { - static Future updateFriends({DateTime? acceptedAt}) async { + static Future updateFriends({ + int page = 0, + DateTime? acceptedAt + }) async { RSAPrivateKey privKey = await MyProfile.getPrivateKey(); Map params = {}; + if (page != 0) { + params['page'] = page.toString(); + } + if (acceptedAt != null) { params['updated_at'] = acceptedAt.toIso8601String(); } diff --git a/mobile/lib/services/messages_service.dart b/mobile/lib/services/messages_service.dart index d617829..d3596f3 100644 --- a/mobile/lib/services/messages_service.dart +++ b/mobile/lib/services/messages_service.dart @@ -127,12 +127,28 @@ class MessagesService { }); } - static Future updateMessageThread(Conversation conversation, {MyProfile? profile}) async { + static Future updateMessageThread( + Conversation conversation, { + int page = 0, + MyProfile? profile, + }) async { + profile ??= await MyProfile.getProfile(); ConversationUser currentUser = await ConversationUsersRepository.getConversationUser(conversation, profile.id); + Map params = {}; + + if (page != 0) { + params['page'] = page.toString(); + } + + print(page); + + var uri = await MyProfile.getServerUrl('api/v1/auth/messages/${currentUser.associationKey}'); + uri = uri.replace(queryParameters: params); + var resp = await http.get( - await MyProfile.getServerUrl('api/v1/auth/messages/${currentUser.associationKey}'), + uri, headers: { 'cookie': await getSessionCookie(), } diff --git a/mobile/lib/views/main/conversation/detail.dart b/mobile/lib/views/main/conversation/detail.dart index fe09c29..1101c78 100644 --- a/mobile/lib/views/main/conversation/detail.dart +++ b/mobile/lib/views/main/conversation/detail.dart @@ -25,6 +25,8 @@ class ConversationDetail extends StatefulWidget{ } class _ConversationDetailState extends State { + late ScrollController _scrollController; + List messages = []; MyProfile profile = MyProfile( @@ -86,10 +88,26 @@ class _ConversationDetailState extends State { @override void initState() { sendDisabled = widget.conversation.adminSendMessages && !widget.conversation.admin; + _scrollController = ScrollController(); + _scrollController.addListener(_scrollListener); super.initState(); fetchMessages(); } + Future _scrollListener() async { + if (!(_scrollController.offset >= _scrollController.position.maxScrollExtent)) { + return; + } + int page = 0; + + if (messages.length > 19) { + page = messages.length ~/ 20; + } + + await MessagesService.updateMessageThread(widget.conversation, page: page); + await fetchMessages(); + } + Widget messagesView() { if (messages.isEmpty) { return const Center( @@ -98,6 +116,7 @@ class _ConversationDetailState extends State { } return ListView.builder( + controller: _scrollController, itemCount: messages.length, shrinkWrap: true, padding: EdgeInsets.only( diff --git a/mobile/lib/views/main/conversation/list.dart b/mobile/lib/views/main/conversation/list.dart index 64725a5..c0c4739 100644 --- a/mobile/lib/views/main/conversation/list.dart +++ b/mobile/lib/views/main/conversation/list.dart @@ -29,6 +29,7 @@ class ConversationList extends StatefulWidget { class _ConversationListState extends State { final GlobalKey _refreshIndicatorKey = GlobalKey(); + late ScrollController _scrollController; List conversations = []; List friends = []; @@ -148,6 +149,8 @@ class _ConversationListState extends State { @override void initState() { + _scrollController = ScrollController(); + _scrollController.addListener(_scrollListener); super.initState(); conversations.addAll(widget.conversations); friends.addAll(widget.friends); @@ -162,6 +165,7 @@ class _ConversationListState extends State { } return ListView.builder( + controller: _scrollController, itemCount: conversations.length, shrinkWrap: false, physics: const AlwaysScrollableScrollPhysics(), @@ -184,6 +188,21 @@ class _ConversationListState extends State { setState(() {}); } + Future _scrollListener() async { + if (!(_scrollController.offset >= _scrollController.position.maxScrollExtent)) { + return; + } + + int page = 0; + + if (conversations.length > 19) { + page = conversations.length ~/ 20; + } + + await ConversationsService.updateConversations(page: page); + onGoBack(null); + } + onGoBack(dynamic value) async { conversations = await ConversationsRepository.getConversations(); friends = await FriendsRepository.getFriends(); diff --git a/mobile/lib/views/main/conversation/list_item.dart b/mobile/lib/views/main/conversation/list_item.dart index 357158f..c2cb378 100644 --- a/mobile/lib/views/main/conversation/list_item.dart +++ b/mobile/lib/views/main/conversation/list_item.dart @@ -110,11 +110,15 @@ class _ConversationListItemState extends State { conversation = widget.conversation; recentMessage = await conversation.getRecentMessage(); loaded = true; - setState(() {}); + if (mounted) { + setState(() {}); + } } onGoBack(dynamic value) async { conversation = await ConversationsRepository.getConversationById(widget.conversation.id); - setState(() {}); + if (mounted) { + setState(() {}); + } } } diff --git a/mobile/lib/views/main/friend/list.dart b/mobile/lib/views/main/friend/list.dart index dcd3385..3b99195 100644 --- a/mobile/lib/views/main/friend/list.dart +++ b/mobile/lib/views/main/friend/list.dart @@ -26,6 +26,7 @@ class FriendList extends StatefulWidget { class _FriendListState extends State { final GlobalKey _refreshIndicatorKey = GlobalKey(); + late ScrollController _scrollController; List friends = []; List friendsDuplicate = []; @@ -130,6 +131,8 @@ class _FriendListState extends State { @override void initState() { + _scrollController = ScrollController(); + _scrollController.addListener(_scrollListener); super.initState(); friends.addAll(widget.friends); } @@ -164,6 +167,7 @@ class _FriendListState extends State { } return ListView.separated( + controller: _scrollController, itemCount: friends.length, shrinkWrap: false, physics: const AlwaysScrollableScrollPhysics(), @@ -204,4 +208,20 @@ class _FriendListState extends State { friends = await FriendsRepository.getFriends(); setState(() {}); } + + Future _scrollListener() async { + if (!(_scrollController.offset >= _scrollController.position.maxScrollExtent)) { + return; + } + + int page = 0; + + if (friends.length > 19) { + page = friends.length ~/ 20; + } + + await FriendsService.updateFriends(page: page); + friends = await FriendsRepository.getFriends(); + setState(() {}); + } }