diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 841eba9..855672d 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -159,7 +159,7 @@ class Room { dynamic res = await client.connection.jsonRequest( type: "PUT", action: - "/client/r0/rooms/${id}/send/m.room.topic/${new DateTime.now()}", + "/client/r0/rooms/${id}/send/m.room.topic/${new DateTime.now()}", data: {"topic": newName}); if (res is ErrorResponse) client.connection.onError.add(res); return res; @@ -176,8 +176,7 @@ class Room { if (txid == null) txid = "txid${DateTime.now().millisecondsSinceEpoch}"; final dynamic res = await client.connection.jsonRequest( type: "PUT", - action: - "/client/r0/rooms/${id}/send/m.room.message/$txid", + action: "/client/r0/rooms/${id}/send/m.room.message/$txid", data: {"msgtype": "m.text", "body": message}); if (res["errcode"] == "M_LIMIT_EXCEEDED") client.connection.onError.add(res["error"]); @@ -185,43 +184,39 @@ class Room { } Future sendTextEvent(String message) async { - final String type = "m.room.message"; final int now = DateTime.now().millisecondsSinceEpoch; final String messageID = "msg$now"; - EventUpdate eventUpdate = EventUpdate( - type: type, - roomID: id, - eventType: "timeline", - content: { - "type": type, - "id": messageID, - "sender": client.userID, - "status": 0, - "origin_server_ts": now, - "content": { - "msgtype": "m.text", - "body": message, - } + EventUpdate eventUpdate = + EventUpdate(type: type, roomID: id, eventType: "timeline", content: { + "type": type, + "id": messageID, + "sender": client.userID, + "status": 0, + "origin_server_ts": now, + "content": { + "msgtype": "m.text", + "body": message, } - ); + }); client.connection.onEvent.add(eventUpdate); await client.store.transaction(() { client.store.storeEventUpdate(eventUpdate); }); final dynamic res = await sendText(message, txid: messageID); if (res is ErrorResponse) { - client.store.db.rawUpdate("UPDATE Events SET status=-1 WHERE id=?", [ messageID ]); - } - else { + client.store.db + .rawUpdate("UPDATE Events SET status=-1 WHERE id=?", [messageID]); + } else { final String newEventID = res["event_id"]; - final List> event = await client.store.db.rawQuery("SELECT * FROM Events WHERE id=?", [ newEventID ]); + final List> event = await client.store.db + .rawQuery("SELECT * FROM Events WHERE id=?", [newEventID]); if (event.length > 0) { client.store.db.rawDelete("DELETE FROM Events WHERE id=?", [messageID]); - } - else { - client.store.db.rawUpdate("UPDATE Events SET id=?, status=1 WHERE id=?", [ newEventID, messageID ]); + } else { + client.store.db.rawUpdate("UPDATE Events SET id=?, status=1 WHERE id=?", + [newEventID, messageID]); } return newEventID; } @@ -306,7 +301,8 @@ class Room { if (resp is ErrorResponse) return; - if (!(resp["chunk"] is List && resp["chunk"].length > 0 && + if (!(resp["chunk"] is List && + resp["chunk"].length > 0 && resp["end"] is String)) return; List history = resp["chunk"]; @@ -321,22 +317,42 @@ class Room { client.connection.onEvent.add(eventUpdate); client.store.storeEventUpdate(eventUpdate); client.store.txn.rawUpdate( - "UPDATE Rooms SET prev_batch=? WHERE id=?", [ resp["end"], id ]); + "UPDATE Rooms SET prev_batch=? WHERE id=?", [resp["end"], id]); } }); } + Future addToDirectChat(String userID) async { + Map> directChats = + await client.store.getAccountDataDirectChats(); + if (directChats.containsKey(userID)) if (!directChats[userID].contains(id)) + directChats[userID].add(id); + else + return null; // Is already in direct chats + else + directChats[userID] = [id]; + + final resp = await client.connection.jsonRequest( + type: "PUT", + action: "/client/r0/user/${client.userID}/account_data/m.direct", + data: directChats); + return resp; + } + Future sendReadReceipt(String eventID) async { final dynamic resp = client.connection.jsonRequest( type: "fully_read", action: "/client/r0/rooms/$id/read_markers", - data: {"m.fully_read": eventID,"m.read": eventID,}); + data: { + "m.fully_read": eventID, + "m.read": eventID, + }); return resp; } /// Returns a Room from a json String which comes normally from the store. - static Future getRoomFromTableRow(Map row, - Client matrix) async { + static Future getRoomFromTableRow( + Map row, Client matrix) async { String name = row["topic"]; if (name == "") name = await matrix.store?.getChatNameFromMemberNames(row["id"]) ?? ""; diff --git a/lib/src/Store.dart b/lib/src/Store.dart index eff9787..cf3e931 100644 --- a/lib/src/Store.dart +++ b/lib/src/Store.dart @@ -183,7 +183,7 @@ class Store { switch (userUpdate.eventType) { case "m.direct": if (userUpdate.content["content"] is Map) { - final Map directMap = userUpdate.content["content"]; + final Map directMap = userUpdate.content["content"]; directMap.forEach((String key, dynamic value) { if (value is List && value.length > 0) txn.rawUpdate( @@ -463,17 +463,18 @@ class Store { bool onlyDirect = false, bool onlyGroups = false}) async { if (onlyDirect && onlyGroups) return []; - List> res = await db.rawQuery("SELECT rooms.*, events.origin_server_ts, events.content_json, events.type, events.sender, events.status, events.state_key " + - " FROM Rooms rooms LEFT JOIN Events events " + - " ON rooms.id=events.chat_id " + - " WHERE rooms.id!='' " + - " AND rooms.membership" + - (onlyLeft ? "=" : "!=") + - "'left' " + - (onlyDirect ? " AND rooms.direct_chat_matrix_id!= '' " : "") + - (onlyGroups ? " AND rooms.direct_chat_matrix_id= '' " : "") + - " GROUP BY rooms.id " + - " ORDER BY origin_server_ts DESC "); + List> res = await db.rawQuery( + "SELECT rooms.*, events.origin_server_ts, events.content_json, events.type, events.sender, events.status, events.state_key " + + " FROM Rooms rooms LEFT JOIN Events events " + + " ON rooms.id=events.chat_id " + + " WHERE rooms.id!='' " + + " AND rooms.membership" + + (onlyLeft ? "=" : "!=") + + "'left' " + + (onlyDirect ? " AND rooms.direct_chat_matrix_id!= '' " : "") + + (onlyGroups ? " AND rooms.direct_chat_matrix_id= '' " : "") + + " GROUP BY rooms.id " + + " ORDER BY origin_server_ts DESC "); List roomList = []; for (num i = 0; i < res.length; i++) { try { @@ -563,6 +564,19 @@ class Store { return powerMap; } + Future>> getAccountDataDirectChats() async { + Map> directChats = {}; + List> res = await db.rawQuery( + "SELECT id, direct_chat_matrix_id FROM Rooms WHERE direct_chat_matrix_id!=''"); + for (int i = 0; i < res.length; i++) { + if (directChats.containsKey(res[i]["direct_chat_matrix_id"])) + directChats[res[i]["direct_chat_matrix_id"]].add(res[i]["id"]); + else + directChats[res[i]["direct_chat_matrix_id"]] = [res[i]["id"]]; + } + return directChats; + } + /// The database sheme for the Client class. static final String ClientsScheme = 'CREATE TABLE IF NOT EXISTS Clients(' + 'client TEXT PRIMARY KEY, ' +