From ea02ce2316053b31ad5ee7056d1f508796113645 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 18 Jul 2019 04:17:35 -0400 Subject: [PATCH] =?UTF-8?q?ES5=20=E2=86=92=207:=20Use=20enhanced=20object?= =?UTF-8?q?=20properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit http://es6-features.org/#PropertyShorthand Instead of doing {"foo": foo, "bar": bar, ...}, we can just do {foo, bar} now. The function parameters of EventHandlers have all been renamed to camelCase to make use of this, as the JS style conventions intend. Other functions will follow in a later commit. --- TODO.md | 2 +- src/python/events/users.py | 4 +- src/qml/Base/HRoomAvatar.qml | 3 +- src/qml/Chat/Banners/InviteBanner.qml | 8 +- src/qml/Chat/Banners/LeftBanner.qml | 6 +- src/qml/Chat/Banners/UnknownDevicesBanner.qml | 6 +- src/qml/EventHandlers/app.js | 4 +- src/qml/EventHandlers/rooms.js | 88 ++++++------------- src/qml/EventHandlers/users.js | 23 ++--- src/qml/Models/Users.qml | 16 ++-- src/qml/Pages/RememberAccount.qml | 8 +- src/qml/Pages/SignIn.qml | 15 ++-- src/qml/UI.qml | 6 +- 13 files changed, 70 insertions(+), 119 deletions(-) diff --git a/TODO.md b/TODO.md index 00219330..ed872f54 100644 --- a/TODO.md +++ b/TODO.md @@ -10,7 +10,6 @@ - `QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling)` ? - Qt 5.12 - - See about ECMAScript 6 and 7 features - .mjs modules - Refactoring @@ -18,6 +17,7 @@ - Unfinished work in button-refactor branch - Button can get "hoverEnabled: false" to let HoverHandlers work - Room Sidepane + - When qml syntax highlighting supports string interpolation, use them - Bug fixes - Past events loading (limit 100) freezes the GUI - need to move upsert func diff --git a/src/python/events/users.py b/src/python/events/users.py index 5596946e..8ddd1d1f 100644 --- a/src/python/events/users.py +++ b/src/python/events/users.py @@ -36,8 +36,8 @@ class UserUpdated(Event): def from_nio(cls, user: MatrixUser) -> "UserUpdated": return cls( user_id = user.user_id, - display_name = user.display_name, - avatar_url = user.avatar_url, + display_name = user.display_name or "", + avatar_url = user.avatar_url or "", ) diff --git a/src/qml/Base/HRoomAvatar.qml b/src/qml/Base/HRoomAvatar.qml index 67539d79..e040f2dc 100644 --- a/src/qml/Base/HRoomAvatar.qml +++ b/src/qml/Base/HRoomAvatar.qml @@ -7,8 +7,7 @@ HAvatar { property string userId: "" property string roomId: "" - readonly property var roomInfo: - rooms.getWhere({"userId": userId, "roomId": roomId}, 1)[0] + readonly property var roomInfo: rooms.getWhere({userId, roomId}, 1)[0] // roomInfo ? → Avoid error messages when a room is forgotten readonly property var dname: roomInfo ? roomInfo.displayName : "" diff --git a/src/qml/Chat/Banners/InviteBanner.qml b/src/qml/Chat/Banners/InviteBanner.qml index 4a8b7c34..36c93945 100644 --- a/src/qml/Chat/Banners/InviteBanner.qml +++ b/src/qml/Chat/Banners/InviteBanner.qml @@ -34,8 +34,8 @@ Banner { } ] - buttonCallbacks: { - "accept": button => { + buttonCallbacks: ({ + accept: button => { button.loading = true py.callClientCoro( chatPage.userId, "join", [chatPage.roomId], () => { @@ -43,12 +43,12 @@ Banner { }) }, - "decline": button => { + decline: button => { button.loading = true py.callClientCoro( chatPage.userId, "room_leave", [chatPage.roomId], () => { button.loading = false }) } - } + }) } diff --git a/src/qml/Chat/Banners/LeftBanner.qml b/src/qml/Chat/Banners/LeftBanner.qml index 00e0ab16..4391bde1 100644 --- a/src/qml/Chat/Banners/LeftBanner.qml +++ b/src/qml/Chat/Banners/LeftBanner.qml @@ -22,13 +22,13 @@ Banner { } ] - buttonCallbacks: { - "forget": button => { + buttonCallbacks: ({ + forget: button => { button.loading = true py.callClientCoro( chatPage.userId, "room_forget", [chatPage.roomId], () => { button.loading = false }) } - } + }) } diff --git a/src/qml/Chat/Banners/UnknownDevicesBanner.qml b/src/qml/Chat/Banners/UnknownDevicesBanner.qml index aee56546..b51c51b4 100644 --- a/src/qml/Chat/Banners/UnknownDevicesBanner.qml +++ b/src/qml/Chat/Banners/UnknownDevicesBanner.qml @@ -20,9 +20,9 @@ Banner { } ] - buttonCallbacks: { - "inspect": button => { + buttonCallbacks: ({ + inspect: button => { print("show") } - } + }) } diff --git a/src/qml/EventHandlers/app.js b/src/qml/EventHandlers/app.js index fd4fc781..d026ada8 100644 --- a/src/qml/EventHandlers/app.js +++ b/src/qml/EventHandlers/app.js @@ -1,8 +1,8 @@ // Copyright 2019 miruka // This file is part of harmonyqml, licensed under LGPLv3. -function onExitRequested(exit_code) { - Qt.exit(exit_code) +function onExitRequested(exitCode) { + Qt.exit(exitCode) } function onCoroutineDone(uuid, result) { diff --git a/src/qml/EventHandlers/rooms.js b/src/qml/EventHandlers/rooms.js index eb8b0aa5..241610a8 100644 --- a/src/qml/EventHandlers/rooms.js +++ b/src/qml/EventHandlers/rooms.js @@ -3,13 +3,12 @@ Qt.include("../utils.js") - -function typingTextFor(members, our_user_id) { +function typingTextFor(members, ourUserId) { var profiles = [] var names = [] for (var i = 0; i < members.length; i++) { - if (members[i] != our_user_id) { + if (members[i] != ourUserId) { profiles.push(users.find(members[i])) } } @@ -37,18 +36,13 @@ function typingTextFor(members, our_user_id) { function onRoomUpdated( - user_id, category, room_id, display_name, avatar_url, topic, - members, typing_members, inviter_id + userId, category, roomId, displayName, avatarUrl, topic, + members, typingMembers, inviterId ) { - roomCategories.upsert({"userId": user_id, "name": category}, { - "userId": user_id, - "name": category - }) + roomCategories.upsert({userId, name: category}, {userId, name: category}) - function find(for_category) { - var found = rooms.getIndices( - {"userId": user_id, "roomId": room_id, "category": for_category}, 1 - ) + function find(category) { + var found = rooms.getIndices({userId, roomId, category}, 1) return found.length > 0 ? found[0] : null } @@ -58,71 +52,45 @@ function onRoomUpdated( else if (category == "Left") { replace = find("Invites") || find("Rooms")} var item = { - "userId": user_id, - "category": category, - "roomId": room_id, - "displayName": display_name, - "avatarUrl": avatar_url, - "topic": topic, - "members": members, - "typingText": typingTextFor(typing_members, user_id), - "inviterId": inviter_id + typingText: typingTextFor(typingMembers, userId), + + userId, category, roomId, displayName, avatarUrl, topic, members, + inviterId } if (replace === null) { - rooms.upsert( - {"userId": user_id, "roomId": room_id, "category": category}, - item - ) + rooms.upsert({userId, roomId, category}, item) } else { rooms.set(replace, item) } } -function onRoomForgotten(user_id, room_id) { - rooms.popWhere({"userId": user_id, "roomId": room_id}) -} - - -function onRoomMemberUpdated(room_id, user_id, typing) { -} - - -function onRoomMemberDeleted(room_id, user_id) { +function onRoomForgotten(userId, roomId) { + rooms.popWhere({userId, roomId}) } function onTimelineEventReceived( - event_type, room_id, event_id, sender_id, date, content, - content_type, is_local_echo, show_name_line, translatable, target_user_id + eventType, roomId, eventId, senderId, date, content, + contentType, isLocalEcho, showNameLine, translatable, targetUserId ) { var item = { - "eventType": py.getattr(event_type, "__name__"), - "roomId": room_id, - "eventId": event_id, - "senderId": sender_id, - "date": date, - "content": content, - "contentType": content_type, - "isLocalEcho": is_local_echo, - "showNameLine": show_name_line, - "translatable": translatable, - "targetUserId": target_user_id, + eventType: py.getattr(eventType, "__name__"), + + roomId, eventId, senderId, date, content, contentType, isLocalEcho, + showNameLine, translatable, targetUserId } - if (is_local_echo) { + if (isLocalEcho) { timelines.append(item) return } // Replace first matching local echo - var found = timelines.getIndices({ - "roomId": room_id, - "senderId": sender_id, - "content": content, - "isLocalEcho": true - }, 1, 250) + var found = timelines.getIndices( + {roomId, senderId, content, "isLocalEcho": true}, 1, 250 + ) if (found.length > 0) { timelines.set(found[0], item) @@ -131,16 +99,12 @@ function onTimelineEventReceived( else if (item.eventType == "OlmEvent" || item.eventType == "MegolmEvent") { // Don't replace if an item with the same eventId is found in these // cases, because it would be the ecrypted version of the event. - timelines.upsert({"eventId": event_id}, item, false, 250) + timelines.upsert({eventId}, item, false, 250) } else { - timelines.upsert({"eventId": event_id}, item, true, 250) + timelines.upsert({eventId}, item, true, 250) } } var onTimelineMessageReceived = onTimelineEventReceived - - -function onTypingNoticeEvent(room_id, members) { -} diff --git a/src/qml/EventHandlers/users.js b/src/qml/EventHandlers/users.js index 1f2ac105..c166812a 100644 --- a/src/qml/EventHandlers/users.js +++ b/src/qml/EventHandlers/users.js @@ -1,26 +1,21 @@ // Copyright 2019 miruka // This file is part of harmonyqml, licensed under LGPLv3. -function onAccountUpdated(user_id) { - accounts.append({"userId": user_id}) +function onAccountUpdated(userId) { + accounts.append({userId}) } -function onAccountDeleted(user_id) { - accounts.popWhere({"userId": user_id}, 1) +function onAccountDeleted(userId) { + accounts.popWhere({userId}, 1) } -function onUserUpdated(user_id, display_name, avatar_url, status_message) { - users.upsert({"userId": user_id}, { - "userId": user_id, - "displayName": display_name, - "avatarUrl": avatar_url, - "statusMessage": status_message - }) +function onUserUpdated(userId, displayName, avatarUrl, statusMessage) { + users.upsert({userId}, {userId, displayName, avatarUrl, statusMessage}) } -function onDeviceUpdated(user_id, device_id, ed25519_key, trust, display_name, - last_seen_ip, last_seen_date) { +function onDeviceUpdated(userId, deviceId, ed25519Key, trust, displayName, + lastSeenIp, lastSeenDate) { } -function onDeviceDeleted(user_id, device_id) { +function onDeviceDeleted(userId, deviceId) { } diff --git a/src/qml/Models/Users.qml b/src/qml/Models/Users.qml index 49d718ab..db7ed8e5 100644 --- a/src/qml/Models/Users.qml +++ b/src/qml/Models/Users.qml @@ -6,21 +6,21 @@ import SortFilterProxyModel 0.2 import "../Base" HListModel { - function find(user_id) { + function find(userId) { // Happens when SortFilterProxyModel ExpressionFilter/Sorter/Role tests // the expression with invalid data to establish property bindings - if (! user_id) { return } + if (! userId) { return } - var found = getWhere({"userId": user_id}, 1) + var found = getWhere({userId}, 1) if (found.length > 0) { return found[0] } - py.callCoro("request_user_update_event", [user_id]) + py.callCoro("request_user_update_event", [userId]) return { - "userId": user_id, - "displayName": "", - "avatarUrl": "", - "statusMessage": "" + userId, + displayName: "", + avatarUrl: "", + statusMessage: "", } } } diff --git a/src/qml/Pages/RememberAccount.qml b/src/qml/Pages/RememberAccount.qml index 4a7c596a..fa6b3eb8 100644 --- a/src/qml/Pages/RememberAccount.qml +++ b/src/qml/Pages/RememberAccount.qml @@ -21,16 +21,16 @@ Item { { name: "no", text: qsTr("No") }, ] - buttonCallbacks: { - "yes": button => { + buttonCallbacks: ({ + yes: button => { py.callCoro("save_account", [userId]) pageStack.showPage("Default") }, - "no": button => { + no: button => { py.callCoro("forget_account", [userId]) pageStack.showPage("Default") }, - } + }) HLabel { text: qsTr( diff --git a/src/qml/Pages/SignIn.qml b/src/qml/Pages/SignIn.qml index fc2f80ee..0c8ba72c 100644 --- a/src/qml/Pages/SignIn.qml +++ b/src/qml/Pages/SignIn.qml @@ -22,24 +22,21 @@ Item { { name: "forgot", text: qsTr("Forgot?") } ] - buttonCallbacks: { - "register": button => {}, + buttonCallbacks: ({ + register: button => {}, - "login": button => { + login: button => { button.loading = true var args = [idField.text, passwordField.text] py.callCoro("login_client", args, user_id => { - pageStack.showPage( - "RememberAccount", - {"loginWith": loginWith, "userId": user_id} - ) + pageStack.showPage("RememberAccount", {loginWith, user_id}) button.loading = false }) }, - "forgot": button => {} - } + forgot: button => {} + }) HRowLayout { spacing: signInBox.margins * 1.25 diff --git a/src/qml/UI.qml b/src/qml/UI.qml index 99bdcc6f..42d41881 100644 --- a/src/qml/UI.qml +++ b/src/qml/UI.qml @@ -64,11 +64,7 @@ Item { } function showRoom(userId, category, roomId) { - var info = rooms.getWhere({ - "userId": userId, - "roomId": roomId, - "category": category - }, 1)[0] + var info = rooms.getWhere({userId, roomId, category}, 1)[0] pageStack.replace("Chat/Chat.qml", {"roomInfo": info}) }