Refactor messages and rooms query.

This commit is contained in:
elweyn 2023-07-25 16:39:43 +02:00
parent 4d7319fac4
commit 2c29437912
4 changed files with 47 additions and 55 deletions

View File

@ -8,7 +8,9 @@
# TODO change this to last message date # TODO change this to last message date
enum _RoomOrdering { enum _RoomOrdering {
lastMessageAt_desc lastMessageAt_desc
lastMessageAt_asc
createdAt_desc createdAt_desc
createdAt_asc
} }
type Room { type Room {

View File

@ -157,16 +157,13 @@ export default {
*/ */
], ],
showDemoOptions: true,
responsiveBreakpoint: 600, responsiveBreakpoint: 600,
rooms: [], rooms: [],
roomsLoaded: false, roomsLoaded: false,
roomPage: 0,
roomPageSize: 10, roomPageSize: 10,
selectedRoom: this.roomId, selectedRoom: this.roomId,
loadingRooms: true, loadingRooms: true,
messagesLoaded: false, messagesLoaded: false,
messagePage: 0,
messagePageSize: 20, messagePageSize: 20,
messages: [], messages: [],
} }
@ -234,77 +231,70 @@ export default {
commitRoomIdFromSingleRoom: 'chat/UPDATE_ROOM_ID', commitRoomIdFromSingleRoom: 'chat/UPDATE_ROOM_ID',
}), }),
async fetchRooms({ room } = {}) { fetchRooms({ room } = {}) {
this.roomsLoaded = false this.roomsLoaded = false
const offset = this.roomPage * this.roomPageSize this.$apollo.query({
try { query: roomQuery(),
const { variables: {
data: { Room }, id: room?.id,
} = await this.$apollo.query({ first: this.roomPageSize,
query: roomQuery(), offset: this.rooms.length,
variables: { },
id: room?.id, fetchPolicy: 'no-cache',
first: this.roomPageSize, }).then(({ data: { Room }})=> {
offset,
},
fetchPolicy: 'no-cache',
})
const rms = [] const rms = []
const rmsIds = [] const rmsIds = []
;[...Room, ...this.rooms].forEach((r) => { ;[...this.rooms, ...Room].forEach((r) => {
if (!rmsIds.find((v) => v === r.id)) { if (!rmsIds.find((v) => v === r.id)) {
rms.push(this.fixRoomObject(r)) rms.push(this.fixRoomObject(r))
rmsIds.push(r.id) rmsIds.push(r.id)
} }
}) })
this.rooms = rms this.rooms = rms
if (Room.length < this.roomPageSize) { if (Room.length < this.roomPageSize) {
this.roomsLoaded = true this.roomsLoaded = true
} }
this.roomPage += 1
if (this.singleRoom && this.rooms.length > 0) { if (this.singleRoom && this.rooms.length > 0) {
this.commitRoomIdFromSingleRoom(this.rooms[0].roomId) this.commitRoomIdFromSingleRoom(this.rooms[0].roomId)
} else if (this.getStoreRoomId.roomId) { } else if (this.getStoreRoomId.roomId) {
// reset store room id // reset store room id
this.commitRoomIdFromSingleRoom(null) this.commitRoomIdFromSingleRoom(this.getStoreRoomId.roomId)
} }
} catch (error) { }).catch((error) => {
this.rooms = [] this.rooms = []
this.$toast.error(error.message) this.$toast.error(error.message)
} }).finally(() => {
// must be set false after initial rooms are loaded and never changed again // must be set false after initial rooms are loaded and never changed again
this.loadingRooms = false this.loadingRooms = false
})
}, },
async fetchMessages({ room, options = {} }) { fetchMessages({ room, options = {} }) {
if (this.selectedRoom?.id !== room.id) { if (this.selectedRoom?.id !== room.id) {
this.messages = [] this.messages = []
this.messagePage = 0
this.selectedRoom = room this.selectedRoom = room
} }
this.messagesLoaded = options.refetch ? this.messagesLoaded : false this.messagesLoaded = options.refetch ? this.messagesLoaded : false
try { this.$apollo.query({
const { query: messageQuery(),
data: { Message }, variables: {
} = await this.$apollo.query({ roomId: room.id,
query: messageQuery(), first: this.messagePageSize,
variables: { offset: this.messages.length,
roomId: room.id, },
first: this.messagePageSize, fetchPolicy: 'no-cache',
offset: this.messages.length, }).then(({ data: { Message }}) => {
},
fetchPolicy: 'no-cache',
})
const newMsgIds = Message.filter( const newMsgIds = Message.filter(
(m) => m.seen === false && m.senderId !== this.currentUser.id, (m) => m.seen === false && m.senderId !== this.currentUser.id,
).map((m) => m.id) ).map((m) => m.id)
if (newMsgIds.length) { if (newMsgIds.length) {
const roomIndex = this.rooms.findIndex((r) => r.id === room.id) const roomIndex = this.rooms.findIndex((r) => r.id === room.id) ?? Message.id
if (roomIndex === -1) {
this.rooms = [room, ...this.rooms]
}
const changedRoom = { ...this.rooms[roomIndex] } const changedRoom = { ...this.rooms[roomIndex] }
changedRoom.unreadCount = changedRoom.unreadCount - newMsgIds.length changedRoom.unreadCount = changedRoom.unreadCount - newMsgIds.length
this.rooms[roomIndex] = changedRoom this.rooms[roomIndex] = changedRoom
@ -338,15 +328,15 @@ export default {
if (Message.length < this.messagePageSize) { if (Message.length < this.messagePageSize) {
this.messagesLoaded = true this.messagesLoaded = true
} }
this.messagePage += 1 }).catch((error) => {
} catch (error) {
this.messages = [] this.messages = []
this.$toast.error(error.message) this.$toast.error(error.message)
} })
}, },
addMessageToCurrentRoom(message) { addMessageToCurrentRoom(message) {
const messages = this.messages const messages = this.messages
message.date = new Date(message.date).toDateString()
messages.push(message) messages.push(message)
this.messages = messages this.messages = messages
}, },
@ -358,13 +348,13 @@ export default {
changedRoom.lastMessage = data.chatMessageAdded changedRoom.lastMessage = data.chatMessageAdded
changedRoom.lastMessage.content = changedRoom.lastMessage.content.trim().substring(0, 30) changedRoom.lastMessage.content = changedRoom.lastMessage.content.trim().substring(0, 30)
changedRoom.lastMessageAt = data.chatMessageAdded.date changedRoom.lastMessageAt = data.chatMessageAdded.date
changedRoom.unreadCount++
this.rooms[roomIndex] = changedRoom
if (data.chatMessageAdded.room.id === this.selectedRoom?.id) { if (data.chatMessageAdded.room.id === this.selectedRoom?.id) {
this.addMessageToCurrentRoom(data.chatMessageAdded) this.addMessageToCurrentRoom(data.chatMessageAdded)
} else { } else {
this.fetchRooms({ options: { refetch: true } }) changedRoom.unreadCount++
this.fetchRooms({room: this.selectedRoom, options: { refetch: true } })
} }
this.rooms[roomIndex] = changedRoom
}, },
sendMessage(message) { sendMessage(message) {
@ -428,13 +418,13 @@ export default {
this.rooms = [room, ...this.rooms] this.rooms = [room, ...this.rooms]
} }
this.fetchMessages({ room, options: { refetch: true } }) this.fetchMessages({ room, options: { refetch: true } })
this.$emit('show-chat', CreateRoom.id) this.$emit('show-room', CreateRoom.id)
}) })
.catch((error) => { .catch((error) => {
this.$toast.error(error.message) this.$toast.error(error.message)
}) })
.finally(() => { .finally(() => {
// this.loading = false this.loadingRooms = false
}) })
}, },
}, },

View File

@ -24,7 +24,7 @@ export const createRoom = () => gql`
export const roomQuery = () => gql` export const roomQuery = () => gql`
query Room($first: Int, $offset: Int, $id: ID) { query Room($first: Int, $offset: Int, $id: ID) {
Room(first: $first, offset: $offset, id: $id, orderBy: [createdAt_desc, lastMessageAt_desc]) { Room(first: $first, offset: $offset, id: $id, orderBy: [lastMessageAt_asc]) {
id id
roomId roomId
roomName roomName

View File

@ -406,11 +406,11 @@ export default {
if (type === 'following') this.followingCount = count if (type === 'following') this.followingCount = count
if (type === 'followedBy') this.followedByCount = count if (type === 'followedBy') this.followedByCount = count
}, },
async showOrChangeChat(roomID) { async showOrChangeChat(userID) {
if (this.getShowChat.showChat) { if (this.getShowChat.showChat) {
await this.showChat({ showChat: false, roomID: null }) await this.showChat({ showChat: false, roomID: null })
} }
await this.showChat({ showChat: true, roomID }) await this.showChat({ showChat: true, roomID: userID })
}, },
}, },
apollo: { apollo: {