2020-05-07 13:10:53 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { WSConnectionStatus } from '../../services/api/api.service.js'
|
|
|
|
import { mapGetters, mapState } from 'vuex'
|
|
|
|
import ChatMessage from '../chat_message/chat_message.vue'
|
|
|
|
import PostStatusForm from '../post_status_form/post_status_form.vue'
|
|
|
|
import ChatTitle from '../chat_title/chat_title.vue'
|
|
|
|
import chatService from '../../services/chat_service/chat_service.js'
|
2020-09-04 08:19:53 +00:00
|
|
|
import { promiseInterval } from '../../services/promise_interval/promise_interval.js'
|
2022-04-05 14:11:50 +00:00
|
|
|
import { getScrollPosition, getNewTopPosition, isBottomedOut, isScrollable } from './chat_layout_utils.js'
|
2020-10-20 18:03:46 +00:00
|
|
|
import { library } from '@fortawesome/fontawesome-svg-core'
|
|
|
|
import {
|
2020-10-20 19:13:19 +00:00
|
|
|
faChevronDown,
|
|
|
|
faChevronLeft
|
2020-10-20 18:03:46 +00:00
|
|
|
} from '@fortawesome/free-solid-svg-icons'
|
2020-10-29 10:33:06 +00:00
|
|
|
import { buildFakeMessage } from '../../services/chat_utils/chat_utils.js'
|
2020-10-20 18:03:46 +00:00
|
|
|
|
|
|
|
library.add(
|
2020-10-20 19:13:19 +00:00
|
|
|
faChevronDown,
|
|
|
|
faChevronLeft
|
2020-10-20 18:03:46 +00:00
|
|
|
)
|
2020-05-07 13:10:53 +00:00
|
|
|
|
2022-04-05 10:19:12 +00:00
|
|
|
const scroller = () => document.getElementById('content')
|
|
|
|
|
2020-05-07 13:10:53 +00:00
|
|
|
const BOTTOMED_OUT_OFFSET = 10
|
2022-04-04 16:41:09 +00:00
|
|
|
const JUMP_TO_BOTTOM_BUTTON_VISIBILITY_OFFSET = 10
|
2020-06-21 14:13:29 +00:00
|
|
|
const SAFE_RESIZE_TIME_OFFSET = 100
|
2020-10-27 08:03:04 +00:00
|
|
|
const MARK_AS_READ_DELAY = 1500
|
2020-10-29 10:33:06 +00:00
|
|
|
const MAX_RETRIES = 10
|
2020-05-07 13:10:53 +00:00
|
|
|
|
|
|
|
const Chat = {
|
|
|
|
components: {
|
|
|
|
ChatMessage,
|
|
|
|
ChatTitle,
|
|
|
|
PostStatusForm
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
jumpToBottomButtonVisible: false,
|
|
|
|
hoveredMessageChainId: undefined,
|
2020-06-21 14:13:29 +00:00
|
|
|
lastScrollPosition: {},
|
2020-05-07 13:10:53 +00:00
|
|
|
scrollableContainerHeight: '100%',
|
2020-10-29 10:33:06 +00:00
|
|
|
errorLoadingChat: false,
|
|
|
|
messageRetriers: {}
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created () {
|
|
|
|
this.startFetching()
|
|
|
|
window.addEventListener('resize', this.handleLayoutChange)
|
|
|
|
},
|
|
|
|
mounted () {
|
2022-04-05 10:19:12 +00:00
|
|
|
scroller().addEventListener('scroll', this.handleScroll)
|
2020-05-07 13:10:53 +00:00
|
|
|
if (typeof document.hidden !== 'undefined') {
|
|
|
|
document.addEventListener('visibilitychange', this.handleVisibilityChange, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.handleResize()
|
|
|
|
})
|
|
|
|
this.setChatLayout()
|
|
|
|
},
|
2021-04-25 10:44:50 +00:00
|
|
|
unmounted () {
|
2022-04-05 10:19:12 +00:00
|
|
|
scroller().removeEventListener('scroll', this.handleScroll)
|
2020-05-07 13:10:53 +00:00
|
|
|
window.removeEventListener('resize', this.handleLayoutChange)
|
|
|
|
this.unsetChatLayout()
|
|
|
|
if (typeof document.hidden !== 'undefined') document.removeEventListener('visibilitychange', this.handleVisibilityChange, false)
|
|
|
|
this.$store.dispatch('clearCurrentChat')
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
recipient () {
|
|
|
|
return this.currentChat && this.currentChat.account
|
|
|
|
},
|
|
|
|
recipientId () {
|
|
|
|
return this.$route.params.recipient_id
|
|
|
|
},
|
|
|
|
formPlaceholder () {
|
|
|
|
if (this.recipient) {
|
2021-02-26 14:23:11 +00:00
|
|
|
return this.$t('chats.message_user', { nickname: this.recipient.screen_name_ui })
|
2020-05-07 13:10:53 +00:00
|
|
|
} else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
chatViewItems () {
|
|
|
|
return chatService.getView(this.currentChatMessageService)
|
|
|
|
},
|
|
|
|
newMessageCount () {
|
|
|
|
return this.currentChatMessageService && this.currentChatMessageService.newMessageCount
|
|
|
|
},
|
|
|
|
streamingEnabled () {
|
|
|
|
return this.mergedConfig.useStreamingApi && this.mastoUserSocketStatus === WSConnectionStatus.JOINED
|
|
|
|
},
|
|
|
|
...mapGetters([
|
|
|
|
'currentChat',
|
|
|
|
'currentChatMessageService',
|
|
|
|
'findOpenedChatByRecipientId',
|
|
|
|
'mergedConfig'
|
|
|
|
]),
|
|
|
|
...mapState({
|
|
|
|
backendInteractor: state => state.api.backendInteractor,
|
|
|
|
mastoUserSocketStatus: state => state.api.mastoUserSocketStatus,
|
|
|
|
mobileLayout: state => state.interface.mobileLayout,
|
|
|
|
layoutHeight: state => state.interface.layoutHeight,
|
|
|
|
currentUser: state => state.users.currentUser
|
|
|
|
})
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
chatViewItems () {
|
|
|
|
// We don't want to scroll to the bottom on a new message when the user is viewing older messages.
|
|
|
|
// Therefore we need to know whether the scroll position was at the bottom before the DOM update.
|
|
|
|
const bottomedOutBeforeUpdate = this.bottomedOut(BOTTOMED_OUT_OFFSET)
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (bottomedOutBeforeUpdate) {
|
2020-10-27 08:03:04 +00:00
|
|
|
this.scrollDown()
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
'$route': function () {
|
|
|
|
this.startFetching()
|
|
|
|
},
|
|
|
|
layoutHeight () {
|
|
|
|
this.handleResize({ expand: true })
|
|
|
|
},
|
|
|
|
mastoUserSocketStatus (newValue) {
|
|
|
|
if (newValue === WSConnectionStatus.JOINED) {
|
|
|
|
this.fetchChat({ isFirstFetch: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
// Used to animate the avatar near the first message of the message chain when any message belonging to the chain is hovered
|
|
|
|
onMessageHover ({ isHovered, messageChainId }) {
|
|
|
|
this.hoveredMessageChainId = isHovered ? messageChainId : undefined
|
|
|
|
},
|
|
|
|
onFilesDropped () {
|
|
|
|
this.$nextTick(() => {
|
2020-06-21 14:13:29 +00:00
|
|
|
this.handleResize()
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
handleVisibilityChange () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (!document.hidden && this.bottomedOut(BOTTOMED_OUT_OFFSET)) {
|
|
|
|
this.scrollDown({ forceRead: true })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2020-06-21 14:13:29 +00:00
|
|
|
setChatLayout () {
|
|
|
|
// This is a hacky way to adjust the global layout to the mobile chat (without modifying the rest of the app).
|
|
|
|
// This layout prevents empty spaces from being visible at the bottom
|
|
|
|
// of the chat on iOS Safari (`safe-area-inset`) when
|
|
|
|
// - the on-screen keyboard appears and the user starts typing
|
|
|
|
// - the user selects the text inside the input area
|
|
|
|
// - the user selects and deletes the text that is multiple lines long
|
|
|
|
// TODO: unify the chat layout with the global layout.
|
|
|
|
let html = document.querySelector('html')
|
|
|
|
if (html) {
|
|
|
|
html.classList.add('chat-layout')
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
2020-06-21 14:13:29 +00:00
|
|
|
},
|
|
|
|
unsetChatLayout () {
|
|
|
|
let html = document.querySelector('html')
|
|
|
|
if (html) {
|
|
|
|
html.classList.remove('chat-layout')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleLayoutChange () {
|
2020-05-07 13:10:53 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollDown()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// Preserves the scroll position when OSK appears or the posting form changes its height.
|
2020-06-21 14:13:29 +00:00
|
|
|
handleResize (opts = {}) {
|
|
|
|
const { expand = false, delayed = false } = opts
|
|
|
|
|
|
|
|
if (delayed) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.handleResize({ ...opts, delayed: false })
|
|
|
|
}, SAFE_RESIZE_TIME_OFFSET)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-07 13:10:53 +00:00
|
|
|
this.$nextTick(() => {
|
2020-06-21 14:13:29 +00:00
|
|
|
const { offsetHeight = undefined } = this.lastScrollPosition
|
2022-04-05 10:19:12 +00:00
|
|
|
this.lastScrollPosition = getScrollPosition(scroller())
|
2020-05-07 13:10:53 +00:00
|
|
|
|
2020-06-21 14:13:29 +00:00
|
|
|
const diff = this.lastScrollPosition.offsetHeight - offsetHeight
|
|
|
|
if (diff < 0 || (!this.bottomedOut() && expand)) {
|
2020-05-07 13:10:53 +00:00
|
|
|
this.$nextTick(() => {
|
2022-04-05 10:19:12 +00:00
|
|
|
scroller().scrollTo({
|
|
|
|
top: scroller().scrollTop - diff,
|
2020-05-07 13:10:53 +00:00
|
|
|
left: 0
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
scrollDown (options = {}) {
|
|
|
|
const { behavior = 'auto', forceRead = false } = options
|
2022-04-05 10:19:12 +00:00
|
|
|
const scrollable = scroller()
|
2020-05-07 13:10:53 +00:00
|
|
|
if (!scrollable) { return }
|
|
|
|
this.$nextTick(() => {
|
|
|
|
scrollable.scrollTo({ top: scrollable.scrollHeight, left: 0, behavior })
|
|
|
|
})
|
2020-10-27 08:03:04 +00:00
|
|
|
if (forceRead) {
|
2020-05-07 13:10:53 +00:00
|
|
|
this.readChat()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
readChat () {
|
2020-09-15 23:34:19 +00:00
|
|
|
if (!(this.currentChatMessageService && this.currentChatMessageService.maxId)) { return }
|
2020-05-07 13:10:53 +00:00
|
|
|
if (document.hidden) { return }
|
2020-09-15 23:34:19 +00:00
|
|
|
const lastReadId = this.currentChatMessageService.maxId
|
2020-10-29 10:33:06 +00:00
|
|
|
this.$store.dispatch('readChat', {
|
|
|
|
id: this.currentChat.id,
|
|
|
|
lastReadId
|
|
|
|
})
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
bottomedOut (offset) {
|
2022-04-05 10:19:12 +00:00
|
|
|
return isBottomedOut(scroller(), offset)
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
reachedTop () {
|
2022-04-05 10:19:12 +00:00
|
|
|
const scrollable = scroller()
|
2020-05-07 13:10:53 +00:00
|
|
|
return scrollable && scrollable.scrollTop <= 0
|
|
|
|
},
|
2021-02-18 08:14:45 +00:00
|
|
|
cullOlderCheck () {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
if (this.bottomedOut(JUMP_TO_BOTTOM_BUTTON_VISIBILITY_OFFSET)) {
|
|
|
|
this.$store.dispatch('cullOlderMessages', this.currentChatMessageService.chatId)
|
|
|
|
}
|
|
|
|
}, 5000)
|
|
|
|
},
|
2020-05-07 13:10:53 +00:00
|
|
|
handleScroll: _.throttle(function () {
|
|
|
|
if (!this.currentChat) { return }
|
|
|
|
|
|
|
|
if (this.reachedTop()) {
|
|
|
|
this.fetchChat({ maxId: this.currentChatMessageService.minId })
|
|
|
|
} else if (this.bottomedOut(JUMP_TO_BOTTOM_BUTTON_VISIBILITY_OFFSET)) {
|
|
|
|
this.jumpToBottomButtonVisible = false
|
2021-02-18 08:14:45 +00:00
|
|
|
this.cullOlderCheck()
|
2020-05-07 13:10:53 +00:00
|
|
|
if (this.newMessageCount > 0) {
|
2020-10-27 08:03:04 +00:00
|
|
|
// Use a delay before marking as read to prevent situation where new messages
|
|
|
|
// arrive just as you're leaving the view and messages that you didn't actually
|
|
|
|
// get to see get marked as read.
|
|
|
|
window.setTimeout(() => {
|
2020-10-29 10:45:44 +00:00
|
|
|
// Don't mark as read if the element doesn't exist, user has left chat view
|
2020-10-27 08:03:04 +00:00
|
|
|
if (this.$el) this.readChat()
|
|
|
|
}, MARK_AS_READ_DELAY)
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.jumpToBottomButtonVisible = true
|
|
|
|
}
|
2020-10-27 08:03:04 +00:00
|
|
|
}, 200),
|
2020-05-07 13:10:53 +00:00
|
|
|
handleScrollUp (positionBeforeLoading) {
|
2022-04-05 10:19:12 +00:00
|
|
|
const positionAfterLoading = getScrollPosition(scroller())
|
|
|
|
scroller().scrollTo({
|
2020-05-07 13:10:53 +00:00
|
|
|
top: getNewTopPosition(positionBeforeLoading, positionAfterLoading),
|
|
|
|
left: 0
|
|
|
|
})
|
|
|
|
},
|
|
|
|
fetchChat ({ isFirstFetch = false, fetchLatest = false, maxId }) {
|
|
|
|
const chatMessageService = this.currentChatMessageService
|
|
|
|
if (!chatMessageService) { return }
|
|
|
|
if (fetchLatest && this.streamingEnabled) { return }
|
|
|
|
|
|
|
|
const chatId = chatMessageService.chatId
|
|
|
|
const fetchOlderMessages = !!maxId
|
2020-09-15 23:34:19 +00:00
|
|
|
const sinceId = fetchLatest && chatMessageService.maxId
|
2020-05-07 13:10:53 +00:00
|
|
|
|
2020-09-02 18:01:31 +00:00
|
|
|
return this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId })
|
2020-05-07 13:10:53 +00:00
|
|
|
.then((messages) => {
|
|
|
|
// Clear the current chat in case we're recovering from a ws connection loss.
|
|
|
|
if (isFirstFetch) {
|
|
|
|
chatService.clear(chatMessageService)
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:19:12 +00:00
|
|
|
const positionBeforeUpdate = getScrollPosition(scroller())
|
2020-05-07 13:10:53 +00:00
|
|
|
this.$store.dispatch('addChatMessages', { chatId, messages }).then(() => {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (fetchOlderMessages) {
|
|
|
|
this.handleScrollUp(positionBeforeUpdate)
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:20:08 +00:00
|
|
|
// In vertical screens, the first batch of fetched messages may not always take the
|
|
|
|
// full height of the scrollable container.
|
|
|
|
// If this is the case, we want to fetch the messages until the scrollable container
|
|
|
|
// is fully populated so that the user has the ability to scroll up and load the history.
|
2022-04-05 14:11:50 +00:00
|
|
|
if (!isScrollable(scroller()) && messages.length > 0) {
|
2020-11-05 22:20:08 +00:00
|
|
|
this.fetchChat({ maxId: this.currentChatMessageService.minId })
|
|
|
|
}
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
async startFetching () {
|
|
|
|
let chat = this.findOpenedChatByRecipientId(this.recipientId)
|
|
|
|
if (!chat) {
|
|
|
|
try {
|
|
|
|
chat = await this.backendInteractor.getOrCreateChat({ accountId: this.recipientId })
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Error creating or getting a chat', e)
|
|
|
|
this.errorLoadingChat = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chat) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.scrollDown({ forceRead: true })
|
|
|
|
})
|
|
|
|
this.$store.dispatch('addOpenedChat', { chat })
|
|
|
|
this.doStartFetching()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
doStartFetching () {
|
|
|
|
this.$store.dispatch('startFetchingCurrentChat', {
|
2020-09-04 08:19:53 +00:00
|
|
|
fetcher: () => promiseInterval(() => this.fetchChat({ fetchLatest: true }), 5000)
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
|
|
|
this.fetchChat({ isFirstFetch: true })
|
|
|
|
},
|
2020-10-29 10:33:06 +00:00
|
|
|
handleAttachmentPosting () {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.handleResize()
|
|
|
|
// When the posting form size changes because of a media attachment, we need an extra resize
|
|
|
|
// to account for the potential delay in the DOM update.
|
|
|
|
this.scrollDown({ forceRead: true })
|
|
|
|
})
|
|
|
|
},
|
|
|
|
sendMessage ({ status, media, idempotencyKey }) {
|
2020-05-07 13:10:53 +00:00
|
|
|
const params = {
|
|
|
|
id: this.currentChat.id,
|
2020-10-29 10:33:06 +00:00
|
|
|
content: status,
|
|
|
|
idempotencyKey
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (media[0]) {
|
|
|
|
params.mediaId = media[0].id
|
|
|
|
}
|
|
|
|
|
2020-10-29 10:33:06 +00:00
|
|
|
const fakeMessage = buildFakeMessage({
|
|
|
|
attachments: media,
|
|
|
|
chatId: this.currentChat.id,
|
|
|
|
content: status,
|
|
|
|
userId: this.currentUser.id,
|
|
|
|
idempotencyKey
|
|
|
|
})
|
|
|
|
|
|
|
|
this.$store.dispatch('addChatMessages', {
|
|
|
|
chatId: this.currentChat.id,
|
|
|
|
messages: [fakeMessage]
|
|
|
|
}).then(() => {
|
|
|
|
this.handleAttachmentPosting()
|
|
|
|
})
|
|
|
|
|
|
|
|
return this.doSendMessage({ params, fakeMessage, retriesLeft: MAX_RETRIES })
|
|
|
|
},
|
|
|
|
doSendMessage ({ params, fakeMessage, retriesLeft = MAX_RETRIES }) {
|
|
|
|
if (retriesLeft <= 0) return
|
|
|
|
|
|
|
|
this.backendInteractor.sendChatMessage(params)
|
2020-05-07 13:10:53 +00:00
|
|
|
.then(data => {
|
2020-09-15 23:34:19 +00:00
|
|
|
this.$store.dispatch('addChatMessages', {
|
|
|
|
chatId: this.currentChat.id,
|
2020-10-29 10:33:06 +00:00
|
|
|
updateMaxId: false,
|
|
|
|
messages: [{ ...data, fakeId: fakeMessage.id }]
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error('Error sending message', error)
|
2020-10-29 10:33:06 +00:00
|
|
|
this.$store.dispatch('handleMessageError', {
|
|
|
|
chatId: this.currentChat.id,
|
|
|
|
fakeId: fakeMessage.id,
|
|
|
|
isRetry: retriesLeft !== MAX_RETRIES
|
|
|
|
})
|
|
|
|
if ((error.statusCode >= 500 && error.statusCode < 600) || error.message === 'Failed to fetch') {
|
|
|
|
this.messageRetriers[fakeMessage.id] = setTimeout(() => {
|
|
|
|
this.doSendMessage({ params, fakeMessage, retriesLeft: retriesLeft - 1 })
|
|
|
|
}, 1000 * (2 ** (MAX_RETRIES - retriesLeft)))
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
2020-10-29 10:33:06 +00:00
|
|
|
return {}
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
2020-10-29 10:33:06 +00:00
|
|
|
|
|
|
|
return Promise.resolve(fakeMessage)
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
goBack () {
|
|
|
|
this.$router.push({ name: 'chats', params: { username: this.currentUser.screen_name } })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Chat
|