| 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; | |
|   // } | |
| } | |
| 
 |