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.

99 lines
2.7 KiB

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