Add FindSomeone page
This commit is contained in:
		| @@ -7,7 +7,7 @@ import platform | |||||||
| import re | import re | ||||||
| import traceback | import traceback | ||||||
| from contextlib import suppress | from contextlib import suppress | ||||||
| from dataclasses import dataclass | from dataclasses import dataclass, field | ||||||
| from datetime import datetime | from datetime import datetime | ||||||
| from functools import partial | from functools import partial | ||||||
| from pathlib import Path | from pathlib import Path | ||||||
| @@ -57,6 +57,15 @@ class MatrixForbidden(MatrixError): | |||||||
|     m_code: str = "M_FORBIDDEN" |     m_code: str = "M_FORBIDDEN" | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @dataclass | ||||||
|  | class UserNotFound(Exception): | ||||||
|  |     user_id: str = field() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @dataclass | ||||||
|  | class InvalidUserInContext(Exception): | ||||||
|  |     user_id: str = field() | ||||||
|  |  | ||||||
| @dataclass | @dataclass | ||||||
| class UploadError(Exception): | class UploadError(Exception): | ||||||
|     http_code: Optional[int] = None |     http_code: Optional[int] = None | ||||||
| @@ -448,7 +457,28 @@ class MatrixClient(nio.AsyncClient): | |||||||
|             more = await self.load_past_events(room_id) |             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, |         self, | ||||||
|         name:     Optional[str] = None, |         name:     Optional[str] = None, | ||||||
|         topic:    Optional[str] = None, |         topic:    Optional[str] = None, | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ import "../../utils.js" as Utils | |||||||
|  |  | ||||||
| HPage { | HPage { | ||||||
|     id: addChatPage |     id: addChatPage | ||||||
|     onFocusChanged: createRoom.forceActiveFocus() |     onFocusChanged: findSomeone.forceActiveFocus() | ||||||
|  |  | ||||||
|  |  | ||||||
|     property string userId |     property string userId | ||||||
| @@ -22,7 +22,7 @@ HPage { | |||||||
|  |  | ||||||
|         HTabBar { |         HTabBar { | ||||||
|             id: tabBar |             id: tabBar | ||||||
|             currentIndex: 2 |             currentIndex: 0 | ||||||
|  |  | ||||||
|             Layout.fillWidth: true |             Layout.fillWidth: true | ||||||
|  |  | ||||||
| @@ -47,13 +47,9 @@ HPage { | |||||||
|  |  | ||||||
|             Layout.fillWidth: true |             Layout.fillWidth: true | ||||||
|  |  | ||||||
|             Item {} |             FindSomeone { id: findSomeone } | ||||||
|  |  | ||||||
|             JoinRoom {} |             JoinRoom {} | ||||||
|  |             CreateRoom {} | ||||||
|             CreateRoom { |  | ||||||
|                 id: createRoom |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -27,14 +27,14 @@ HBox { | |||||||
|                 ! blockOtherServersCheckBox.checked, |                 ! blockOtherServersCheckBox.checked, | ||||||
|             ] |             ] | ||||||
|  |  | ||||||
|             py.callClientCoro(userId, "room_create", args, roomId => { |             py.callClientCoro(userId, "new_group_chat", args, roomId => { | ||||||
|                 button.loading = false |                 button.loading = false | ||||||
|                 pageLoader.showRoom(userId, roomId) |                 pageLoader.showRoom(userId, roomId) | ||||||
|  |  | ||||||
|             }, (type, args) => { |             }, (type, args) => { | ||||||
|                 button.loading = false |                 button.loading = false | ||||||
|                 errorMessage.text = |                 errorMessage.text = | ||||||
|                     qsTr("Unknown error - %1, %2").arg(type).arg(args) |                     qsTr("Unknown error - %1: %2").arg(type).arg(args) | ||||||
|             }) |             }) | ||||||
|         }, |         }, | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										76
									
								
								src/qml/Pages/AddChat/FindSomeone.qml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/qml/Pages/AddChat/FindSomeone.qml
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -30,7 +30,7 @@ HBox { | |||||||
|             }, (type, args) => { |             }, (type, args) => { | ||||||
|                 button.loading = false |                 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") |                 if (type === "ValueError") | ||||||
|                     txt = qsTr("Unrecognized alias, room ID or URL") |                     txt = qsTr("Unrecognized alias, room ID or URL") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	