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.

98 lines
2.7 KiB

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