[Event] load reply event when loading timeline

[Room] load creator user
[Timeline] fix sendUser not being passed, fix stateKeyUser not being resolved

Took 46 minutes
This commit is contained in:
Marcel 2019-07-11 22:17:40 +02:00
parent fab4604b08
commit 0823473bdf
4 changed files with 72 additions and 16 deletions

View file

@ -22,11 +22,13 @@
*/ */
import 'dart:convert'; import 'dart:convert';
import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/Client.dart';
import './User.dart';
import './Room.dart'; import './Room.dart';
import './User.dart';
/// A single Matrix event, e.g. a message in a chat. /// A single Matrix event, e.g. a message in a chat.
class Event { class Event {
@ -48,6 +50,8 @@ class Event {
/// The type of this event. Mostly this is 'timeline'. /// The type of this event. Mostly this is 'timeline'.
final String environment; final String environment;
Event replyEvent;
/// The status of this event. /// The status of this event.
/// -1=ERROR /// -1=ERROR
/// 0=SENDING /// 0=SENDING
@ -67,6 +71,7 @@ class Event {
this.status = 2, this.status = 2,
this.environment, this.environment,
this.content, this.content,
this.replyEvent,
}); });
/// Returns the body of this event if it has a body. /// Returns the body of this event if it has a body.

View file

@ -22,11 +22,12 @@
*/ */
import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/Event.dart';
import 'package:famedlysdk/src/utils/MxContent.dart';
import 'package:famedlysdk/src/responses/ErrorResponse.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart';
import 'package:famedlysdk/src/sync/EventUpdate.dart'; import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/Event.dart'; import 'package:famedlysdk/src/utils/ChatTime.dart';
import 'package:famedlysdk/src/utils/MxContent.dart';
import './User.dart'; import './User.dart';
import 'Timeline.dart'; import 'Timeline.dart';
@ -86,6 +87,9 @@ class Room {
Event lastEvent; Event lastEvent;
// The user who created the room based on m.create events
User creator;
/// Your current client instance. /// Your current client instance.
final Client client; final Client client;
@ -118,6 +122,7 @@ class Room {
this.joinRules, this.joinRules,
this.powerLevels, this.powerLevels,
this.lastEvent, this.lastEvent,
this.creator,
this.client, this.client,
}); });
@ -365,6 +370,13 @@ class Room {
return resp; return resp;
} }
/// Load the creator event of this room
Future<Event> getCreatorEvent(Client matrix) async {
List<Event> events = await this.loadEvents();
return events.firstWhere((event) => event.type == EventTypes.RoomCreate,
orElse: null);
}
/// Returns a Room from a json String which comes normally from the store. /// Returns a Room from a json String which comes normally from the store.
static Future<Room> getRoomFromTableRow( static Future<Room> getRoomFromTableRow(
Map<String, dynamic> row, Client matrix) async { Map<String, dynamic> row, Client matrix) async {
@ -376,7 +388,7 @@ class Room {
if (avatarUrl == "") if (avatarUrl == "")
avatarUrl = await matrix.store?.getAvatarFromSingleChat(row["id"]) ?? ""; avatarUrl = await matrix.store?.getAvatarFromSingleChat(row["id"]) ?? "";
return Room( Room room = Room(
id: row["id"], id: row["id"],
name: name, name: name,
membership: row["membership"], membership: row["membership"],
@ -411,6 +423,17 @@ class Room {
lastEvent: Event.fromJson(row, null), lastEvent: Event.fromJson(row, null),
client: matrix, client: matrix,
); );
// Lets get the creator too if not a test as tests cant use the stpre :(
if (matrix.homeserver != "https://fakeServer.notExisting") {
Event creatorEvent = await room.getCreatorEvent(matrix);
if (creatorEvent != null) {
room.creator = await matrix.store
?.getUser(matrixID: creatorEvent.id, room: row["id"]);
}
}
return room;
} }
@Deprecated("Use client.store.getRoomById(String id) instead!") @Deprecated("Use client.store.getRoomById(String id) instead!")

View file

@ -22,6 +22,7 @@
*/ */
import 'dart:async'; import 'dart:async';
import 'Event.dart'; import 'Event.dart';
import 'Room.dart'; import 'Room.dart';
import 'User.dart'; import 'User.dart';
@ -79,14 +80,41 @@ class Timeline {
events[i] = Event.fromJson(eventUpdate.content, room); events[i] = Event.fromJson(eventUpdate.content, room);
} }
} else { } else {
User user = await room.client.store Event newEvent;
User senderUser = await room.client.store
?.getUser(matrixID: eventUpdate.content["sender"], room: room); ?.getUser(matrixID: eventUpdate.content["sender"], room: room);
if (user != null) { if (senderUser != null) {
eventUpdate.content["displayname"] = user.displayName; eventUpdate.content["displayname"] = senderUser.displayName;
eventUpdate.content["avatar_url"] = user.avatarUrl.mxc; eventUpdate.content["avatar_url"] = senderUser.avatarUrl.mxc;
} }
Event newEvent = Event.fromJson(eventUpdate.content, room); User stateKeyUser;
if (eventUpdate.content.containsKey("state_key")) {
stateKeyUser = await room.client.store?.getUser(
matrixID: eventUpdate.content["state_key"], room: room);
}
if (senderUser != null && stateKeyUser != null) {
newEvent = Event.fromJson(eventUpdate.content, room,
senderUser: senderUser, stateKeyUser: stateKeyUser);
} else if (senderUser != null) {
newEvent = Event.fromJson(eventUpdate.content, room,
senderUser: senderUser);
} else if (stateKeyUser != null) {
newEvent = Event.fromJson(eventUpdate.content, room,
stateKeyUser: stateKeyUser);
} else {
newEvent = Event.fromJson(eventUpdate.content, room);
}
// TODO update to type check when https://gitlab.com/famedly/famedlysdk/merge_requests/28/ is merged
if (newEvent.content.containsKey("m.relates_to")) {
Map<String, dynamic> relates_to = newEvent.content["m.relates_to"];
if (relates_to.containsKey("m.in_reply_to")) {
newEvent.replyEvent = await room.getEventById(newEvent
.content["m.relates_to"]["m.in_reply_to"]["event_id"]);
}
}
events.insert(0, newEvent); events.insert(0, newEvent);
if (onInsert != null) onInsert(0); if (onInsert != null) onInsert(0);

View file

@ -1,5 +1,5 @@
# Generated by pub # Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
analyzer: analyzer:
dependency: transitive dependency: transitive
@ -21,7 +21,7 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0" version: "2.2.0"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
@ -297,7 +297,7 @@ packages:
name: pedantic name: pedantic
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.5.0" version: "1.7.0"
pool: pool:
dependency: transitive dependency: transitive
description: description:
@ -325,7 +325,7 @@ packages:
name: quiver name: quiver
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.2" version: "2.0.3"
shelf: shelf:
dependency: transitive dependency: transitive
description: description:
@ -414,7 +414,7 @@ packages:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.4" version: "0.2.5"
timing: timing:
dependency: transitive dependency: transitive
description: description: