Skip to content

Commit b358e03

Browse files
committed
(fix) user state not realtime
1 parent 1c11692 commit b358e03

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

demo/src/ChatContainer.vue

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,6 @@ export default {
191191
192192
users.map(user => roomList[user.roomId].users.push(user))
193193
194-
this.listenUsersOnlineStatus(users)
195-
196194
const roomMessages = await Promise.all(rawMessages).then(messages => {
197195
return messages.map(message => {
198196
return {
@@ -234,6 +232,7 @@ export default {
234232
this.loadingRooms = false
235233
this.rooms.map((room, index) => this.listenLastMessage(room, index))
236234
235+
this.listenUsersOnlineStatus()
237236
this.listenRoomsTypingUsers(query)
238237
},
239238
@@ -522,35 +521,41 @@ export default {
522521
})
523522
},
524523
525-
listenUsersOnlineStatus(users) {
526-
users.map(user => {
527-
firebase
528-
.database()
529-
.ref('/status/' + user._id)
530-
.on('value', snapshot => {
531-
if (!snapshot.val()) return
532-
533-
const foundUser = users.find(u => snapshot.key === u._id)
534-
535-
if (foundUser) {
536-
const timestampFormat = isSameDay(
537-
new Date(snapshot.val().last_changed),
538-
new Date()
539-
)
540-
? 'HH:mm'
541-
: 'DD MMMM, HH:mm'
542-
543-
const timestamp = parseTimestamp(
544-
new Date(snapshot.val().last_changed),
545-
timestampFormat
546-
)
547-
548-
const last_changed =
549-
timestampFormat === 'HH:mm' ? `today, ${timestamp}` : timestamp
550-
551-
foundUser.status = { ...snapshot.val(), last_changed }
552-
}
553-
})
524+
listenUsersOnlineStatus() {
525+
this.rooms.map((room, index) => {
526+
room.users.map(user => {
527+
firebase
528+
.database()
529+
.ref('/status/' + user._id)
530+
.on('value', snapshot => {
531+
if (!snapshot.val()) return
532+
533+
const foundUser = room.users.find(u => snapshot.key === u._id)
534+
535+
if (foundUser) {
536+
const timestampFormat = isSameDay(
537+
new Date(snapshot.val().last_changed),
538+
new Date()
539+
)
540+
? 'HH:mm'
541+
: 'DD MMMM, HH:mm'
542+
543+
const timestamp = parseTimestamp(
544+
new Date(snapshot.val().last_changed),
545+
timestampFormat
546+
)
547+
548+
const last_changed =
549+
timestampFormat === 'HH:mm'
550+
? `today, ${timestamp}`
551+
: timestamp
552+
553+
user.status = { ...snapshot.val(), last_changed }
554+
555+
this.$set(this.rooms, index, room)
556+
}
557+
})
558+
})
554559
})
555560
},
556561

src/ChatWindow/ChatWindow.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div class="card-window" :style="[{ height }, cssVars]">
33
<div class="chat-container">
44
<rooms-list
5+
:currentUserId="currentUserId"
56
:rooms="rooms"
67
:loadingRooms="loadingRooms"
78
:room="room"
@@ -15,7 +16,8 @@
1516
</rooms-list>
1617
<room
1718
:currentUserId="currentUserId"
18-
:room="room"
19+
:rooms="rooms"
20+
:roomId="room.roomId || ''"
1921
:messages="messages"
2022
:messagesLoaded="messagesLoaded"
2123
:menuActions="menuActions"

src/ChatWindow/Room.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</infinite-loading>
7272
</transition>
7373
<transition-group name="fade-message">
74-
<div v-for="(message, i) in messages" :key="i">
74+
<div v-for="(message, i) in messages" :key="message._id">
7575
<message
7676
:currentUserId="currentUserId"
7777
:message="message"
@@ -234,7 +234,8 @@ export default {
234234
textMessages: { type: Object, required: true },
235235
showRoomsList: { type: Boolean, required: true },
236236
isMobile: { type: Boolean, required: true },
237-
room: { type: Object, required: true },
237+
rooms: { type: Array, required: true },
238+
roomId: { type: String, required: true },
238239
messages: { type: Array, required: true },
239240
messagesLoaded: { type: Boolean, required: true },
240241
menuActions: { type: Array, required: true },
@@ -310,14 +311,14 @@ export default {
310311
setTimeout(() => {
311312
options.behavior = 'smooth'
312313
element.scrollTo(options)
313-
}, 0)
314+
}, 50)
314315
}
315316
316317
if (this.infiniteState) {
317318
this.infiniteState.loaded()
318319
} else if (newVal.length) {
319320
this.loadingMessages = false
320-
setTimeout(() => element.scrollTo(options), 0)
321+
setTimeout(() => element.scrollTo(options), 50)
321322
}
322323
},
323324
messagesLoaded(val) {
@@ -338,6 +339,9 @@ export default {
338339
},
339340
340341
computed: {
342+
room() {
343+
return this.rooms.find(room => room.roomId === this.roomId) || {}
344+
},
341345
showNoMessages() {
342346
return (
343347
this.room.roomId &&

0 commit comments

Comments
 (0)