diff --git a/app/models/message.rb b/app/models/message.rb index 203e8adb..01cd4c34 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -7,7 +7,7 @@ class Message < ApplicationRecord after_create :after_created #after_create :after_created_async - + def user_image user.image.url @@ -21,7 +21,7 @@ class Message < ApplicationRecord def after_created ActionCable.server.broadcast 'map_' + resource.id.to_s, type: 'messageCreated', message: as_json end - + def after_created_async FollowService.follow(resource, user, 'commented') NotificationService.notify_followers(resource, 'map_message', self) diff --git a/frontend/src/Metamaps/GlobalUI/ReactApp.js b/frontend/src/Metamaps/GlobalUI/ReactApp.js index 2d176ebf..14c80f2b 100644 --- a/frontend/src/Metamaps/GlobalUI/ReactApp.js +++ b/frontend/src/Metamaps/GlobalUI/ReactApp.js @@ -157,6 +157,7 @@ const ReactApp = { getChatProps: function() { const self = ReactApp return { + unreadMessages: ChatView.unreadMessages, conversationLive: ChatView.conversationLive, isParticipating: ChatView.isParticipating, onOpen: ChatView.onOpen, diff --git a/frontend/src/Metamaps/Realtime/index.js b/frontend/src/Metamaps/Realtime/index.js index b73cf52c..9e15ef62 100644 --- a/frontend/src/Metamaps/Realtime/index.js +++ b/frontend/src/Metamaps/Realtime/index.js @@ -151,8 +151,6 @@ let Realtime = { config: { DOUBLE_CLICK_TOLERANCE: 200 } }) self.room.videoAdded(self.handleVideoAdded) - - self.startActiveMap() } // if Active.Mapper }, addJuntoListeners: function() { @@ -201,9 +199,6 @@ let Realtime = { self.leaveMap() $('.collabCompass').remove() if (self.room) self.room.leave() - ChatView.hide() - ChatView.close() - ChatView.reset() Cable.unsubscribeFromMap() }, turnOn: function(notify) { @@ -228,7 +223,6 @@ let Realtime = { ChatView.setNewMap() ChatView.addParticipant(self.activeMapper) ChatView.addMessages(new DataModel.MessageCollection(DataModel.Messages), true) - ChatView.show() }, setupLocalEvents: function() { var self = Realtime diff --git a/frontend/src/Metamaps/Views/ChatView.js b/frontend/src/Metamaps/Views/ChatView.js index f7c41a9d..0be3d9f2 100644 --- a/frontend/src/Metamaps/Views/ChatView.js +++ b/frontend/src/Metamaps/Views/ChatView.js @@ -14,10 +14,10 @@ import ReactApp from '../GlobalUI/ReactApp' const ChatView = { isOpen: false, + unreadMessages: 0, messages: new Backbone.Collection(), conversationLive: false, isParticipating: false, - mapChat: null, domId: 'chat-box-wrapper', init: function(urls) { const self = ChatView @@ -34,29 +34,32 @@ const ChatView = { }, setNewMap: function() { const self = ChatView + self.unreadMessages = 0 + self.isOpen = false self.conversationLive = false self.isParticipating = false self.alertSound = true // whether to play sounds on arrival of new messages or not self.cursorsShowing = true self.videosShowing = true self.participants = new Backbone.Collection() + self.messages = new Backbone.Collection() self.render() }, - show: () => { - $('#' + ChatView.domId).show() - }, - hide: () => { - $('#' + ChatView.domId).hide() - }, render: () => { if (!Active.Map) return const self = ChatView ReactApp.render() }, onOpen: () => { + const self = ChatView + self.isOpen = true + self.unreadMessages = 0 + self.render() $(document).trigger(ChatView.events.openTray) }, onClose: () => { + const self = ChatView + self.isOpen = false $(document).trigger(ChatView.events.closeTray) }, addParticipant: participant => { @@ -102,12 +105,6 @@ const ChatView = { ChatView.participants.forEach(p => p.set({isParticipating: false, isPending: false})) ChatView.render() }, - close: () => { - ChatView.mapChat && ChatView.mapChat.close() - }, - open: () => { - ChatView.mapChat && ChatView.mapChat.open() - }, videoToggleClick: function() { ChatView.videosShowing = !ChatView.videosShowing $(document).trigger(ChatView.videosShowing ? ChatView.events.videosOn : ChatView.events.videosOff) @@ -127,11 +124,10 @@ const ChatView = { }, addMessage: (message, isInitial, wasMe) => { const self = ChatView - if (!isInitial) self.mapChat.newMessage() + if (!isInitial && !self.isOpen) self.unreadMessages += 1 if (!wasMe && !isInitial && self.alertSound) self.sound.play('receivechat') self.messages.add(message) self.render() - if (!isInitial) self.mapChat.scroll() }, sendChatMessage: message => { var self = ChatView @@ -157,23 +153,9 @@ const ChatView = { // passed to this function addMessages: (messages, isInitial, wasMe) => { messages.models.forEach(m => ChatView.addMessage(m, isInitial, wasMe)) - }, - reset: () => { - ChatView.mapChat && ChatView.mapChat.reset() - ChatView.participants && ChatView.participants.reset() - ChatView.messages && ChatView.messages.reset() - ChatView.render() } } -// ChatView.prototype.scrollMessages = function(duration) { -// duration = duration || 0 - -// this.$messages.animate({ -// scrollTop: this.$messages[0].scrollHeight -// }, duration) -// } - /** * @class * @static diff --git a/frontend/src/Metamaps/Views/TopicCard.js b/frontend/src/Metamaps/Views/TopicCard.js index 0c7b3e3f..a352cb44 100644 --- a/frontend/src/Metamaps/Views/TopicCard.js +++ b/frontend/src/Metamaps/Views/TopicCard.js @@ -43,7 +43,6 @@ const TopicCard = { var self = TopicCard var topic = node.getData('topic') self.openTopic = topic - // populate the card that's about to show with the right topics data self.render() $('.showcard').fadeIn('fast', () => { $('.showcard').draggable({ diff --git a/frontend/src/components/MapView/MapChat/index.js b/frontend/src/components/MapView/MapChat/index.js index d1bd5d50..25eb8e41 100644 --- a/frontend/src/components/MapView/MapChat/index.js +++ b/frontend/src/components/MapView/MapChat/index.js @@ -26,7 +26,6 @@ class MapChat extends Component { super(props) this.state = { - unreadMessages: 0, messageText: '', alertSound: true, // whether to play sounds on arrival of new messages or not cursorsShowing: true, @@ -34,9 +33,14 @@ class MapChat extends Component { } } + componentDidUpdate(prevProps) { + const { messages } = this.props + const prevMessages = prevProps.messages + if (messages.length !== prevMessages.length) setTimeout(() => this.scroll(), 50) + } + reset = () => { this.setState({ - unreadMessages: 0, messageText: '', alertSound: true, // whether to play sounds on arrival of new messages or not cursorsShowing: true, @@ -46,22 +50,16 @@ class MapChat extends Component { close = () => { this.props.onClose() - this.messageInput.blur() } open = () => { - this.scroll() - this.setState({unreadMessages: 0}) this.props.onOpen() - this.messageInput.focus() - } - - newMessage = () => { - if (!this.state.open) this.setState({unreadMessages: this.state.unreadMessages + 1}) + setTimeout(() => this.scroll(), 50) } scroll = () => { - this.messagesDiv.scrollTop = this.messagesDiv.scrollHeight + // hack: figure out how to do this right + this.messagesDiv.scrollTop = this.messagesDiv.scrollHeight + 100 } toggleDrawer = () => { @@ -99,14 +97,10 @@ class MapChat extends Component { } } - focusMessageInput = () => { - if (!this.messageInput) return - this.messageInput.focus() - } - render = () => { - const { chatOpen, conversationLive, isParticipating, participants, messages, inviteACall, inviteToJoin } = this.props - const { videosShowing, cursorsShowing, alertSound, unreadMessages } = this.state + const { unreadMessages, chatOpen, conversationLive, + isParticipating, participants, messages, inviteACall, inviteToJoin } = this.props + const { videosShowing, cursorsShowing, alertSound } = this.state const rightOffset = chatOpen ? '0' : '-300px' return (