Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB

import 'dart:convert';
import 'package:Envelope/database/models/device_tokens.dart';
import 'package:http/http.dart' as http;
import 'package:pointycastle/export.dart';
import 'package:sqflite/sqflite.dart';
import '/database/models/friends.dart';
import '/database/models/my_profile.dart';
import '/utils/storage/database.dart';
import '/utils/storage/session_cookie.dart';
class FriendsService {
static Future<void> updateFriends({
int page = 0,
DateTime? acceptedAt
}) async {
RSAPrivateKey privKey = await MyProfile.getPrivateKey();
Map<String, String> params = {};
if (page != 0) {
params['page'] = page.toString();
}
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(
uri,
headers: {
'cookie': await getSessionCookie(),
}
);
if (resp.statusCode != 200) {
throw Exception(resp.body);
}
final db = await getDatabaseConnection();
List<dynamic> friendsRequestJson = jsonDecode(resp.body);
for (var i = 0; i < friendsRequestJson.length; i++) {
Friend friend = Friend.fromJson(
friendsRequestJson[i] as Map<String, dynamic>,
privKey,
);
await db.insert(
'friends',
friend.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
List<dynamic> tokens = (friendsRequestJson[i]['tokens']);
for (var j = 0; j < tokens.length; j++) {
DeviceToken token = DeviceToken.fromJson(
tokens[j]['device_token'],
friend.friendId,
privKey,
);
await db.insert(
'device_tokens',
token.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
}
}
}