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