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.

60 lines
1.6 KiB

  1. import 'dart:convert';
  2. import 'package:Envelope/database/models/device_tokens.dart';
  3. import 'package:http/http.dart' as http;
  4. import 'package:pointycastle/export.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import '/database/models/friends.dart';
  7. import '/database/models/my_profile.dart';
  8. import '/utils/storage/database.dart';
  9. import '/utils/storage/session_cookie.dart';
  10. class FriendsService {
  11. static Future<void> updateFriends() async {
  12. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  13. var resp = await http.get(
  14. await MyProfile.getServerUrl('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. List<dynamic> tokens = (friendsRequestJson[i]['tokens']);
  35. for (var j = 0; j < tokens.length; j++) {
  36. DeviceToken token = DeviceToken.fromJson(
  37. tokens[j]['device_token'],
  38. friend.friendId,
  39. privKey,
  40. );
  41. await db.insert(
  42. 'device_tokens',
  43. token.toMap(),
  44. conflictAlgorithm: ConflictAlgorithm.replace,
  45. );
  46. }
  47. }
  48. }
  49. }