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.

105 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. bool 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'] == 'true',
  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 ? 1 : 0,
  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. orderBy: 'admin',
  43. );
  44. return List.generate(maps.length, (i) {
  45. return ConversationUser(
  46. id: maps[i]['id'],
  47. conversationId: maps[i]['conversation_id'],
  48. username: maps[i]['username'],
  49. associationKey: maps[i]['association_key'],
  50. admin: maps[i]['admin'] == 1,
  51. );
  52. });
  53. }
  54. Future<ConversationUser> getConversationUserById(Conversation conversation, String id) async {
  55. final db = await getDatabaseConnection();
  56. final List<Map<String, dynamic>> maps = await db.query(
  57. 'conversation_users',
  58. where: 'conversation_id = ? AND id = ?',
  59. whereArgs: [conversation.id, id],
  60. );
  61. if (maps.length != 1) {
  62. throw ArgumentError('Invalid conversation_id or id');
  63. }
  64. return ConversationUser(
  65. id: maps[0]['id'],
  66. conversationId: maps[0]['conversation_id'],
  67. username: maps[0]['username'],
  68. associationKey: maps[0]['association_key'],
  69. admin: maps[0]['admin'] == 1,
  70. );
  71. }
  72. Future<ConversationUser> getConversationUserByUsername(Conversation conversation, String username) async {
  73. final db = await getDatabaseConnection();
  74. final List<Map<String, dynamic>> maps = await db.query(
  75. 'conversation_users',
  76. where: 'conversation_id = ? AND username = ?',
  77. whereArgs: [conversation.id, username],
  78. );
  79. if (maps.length != 1) {
  80. throw ArgumentError('Invalid conversation_id or username');
  81. }
  82. return ConversationUser(
  83. id: maps[0]['id'],
  84. conversationId: maps[0]['conversation_id'],
  85. username: maps[0]['username'],
  86. associationKey: maps[0]['association_key'],
  87. admin: maps[0]['admin'] == 1,
  88. );
  89. }