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.

49 lines
1.2 KiB

  1. import 'dart:convert';
  2. import 'package:flutter_dotenv/flutter_dotenv.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:pointycastle/export.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import '/models/friends.dart';
  7. import '/models/my_profile.dart';
  8. import '/utils/storage/database.dart';
  9. import '/utils/storage/session_cookie.dart';
  10. Future<void> updateFriends() async {
  11. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  12. // try {
  13. var resp = await http.get(
  14. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/friend_requests'),
  15. headers: {
  16. 'cookie': await getSessionCookie(),
  17. }
  18. );
  19. if (resp.statusCode != 200) {
  20. throw Exception(resp.body);
  21. }
  22. final db = await getDatabaseConnection();
  23. List<dynamic> friendsRequestJson = jsonDecode(resp.body);
  24. for (var i = 0; i < friendsRequestJson.length; i++) {
  25. Friend friend = Friend.fromJson(
  26. friendsRequestJson[i] as Map<String, dynamic>,
  27. privKey,
  28. );
  29. await db.insert(
  30. 'friends',
  31. friend.toMap(),
  32. conflictAlgorithm: ConflictAlgorithm.replace,
  33. );
  34. }
  35. // } catch (SocketException) {
  36. // return;
  37. // }
  38. }