diff --git a/.eslintrc.js b/.eslintrc.js index 7ffba41..ccc278f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -34,6 +34,7 @@ module.exports = { 'ember/no-incorrect-calls-with-inline-anonymous-functions': 'off', 'ember/no-private-routing-service': 'off', 'no-useless-escape': 'off', + 'no-inner-declarations': 'off', 'n/no-unpublished-require': [ 'error', { diff --git a/addon/services/app-cache.js b/addon/services/app-cache.js index a616f3d..4aaab00 100644 --- a/addon/services/app-cache.js +++ b/addon/services/app-cache.js @@ -38,8 +38,12 @@ export default class AppCacheService extends Service { return this; } - @action get(key) { - return this.localCache.get(`${this.cachePrefix}${dasherize(key)}`); + @action get(key, defaultValue = null) { + const value = this.localCache.get(`${this.cachePrefix}${dasherize(key)}`); + if (value === undefined) { + return defaultValue; + } + return value; } @action has(key) { diff --git a/addon/services/chat.js b/addon/services/chat.js new file mode 100644 index 0000000..eaee386 --- /dev/null +++ b/addon/services/chat.js @@ -0,0 +1,263 @@ +import Service, { inject as service } from '@ember/service'; +import Evented from '@ember/object/evented'; +import { tracked } from '@glimmer/tracking'; +import { isArray } from '@ember/array'; +import { task } from 'ember-concurrency'; +import { all } from 'rsvp'; + +export default class ChatService extends Service.extend(Evented) { + @service store; + @service currentUser; + @service appCache; + @tracked channels = []; + @tracked openChannels = []; + + openChannel(chatChannelRecord) { + if (this.openChannels.includes(chatChannelRecord)) { + return; + } + this.openChannels.pushObject(chatChannelRecord); + this.rememberOpenedChannel(chatChannelRecord); + this.trigger('chat.opened', chatChannelRecord); + } + + closeChannel(chatChannelRecord) { + const index = this.openChannels.findIndex((_) => _.id === chatChannelRecord.id); + if (index >= 0) { + this.openChannels.removeAt(index); + this.trigger('chat.closed', chatChannelRecord); + } + this.forgetOpenedChannel(chatChannelRecord); + } + + rememberOpenedChannel(chatChannelRecord) { + let openedChats = this.appCache.get('open-chats', []); + if (isArray(openedChats) && !openedChats.includes(chatChannelRecord.id)) { + openedChats.pushObject(chatChannelRecord.id); + } else { + openedChats = [chatChannelRecord.id]; + } + this.appCache.set('open-chats', openedChats); + } + + forgetOpenedChannel(chatChannelRecord) { + let openedChats = this.appCache.get('open-chats', []); + if (isArray(openedChats)) { + openedChats.removeObject(chatChannelRecord.id); + } else { + openedChats = []; + } + this.appCache.set('open-chats', openedChats); + } + + restoreOpenedChats() { + const openedChats = this.appCache.get('open-chats', []); + if (isArray(openedChats)) { + const findAll = openedChats.map((id) => this.store.findRecord('chat-channel', id)); + return all(findAll).then((openedChatRecords) => { + if (isArray(openedChatRecords)) { + for (let i = 0; i < openedChatRecords.length; i++) { + const chatChannelRecord = openedChatRecords[i]; + this.openChannel(chatChannelRecord); + } + } + return openedChatRecords; + }); + } + + return []; + } + + getOpenChannels() { + return this.openChannels; + } + + createChatChannel(name) { + const chatChannelRecord = this.store.createRecord('chat-channel', { name }); + return chatChannelRecord.save().finally(() => { + this.trigger('chat.created', chatChannelRecord); + }); + } + + deleteChatChannel(chatChannelRecord) { + return chatChannelRecord.destroyRecord().finally(() => { + this.trigger('chat.deleted', chatChannelRecord); + }); + } + + updateChatChannel(chatChannelRecord, props = {}) { + chatChannelRecord.setProperties(props); + return chatChannelRecord.save().finally(() => { + this.trigger('chat.updated', chatChannelRecord); + }); + } + + addParticipant(chatChannelRecord, userRecord) { + const chatParticipant = this.store.createRecord('chat-participant', { + chat_channel_uuid: chatChannelRecord.id, + user_uuid: userRecord.id, + }); + return chatParticipant.save().finally(() => { + this.trigger('chat.added_participant', chatParticipant, chatChannelRecord); + }); + } + + removeParticipant(chatChannelRecord, chatParticipant) { + return chatParticipant.destroyRecord().finally(() => { + this.trigger('chat.removed_participant', chatParticipant, chatChannelRecord); + }); + } + + async sendMessage(chatChannelRecord, senderRecord, messageContent = '', attachments = []) { + const chatMessage = this.store.createRecord('chat-message', { + chat_channel_uuid: chatChannelRecord.id, + sender_uuid: senderRecord.id, + content: messageContent, + attachment_files: attachments, + }); + + return chatMessage + .save() + .then((chatMessageRecord) => { + if (chatChannelRecord.doesntExistsInFeed('message', chatMessageRecord)) { + chatChannelRecord.feed.pushObject({ + type: 'message', + created_at: chatMessageRecord.created_at, + data: chatMessageRecord.serialize(), + record: chatMessageRecord, + }); + } + return chatMessageRecord; + }) + .finally(() => { + this.trigger('chat.feed_updated', chatMessage, chatChannelRecord); + this.trigger('chat.message_created', chatMessage, chatChannelRecord); + }); + } + + deleteMessage(chatMessageRecord) { + return chatMessageRecord.destroyRecord().finally(() => { + this.trigger('chat.feed_updated', chatMessageRecord); + this.trigger('chat.message_deleted', chatMessageRecord); + }); + } + + insertChatMessageFromSocket(chatChannelRecord, data) { + // normalize and create record + const normalized = this.store.normalize('chat-message', data); + const record = this.store.push(normalized); + + // make sure it doesn't exist in feed already + if (chatChannelRecord.existsInFeed('message', record)) { + return; + } + + // create feed item + const item = { + type: 'message', + created_at: record.created_at, + data, + record, + }; + + // add item to feed + chatChannelRecord.feed.pushObject(item); + + // trigger event + this.trigger('chat.feed_updated', record, chatChannelRecord); + this.trigger('chat.message_created', record, chatChannelRecord); + } + + insertChatLogFromSocket(chatChannelRecord, data) { + // normalize and create record + const normalized = this.store.normalize('chat-log', data); + const record = this.store.push(normalized); + + // make sure it doesn't exist in feed already + if (chatChannelRecord.existsInFeed('log', record)) { + return; + } + + // create feed item + const item = { + type: 'log', + created_at: record.created_at, + data, + record, + }; + + // add item to feed + chatChannelRecord.feed.pushObject(item); + + // trigger event + this.trigger('chat.feed_updated', record, chatChannelRecord); + this.trigger('chat.log_created', record, chatChannelRecord); + } + + insertChatAttachmentFromSocket(chatChannelRecord, data) { + // normalize and create record + const normalized = this.store.normalize('chat-attachment', data); + const record = this.store.push(normalized); + + // Find the chat message the record belongs to in the feed + const chatMessage = chatChannelRecord.feed.find((item) => { + return item.type === 'message' && item.record.id === record.chat_message_uuid; + }); + + // If we have the chat message then we can insert it to attachments + // This should work because chat message will always be created before the chat attachment + if (chatMessage) { + // Make sure the attachment isn't already attached to the message + const isNotAttached = chatMessage.record.attachments.find((attachment) => attachment.id === record.id) === undefined; + if (isNotAttached) { + chatMessage.record.attachments.pushObject(record); + // trigger event + this.trigger('chat.feed_updated', record, chatChannelRecord); + this.trigger('chat.attachment_created', record, chatChannelRecord); + } + } + } + + insertChatReceiptFromSocket(chatChannelRecord, data) { + // normalize and create record + const normalized = this.store.normalize('chat-receipt', data); + const record = this.store.push(normalized); + + // Find the chat message the record belongs to in the feed + const chatMessage = chatChannelRecord.feed.find((item) => { + return item.type === 'message' && item.record.id === record.chat_message_uuid; + }); + + // If we have the chat message then we can insert it to receipts + // This should work because chat message will always be created before the chat receipt + if (chatMessage) { + // Make sure the receipt isn't already attached to the message + const isNotAttached = chatMessage.record.receipts.find((receipt) => receipt.id === record.id) === undefined; + if (isNotAttached) { + chatMessage.record.receipts.pushObject(record); + // trigger event + this.trigger('chat.receipt_created', record, chatChannelRecord); + } + } + } + + @task *loadMessages(chatChannelRecord) { + const messages = yield this.store.query('chat-message', { chat_channel_uuid: chatChannelRecord.id }); + chatChannelRecord.set('messages', messages); + return messages; + } + + @task *loadChannels(options = {}) { + const params = options.params || {}; + const channels = yield this.store.query('chat-channel', params); + if (isArray(channels)) { + this.channels = channels; + } + + if (typeof options.withChannels === 'function') { + options.withChannels(channels); + } + + return channels; + } +} diff --git a/addon/services/socket.js b/addon/services/socket.js index 0feed69..0e700b7 100644 --- a/addon/services/socket.js +++ b/addon/services/socket.js @@ -1,6 +1,7 @@ import Service from '@ember/service'; import { tracked } from '@glimmer/tracking'; import { isBlank } from '@ember/utils'; +import { later } from '@ember/runloop'; import toBoolean from '../utils/to-boolean'; import config from 'ember-get-config'; @@ -29,22 +30,28 @@ export default class SocketService extends Service { } async listen(channelId, callback) { - const channel = this.socket.subscribe(channelId); - - // Track channel - this.channels.pushObject(channel); - - // Listen to channel for events - await channel.listener('subscribe').once(); - - // Listen for channel subscription - (async () => { - for await (let output of channel) { - if (typeof callback === 'function') { - callback(output); - } - } - })(); + later( + this, + async () => { + const channel = this.socket.subscribe(channelId); + + // Track channel + this.channels.pushObject(channel); + + // Listen to channel for events + await channel.listener('subscribe').once(); + + // Listen for channel subscription + (async () => { + for await (let output of channel) { + if (typeof callback === 'function') { + callback(output); + } + } + })(); + }, + 300 + ); } closeChannels() { diff --git a/addon/services/universe.js b/addon/services/universe.js index c2f17b0..917b4d7 100644 --- a/addon/services/universe.js +++ b/addon/services/universe.js @@ -993,7 +993,10 @@ export default class UniverseService extends Service.extend(Evented) { const index = this._getOption(options, 'index', 0); const onClick = this._getOption(options, 'onClick', null); const section = this._getOption(options, 'section', null); + const iconComponent = this._getOption(options, 'iconComponent', null); + const iconComponentOptions = this._getOption(options, 'iconComponentOptions', {}); const iconSize = this._getOption(options, 'iconSize', null); + const iconPrefix = this._getOption(options, 'iconPrefix', null); const iconClass = this._getOption(options, 'iconClass', null); const itemClass = this._getOption(options, 'class', null); const inlineClass = this._getOption(options, 'inlineClass', null); @@ -1024,7 +1027,10 @@ export default class UniverseService extends Service.extend(Evented) { index, section, onClick, + iconComponent, + iconComponentOptions, iconSize, + iconPrefix, iconClass, class: itemClass, inlineClass, diff --git a/addon/utils/download.js b/addon/utils/download.js index 4f3fc61..bd6a781 100644 --- a/addon/utils/download.js +++ b/addon/utils/download.js @@ -91,10 +91,11 @@ export default function download(data, strFileName, strMimeType) { anchor.className = 'download-js-link'; anchor.innerHTML = 'downloading...'; anchor.style.display = 'none'; - anchor.addEventListener('click', function (e) { + function handleClick(e) { e.stopPropagation(); - this.removeEventListener('click', arguments.callee); - }); + anchor.removeEventListener('click', handleClick); + } + anchor.addEventListener('click', handleClick); document.body.appendChild(anchor); later( this, diff --git a/app/services/chat.js b/app/services/chat.js new file mode 100644 index 0000000..19d51c4 --- /dev/null +++ b/app/services/chat.js @@ -0,0 +1 @@ +export { default } from '@fleetbase/ember-core/services/chat'; diff --git a/package.json b/package.json index b43a24a..2b68191 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/ember-core", - "version": "0.2.8", + "version": "0.2.9", "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.", "keywords": [ "fleetbase-core", diff --git a/tests/unit/services/chat-test.js b/tests/unit/services/chat-test.js new file mode 100644 index 0000000..defa706 --- /dev/null +++ b/tests/unit/services/chat-test.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'dummy/tests/helpers'; + +module('Unit | Service | chat', function (hooks) { + setupTest(hooks); + + // TODO: Replace this with your real tests. + test('it exists', function (assert) { + let service = this.owner.lookup('service:chat'); + assert.ok(service); + }); +});