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.

44 lines
1.1 KiB

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