diff --git a/extensions/pusher/js/package.json b/extensions/pusher/js/package.json index 47acfbe23..d22f39bc9 100644 --- a/extensions/pusher/js/package.json +++ b/extensions/pusher/js/package.json @@ -15,12 +15,13 @@ }, "devDependencies": { "@flarum/prettier-config": "^1.0.0", + "@types/pusher-js": "^5.1.0", "flarum-tsconfig": "^1.0.2", - "prettier": "^2.5.1", "flarum-webpack-config": "^2.0.0", - "webpack": "^5.65.0", - "webpack-cli": "^4.9.1", + "prettier": "^2.5.1", "typescript": "^4.5.4", - "typescript-coverage-report": "^0.6.1" + "typescript-coverage-report": "^0.6.1", + "webpack": "^5.65.0", + "webpack-cli": "^4.9.1" } } diff --git a/extensions/pusher/js/src/forum/index.ts b/extensions/pusher/js/src/forum/index.ts index a36282752..59bab5680 100644 --- a/extensions/pusher/js/src/forum/index.ts +++ b/extensions/pusher/js/src/forum/index.ts @@ -6,12 +6,22 @@ import DiscussionPage from 'flarum/forum/components/DiscussionPage'; import IndexPage from 'flarum/forum/components/IndexPage'; import Button from 'flarum/common/components/Button'; import ItemList from 'flarum/common/utils/ItemList'; -import { VnodeDOM } from 'Mithril'; +import { Children, VnodeDOM } from 'mithril'; + +export type PusherBinding = { + channels: { + main: PusherTypes.Channel; + user: PusherTypes.Channel | null; + }; + pusher: PusherTypes.default; +}; app.initializers.add('flarum-pusher', () => { app.pusher = (async () => { + // @ts-expect-error await import('//cdn.jsdelivr.net/npm/pusher-js@7.0.3/dist/web/pusher.min.js' /* webpackIgnore: true, webpackPrefetch: true */); + // @ts-expect-error Imported dynamically const socket: PusherTypes.default = new Pusher(app.forum.attribute('pusherKey'), { authEndpoint: `${app.forum.attribute('apiUrl')}/pusher/auth`, cluster: app.forum.attribute('pusherCluster'), @@ -34,7 +44,7 @@ app.initializers.add('flarum-pusher', () => { app.pushedUpdates = []; extend(DiscussionList.prototype, 'oncreate', function () { - app.pusher.then((binding) => { + app.pusher.then((binding: PusherBinding) => { const pusher = binding.pusher; pusher.bind('newPost', (data: { tagIds: number[]; discussionId: number }) => { @@ -44,7 +54,7 @@ app.initializers.add('flarum-pusher', () => { if (params.tags) { const tag = app.store.getBy('tags', 'slug', params.tags); - if (data.tagIds.indexOf(tag.id()) === -1) return; + if (!data.tagIds.includes(tag.id())) return; } const id = String(data.discussionId); @@ -64,12 +74,12 @@ app.initializers.add('flarum-pusher', () => { }); extend(DiscussionList.prototype, 'onremove', function () { - app.pusher.then((binding) => { + app.pusher.then((binding: PusherBinding) => { binding.pusher.unbind('newPost'); }); }); - extend(DiscussionList.prototype, 'view', function (vdom: VnodeDOM) { + extend(DiscussionList.prototype, 'view', function (this: DiscussionList, vdom: VnodeDOM) { if (app.pushedUpdates) { const count = app.pushedUpdates.length; @@ -96,21 +106,22 @@ app.initializers.add('flarum-pusher', () => { } }); - extend(DiscussionPage.prototype, 'oncreate', function () { - app.pusher.then((binding) => { + extend(DiscussionPage.prototype, 'oncreate', function (this: DiscussionPage) { + app.pusher.then((binding: PusherBinding) => { const pusher = binding.pusher; pusher.bind('newPost', (data: { discussionId: number }) => { const id = String(data.discussionId); + const discussionId = this.discussion?.id(); - if (this.discussion && this.discussion.id() === id && this.stream) { - const oldCount = this.discussion.commentCount(); + if (this.discussion && discussionId === id && this.stream) { + const oldCount = this.discussion.commentCount() ?? 0; - app.store.find('discussions', this.discussion.id()).then(() => { - this.stream.update().then(m.redraw); + app.store.find('discussions', discussionId).then(() => { + this.stream?.update().then(m.redraw); if (!document.hasFocus()) { - app.setTitleCount(Math.max(0, this.discussion.commentCount() - oldCount)); + app.setTitleCount(Math.max(0, (this.discussion?.commentCount() ?? 0) - oldCount)); window.addEventListener('focus', () => app.setTitleCount(0), { once: true }); } @@ -121,24 +132,26 @@ app.initializers.add('flarum-pusher', () => { }); extend(DiscussionPage.prototype, 'onremove', function () { - app.pusher.then((binding) => { + app.pusher.then((binding: PusherBinding) => { binding.pusher.unbind('newPost'); }); }); - extend(IndexPage.prototype, 'actionItems', (items: ItemList) => { + extend(IndexPage.prototype, 'actionItems', (items: ItemList) => { items.remove('refresh'); }); - app.pusher.then((binding) => { + app.pusher.then((binding: PusherBinding) => { const channels = binding.channels; if (channels.user) { channels.user.bind('notification', () => { - app.session.user.pushAttributes({ - unreadNotificationCount: app.session.user.unreadNotificationCount() + 1, - newNotificationCount: app.session.user.newNotificationCount() + 1, - }); + if (app.session.user) { + app.session.user.pushAttributes({ + unreadNotificationCount: app.session.user.unreadNotificationCount() ?? 0 + 1, + newNotificationCount: app.session.user.newNotificationCount() ?? 0 + 1, + }); + } app.notifications.clear(); m.redraw(); }); diff --git a/extensions/pusher/js/yarn.lock b/extensions/pusher/js/yarn.lock index 82871add8..542f86476 100644 --- a/extensions/pusher/js/yarn.lock +++ b/extensions/pusher/js/yarn.lock @@ -1096,6 +1096,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.10.tgz#616f16e9d3a2a3d618136b1be244315d95bd7cab" integrity sha512-S/3xB4KzyFxYGCppyDt68yzBU9ysL88lSdIah4D6cptdcltc4NCPCAMc0+PCpg/lLIyC7IPvj2Z52OJWeIUkog== +"@types/pusher-js@^5.1.0": + version "5.1.0" + resolved "https://registry.yarnpkg.com/@types/pusher-js/-/pusher-js-5.1.0.tgz#73d062489dd4e6981c2f2366da1bc2d4634783ba" + integrity sha512-VubdW1NvHAojLFtJ+FWmZsIlkSE+EiaLrrfCKcW/t9UDlXWF85rSPVc+5zdpZLVzkwaUaFeyIYSgeNfXc9Dxlg== + dependencies: + pusher-js "*" + "@types/sizzle@*": version "2.3.3" resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.3.tgz#ff5e2f1902969d305225a047c8a0fd5c915cebef" @@ -2269,6 +2276,13 @@ punycode@^2.1.0: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== +pusher-js@*: + version "7.0.4" + resolved "https://registry.yarnpkg.com/pusher-js/-/pusher-js-7.0.4.tgz#44708ce2c24cc0695f9e3504e967bfbdd540e0b0" + integrity sha512-P/1ZVNmiBC48BZdo6eB403DyrWnKMxgpAm91XaHay3igxGf4kZHjVgxM3WqkZ1dS+ooGCM1rWjStizQvCvtQrw== + dependencies: + tweetnacl "^1.0.3" + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -2658,6 +2672,11 @@ tsutils@3: dependencies: tslib "^1.8.1" +tweetnacl@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596" + integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw== + type-coverage-core@^2.17.2: version "2.19.1" resolved "https://registry.yarnpkg.com/type-coverage-core/-/type-coverage-core-2.19.1.tgz#5d6298877c391dae777ef97076472d0bc8d4301e"