From 3dbe0d1e61f3562d7c4056113a598b292132fbc1 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Fri, 15 May 2020 07:55:18 +0300 Subject: [PATCH] WIP display updated_at in the chat list, use updated_at to keep the chat list sorted --- src/components/chat_list/chat_list.js | 2 +- .../chat_list_item/chat_list_item.vue | 6 +++ src/modules/chats.js | 43 +++++++++++-------- src/services/api/api.service.js | 9 ++-- .../entity_normalizer.service.js | 3 +- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/components/chat_list/chat_list.js b/src/components/chat_list/chat_list.js index d150cc96..6b7f5fc0 100644 --- a/src/components/chat_list/chat_list.js +++ b/src/components/chat_list/chat_list.js @@ -6,7 +6,7 @@ import withLoadMore from '../../hocs/with_load_more/with_load_more' const Chats = withLoadMore({ fetch: (props, $store) => $store.dispatch('fetchChats'), - select: (props, $store) => $store.state.chats.chatList.data, + select: (props, $store) => $store.getters.sortedChatList, destroy: (props, $store) => undefined, childPropName: 'items' })(List) diff --git a/src/components/chat_list_item/chat_list_item.vue b/src/components/chat_list_item/chat_list_item.vue index 94482706..18434392 100644 --- a/src/components/chat_list_item/chat_list_item.vue +++ b/src/components/chat_list_item/chat_list_item.vue @@ -40,6 +40,12 @@ +
+ +
diff --git a/src/modules/chats.js b/src/modules/chats.js index 3c29ebf7..0e18723a 100644 --- a/src/modules/chats.js +++ b/src/modules/chats.js @@ -1,5 +1,5 @@ import { set } from 'vue' -import { find, omitBy, debounce, last } from 'lodash' +import { find, omitBy, debounce, last, orderBy } from 'lodash' import chatService from '../services/chat_service/chat_service.js' import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js' @@ -18,12 +18,21 @@ const defaultState = { currentChatId: null } +const getChatById = (state, id) => { + return find(state.chatList.data, { id }) +} + +const sortedChatList = (state) => { + return orderBy(state.chatList.data, ['updated_at'], ['desc']) +} + const chats = { state: { ...defaultState }, getters: { currentChat: state => state.openedChats[state.currentChatId], currentChatMessageService: state => state.openedChatMessageServices[state.currentChatId], - findOpenedChatByRecipientId: state => recipientId => find(state.openedChats, c => c.account.id === recipientId) + findOpenedChatByRecipientId: state => recipientId => find(state.openedChats, c => c.account.id === recipientId), + sortedChatList }, actions: { // Chat list @@ -116,28 +125,27 @@ const chats = { if (chats.length > 0) { state.chatList.pagination = { maxId: last(chats).id } } - chats.forEach((conversation) => { - // This is to prevent duplicate conversations being added - // (right now, backend can return the same conversation on different pages) - if (!state.chatList.idStore[conversation.id]) { - state.chatList.data.push(conversation) - state.chatList.idStore[conversation.id] = conversation + chats.forEach((updatedChat) => { + let chat = getChatById(state, updatedChat.id) + + if (chat) { + chat.lastMessage = updatedChat.lastMessage + chat.unread = updatedChat.unread } else { - const chat = find(state.chatList.data, { id: conversation.id }) - chat.last_status = conversation.last_status - chat.unread = conversation.unread - state.chatList.idStore[conversation.id] = conversation + state.chatList.data.push(updatedChat) + set(state.chatList.idStore, updatedChat.id, updatedChat) } }) }, updateChat (state, { _dispatch, chat: updatedChat, _rootGetters }) { - let chat = find(state.chatList.data, { id: updatedChat.id }) + let chat = getChatById(state, updatedChat.id) if (chat) { chat.lastMessage = updatedChat.lastMessage chat.unread = updatedChat.unread + chat.updated_at = updatedChat.updated_at } if (!chat) { state.chatList.data.unshift(updatedChat) } - state.chatList.idStore[updatedChat.id] = updatedChat + set(state.chatList.idStore, updatedChat.id, updatedChat) }, deleteChat (state, { _dispatch, id, _rootGetters }) { state.chats.data = state.chats.data.filter(conversation => @@ -156,15 +164,16 @@ const chats = { const chatMessageService = state.openedChatMessageServices[chatId] if (chatMessageService) { chatService.add(chatMessageService, { messages: messages.map(parseChatMessage) }) - commit('refreshLastMessage', { commit, chatId }) + commit('refreshLastMessage', { chatId }) } }, refreshLastMessage (state, { chatId }) { const chatMessageService = state.openedChatMessageServices[chatId] if (chatMessageService) { - const chat = state.chatList.data.find(c => c.id === chatId) + let chat = getChatById(state, chatId) if (chat) { chat.lastMessage = chatMessageService.lastMessage + chat.updated_at = chatMessageService.lastMessage.created_at } } }, @@ -172,7 +181,7 @@ const chats = { const chatMessageService = state.openedChatMessageServices[chatId] if (chatMessageService) { chatService.deleteMessage(chatMessageService, messageId) - commit('refreshLastMessage', { commit, chatId }) + commit('refreshLastMessage', { chatId }) } }, resetChatNewMessageCount (state, _value) { diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 5c2bf2a2..74645afb 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -1124,22 +1124,19 @@ export const handleMastoWS = (wsEvent) => { } } -const chats = ({ maxId, sinceId, limit = 20, recipients = [], credentials }) => { +const chats = ({ maxId, sinceId, limit = 20, credentials }) => { let url = PLEROMA_CHATS_URL - const recipientIds = recipients.map(r => `recipients[]=${r}`).join('&') const args = [ maxId && `max_id=${maxId}`, sinceId && `since_id=${sinceId}`, - limit && `limit=${limit}`, - recipientIds && `${recipientIds}` + limit && `limit=${limit}` ].filter(_ => _).join('&') - let pagination = {} url = url + (args ? '?' + args : '') return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) .then((data) => { - return { chats: data.map(parseChat).filter(c => c), pagination } + return { chats: data.map(parseChat).filter(c => c) } }) } diff --git a/src/services/entity_normalizer/entity_normalizer.service.js b/src/services/entity_normalizer/entity_normalizer.service.js index ea97f245..2db403de 100644 --- a/src/services/entity_normalizer/entity_normalizer.service.js +++ b/src/services/entity_normalizer/entity_normalizer.service.js @@ -373,11 +373,12 @@ const isNsfw = (status) => { } export const parseChat = (chat) => { - let output = chat + let output = {} output.id = parseInt(chat.id, 10) output.account = parseUser(chat.account) output.unread = chat.unread output.lastMessage = parseChatMessage(chat.last_message) + output.updated_at = new Date(chat.updated_at) return output }