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.

65 lines
2.3 KiB

  1. import 'dart:async';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:path/path.dart';
  4. import 'package:sqflite/sqflite.dart';
  5. Future<void> deleteDb() async {
  6. final path = join(await getDatabasesPath(), 'envelope.db');
  7. deleteDatabase(path);
  8. }
  9. Future<Database> getDatabaseConnection() async {
  10. WidgetsFlutterBinding.ensureInitialized();
  11. final path = join(await getDatabasesPath(), 'envelope.db');
  12. final database = openDatabase(
  13. path,
  14. onCreate: (db, version) async {
  15. await db.execute(
  16. '''
  17. CREATE TABLE IF NOT EXISTS friends(
  18. id TEXT PRIMARY KEY,
  19. user_id TEXT,
  20. username TEXT,
  21. friend_id TEXT,
  22. symmetric_key TEXT,
  23. accepted_at TEXT
  24. );
  25. ''');
  26. // TODO: Change users to use its own table, as it is a json blob
  27. await db.execute(
  28. '''
  29. CREATE TABLE IF NOT EXISTS conversations(
  30. id TEXT PRIMARY KEY,
  31. user_id TEXT,
  32. conversation_detail_id TEXT,
  33. message_thread_key TEXT,
  34. symmetric_key TEXT,
  35. admin INTEGER,
  36. name TEXT,
  37. users TEXT
  38. );
  39. ''');
  40. await db.execute(
  41. '''
  42. CREATE TABLE IF NOT EXISTS messages(
  43. id TEXT PRIMARY KEY,
  44. message_thread_key TEXT,
  45. symmetric_key TEXT,
  46. data TEXT,
  47. sender_id TEXT,
  48. sender_username TEXT,
  49. created_at TEXT
  50. );
  51. ''');
  52. },
  53. // Set the version. This executes the onCreate function and provides a
  54. // path to perform database upgrades and downgrades.
  55. version: 1,
  56. );
  57. return database;
  58. }