From 46ff911bfa750adf9f1bffe2e8a1102adc6e28c2 Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 9 Nov 2019 14:20:53 -0400 Subject: [PATCH] Add FindSomeone page --- src/python/matrix_client.py | 34 +++++++++++- src/qml/Pages/AddChat/AddChat.qml | 12 ++--- src/qml/Pages/AddChat/CreateRoom.qml | 4 +- src/qml/Pages/AddChat/FindSomeone.qml | 76 +++++++++++++++++++++++++++ src/qml/Pages/AddChat/JoinRoom.qml | 2 +- 5 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 src/qml/Pages/AddChat/FindSomeone.qml diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index 5ff811e0..98135c6b 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -7,7 +7,7 @@ import platform import re import traceback from contextlib import suppress -from dataclasses import dataclass +from dataclasses import dataclass, field from datetime import datetime from functools import partial from pathlib import Path @@ -57,6 +57,15 @@ class MatrixForbidden(MatrixError): m_code: str = "M_FORBIDDEN" +@dataclass +class UserNotFound(Exception): + user_id: str = field() + + +@dataclass +class InvalidUserInContext(Exception): + user_id: str = field() + @dataclass class UploadError(Exception): http_code: Optional[int] = None @@ -448,7 +457,28 @@ class MatrixClient(nio.AsyncClient): more = await self.load_past_events(room_id) - async def room_create( + async def new_direct_chat(self, invite: str, encrypt: bool = False) -> str: + if invite == self.user_id: + raise InvalidUserInContext(invite) + + if isinstance(await self.get_profile(invite), nio.ProfileGetError): + raise UserNotFound(invite) + + response = await super().room_create( + invite = [invite], + is_direct = True, + visibility = nio.RoomVisibility.private, + initial_state = + [nio.EnableEncryptionBuilder().as_dict()] if encrypt else [], + ) + + if isinstance(response, nio.RoomCreateError): + raise MatrixError.from_nio(response) + + return response.room_id + + + async def new_group_chat( self, name: Optional[str] = None, topic: Optional[str] = None, diff --git a/src/qml/Pages/AddChat/AddChat.qml b/src/qml/Pages/AddChat/AddChat.qml index a6482526..3a73bffd 100644 --- a/src/qml/Pages/AddChat/AddChat.qml +++ b/src/qml/Pages/AddChat/AddChat.qml @@ -6,7 +6,7 @@ import "../../utils.js" as Utils HPage { id: addChatPage - onFocusChanged: createRoom.forceActiveFocus() + onFocusChanged: findSomeone.forceActiveFocus() property string userId @@ -22,7 +22,7 @@ HPage { HTabBar { id: tabBar - currentIndex: 2 + currentIndex: 0 Layout.fillWidth: true @@ -47,13 +47,9 @@ HPage { Layout.fillWidth: true - Item {} - + FindSomeone { id: findSomeone } JoinRoom {} - - CreateRoom { - id: createRoom - } + CreateRoom {} } } } diff --git a/src/qml/Pages/AddChat/CreateRoom.qml b/src/qml/Pages/AddChat/CreateRoom.qml index 9771fc41..9298bdb0 100644 --- a/src/qml/Pages/AddChat/CreateRoom.qml +++ b/src/qml/Pages/AddChat/CreateRoom.qml @@ -27,14 +27,14 @@ HBox { ! blockOtherServersCheckBox.checked, ] - py.callClientCoro(userId, "room_create", args, roomId => { + py.callClientCoro(userId, "new_group_chat", args, roomId => { button.loading = false pageLoader.showRoom(userId, roomId) }, (type, args) => { button.loading = false errorMessage.text = - qsTr("Unknown error - %1, %2").arg(type).arg(args) + qsTr("Unknown error - %1: %2").arg(type).arg(args) }) }, diff --git a/src/qml/Pages/AddChat/FindSomeone.qml b/src/qml/Pages/AddChat/FindSomeone.qml new file mode 100644 index 00000000..1dc6b97c --- /dev/null +++ b/src/qml/Pages/AddChat/FindSomeone.qml @@ -0,0 +1,76 @@ +import QtQuick 2.12 +import QtQuick.Layouts 1.12 +import "../../Base" +import "../../utils.js" as Utils + +HBox { + id: addChatBox + clickButtonOnEnter: "apply" + + onFocusChanged: userField.forceActiveFocus() + + buttonModel: [ + { name: "apply", text: qsTr("Start chat"), iconName: "join", + enabled: Boolean(userField.text), }, + { name: "cancel", text: qsTr("Cancel"), iconName: "cancel" }, + ] + + buttonCallbacks: ({ + apply: button => { + button.loading = true + errorMessage.text = "" + + let args = [userField.text] + + py.callClientCoro(userId, "new_direct_chat", args, roomId => { + button.loading = false + errorMessage.text = "" + pageLoader.showRoom(userId, roomId) + + }, (type, args) => { + button.loading = false + + let txt = qsTr("Unknown error - %1: %2").arg(type).arg(args) + + if (type === "InvalidUserInContext") + txt = qsTr("You cannot invite yourself!") + + if (type === "UserNotFound") + txt = qsTr("This user does not exist.") + + errorMessage.text = txt + }) + }, + + cancel: button => { + userField.text = "" + errorMessage.text = "" + pageLoader.showPrevious() + } + }) + + + readonly property string userId: addChatPage.userId + + + HTextField { + id: userField + placeholderText: qsTr("User ID (e.g. @john:matrix.org)") + error: Boolean(errorMessage.text) + + Layout.fillWidth: true + } + + HLabel { + id: errorMessage + wrapMode: Text.Wrap + horizontalAlignment: Text.AlignHCenter + color: theme.colors.errorText + + visible: Layout.maximumHeight > 0 + Layout.maximumHeight: text ? implicitHeight : 0 + Behavior on Layout.maximumHeight { HNumberAnimation {} } + + Layout.fillWidth: true + } +} diff --git a/src/qml/Pages/AddChat/JoinRoom.qml b/src/qml/Pages/AddChat/JoinRoom.qml index e23179ce..e3afc5d7 100644 --- a/src/qml/Pages/AddChat/JoinRoom.qml +++ b/src/qml/Pages/AddChat/JoinRoom.qml @@ -30,7 +30,7 @@ HBox { }, (type, args) => { button.loading = false - let txt = qsTr("Unknown error - %1, %2").arg(type).arg(args) + let txt = qsTr("Unknown error - %1: %2").arg(type).arg(args) if (type === "ValueError") txt = qsTr("Unrecognized alias, room ID or URL")