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.

50 lines
1.2 KiB

import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'package:pointycastle/export.dart';
import 'package:sqflite/sqflite.dart';
import '/models/friends.dart';
import '/models/my_profile.dart';
import '/utils/storage/database.dart';
import '/utils/storage/session_cookie.dart';
Future<void> updateFriends() async {
RSAPrivateKey privKey = await MyProfile.getPrivateKey();
try {
var resp = await http.get(
Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/friend_requests'),
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,
);
}
} catch (SocketException) {
return;
}
}