Add FindSomeone page

This commit is contained in:
miruka 2019-11-09 14:20:53 -04:00
parent 0aedc1a7d0
commit 46ff911bfa
5 changed files with 115 additions and 13 deletions

View File

@ -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,

View File

@ -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 {}
}
}
}

View File

@ -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)
})
},

View 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
}
}

View File

@ -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")