From d5a2d315dbccbc64b522cf58188cec93e9363ceb Mon Sep 17 00:00:00 2001 From: John Gibbon Date: Mon, 16 Nov 2020 10:28:16 +0100 Subject: [PATCH] forwarding/new message/attachment permissions enhances #159 PR, hopefully fixes #164 issue --- qml/js/functions.js | 25 +++++++++++++++++ qml/pages/ChatPage.qml | 31 ++++++++++++-------- qml/pages/ChatSelectionPage.qml | 50 +++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 11 deletions(-) diff --git a/qml/js/functions.js b/qml/js/functions.js index e7fa25e..a0954ac 100644 --- a/qml/js/functions.js +++ b/qml/js/functions.js @@ -406,3 +406,28 @@ function handleErrorMessage(code, message) { appNotification.show(message); } } + +function getMessagesNeededForwardPermissions(messages) { + var neededPermissions = ["can_send_messages"]; + + var mediaMessageTypes = ["messageAudio", "messageDocument", "messagePhoto", "messageVideo", "messageVideoNote", "messageVoiceNote"]; + var otherMessageTypes = ["messageAnimation", "messageGame", "messageSticker"] + for(var i = 0; i < messages.length; i += 1) { + var type = messages[i]["content"]["@type"]; + var permission = ""; + if(type === "messageText") { + continue; + } else if(type === "messagePoll") { + permission = "can_send_polls"; + } else if(mediaMessageTypes.indexOf(type) > -1) { + permission = "can_send_media_messages"; + } else if(otherMessageTypes.indexOf(type) > -1) { + permission = "can_send_other_messages"; + } + + if(permission !== "" && neededPermissions.indexOf(permission) === -1) { + neededPermissions.push(permission); + } + } + return neededPermissions; +} diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index b5d89d1..bd97589 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -266,6 +266,17 @@ Page { forwardMessagesTimer.messageIds = messageIds; forwardMessagesTimer.start(); } + function hasSendPrivilege(privilege) { + return chatPage.isPrivateChat || + chatGroupInformation && + ( + (chatGroupInformation.status["@type"] === "chatMemberStatusMember" && chatGroupInformation.status.permissions[privilege]) + || chatGroupInformation.status["@type"] === "chatMemberStatusAdministrator" + || chatGroupInformation.status["@type"] === "chatMemberStatusCreator" + || (chatGroupInformation.status["@type"] === "chatMemberStatusRestricted" && chatInformation.permissions[privilege]) + ) + } + Timer { id: forwardMessagesTimer interval: 200 @@ -822,7 +833,7 @@ Page { topPadding: Theme.paddingSmall width: parent.width - ( 2 * Theme.horizontalPageMargin ) anchors.horizontalCenter: parent.horizontalCenter - visible: !chatPage.isChannel + visible: chatPage.hasSendPrivilege("can_send_messages") height: visible ? implicitHeight : 0 Behavior on opacity { FadeAnimation {} } @@ -859,7 +870,7 @@ Page { layoutDirection: Qt.RightToLeft spacing: Theme.paddingMedium IconButton { - id: imageAttachmentButton + visible: chatPage.hasSendPrivilege("can_send_media_messages") icon.source: "image://theme/icon-m-image" onClicked: { var picker = pageStack.push("Sailfish.Pickers.ImagePickerPage"); @@ -874,7 +885,7 @@ Page { } } IconButton { - id: videoAttachmentButton + visible: chatPage.hasSendPrivilege("can_send_media_messages") icon.source: "image://theme/icon-m-video" onClicked: { var picker = pageStack.push("Sailfish.Pickers.VideoPickerPage"); @@ -889,7 +900,7 @@ Page { } } IconButton { - id: documentAttachmentButton + visible: chatPage.hasSendPrivilege("can_send_media_messages") icon.source: "image://theme/icon-m-document" onClicked: { var picker = pageStack.push("Sailfish.Pickers.DocumentPickerPage"); @@ -905,6 +916,8 @@ Page { } IconButton { + + visible: chatPage.hasSendPrivilege("can_send_other_messages") icon.source: "../../images/icon-m-sticker.svg" icon.sourceSize { width: Theme.iconSizeMedium @@ -917,10 +930,7 @@ Page { } } IconButton { - visible: !chatPage.isPrivateChat && chatGroupInformation && - (chatGroupInformation.status["@type"] === "chatMemberStatusCreator" - || chatGroupInformation.status["@type"] === "chatMemberStatusAdministrator" - || (chatGroupInformation.status["@type"] === "chatMemberStatusMember" && chatInformation.permissions.can_send_polls)) + visible: !chatPage.isPrivateChat && chatPage.hasSendPrivilege("can_send_polls") icon.source: "image://theme/icon-m-question" onClicked: { pageStack.push(Qt.resolvedUrl("../pages/PollCreationPage.qml"), { "chatId" : chatInformation.id, groupName: chatInformation.title}); @@ -1205,12 +1215,11 @@ Page { icon.source: "image://theme/icon-m-forward" onClicked: { var ids = Functions.getMessagesArrayIds(chatPage.selectedMessages); - + var neededPermissions = Functions.getMessagesNeededForwardPermissions(chatPage.selectedMessages); var chatId = chatInformation.id; - pageStack.push(Qt.resolvedUrl("../pages/ChatSelectionPage.qml"), { headerDescription: qsTr("Forward %n messages", "dialog header", ids.length).arg(ids.length), - payload: {fromChatId: chatId, messageIds:ids}, + payload: {fromChatId: chatId, messageIds:ids, neededPermissions: neededPermissions}, state: "forwardMessages" }); } diff --git a/qml/pages/ChatSelectionPage.qml b/qml/pages/ChatSelectionPage.qml index 4dec379..9609401 100644 --- a/qml/pages/ChatSelectionPage.qml +++ b/qml/pages/ChatSelectionPage.qml @@ -31,6 +31,10 @@ Dialog { acceptDestinationReplaceTarget: pageStack.find( function(page){ return(page._depth === 0)} ) property alias headerTitle: pageHeader.title property alias headerDescription: pageHeader.description + /* + payload dependent on chatSelectionPage.state + - forwardMessages: {fromChatId, messageIds, neededPermissions} + */ property var payload: ({}) onAccepted: { @@ -38,6 +42,7 @@ Dialog { case "forwardMessages": acceptDestinationInstance.forwardMessages(payload.fromChatId, payload.messageIds) break; + // future uses of chat selection can be processed here } } @@ -62,7 +67,52 @@ Dialog { model: chatListModel delegate: ChatListViewItem { ownUserId: overviewPage.ownUserId + Loader { // checking permissions takes a while, so we defer those calculations + id: visibleLoader + asynchronous: true + sourceComponent: Component { + QtObject { + property var chatGroupInformation: ({}) + property bool visible: false + Component.onCompleted: { + if(chatSelectionPage.state === "forwardMessages") { + var chatType = display.type['@type']; + if(chatType === "chatTypePrivate" || chatType === "chatTypeSecret") { + visible = true + return; + } + else if (chatType === "chatTypeBasicGroup" ) { + chatGroupInformation = tdLibWrapper.getBasicGroup(display.type.basic_group_id); + } + else if (chatType === "chatTypeSupergroup" ) { + chatGroupInformation = tdLibWrapper.getSuperGroup(display.type.supergroup_id); + } + visible = (chatGroupInformation.status["@type"] === "chatMemberStatusCreator" + || chatGroupInformation.status["@type"] === "chatMemberStatusAdministrator" + || (chatGroupInformation.status["@type"] === "chatMemberStatusMember" + && chatSelectionPage.payload.neededPermissions.every(function(neededPermission){ + return display.permissions[neededPermission]; + }) + ) + ); + } else { // future uses of chat selection can be processed here + visible = true; + } + } + } + } + } + + property bool valid: visibleLoader && visibleLoader.item && visibleLoader.item.visible + opacity: valid ? 1.0 : 0.5 + + Behavior on opacity { FadeAnimation {}} + Behavior on height { NumberAnimation {}} + + // normal height while calculating, otherwise all elements get displayed at once + height: !visibleLoader.item || visible ? contentHeight : 0 + enabled: valid onClicked: { var chat = tdLibWrapper.getChat(display.id); switch(chatSelectionPage.state) {