Merge branch 'soru/fix-members-requests' into 'master'
try to load members from database first and cache them in-memory Closes app#596 See merge request famedly/famedlysdk!382
This commit is contained in:
commit
a46942a140
|
@ -529,6 +529,15 @@ class Database extends _$Database {
|
||||||
return sdk.Event.fromDb(res.first, room).asUser;
|
return sdk.Event.fromDb(res.first, room).asUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<sdk.User>> getUsers(int clientId, sdk.Room room) async {
|
||||||
|
final res = await dbGetUsers(clientId, room.id).get();
|
||||||
|
final ret = <sdk.User>[];
|
||||||
|
for (final r in res) {
|
||||||
|
ret.add(sdk.Event.fromDb(r, room).asUser);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
Future<List<sdk.Event>> getEventList(int clientId, sdk.Room room) async {
|
Future<List<sdk.Event>> getEventList(int clientId, sdk.Room room) async {
|
||||||
final res = await dbGetEventList(clientId, room.id).get();
|
final res = await dbGetEventList(clientId, room.id).get();
|
||||||
final eventList = <sdk.Event>[];
|
final eventList = <sdk.Event>[];
|
||||||
|
|
|
@ -6197,6 +6197,13 @@ abstract class _$Database extends GeneratedDatabase {
|
||||||
}).map(_rowToDbRoomState);
|
}).map(_rowToDbRoomState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Selectable<DbRoomState> dbGetUsers(int client_id, String room_id) {
|
||||||
|
return customSelect(
|
||||||
|
'SELECT * FROM room_states WHERE client_id = :client_id AND type = \'m.room.member\' AND room_id = :room_id',
|
||||||
|
variables: [Variable.withInt(client_id), Variable.withString(room_id)],
|
||||||
|
readsFrom: {roomStates}).map(_rowToDbRoomState);
|
||||||
|
}
|
||||||
|
|
||||||
DbEvent _rowToDbEvent(QueryRow row) {
|
DbEvent _rowToDbEvent(QueryRow row) {
|
||||||
return DbEvent(
|
return DbEvent(
|
||||||
clientId: row.readInt('client_id'),
|
clientId: row.readInt('client_id'),
|
||||||
|
|
|
@ -216,6 +216,7 @@ storeRoomState: INSERT OR REPLACE INTO room_states (client_id, event_id, room_id
|
||||||
getAllRoomAccountData: SELECT * FROM room_account_data WHERE client_id = :client_id;
|
getAllRoomAccountData: SELECT * FROM room_account_data WHERE client_id = :client_id;
|
||||||
storeRoomAccountData: INSERT OR REPLACE INTO room_account_data (client_id, type, room_id, content) VALUES (:client_id, :type, :room_id, :content);
|
storeRoomAccountData: INSERT OR REPLACE INTO room_account_data (client_id, type, room_id, content) VALUES (:client_id, :type, :room_id, :content);
|
||||||
dbGetUser: SELECT * FROM room_states WHERE client_id = :client_id AND type = 'm.room.member' AND state_key = :state_key AND room_id = :room_id;
|
dbGetUser: SELECT * FROM room_states WHERE client_id = :client_id AND type = 'm.room.member' AND state_key = :state_key AND room_id = :room_id;
|
||||||
|
dbGetUsers: SELECT * FROM room_states WHERE client_id = :client_id AND type = 'm.room.member' AND room_id = :room_id;
|
||||||
dbGetEventList: SELECT * FROM events WHERE client_id = :client_id AND room_id = :room_id GROUP BY event_id ORDER BY sort_order DESC;
|
dbGetEventList: SELECT * FROM events WHERE client_id = :client_id AND room_id = :room_id GROUP BY event_id ORDER BY sort_order DESC;
|
||||||
getStates: SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id;
|
getStates: SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id;
|
||||||
resetNotificationCount: UPDATE rooms SET notification_count = 0, highlight_count = 0 WHERE client_id = :client_id AND room_id = :room_id;
|
resetNotificationCount: UPDATE rooms SET notification_count = 0, highlight_count = 0 WHERE client_id = :client_id AND room_id = :room_id;
|
||||||
|
|
|
@ -984,10 +984,20 @@ class Room {
|
||||||
/// Request the full list of participants from the server. The local list
|
/// Request the full list of participants from the server. The local list
|
||||||
/// from the store is not complete if the client uses lazy loading.
|
/// from the store is not complete if the client uses lazy loading.
|
||||||
Future<List<User>> requestParticipants() async {
|
Future<List<User>> requestParticipants() async {
|
||||||
|
if (!participantListComplete && partial && client.database != null) {
|
||||||
|
// we aren't fully loaded, maybe the users are in the database
|
||||||
|
final users = await client.database.getUsers(client.id, this);
|
||||||
|
for (final user in users) {
|
||||||
|
setState(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (participantListComplete) return getParticipants();
|
if (participantListComplete) return getParticipants();
|
||||||
final matrixEvents = await client.api.requestMembers(id);
|
final matrixEvents = await client.api.requestMembers(id);
|
||||||
final users =
|
final users =
|
||||||
matrixEvents.map((e) => Event.fromMatrixEvent(e, this).asUser).toList();
|
matrixEvents.map((e) => Event.fromMatrixEvent(e, this).asUser).toList();
|
||||||
|
for (final user in users) {
|
||||||
|
setState(user); // at *least* cache this in-memory
|
||||||
|
}
|
||||||
users.removeWhere(
|
users.removeWhere(
|
||||||
(u) => [Membership.leave, Membership.ban].contains(u.membership));
|
(u) => [Membership.leave, Membership.ban].contains(u.membership));
|
||||||
return users;
|
return users;
|
||||||
|
|
|
@ -300,6 +300,9 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('getParticipants', () async {
|
test('getParticipants', () async {
|
||||||
|
var userList = room.getParticipants();
|
||||||
|
expect(userList.length, 4);
|
||||||
|
// add new user
|
||||||
room.setState(Event(
|
room.setState(Event(
|
||||||
senderId: '@alice:test.abc',
|
senderId: '@alice:test.abc',
|
||||||
type: 'm.room.member',
|
type: 'm.room.member',
|
||||||
|
@ -309,9 +312,9 @@ void main() {
|
||||||
originServerTs: DateTime.now(),
|
originServerTs: DateTime.now(),
|
||||||
content: {'displayname': 'alice'},
|
content: {'displayname': 'alice'},
|
||||||
stateKey: '@alice:test.abc'));
|
stateKey: '@alice:test.abc'));
|
||||||
final userList = room.getParticipants();
|
userList = room.getParticipants();
|
||||||
expect(userList.length, 4);
|
expect(userList.length, 5);
|
||||||
expect(userList[3].displayName, 'alice');
|
expect(userList[4].displayName, 'alice');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('addToDirectChat', () async {
|
test('addToDirectChat', () async {
|
||||||
|
|
Loading…
Reference in a new issue