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

import '/utils/storage/database.dart';
import '/models/conversations.dart';
class ConversationUser{
String id;
String conversationId;
String username;
String associationKey;
bool admin;
ConversationUser({
required this.id,
required this.conversationId,
required this.username,
required this.associationKey,
required this.admin,
});
factory ConversationUser.fromJson(Map<String, dynamic> json, String conversationId) {
return ConversationUser(
id: json['id'],
conversationId: conversationId,
username: json['username'],
associationKey: json['association_key'],
admin: json['admin'] == 'true',
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'conversation_id': conversationId,
'username': username,
'association_key': associationKey,
'admin': admin ? 1 : 0,
};
}
}
// A method that retrieves all the dogs from the dogs table.
Future<List<ConversationUser>> getConversationUsers(Conversation conversation) async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query(
'conversation_users',
where: 'conversation_id = ?',
whereArgs: [conversation.id],
orderBy: 'admin',
);
return List.generate(maps.length, (i) {
return ConversationUser(
id: maps[i]['id'],
conversationId: maps[i]['conversation_id'],
username: maps[i]['username'],
associationKey: maps[i]['association_key'],
admin: maps[i]['admin'] == 1,
);
});
}
Future<ConversationUser> getConversationUserById(Conversation conversation, String id) async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query(
'conversation_users',
where: 'conversation_id = ? AND id = ?',
whereArgs: [conversation.id, id],
);
if (maps.length != 1) {
throw ArgumentError('Invalid conversation_id or id');
}
return ConversationUser(
id: maps[0]['id'],
conversationId: maps[0]['conversation_id'],
username: maps[0]['username'],
associationKey: maps[0]['association_key'],
admin: maps[0]['admin'] == 1,
);
}
Future<ConversationUser> getConversationUserByUsername(Conversation conversation, String username) async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query(
'conversation_users',
where: 'conversation_id = ? AND username = ?',
whereArgs: [conversation.id, username],
);
if (maps.length != 1) {
throw ArgumentError('Invalid conversation_id or username');
}
return ConversationUser(
id: maps[0]['id'],
conversationId: maps[0]['conversation_id'],
username: maps[0]['username'],
associationKey: maps[0]['association_key'],
admin: maps[0]['admin'] == 1,
);
}