[SDK] Refactoring

This commit is contained in:
Christian Pauly 2019-08-07 11:38:51 +02:00
parent beff166037
commit ac39be9a1e
5 changed files with 27 additions and 64 deletions

View file

@ -86,6 +86,8 @@ class Client {
/// Returns the current login state. /// Returns the current login state.
bool isLogged() => accessToken != null; bool isLogged() => accessToken != null;
RoomList roomList;
/// Checks the supported versions of the Matrix protocol and the supported /// Checks the supported versions of the Matrix protocol and the supported
/// login types. Returns false if the server is not compatible with the /// login types. Returns false if the server is not compatible with the
/// client. Automatically sets [matrixVersions] and [lazyLoadMembers]. /// client. Automatically sets [matrixVersions] and [lazyLoadMembers].

View file

@ -25,6 +25,8 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:core'; import 'dart:core';
import 'package:famedlysdk/src/Room.dart';
import 'package:famedlysdk/src/RoomList.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -149,6 +151,18 @@ class Connection {
client.store?.storeClient(); client.store?.storeClient();
List<Room> rooms = await client.store
?.getRoomList(onlyLeft: false, onlyGroups: false, onlyDirect: false);
client.roomList = RoomList(
client: client,
onlyLeft: false,
onlyDirect: false,
onlyGroups: false,
onUpdate: null,
onInsert: null,
onRemove: null,
rooms: rooms);
onLoginStateChanged.add(LoginState.logged); onLoginStateChanged.add(LoginState.logged);
_sync(); _sync();

View file

@ -294,7 +294,8 @@ class Room {
/// Call the Matrix API to unban a banned user from this room. /// Call the Matrix API to unban a banned user from this room.
Future<dynamic> setPower(String userID, int power) async { Future<dynamic> setPower(String userID, int power) async {
Map<String, int> powerMap = await client.store.getPowerLevels(id); Map<String, int> powerMap = states["m.room.power_levels"].content["users"];
if (powerMap == null) return null;
powerMap[userID] = power; powerMap[userID] = power;
dynamic res = await client.connection.jsonRequest( dynamic res = await client.connection.jsonRequest(

View file

@ -24,6 +24,8 @@
import 'dart:async'; import 'dart:async';
import 'dart:core'; import 'dart:core';
import 'package:famedlysdk/src/State.dart';
import 'Client.dart'; import 'Client.dart';
import 'Event.dart'; import 'Event.dart';
import 'Room.dart'; import 'Room.dart';
@ -87,7 +89,6 @@ class RoomList {
// Add the new chat to the list // Add the new chat to the list
Room newRoom = Room( Room newRoom = Room(
id: chatUpdate.id, id: chatUpdate.id,
name: "",
membership: chatUpdate.membership, membership: chatUpdate.membership,
prev_batch: chatUpdate.prev_batch, prev_batch: chatUpdate.prev_batch,
highlightCount: chatUpdate.highlight_count, highlightCount: chatUpdate.highlight_count,
@ -125,11 +126,6 @@ class RoomList {
} }
void _handleEventUpdate(EventUpdate eventUpdate) { void _handleEventUpdate(EventUpdate eventUpdate) {
// Is the event necessary for the chat list? If not, then return
if (!(eventUpdate.type == "timeline" ||
eventUpdate.eventType == "m.room.avatar" ||
eventUpdate.eventType == "m.room.name")) return;
// Search the room in the rooms // Search the room in the rooms
num j = 0; num j = 0;
for (j = 0; j < rooms.length; j++) { for (j = 0; j < rooms.length; j++) {
@ -138,34 +134,10 @@ class RoomList {
final bool found = (j < rooms.length && rooms[j].id == eventUpdate.roomID); final bool found = (j < rooms.length && rooms[j].id == eventUpdate.roomID);
if (!found) return; if (!found) return;
// Is this an old timeline event? Then stop here... State stateEvent = State.fromJson(eventUpdate.content, rooms[j]);
/*if (eventUpdate.type == "timeline" && if (rooms[j].states[stateEvent.key] != null &&
ChatTime(eventUpdate.content["origin_server_ts"]) <= rooms[j].states[stateEvent.key].time > stateEvent.time) return;
rooms[j].timeCreated) return;*/ rooms[j].states[stateEvent.key] = stateEvent;
if (eventUpdate.type == "timeline") {
User stateKey = null;
if (eventUpdate.content["state_key"] is String)
stateKey = User(eventUpdate.content["state_key"]);
// Update the last message preview
rooms[j].lastEvent = Event(
eventUpdate.content["id"],
User(eventUpdate.content["sender"]),
ChatTime(eventUpdate.content["origin_server_ts"]),
room: rooms[j],
stateKey: stateKey,
content: eventUpdate.content["content"],
environment: eventUpdate.eventType,
status: 2,
);
}
if (eventUpdate.eventType == "m.room.name") {
// Update the room name
rooms[j].name = eventUpdate.content["content"]["name"];
} else if (eventUpdate.eventType == "m.room.avatar") {
// Update the room avatar
rooms[j].avatar = MxContent(eventUpdate.content["content"]["url"]);
}
sortAndUpdate(); sortAndUpdate();
} }

View file

@ -47,8 +47,8 @@ class Timeline {
int _findEvent({String event_id, String unsigned_txid}) { int _findEvent({String event_id, String unsigned_txid}) {
int i; int i;
for (i = 0; i < events.length; i++) { for (i = 0; i < events.length; i++) {
if (events[i].id == event_id || if (events[i].eventId == event_id ||
(unsigned_txid != null && events[i].id == unsigned_txid)) break; (unsigned_txid != null && events[i].eventId == unsigned_txid)) break;
} }
return i; return i;
} }
@ -82,33 +82,7 @@ class Timeline {
eventUpdate.content["avatar_url"] = senderUser.avatarUrl.mxc; eventUpdate.content["avatar_url"] = senderUser.avatarUrl.mxc;
} }
User stateKeyUser; newEvent = Event.fromJson(eventUpdate.content, room);
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);