Browse Source

Add pull to refresh on conversations and friends lists

develop
Tovi Jaeschke-Rogers 2 years ago
parent
commit
23a5e38145
11 changed files with 118 additions and 31 deletions
  1. +22
    -9
      Backend/Api/Friends/Friends.go
  2. +2
    -2
      Backend/Database/ConversationDetails.go
  3. +15
    -5
      Backend/Database/FriendRequests.go
  4. +8
    -0
      mobile/lib/database/models/conversations.dart
  5. +22
    -0
      mobile/lib/database/repositories/conversations_repository.dart
  6. +14
    -0
      mobile/lib/database/repositories/friends_repository.dart
  7. +4
    -0
      mobile/lib/services/conversations_service.dart
  8. +11
    -2
      mobile/lib/services/friends_service.dart
  9. +3
    -1
      mobile/lib/utils/storage/database.dart
  10. +8
    -6
      mobile/lib/views/main/conversation/list.dart
  11. +9
    -6
      mobile/lib/views/main/friend/list.dart

+ 22
- 9
Backend/Api/Friends/Friends.go View File

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"strconv"
"time"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
@ -13,24 +14,36 @@ import (
// FriendRequestList gets friend request list
func FriendRequestList(w http.ResponseWriter, r *http.Request) {
var (
userSession Database.Session
friends Database.FriendRequestList
values url.Values
returnJSON []byte
page int
err error
userSession Database.Session
friends Database.FriendRequestList
query url.Values
acceptedAtString []string
acceptedAt time.Time
page int
ok bool
returnJSON []byte
err error
)
values = r.URL.Query()
query = r.URL.Query()
page, err = strconv.Atoi(values.Get("page"))
page, err = strconv.Atoi(query.Get("page"))
if err != nil {
page = 0
}
acceptedAtString, ok = query["updated_at"]
if ok {
acceptedAt, err = time.Parse(time.RFC3339, acceptedAtString[0])
if err != nil {
http.Error(w, "Invalid Data", http.StatusBadGateway)
return
}
}
userSession, _ = Auth.CheckCookie(r)
friends, err = Database.GetFriendRequestsByUserID(userSession.UserID.String(), page)
friends, err = Database.GetFriendRequestsByUserID(userSession.UserID.String(), page, acceptedAt)
if err != nil {
http.Error(w, "Error", http.StatusInternalServerError)
return


+ 2
- 2
Backend/Database/ConversationDetails.go View File

@ -22,8 +22,8 @@ type ConversationDetail struct {
AdminAddMembers string ` json:"admin_add_members"` // Stored encrypted
AdminEditInfo string ` json:"admin_edit_info"` // Stored encrypted
AdminSendMessages string ` json:"admin_send_messages"` // Stored encrypted
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time ` json:"created_at"`
UpdatedAt time.Time ` json:"updated_at"`
}
// GetConversationDetailByID gets by id


+ 15
- 5
Backend/Database/FriendRequests.go View File

@ -42,8 +42,9 @@ func GetFriendRequestByID(id string) (FriendRequest, error) {
}
// GetFriendRequestsByUserID gets friend request by user id
func GetFriendRequestsByUserID(userID string, page int) ([]FriendRequest, error) {
func GetFriendRequestsByUserID(userID string, page int, acceptedAt time.Time) ([]FriendRequest, error) {
var (
query *gorm.DB
friends []FriendRequest
offset int
err error
@ -51,11 +52,20 @@ func GetFriendRequestsByUserID(userID string, page int) ([]FriendRequest, error)
offset = page * PageSize
err = DB.Model(FriendRequest{}).
query = DB.Model(FriendRequest{}).
Preload("FriendRequestDeviceTokens.DeviceToken").
Where("user_id = ?", userID).
Offset(offset).
Limit(PageSize).
Where("user_id = ?", userID)
if !acceptedAt.IsZero() {
query = query.Where("accepted_at > ?", acceptedAt).
Or("created_at > ?", acceptedAt)
} else {
query = query.
Offset(offset).
Limit(PageSize)
}
err = query.
Order("created_at DESC").
Find(&friends).
Error


+ 8
- 0
mobile/lib/database/models/conversations.dart View File

@ -37,6 +37,8 @@ class Conversation {
bool adminAddMembers = true;
bool adminEditInfo = true;
bool adminSendMessages = false;
DateTime createdAt;
DateTime updatedAt;
File? icon;
Conversation({
@ -52,6 +54,8 @@ class Conversation {
required this.adminAddMembers,
required this.adminEditInfo,
required this.adminSendMessages,
required this.createdAt,
required this.updatedAt,
this.icon,
});
@ -85,6 +89,8 @@ class Conversation {
adminAddMembers: true,
adminEditInfo: true,
adminSendMessages: false,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
}
@ -175,6 +181,8 @@ class Conversation {
'admin_add_members': adminAddMembers ? 1 : 0,
'admin_edit_info': adminEditInfo ? 1 : 0,
'admin_send_messages': adminSendMessages ? 1 : 0,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
};
}


+ 22
- 0
mobile/lib/database/repositories/conversations_repository.dart View File

@ -38,6 +38,8 @@ class ConversationsRepository {
adminAddMembers: true,
adminEditInfo: true,
adminSendMessages: false,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
await db.insert(
@ -131,6 +133,8 @@ class ConversationsRepository {
adminAddMembers: maps[0]['admin_add_members'] == 1,
adminEditInfo: maps[0]['admin_edit_info'] == 1,
adminSendMessages: maps[0]['admin_send_messages'] == 1,
createdAt: DateTime.parse(maps[0]['created_at']),
updatedAt: DateTime.parse(maps[0]['updated_at']),
);
}
@ -163,6 +167,8 @@ class ConversationsRepository {
adminAddMembers: maps[i]['admin_add_members'] == 1,
adminEditInfo: maps[i]['admin_edit_info'] == 1,
adminSendMessages: maps[i]['admin_send_messages'] == 1,
createdAt: DateTime.parse(maps[i]['created_at']),
updatedAt: DateTime.parse(maps[i]['updated_at']),
);
});
}
@ -200,7 +206,23 @@ class ConversationsRepository {
adminAddMembers: maps[0]['admin_add_members'] == 1,
adminEditInfo: maps[0]['admin_edit_info'] == 1,
adminSendMessages: maps[0]['admin_send_messages'] == 1,
createdAt: DateTime.parse(maps[0]['created_at']),
updatedAt: DateTime.parse(maps[0]['updated_at']),
);
}
static Future<DateTime?> getLatestUpdatedAt() async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.rawQuery(
'''
SELECT conversations.updated_at FROM conversations
ORDER BY updated_at DESC
LIMIT 1;
'''
);
return maps.isNotEmpty ? DateTime.parse(maps[0]['updated_at']) : null;
}
}

+ 14
- 0
mobile/lib/database/repositories/friends_repository.dart View File

@ -60,4 +60,18 @@ class FriendsRepository {
);
});
}
static Future<DateTime?> getLatestAcceptedAt() async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.rawQuery(
'''
SELECT friends.accepted_at FROM friends
ORDER BY accepted_at DESC
LIMIT 1;
'''
);
return maps.isNotEmpty ? DateTime.parse(maps[0]['accepted_at']) : null;
}
}

+ 4
- 0
mobile/lib/services/conversations_service.dart View File

@ -160,6 +160,10 @@ class ConversationsService {
static Future<void> _storeConversations(Database db, Conversation conversation, Map<String, dynamic> detailsJson) async {
conversation.createdAt = DateTime.parse(detailsJson['created_at']);
conversation.updatedAt = DateTime.parse(detailsJson['updated_at']);
conversation.messageExpiryDefault = detailsJson['message_expiry'];
conversation.twoUser = AesHelper.aesDecrypt(


+ 11
- 2
mobile/lib/services/friends_service.dart View File

@ -11,11 +11,20 @@ import '/utils/storage/database.dart';
import '/utils/storage/session_cookie.dart';
class FriendsService {
static Future<void> updateFriends() async {
static Future<void> updateFriends({DateTime? acceptedAt}) async {
RSAPrivateKey privKey = await MyProfile.getPrivateKey();
Map<String, String> params = {};
if (acceptedAt != null) {
params['updated_at'] = acceptedAt.toIso8601String();
}
var uri = await MyProfile.getServerUrl('api/v1/auth/friend_requests');
uri = uri.replace(queryParameters: params);
var resp = await http.get(
await MyProfile.getServerUrl('api/v1/auth/friend_requests'),
uri,
headers: {
'cookie': await getSessionCookie(),
}


+ 3
- 1
mobile/lib/utils/storage/database.dart View File

@ -44,7 +44,9 @@ Future<Database> getDatabaseConnection() async {
message_expiry TEXT,
admin_add_members INTEGER,
admin_edit_info INTEGER,
admin_send_messages INTEGER
admin_send_messages INTEGER,
created_at TEXT,
updated_at TEXT
);
''');


+ 8
- 6
mobile/lib/views/main/conversation/list.dart View File

@ -174,12 +174,14 @@ class _ConversationListState extends State<ConversationList> {
}
Future<void> _refresh() async {
Future.delayed(
const Duration(seconds: 1),
() {
print('Doing thing');
}
);
DateTime? updatedAt = await ConversationsRepository.getLatestUpdatedAt();
if (updatedAt == null) {
return;
}
ConversationsService.updateConversations(updatedAt: updatedAt);
conversations = await ConversationsRepository.getConversations();
setState(() {});
}
onGoBack(dynamic value) async {


+ 9
- 6
mobile/lib/views/main/friend/list.dart View File

@ -1,4 +1,5 @@
import 'package:Envelope/database/repositories/friends_repository.dart';
import 'package:Envelope/services/friends_service.dart';
import 'package:flutter/material.dart';
import '/components/custom_title_bar.dart';
@ -194,11 +195,13 @@ class _FriendListState extends State<FriendList> {
}
Future<void> _refresh() async {
Future.delayed(
const Duration(seconds: 1),
() {
print('Doing thing');
}
);
DateTime? acceptedAt = await FriendsRepository.getLatestAcceptedAt();
if (acceptedAt == null) {
return;
}
FriendsService.updateFriends(acceptedAt: acceptedAt);
friends = await FriendsRepository.getFriends();
setState(() {});
}
}

Loading…
Cancel
Save