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.

104 lines
3.0 KiB

  1. import '/utils/storage/database.dart';
  2. import '/models/conversations.dart';
  3. class ConversationUser{
  4. String id;
  5. String conversationId;
  6. String username;
  7. String associationKey;
  8. String admin;
  9. ConversationUser({
  10. required this.id,
  11. required this.conversationId,
  12. required this.username,
  13. required this.associationKey,
  14. required this.admin,
  15. });
  16. factory ConversationUser.fromJson(Map<String, dynamic> json, String conversationId) {
  17. return ConversationUser(
  18. id: json['id'],
  19. conversationId: conversationId,
  20. username: json['username'],
  21. associationKey: json['association_key'],
  22. admin: json['admin'],
  23. );
  24. }
  25. Map<String, dynamic> toMap() {
  26. return {
  27. 'id': id,
  28. 'conversation_id': conversationId,
  29. 'username': username,
  30. 'association_key': associationKey,
  31. 'admin': admin,
  32. };
  33. }
  34. }
  35. // A method that retrieves all the dogs from the dogs table.
  36. Future<List<ConversationUser>> getConversationUsers(Conversation conversation) async {
  37. final db = await getDatabaseConnection();
  38. final List<Map<String, dynamic>> maps = await db.query(
  39. 'conversation_users',
  40. where: 'conversation_id = ?',
  41. whereArgs: [conversation.id],
  42. );
  43. return List.generate(maps.length, (i) {
  44. return ConversationUser(
  45. id: maps[i]['id'],
  46. conversationId: maps[i]['conversation_id'],
  47. username: maps[i]['username'],
  48. associationKey: maps[i]['association_key'],
  49. admin: maps[i]['admin'],
  50. );
  51. });
  52. }
  53. Future<ConversationUser> getConversationUserById(Conversation conversation, String id) async {
  54. final db = await getDatabaseConnection();
  55. final List<Map<String, dynamic>> maps = await db.query(
  56. 'conversation_users',
  57. where: 'conversation_id = ? AND id = ?',
  58. whereArgs: [conversation.id, id],
  59. );
  60. if (maps.length != 1) {
  61. throw ArgumentError('Invalid conversation_id or id');
  62. }
  63. return ConversationUser(
  64. id: maps[0]['id'],
  65. conversationId: maps[0]['conversation_id'],
  66. username: maps[0]['username'],
  67. associationKey: maps[0]['association_key'],
  68. admin: maps[0]['admin'],
  69. );
  70. }
  71. Future<ConversationUser> getConversationUserByUsername(Conversation conversation, String username) async {
  72. final db = await getDatabaseConnection();
  73. final List<Map<String, dynamic>> maps = await db.query(
  74. 'conversation_users',
  75. where: 'conversation_id = ? AND username = ?',
  76. whereArgs: [conversation.id, username],
  77. );
  78. if (maps.length != 1) {
  79. throw ArgumentError('Invalid conversation_id or username');
  80. }
  81. return ConversationUser(
  82. id: maps[0]['id'],
  83. conversationId: maps[0]['conversation_id'],
  84. username: maps[0]['username'],
  85. associationKey: maps[0]['association_key'],
  86. admin: maps[0]['admin'],
  87. );
  88. }