Something went wrong. Try again.
Self-hosted, federated location sharing app and server that prioritizes user privacy and security
Something went wrong. Try again.
7.5 kB · 247 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;import 'storage.dart';import 'crypto.dart' as crypto;
//
// Example API function:Future<String> exampleAPIFunction() async { // Replace the function name and return type to match the actual API needs. if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No account'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); // Replace path with the actual endpoint. var uri = Uri.parse('$serverUrl/v0/path'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: jsonEncode({}), // Replace with the actual request body );
if (response.statusCode == 200) { return jsonDecode( response.body, )['signup_key']; // Replace with the actual response field; If it's a bool return false. }
return '${response.statusCode} ${response.body}'; // This is just to return the error; If it's a bool return false.}
//
// API functions from ```privacypin/API.md```Future<bool> createAccount(String signupKey) async { // returns true if the account was created successfully, false otherwise, and stores it in appStorage. String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/create-account'); var response = await http.post( uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode({'signup_key': signupKey}), );
if (response.statusCode == 200) { appStorage.write( key: 'user_id', value: jsonDecode(response.body)['user_id'], ); appStorage.write(key: 'token', value: jsonDecode(response.body)['token']); appStorage.write( key: 'is_admin', value: jsonDecode(response.body)['is_admin'], ); appStorage.write(key: 'account_created', value: 'true'); appStorage.write(key: 'viewers', value: jsonEncode({})); return true; }
// This code would execute if the response status code is not 200(meaning the request was not successful) debugPrint('!!!!! ${response.statusCode} ${response.body}'); return false;}
Future<String> generateSignupKey() async { if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No token'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/generate-signup-key'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: jsonEncode({}), );
if (response.statusCode == 200) { return jsonDecode(response.body)['signup_key']; }
return '${response.statusCode} ${response.body}';}
Future<String> createInvite() async { // This returns the token and the unix timestamp; I'm not sure if the token needs to be stored or not so I didn't store it if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No account'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/create-invite'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: jsonEncode({}), );
if (response.statusCode == 200) { return response.body; }
return '${response.statusCode} ${response.body}';}
Future<String> acceptInvite(String inviteToken) async { // This function adds the viewer_id to the viewers list and returns the viewer_id of the accepted invite if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No account'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/accept-invite'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: jsonEncode({'invite_token': inviteToken}), );
if (response.statusCode == 200) { await addItemToJsonStorage( 'viewers', jsonDecode(response.body)['viewer_id'], 'some encryption key?', ); return jsonDecode(response.body)['viewer_id']; }
return '${response.statusCode} ${response.body}';}
Future<bool> sendPings() async { // This function sends pings to the server for each viewer_id in the viewers list if (appStorage.read(key: 'account_created').toString() != 'true') { return false; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/send-pings'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: await crypto.getPings(), );
if (response.statusCode == 200) { return true; }
return false;}
Future<String> getPings(String viewerId) async { if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No account'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/get-pings'); var response = await http.post( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, body: jsonEncode({'sender_id': viewerId}), );
if (response.statusCode == 200) { return response.body; }
return '${response.statusCode} ${response.body}';}
Future<String> getEvents(String viewerId) async { if (appStorage.read(key: 'account_created').toString() != 'true') { return 'No account'; }
String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/events'); var response = await http.get( uri, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ${(await appStorage.read(key: 'token')).toString()}', 'X-User-Id': (await appStorage.read(key: 'user_id')).toString(), }, );
if (response.statusCode == 200) { return response.body; }
return '${response.statusCode} ${response.body}';}
//
//
// Testing functionsFuture<void> sendLocation(double latitude, double longitude) async { String serverUrl = (await appStorage.read(key: 'server-url')).toString(); var uri = Uri.parse('$serverUrl/v0/send-location'); var response = await http.post( uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode({'lat': latitude, 'lon': longitude}), );
debugPrint('!!!!! ${response.body}'); debugPrint('!!!!! ${response.statusCode.toString()}');}