Mention user in chat

This commit is contained in:
LittleSheep 2024-08-01 14:01:12 +08:00
parent 2bc4513bb6
commit d02ed68afa
2 changed files with 38 additions and 10 deletions

View File

@ -287,7 +287,7 @@ const i18nSimplifiedChinese = {
'themeColor': '全局主题色', 'themeColor': '全局主题色',
'themeColorRed': '现代红', 'themeColorRed': '现代红',
'themeColorBlue': '经典蓝', 'themeColorBlue': '经典蓝',
'themeColorMiku': '未来', 'themeColorMiku': '未来',
'themeColorKagamine': '镜音黄', 'themeColorKagamine': '镜音黄',
'themeColorLuka': '流音粉', 'themeColorLuka': '流音粉',
'themeColorApplied': '全局主题颜色已应用', 'themeColorApplied': '全局主题颜色已应用',

View File

@ -43,7 +43,7 @@ class _ChatMessageInputState extends State<ChatMessageInput> {
Event? _editTo; Event? _editTo;
Event? _replyTo; Event? _replyTo;
void showAttachments() { void _editAttachments() {
showModalBottomSheet( showModalBottomSheet(
context: context, context: context,
isScrollControlled: true, isScrollControlled: true,
@ -55,30 +55,59 @@ class _ChatMessageInputState extends State<ChatMessageInput> {
); );
} }
Future<void> sendMessage() async { List<String> _findMentionedUsers(String text) {
RegExp regExp = RegExp(r'@[a-zA-Z0-9_]+');
Iterable<RegExpMatch> matches = regExp.allMatches(text);
List<String> mentionedUsers =
matches.map((match) => match.group(0)!.substring(1)).toList();
return mentionedUsers;
}
Future<void> _sendMessage() async {
_focusNode.requestFocus(); _focusNode.requestFocus();
final AuthProvider auth = Get.find(); final AuthProvider auth = Get.find();
final prof = auth.userProfile.value!; final prof = auth.userProfile.value!;
if (auth.isAuthorized.isFalse) return; if (auth.isAuthorized.isFalse) return;
final client = auth.configureClient('messaging'); Response resp;
// TODO Deal with the @ ping (query uid with username), and then add into related_user and replace the @ with internal link in body final mentionedUserNames = _findMentionedUsers(_textController.text);
final mentionedUserIds = List<int>.empty(growable: true);
var client = auth.configureClient('auth');
if (mentionedUserNames.isNotEmpty) {
resp = await client.get('/users?name=${mentionedUserNames.join(',')}');
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
return;
} else {
mentionedUserIds.addAll(
resp.body.map((x) => Account.fromJson(x).id).toList().cast<int>(),
);
}
}
client = auth.configureClient('messaging');
const uuid = Uuid(); const uuid = Uuid();
final payload = { final payload = {
'uuid': uuid.v4(), 'uuid': uuid.v4(),
'type': _editTo == null ? 'messages.new' : 'messages.edit', 'type': _editTo == null ? 'messages.new' : 'messages.edit',
'body': { 'body': {
'text': _textController.value.text, 'text': _textController.text,
'algorithm': 'plain', 'algorithm': 'plain',
'attachments': List.from(_attachments), 'attachments': List.from(_attachments),
'related_users': [ 'related_users': [
if (_replyTo != null) _replyTo!.sender.accountId, if (_replyTo != null) _replyTo!.sender.accountId,
...mentionedUserIds,
], ],
if (_replyTo != null) 'quote_event': _replyTo!.id, if (_replyTo != null) 'quote_event': _replyTo!.id,
if (_editTo != null) 'related_event': _editTo!.id, if (_editTo != null) 'related_event': _editTo!.id,
if (_editTo != null && _editTo!.body['quote_event'] != null)
'quote_event': _editTo!.body['quote_event'],
} }
}; };
@ -111,7 +140,6 @@ class _ChatMessageInputState extends State<ChatMessageInput> {
resetInput(); resetInput();
Response resp;
if (_editTo != null) { if (_editTo != null) {
resp = await client.put( resp = await client.put(
'/channels/${widget.realm}/${widget.channel.alias}/messages/${_editTo!.id}', '/channels/${widget.realm}/${widget.channel.alias}/messages/${_editTo!.id}',
@ -217,7 +245,7 @@ class _ChatMessageInputState extends State<ChatMessageInput> {
{'channel': '#${widget.channel.alias}'}, {'channel': '#${widget.channel.alias}'},
), ),
), ),
onSubmitted: (_) => sendMessage(), onSubmitted: (_) => _sendMessage(),
onTapOutside: (_) => onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(), FocusManager.instance.primaryFocus?.unfocus(),
), ),
@ -225,12 +253,12 @@ class _ChatMessageInputState extends State<ChatMessageInput> {
IconButton( IconButton(
icon: const Icon(Icons.attach_file), icon: const Icon(Icons.attach_file),
color: Colors.teal, color: Colors.teal,
onPressed: () => showAttachments(), onPressed: () => _editAttachments(),
), ),
IconButton( IconButton(
icon: const Icon(Icons.send), icon: const Icon(Icons.send),
color: Theme.of(context).colorScheme.primary, color: Theme.of(context).colorScheme.primary,
onPressed: () => sendMessage(), onPressed: () => _sendMessage(),
) )
], ],
).paddingOnly(left: 20, right: 16), ).paddingOnly(left: 20, right: 16),