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.

80 lines
2.9 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. asymmetric_public_key TEXT,
  24. accepted_at TEXT
  25. );
  26. ''');
  27. // TODO: Change users to use its own table, as it is a json blob
  28. await db.execute(
  29. '''
  30. CREATE TABLE IF NOT EXISTS conversations(
  31. id TEXT PRIMARY KEY,
  32. user_id TEXT,
  33. conversation_detail_id TEXT,
  34. symmetric_key TEXT,
  35. admin INTEGER,
  36. name TEXT,
  37. status INTEGER,
  38. is_read INTEGER
  39. );
  40. ''');
  41. await db.execute(
  42. '''
  43. CREATE TABLE IF NOT EXISTS conversation_users(
  44. id TEXT PRIMARY KEY,
  45. user_id TEXT,
  46. conversation_id TEXT,
  47. username TEXT,
  48. data TEXT,
  49. association_key TEXT,
  50. admin INTEGER
  51. );
  52. ''');
  53. await db.execute(
  54. '''
  55. CREATE TABLE IF NOT EXISTS messages(
  56. id TEXT PRIMARY KEY,
  57. symmetric_key TEXT,
  58. user_symmetric_key TEXT,
  59. data TEXT,
  60. sender_id TEXT,
  61. sender_username TEXT,
  62. association_key TEXT,
  63. created_at TEXT,
  64. failed_to_send INTEGER
  65. );
  66. ''');
  67. },
  68. version: 2,
  69. );
  70. return database;
  71. }