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.

97 lines
3.6 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. await db.execute(
  28. '''
  29. CREATE TABLE IF NOT EXISTS conversations(
  30. id TEXT PRIMARY KEY,
  31. user_id TEXT,
  32. symmetric_key TEXT,
  33. admin INTEGER,
  34. name TEXT,
  35. two_user INTEGER,
  36. status INTEGER,
  37. is_read INTEGER,
  38. file TEXT,
  39. message_expiry TEXT,
  40. admin_add_members INTEGER,
  41. admin_edit_info INTEGER,
  42. admin_send_messages INTEGER,
  43. created_at TEXT,
  44. updated_at TEXT
  45. );
  46. ''');
  47. await db.execute(
  48. '''
  49. CREATE TABLE IF NOT EXISTS conversation_users(
  50. id TEXT PRIMARY KEY,
  51. user_id TEXT,
  52. conversation_id TEXT,
  53. username TEXT,
  54. association_key TEXT,
  55. admin INTEGER,
  56. asymmetric_public_key TEXT
  57. );
  58. ''');
  59. await db.execute(
  60. '''
  61. CREATE TABLE IF NOT EXISTS device_tokens(
  62. id TEXT PRIMARY KEY,
  63. user_id TEXT,
  64. token TEXT,
  65. type TEXT
  66. );
  67. ''');
  68. await db.execute(
  69. '''
  70. CREATE TABLE IF NOT EXISTS messages(
  71. id TEXT PRIMARY KEY,
  72. symmetric_key TEXT,
  73. user_symmetric_key TEXT,
  74. data TEXT,
  75. file TEXT,
  76. sender_id TEXT,
  77. sender_username TEXT,
  78. association_key TEXT,
  79. created_at TEXT,
  80. failed_to_send INTEGER
  81. );
  82. ''');
  83. },
  84. version: 2,
  85. );
  86. return database;
  87. }