From f352be63dc5ea89f2e8bd0b9591e7b88c49e05df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 8 Nov 2021 22:20:41 -0500 Subject: [PATCH] refactor: deprecate app.openChat/newChat use chat.openChat/newChat instead --- public/src/app.js | 68 ++--------------------- public/src/client/account/header.js | 24 ++++----- public/src/client/chats.js | 25 ++++----- public/src/client/chats/search.js | 4 +- public/src/client/flags/detail.js | 8 ++- public/src/client/topic/postTools.js | 5 +- public/src/modules/chat.js | 80 +++++++++++++++++++++++++--- 7 files changed, 112 insertions(+), 102 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 3374f1b68c..222a2fa6e1 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -295,74 +295,16 @@ app.cacheBuster = null; }; app.openChat = function (roomId, uid) { - if (!app.user.uid) { - return app.alertError('[[error:not-logged-in]]'); - } - + console.warn('[deprecated] app.openChat is deprecated, please use chat.openChat'); require(['chat'], function (chat) { - function loadAndCenter(chatModal) { - chat.load(chatModal.attr('data-uuid')); - chat.center(chatModal); - chat.focusInput(chatModal); - } - - if (chat.modalExists(roomId)) { - loadAndCenter(chat.getModal(roomId)); - } else { - socket.emit('modules.chats.loadRoom', { roomId: roomId, uid: uid || app.user.uid }, function (err, roomData) { - if (err) { - return app.alertError(err.message); - } - roomData.users = roomData.users.filter(function (user) { - return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); - }); - roomData.uid = uid || app.user.uid; - roomData.isSelf = true; - chat.createModal(roomData, loadAndCenter); - }); - } + chat.openChat(roomId, uid); }); }; app.newChat = function (touid, callback) { - function createChat() { - socket.emit('modules.chats.newRoom', { touid: touid }, function (err, roomId) { - if (err) { - return app.alertError(err.message); - } - - if (!ajaxify.data.template.chats) { - app.openChat(roomId); - } else { - ajaxify.go('chats/' + roomId); - } - - callback(false, roomId); - }); - } - - callback = callback || function () { }; - if (!app.user.uid) { - return app.alertError('[[error:not-logged-in]]'); - } - - if (parseInt(touid, 10) === parseInt(app.user.uid, 10)) { - return app.alertError('[[error:cant-chat-with-yourself]]'); - } - socket.emit('modules.chats.isDnD', touid, function (err, isDnD) { - if (err) { - return app.alertError(err.message); - } - if (!isDnD) { - return createChat(); - } - require(['bootbox'], function (bootbox) { - bootbox.confirm('[[modules:chat.confirm-chat-with-dnd-user]]', function (ok) { - if (ok) { - createChat(); - } - }); - }); + console.warn('[deprecated] app.newChat is deprecated, please use chat.newChat'); + require(['chat'], function (chat) { + chat.newChat(touid, callback); }); }; diff --git a/public/src/client/account/header.js b/public/src/client/account/header.js index e46bded766..5bb46becaf 100644 --- a/public/src/client/account/header.js +++ b/public/src/client/account/header.js @@ -32,21 +32,19 @@ define('forum/account/header', [ toggleFollow('unfollow'); }); - components.get('account/chat').on('click', function () { - socket.emit('modules.chats.hasPrivateChat', ajaxify.data.uid, function (err, roomId) { - if (err) { - return app.alertError(err.message); - } - if (roomId) { - app.openChat(roomId); - } else { - app.newChat(ajaxify.data.uid); - } - }); + components.get('account/chat').on('click', async function () { + const roomId = await socket.emit('modules.chats.hasPrivateChat', ajaxify.data.uid); + const chat = await app.require('chat'); + if (roomId) { + chat.openChat(roomId); + } else { + chat.newChat(ajaxify.data.uid); + } }); - components.get('account/new-chat').on('click', function () { - app.newChat(ajaxify.data.uid, function () { + components.get('account/new-chat').on('click', async function () { + const chat = await app.require('chat'); + chat.newChat(ajaxify.data.uid, function () { components.get('account/chat').parent().removeClass('hidden'); }); }); diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 45b958e4f1..c3fc8ff560 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -8,14 +8,14 @@ define('forum/chats', [ 'forum/chats/recent', 'forum/chats/search', 'forum/chats/messages', - 'benchpress', 'composer/autocomplete', 'hooks', 'bootbox', + 'chat', ], function ( components, translator, mousetrap, - recentChats, search, messages, Benchpress, - autocomplete, hooks, bootbox + recentChats, search, messages, + autocomplete, hooks, bootbox, chatModule ) { const Chats = { initialised: false, @@ -93,11 +93,11 @@ define('forum/chats', [ if (app.previousUrl && app.previousUrl.match(/chats/)) { ajaxify.go('user/' + ajaxify.data.userslug + '/chats', function () { - app.openChat(roomId, ajaxify.data.uid); + chatModule.openChat(roomId, ajaxify.data.uid); }, true); } else { window.history.go(-1); - app.openChat(roomId, ajaxify.data.uid); + chatModule.openChat(roomId, ajaxify.data.uid); } $(window).one('action:chat.loaded', function () { @@ -294,9 +294,7 @@ define('forum/chats', [ // Return user to chats page. If modal, close modal. const modal = buttonEl.parents('.chat-modal'); if (modal.length) { - require(['chat'], function (chatLib) { - chatLib.close(modal); - }); + chatModule.close(modal); } else { ajaxify.go('chats'); } @@ -408,12 +406,11 @@ define('forum/chats', [ } else { el.remove(); } - require(['chat'], function (chat) { - const modal = chat.getModal(roomId); - if (modal.length) { - chat.close(modal); - } - }); + + const modal = chatModule.getModal(roomId); + if (modal.length) { + chatModule.close(modal); + } }); }; diff --git a/public/src/client/chats/search.js b/public/src/client/chats/search.js index 82bfcba8cc..fac37f908e 100644 --- a/public/src/client/chats/search.js +++ b/public/src/client/chats/search.js @@ -69,7 +69,9 @@ define('forum/chats/search', ['components', 'api'], function (components, api) { chats.switchChat(roomId); }); } else { - app.newChat(userObj.uid); + require(['chat'], function (chat) { + chat.newChat(userObj.uid); + }); } }); }); diff --git a/public/src/client/flags/detail.js b/public/src/client/flags/detail.js index 81b1592a73..aa8ef07c21 100644 --- a/public/src/client/flags/detail.js +++ b/public/src/client/flags/detail.js @@ -1,6 +1,8 @@ 'use strict'; -define('forum/flags/detail', ['forum/flags/list', 'components', 'translator', 'benchpress', 'forum/account/header', 'accounts/delete', 'api', 'bootbox'], function (FlagsList, components, translator, Benchpress, AccountHeader, AccountsDelete, api, bootbox) { +define('forum/flags/detail', [ + 'components', 'translator', 'benchpress', 'forum/account/header', 'accounts/delete', 'api', 'bootbox', +], function (components, translator, Benchpress, AccountHeader, AccountsDelete, api, bootbox) { const Detail = {}; Detail.init = function () { @@ -59,7 +61,9 @@ define('forum/flags/detail', ['forum/flags/list', 'components', 'translator', 'b break; } case 'chat': - app.newChat(uid); + require(['chat'], function (chat) { + chat.newChat(uid); + }); break; case 'ban': diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 8dab0b0874..36c752fa9f 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -420,8 +420,9 @@ define('forum/topic/postTools', [ function openChat(button) { const post = button.parents('[data-pid]'); - - app.newChat(post.attr('data-uid')); + require(['chat'], function (chat) { + chat.newChat(post.attr('data-uid')); + }); button.parents('.btn-group').find('.dropdown-toggle').click(); return false; } diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index 94e4764ec8..d57386040a 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -1,14 +1,80 @@ 'use strict'; define('chat', [ - 'components', - 'taskbar', - 'translator', - 'hooks', -], function (components, taskbar, translator, hooks) { + 'components', 'taskbar', 'translator', 'hooks', 'bootbox', +], function (components, taskbar, translator, hooks, bootbox) { const module = {}; let newMessage = false; + module.openChat = function (roomId, uid) { + if (!app.user.uid) { + return app.alertError('[[error:not-logged-in]]'); + } + + function loadAndCenter(chatModal) { + module.load(chatModal.attr('data-uuid')); + module.center(chatModal); + module.focusInput(chatModal); + } + + if (module.modalExists(roomId)) { + loadAndCenter(module.getModal(roomId)); + } else { + socket.emit('modules.chats.loadRoom', { roomId: roomId, uid: uid || app.user.uid }, function (err, roomData) { + if (err) { + return app.alertError(err.message); + } + roomData.users = roomData.users.filter(function (user) { + return user && parseInt(user.uid, 10) !== parseInt(app.user.uid, 10); + }); + roomData.uid = uid || app.user.uid; + roomData.isSelf = true; + module.createModal(roomData, loadAndCenter); + }); + } + }; + + module.newChat = function (touid, callback) { + function createChat() { + socket.emit('modules.chats.newRoom', { touid: touid }, function (err, roomId) { + if (err) { + return app.alertError(err.message); + } + + if (!ajaxify.data.template.chats) { + module.openChat(roomId); + } else { + ajaxify.go('chats/' + roomId); + } + + callback(null, roomId); + }); + } + + callback = callback || function () { }; + if (!app.user.uid) { + return app.alertError('[[error:not-logged-in]]'); + } + + if (parseInt(touid, 10) === parseInt(app.user.uid, 10)) { + return app.alertError('[[error:cant-chat-with-yourself]]'); + } + socket.emit('modules.chats.isDnD', touid, function (err, isDnD) { + if (err) { + return app.alertError(err.message); + } + if (!isDnD) { + return createChat(); + } + + bootbox.confirm('[[modules:chat.confirm-chat-with-dnd-user]]', function (ok) { + if (ok) { + createChat(); + } + }); + }); + }; + module.loadChatsDropdown = function (chatsListEl) { socket.emit('modules.chats.getRecentChats', { uid: app.user.uid, @@ -37,7 +103,7 @@ define('chat', [ } const roomId = $(this).attr('data-roomid'); if (!ajaxify.currentPage.match(/^chats\//)) { - app.openChat(roomId); + module.openChat(roomId); } else { ajaxify.go('user/' + app.user.userslug + '/chats/' + roomId); } @@ -206,7 +272,7 @@ define('chat', [ module.minimize(uuid); }); - chatModal.on('click', ':not(.close)', function () { + chatModal.on('mouseup', function () { taskbar.updateActive(chatModal.attr('data-uuid')); if (dragged) {