[User] Request user sync and async

This commit is contained in:
Christian Pauly 2019-11-15 11:08:43 +00:00
parent dcb1c42044
commit b58dd46e8d
3 changed files with 46 additions and 9 deletions

View File

@ -27,7 +27,6 @@ import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/utils/Receipt.dart'; import 'package:famedlysdk/src/utils/Receipt.dart';
import './Room.dart'; import './Room.dart';
import 'User.dart';
/// Defines a timeline event for a room. /// Defines a timeline event for a room.
class Event extends RoomState { class Event extends RoomState {
@ -106,8 +105,7 @@ class Event extends RoomState {
for (var entry in room.roomAccountData["m.receipt"].content.entries) { for (var entry in room.roomAccountData["m.receipt"].content.entries) {
if (entry.value["event_id"] == eventId) if (entry.value["event_id"] == eventId)
receiptsList.add(Receipt( receiptsList.add(Receipt(
room.states[entry.key]?.asUser ?? User(entry.key), room.getUserByMXIDSync(entry.key), ChatTime(entry.value["ts"])));
ChatTime(entry.value["ts"])));
} }
return receiptsList; return receiptsList;
} }

View File

@ -176,8 +176,7 @@ class Room {
List<dynamic> typingMxid = ephemerals["m.typing"].content["user_ids"]; List<dynamic> typingMxid = ephemerals["m.typing"].content["user_ids"];
List<User> typingUsers = []; List<User> typingUsers = [];
for (int i = 0; i < typingMxid.length; i++) for (int i = 0; i < typingMxid.length; i++)
typingUsers.add( typingUsers.add(getUserByMXIDSync(typingMxid[i]));
states[typingMxid[i]]?.asUser ?? User(typingMxid[i], room: this));
return typingUsers; return typingUsers;
} }
@ -707,16 +706,56 @@ class Room {
return participants; return participants;
} }
/// Returns the [User] object for the given [mxID] or requests it from
/// the homeserver and waits for a response.
Future<User> getUserByMXID(String mxID) async { Future<User> getUserByMXID(String mxID) async {
if (states[mxID] != null) return states[mxID].asUser; if (states[mxID] != null) return states[mxID].asUser;
return requestUser(mxID);
}
/// Returns the [User] object for the given [mxID] or requests it from
/// the homeserver and returns a default [User] object while waiting.
User getUserByMXIDSync(String mxID) {
if (states[mxID] != null)
return states[mxID].asUser;
else {
requestUser(mxID);
return User(mxID, room: this);
}
}
Set<String> _requestingMatrixIds = Set();
/// Requests a missing [User] for this room. Important for clients using
/// lazy loading.
Future<User> requestUser(String mxID) async {
if (mxID == null || !_requestingMatrixIds.add(mxID)) return null;
final dynamic resp = await client.connection.jsonRequest( final dynamic resp = await client.connection.jsonRequest(
type: HTTPType.GET, type: HTTPType.GET,
action: "/client/r0/rooms/$id/state/m.room.member/$mxID"); action: "/client/r0/rooms/$id/state/m.room.member/$mxID");
if (resp is ErrorResponse) return null; if (resp is ErrorResponse) {
return User(mxID, _requestingMatrixIds.remove(mxID);
return null;
}
final User user = User(mxID,
displayName: resp["displayname"], displayName: resp["displayname"],
avatarUrl: resp["avatar_url"], avatarUrl: resp["avatar_url"],
room: this); room: this);
states[mxID] = user;
if (client.store != null)
client.store.transaction(() {
client.store.storeEventUpdate(
EventUpdate(
content: resp,
roomID: id,
type: "state",
eventType: "m.room.member"),
);
return;
});
if (onUpdate != null) onUpdate();
_requestingMatrixIds.remove(mxID);
return user;
} }
/// Searches for the event in the store. If it isn't found, try to request it /// Searches for the event in the store. If it isn't found, try to request it

View File

@ -43,7 +43,7 @@ class RoomState {
/// The user who has sent this event if it is not a global account data event. /// The user who has sent this event if it is not a global account data event.
final String senderId; final String senderId;
User get sender => room.states[senderId]?.asUser ?? User(senderId); User get sender => room.getUserByMXIDSync(senderId);
/// The time this event has received at the server. May be null for events like /// The time this event has received at the server. May be null for events like
/// account data. /// account data.
@ -64,7 +64,7 @@ class RoomState {
/// the overwriting semantics for this piece of room state. /// the overwriting semantics for this piece of room state.
final String stateKey; final String stateKey;
User get stateKeyUser => room.states[stateKey]?.asUser ?? User(stateKey); User get stateKeyUser => room.getUserByMXIDSync(stateKey);
RoomState( RoomState(
{this.content, {this.content,