Skip to content

Commit

Permalink
support join chat room
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Jul 27, 2023
1 parent 3db03c6 commit 16ccd9d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
60 changes: 57 additions & 3 deletions lib/components/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,16 @@ class NewChatButton extends StatelessWidget {
},
),
),
const PopupMenuItem(
PopupMenuItem(
child: ListTile(
leading: Icon(Icons.group_add),
title: Align(
leading: const Icon(Icons.group_add),
title: const Align(
alignment: Alignment(-1.2, 0),
child: Text("Join Chat Room"),
),
onTap: () {
_joinChatRoom(context);
},
),
),
PopupMenuItem(
Expand Down Expand Up @@ -234,6 +237,57 @@ class NewChatButton extends StatelessWidget {
Navigator.pop(context);
}

_joinChatRoom(BuildContext context) {
final theme = Theme.of(context);
final dialogTextStyle = theme.textTheme.titleMedium!
.copyWith(color: theme.textTheme.bodySmall!.color);
showDialog(context: context, builder: (BuildContext context) {
var connToken = "";
return AlertDialog(
content: TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter the connection token';
}
return null;
},
style: dialogTextStyle,
decoration: InputDecoration(
labelText: "Connection Token",
floatingLabelBehavior: FloatingLabelBehavior.auto,
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
),
filled: true,
),
autovalidateMode: AutovalidateMode.always,
onChanged: (value) {
connToken = value;
},
),
actions: [
_DialogButton(
text: "Join",
onPressed: () => _handleConnToken(context, connToken),
),
_DialogButton(
text: "Cancel",
onPressed: () {},
),
],
);
});
}


_handleConnToken(BuildContext context, String token) {
final comp.ChatRoomController chatRoomController = Get.find();
chatRoomController.joinChatRoom(token);
Navigator.pop(context);
}

_loadChatRooms(BuildContext context) {
final comp.ChatRoomController chatRoomController = Get.find();
chatRoomController.loadChatRooms();
Expand Down
8 changes: 8 additions & 0 deletions lib/controller/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class ChatRoomController extends GetxController {
update();
}

void joinChatRoom(String connToken) async {
final joinRoom = await ChatRoomRepository().joinChatRoom(connToken);
roomList.add(joinRoom);
currentRoomIndex.value = IntegerWrapper(roomList.length - 1);
currentChatRoomUuid.value = joinRoom.uuid;
update();
}

void loadChatRooms() async {
var remoteRooms = await ChatRoomRepository().getChatRoomsRemote();
await ChatRoomRepository().upsertLocalChatRooms(remoteRooms);
Expand Down
2 changes: 1 addition & 1 deletion lib/controller/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class SettingsController extends GetxController {
String? updateTiDBCmdToRepo(String cmd) {
if (cmd.isEmpty) return "Empty";

var (host, port, user, password) = parseTiDBConnectionText(cmd);
var (host, port, user, password, _) = parseTiDBConnectionText(cmd);
if (port == 0) {
return user;
}
Expand Down
26 changes: 20 additions & 6 deletions lib/repository/chat_room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'dart:convert';
import 'dart:math';

import 'package:uuid/uuid.dart';

class TiDBConnection {
MySQLConnection? connection;

Expand All @@ -16,6 +18,7 @@ class TiDBConnection {
String userName = "";
String userNamePrefix = "";
String password = "";
String msgTable = "";

close() async {
if (connection != null) {
Expand All @@ -37,11 +40,12 @@ class TiDBConnection {
password.isNotEmpty;
}

setConnect(String host, int port, String userName, String password) async {
setConnect(String host, int port, String userName, String password, String msgTableName) async {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
msgTable = msgTableName;
userNamePrefix = userName.split(".").first;

close();
Expand All @@ -53,6 +57,7 @@ class TiDBConnection {
userName = "";
userNamePrefix = "";
password = "";
msgTable = "";

close();
}
Expand All @@ -64,7 +69,7 @@ class TiDBConnection {

String toToken() {
String connText = hasSet() //
? "mysql -u '$userName' -h $host -P 4000 -p$password"
? "mysql -u '$userName' -h $host -P 4000 -p$password --tb $msgTable"
: "";
return base64.encode(utf8.encode(connText));
}
Expand All @@ -81,8 +86,8 @@ class TiDBConnection {
static TiDBConnection fromToken(String token) {
String str = utf8.decode(base64.decode(token));
var conn = TiDBConnection();
var (host, port, userName, password) = parseTiDBConnectionText(str);
conn.setConnect(host, port, userName, password);
var (host, port, userName, password, msgTable) = parseTiDBConnectionText(str);
conn.setConnect(host, port, userName, password, msgTable);
return conn;
}
}
Expand Down Expand Up @@ -237,7 +242,7 @@ class ChatRoomRepository {
String userName,
String password,
) {
myTiDBConn.setConnect(host, port, userName, password);
myTiDBConn.setConnect(host, port, userName, password, "");
}

static Future<String?> validateRemoteDB(TiDBConnection conn) async {
Expand Down Expand Up @@ -396,6 +401,15 @@ class ChatRoomRepository {
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
}

Future<ChatRoom> joinChatRoom(String connToken) async {
var roomConn = TiDBConnection.fromToken(connToken);
final room = ChatRoom(uuid: roomConn.msgTable, name: "Other Chat Room",
createTime: DateTime.now().toUtc(),
connectionToken: connToken, role: Role.guest);
addChatRoom(room);
return room;
}

Future<void> addChatRoom(ChatRoom room) async {
var roomConn = TiDBConnection.fromToken(room.connectionToken);

Expand Down Expand Up @@ -423,7 +437,7 @@ class ChatRoomRepository {
''');

// Update the connection to use the new user.
roomConn.setConnect(myTiDBConn.host, myTiDBConn.port, user, pwd);
roomConn.setConnect(myTiDBConn.host, myTiDBConn.port, user, pwd, "");
// Looks like TiDB Serverless need some time to prepare the new users' connection.
// And immediate connection will fail.
}
Expand Down
13 changes: 8 additions & 5 deletions lib/utils/tidb.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Return (host, port, user, password).
// Return (host, port, user, password, msgTable).
// If port == 0, means "host" is the error message.
(String, int, String, String) parseTiDBConnectionText(String text) {
(String, int, String, String, String) parseTiDBConnectionText(String text) {
if (text.isEmpty) {
return ("", 0, "", "");
return ("", 0, "", "", "");
}

text = text.replaceFirst(" -p", " -p ");
Expand All @@ -14,6 +14,7 @@
String host = "";
int port = 0;
String password = "";
String msgTable = "";

try {
for (int i = 0; i < options.length; i += 1) {
Expand All @@ -38,12 +39,14 @@
case "--password":
password = nextOpt;
break;
case "--tb":
msgTable = nextOpt;
default:
}
}

return (host, port, user, password);
return (host, port, user, password, msgTable);
} catch (e) {
return (e.toString(), 0, "", "");
return (e.toString(), 0, "", "", "");
}
}

0 comments on commit 16ccd9d

Please sign in to comment.