forked from AkkomaGang/akkoma-fe
WIP display updated_at in the chat list, use updated_at to keep the chat list sorted
This commit is contained in:
parent
cf4d3ee1bf
commit
3dbe0d1e61
5 changed files with 38 additions and 25 deletions
|
@ -6,7 +6,7 @@ import withLoadMore from '../../hocs/with_load_more/with_load_more'
|
||||||
|
|
||||||
const Chats = withLoadMore({
|
const Chats = withLoadMore({
|
||||||
fetch: (props, $store) => $store.dispatch('fetchChats'),
|
fetch: (props, $store) => $store.dispatch('fetchChats'),
|
||||||
select: (props, $store) => $store.state.chats.chatList.data,
|
select: (props, $store) => $store.getters.sortedChatList,
|
||||||
destroy: (props, $store) => undefined,
|
destroy: (props, $store) => undefined,
|
||||||
childPropName: 'items'
|
childPropName: 'items'
|
||||||
})(List)
|
})(List)
|
||||||
|
|
|
@ -40,6 +40,12 @@
|
||||||
</div>
|
</div>
|
||||||
<!-- eslint-enable vue/no-v-html -->
|
<!-- eslint-enable vue/no-v-html -->
|
||||||
</div>
|
</div>
|
||||||
|
<div style="float: right; text-align: right;">
|
||||||
|
<Timeago
|
||||||
|
:time="chat.updated_at"
|
||||||
|
:auto-update="60"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { set } from 'vue'
|
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 chatService from '../services/chat_service/chat_service.js'
|
||||||
import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js'
|
import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js'
|
||||||
|
|
||||||
|
@ -18,12 +18,21 @@ const defaultState = {
|
||||||
currentChatId: null
|
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 = {
|
const chats = {
|
||||||
state: { ...defaultState },
|
state: { ...defaultState },
|
||||||
getters: {
|
getters: {
|
||||||
currentChat: state => state.openedChats[state.currentChatId],
|
currentChat: state => state.openedChats[state.currentChatId],
|
||||||
currentChatMessageService: state => state.openedChatMessageServices[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: {
|
actions: {
|
||||||
// Chat list
|
// Chat list
|
||||||
|
@ -116,28 +125,27 @@ const chats = {
|
||||||
if (chats.length > 0) {
|
if (chats.length > 0) {
|
||||||
state.chatList.pagination = { maxId: last(chats).id }
|
state.chatList.pagination = { maxId: last(chats).id }
|
||||||
}
|
}
|
||||||
chats.forEach((conversation) => {
|
chats.forEach((updatedChat) => {
|
||||||
// This is to prevent duplicate conversations being added
|
let chat = getChatById(state, updatedChat.id)
|
||||||
// (right now, backend can return the same conversation on different pages)
|
|
||||||
if (!state.chatList.idStore[conversation.id]) {
|
if (chat) {
|
||||||
state.chatList.data.push(conversation)
|
chat.lastMessage = updatedChat.lastMessage
|
||||||
state.chatList.idStore[conversation.id] = conversation
|
chat.unread = updatedChat.unread
|
||||||
} else {
|
} else {
|
||||||
const chat = find(state.chatList.data, { id: conversation.id })
|
state.chatList.data.push(updatedChat)
|
||||||
chat.last_status = conversation.last_status
|
set(state.chatList.idStore, updatedChat.id, updatedChat)
|
||||||
chat.unread = conversation.unread
|
|
||||||
state.chatList.idStore[conversation.id] = conversation
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
updateChat (state, { _dispatch, chat: updatedChat, _rootGetters }) {
|
updateChat (state, { _dispatch, chat: updatedChat, _rootGetters }) {
|
||||||
let chat = find(state.chatList.data, { id: updatedChat.id })
|
let chat = getChatById(state, updatedChat.id)
|
||||||
if (chat) {
|
if (chat) {
|
||||||
chat.lastMessage = updatedChat.lastMessage
|
chat.lastMessage = updatedChat.lastMessage
|
||||||
chat.unread = updatedChat.unread
|
chat.unread = updatedChat.unread
|
||||||
|
chat.updated_at = updatedChat.updated_at
|
||||||
}
|
}
|
||||||
if (!chat) { state.chatList.data.unshift(updatedChat) }
|
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 }) {
|
deleteChat (state, { _dispatch, id, _rootGetters }) {
|
||||||
state.chats.data = state.chats.data.filter(conversation =>
|
state.chats.data = state.chats.data.filter(conversation =>
|
||||||
|
@ -156,15 +164,16 @@ const chats = {
|
||||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||||
if (chatMessageService) {
|
if (chatMessageService) {
|
||||||
chatService.add(chatMessageService, { messages: messages.map(parseChatMessage) })
|
chatService.add(chatMessageService, { messages: messages.map(parseChatMessage) })
|
||||||
commit('refreshLastMessage', { commit, chatId })
|
commit('refreshLastMessage', { chatId })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
refreshLastMessage (state, { chatId }) {
|
refreshLastMessage (state, { chatId }) {
|
||||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||||
if (chatMessageService) {
|
if (chatMessageService) {
|
||||||
const chat = state.chatList.data.find(c => c.id === chatId)
|
let chat = getChatById(state, chatId)
|
||||||
if (chat) {
|
if (chat) {
|
||||||
chat.lastMessage = chatMessageService.lastMessage
|
chat.lastMessage = chatMessageService.lastMessage
|
||||||
|
chat.updated_at = chatMessageService.lastMessage.created_at
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -172,7 +181,7 @@ const chats = {
|
||||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||||
if (chatMessageService) {
|
if (chatMessageService) {
|
||||||
chatService.deleteMessage(chatMessageService, messageId)
|
chatService.deleteMessage(chatMessageService, messageId)
|
||||||
commit('refreshLastMessage', { commit, chatId })
|
commit('refreshLastMessage', { chatId })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
resetChatNewMessageCount (state, _value) {
|
resetChatNewMessageCount (state, _value) {
|
||||||
|
|
|
@ -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
|
let url = PLEROMA_CHATS_URL
|
||||||
const recipientIds = recipients.map(r => `recipients[]=${r}`).join('&')
|
|
||||||
const args = [
|
const args = [
|
||||||
maxId && `max_id=${maxId}`,
|
maxId && `max_id=${maxId}`,
|
||||||
sinceId && `since_id=${sinceId}`,
|
sinceId && `since_id=${sinceId}`,
|
||||||
limit && `limit=${limit}`,
|
limit && `limit=${limit}`
|
||||||
recipientIds && `${recipientIds}`
|
|
||||||
].filter(_ => _).join('&')
|
].filter(_ => _).join('&')
|
||||||
|
|
||||||
let pagination = {}
|
|
||||||
url = url + (args ? '?' + args : '')
|
url = url + (args ? '?' + args : '')
|
||||||
return fetch(url, { headers: authHeaders(credentials) })
|
return fetch(url, { headers: authHeaders(credentials) })
|
||||||
.then((data) => data.json())
|
.then((data) => data.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
return { chats: data.map(parseChat).filter(c => c), pagination }
|
return { chats: data.map(parseChat).filter(c => c) }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -373,11 +373,12 @@ const isNsfw = (status) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const parseChat = (chat) => {
|
export const parseChat = (chat) => {
|
||||||
let output = chat
|
let output = {}
|
||||||
output.id = parseInt(chat.id, 10)
|
output.id = parseInt(chat.id, 10)
|
||||||
output.account = parseUser(chat.account)
|
output.account = parseUser(chat.account)
|
||||||
output.unread = chat.unread
|
output.unread = chat.unread
|
||||||
output.lastMessage = parseChatMessage(chat.last_message)
|
output.lastMessage = parseChatMessage(chat.last_message)
|
||||||
|
output.updated_at = new Date(chat.updated_at)
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue