WIP display updated_at in the chat list, use updated_at to keep the chat list sorted

This commit is contained in:
eugenijm 2020-05-15 07:55:18 +03:00
parent cf4d3ee1bf
commit 3dbe0d1e61
5 changed files with 38 additions and 25 deletions

View file

@ -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)

View file

@ -40,6 +40,12 @@
</div>
<!-- eslint-enable vue/no-v-html -->
</div>
<div style="float: right; text-align: right;">
<Timeago
:time="chat.updated_at"
:auto-update="60"
/>
</div>
</div>
</template>

View file

@ -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) {

View file

@ -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) }
})
}

View file

@ -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
}