Merge branch 'master' into 6557-chat-reactive-language-switch

This commit is contained in:
Ulf Gebhardt 2023-07-15 01:39:14 +02:00 committed by GitHub
commit dafdd26eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 155 additions and 41 deletions

View File

@ -16,10 +16,11 @@ export const createMessageMutation = () => {
export const messageQuery = () => { export const messageQuery = () => {
return gql` return gql`
query ($roomId: ID!) { query ($roomId: ID!, $first: Int, $offset: Int) {
Message(roomId: $roomId) { Message(roomId: $roomId, first: $first, offset: $offset, orderBy: createdAt_desc) {
_id _id
id id
indexId
content content
senderId senderId
username username

View File

@ -215,6 +215,7 @@ describe('Message', () => {
{ {
id: expect.any(String), id: expect.any(String),
_id: result.data.Message[0].id, _id: result.data.Message[0].id,
indexId: 0,
content: 'Some nice message to other chatting user', content: 'Some nice message to other chatting user',
senderId: 'chatting-user', senderId: 'chatting-user',
username: 'Chatting User', username: 'Chatting User',
@ -259,9 +260,10 @@ describe('Message', () => {
).resolves.toMatchObject({ ).resolves.toMatchObject({
errors: undefined, errors: undefined,
data: { data: {
Message: expect.arrayContaining([ Message: [
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
indexId: 0,
content: 'Some nice message to other chatting user', content: 'Some nice message to other chatting user',
senderId: 'chatting-user', senderId: 'chatting-user',
username: 'Chatting User', username: 'Chatting User',
@ -273,6 +275,7 @@ describe('Message', () => {
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
indexId: 1,
content: 'A nice response message to chatting user', content: 'A nice response message to chatting user',
senderId: 'other-chatting-user', senderId: 'other-chatting-user',
username: 'Other Chatting User', username: 'Other Chatting User',
@ -284,6 +287,7 @@ describe('Message', () => {
}), }),
expect.objectContaining({ expect.objectContaining({
id: expect.any(String), id: expect.any(String),
indexId: 2,
content: 'And another nice message to other chatting user', content: 'And another nice message to other chatting user',
senderId: 'chatting-user', senderId: 'chatting-user',
username: 'Chatting User', username: 'Chatting User',
@ -293,7 +297,70 @@ describe('Message', () => {
distributed: false, distributed: false,
seen: false, seen: false,
}), }),
]), ],
},
})
})
it('returns the messages paginated', async () => {
await expect(
query({
query: messageQuery(),
variables: {
roomId,
first: 2,
offset: 0,
},
}),
).resolves.toMatchObject({
errors: undefined,
data: {
Message: [
expect.objectContaining({
id: expect.any(String),
indexId: 1,
content: 'A nice response message to chatting user',
senderId: 'other-chatting-user',
username: 'Other Chatting User',
avatar: expect.any(String),
date: expect.any(String),
}),
expect.objectContaining({
id: expect.any(String),
indexId: 2,
content: 'And another nice message to other chatting user',
senderId: 'chatting-user',
username: 'Chatting User',
avatar: expect.any(String),
date: expect.any(String),
}),
],
},
})
await expect(
query({
query: messageQuery(),
variables: {
roomId,
first: 2,
offset: 2,
},
}),
).resolves.toMatchObject({
errors: undefined,
data: {
Message: [
expect.objectContaining({
id: expect.any(String),
indexId: 0,
content: 'Some nice message to other chatting user',
senderId: 'chatting-user',
username: 'Chatting User',
avatar: expect.any(String),
date: expect.any(String),
}),
],
}, },
}) })
}) })

View File

@ -13,6 +13,7 @@ export default {
id: context.user.id, id: context.user.id,
}, },
} }
const resolved = await neo4jgraphql(object, params, context, resolveInfo) const resolved = await neo4jgraphql(object, params, context, resolveInfo)
if (resolved) { if (resolved) {
@ -47,7 +48,7 @@ export default {
} }
}) })
} }
return resolved return resolved.reverse()
}, },
}, },
Mutation: { Mutation: {
@ -60,9 +61,12 @@ export default {
const writeTxResultPromise = session.writeTransaction(async (transaction) => { const writeTxResultPromise = session.writeTransaction(async (transaction) => {
const createMessageCypher = ` const createMessageCypher = `
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId }) MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
OPTIONAL MATCH (m:Message)-[:INSIDE]->(room)
WITH MAX(m.indexId) as maxIndex, room, currentUser
CREATE (currentUser)-[:CREATED]->(message:Message { CREATE (currentUser)-[:CREATED]->(message:Message {
createdAt: toString(datetime()), createdAt: toString(datetime()),
id: apoc.create.uuid(), id: apoc.create.uuid(),
indexId: CASE WHEN maxIndex IS NOT NULL THEN maxIndex + 1 ELSE 0 END,
content: $content, content: $content,
saved: true, saved: true,
distributed: false, distributed: false,

View File

@ -2,8 +2,14 @@
# room: _RoomFilter # room: _RoomFilter
# } # }
enum _MessageOrdering {
createdAt_asc
createdAt_desc
}
type Message { type Message {
id: ID! id: ID!
indexId: Int!
createdAt: String createdAt: String
updatedAt: String updatedAt: String
@ -32,5 +38,10 @@ type Mutation {
} }
type Query { type Query {
Message(roomId: ID!): [Message] Message(
roomId: ID!,
first: Int
offset: Int
orderBy: [_MessageOrdering]
): [Message]
} }

View File

@ -22,6 +22,7 @@
@fetch-messages="fetchMessages($event.detail[0])" @fetch-messages="fetchMessages($event.detail[0])"
:responsive-breakpoint="responsiveBreakpoint" :responsive-breakpoint="responsiveBreakpoint"
:single-room="singleRoom" :single-room="singleRoom"
show-reaction-emojis="false"
@show-demo-options="showDemoOptions = $event" @show-demo-options="showDemoOptions = $event"
> >
<div slot="menu-icon" @click.prevent.stop="$emit('close-single-room', true)"> <div slot="menu-icon" @click.prevent.stop="$emit('close-single-room', true)">
@ -92,17 +93,20 @@ export default {
{ {
name: 'deleteRoom', name: 'deleteRoom',
title: 'Delete Room', title: 'Delete Room',
}, */ },
*/
], ],
messageActions: [ messageActions: [
// { /*
// name: 'addMessageToFavorite', {
// title: 'Add To Favorite', name: 'addMessageToFavorite',
// }, title: 'Add To Favorite',
// { },
// name: 'shareMessage', {
// title: 'Share Message', name: 'shareMessage',
// }, title: 'Share Message',
},
*/
], ],
templatesText: [ templatesText: [
{ {
@ -131,6 +135,10 @@ export default {
showDemoOptions: true, showDemoOptions: true,
responsiveBreakpoint: 600, responsiveBreakpoint: 600,
singleRoom: !!this.singleRoomId || false, singleRoom: !!this.singleRoomId || false,
messagePage: 0,
messagePageSize: 20,
roomPage: 0,
roomPageSize: 999, // TODO pagination is a problem with single rooms - cant use
selectedRoom: null, selectedRoom: null,
} }
}, },
@ -181,32 +189,48 @@ export default {
}, },
}, },
methods: { methods: {
fetchMessages({ room, options = {} }) { async fetchMessages({ room, options = {} }) {
this.messagesLoaded = false if (this.selectedRoom !== room.id) {
setTimeout(async () => { this.messages = []
try { this.messagePage = 0
const { this.selectedRoom = room.id
data: { Message }, }
} = await this.$apollo.query({ this.messagesLoaded = options.refetch ? this.messagesLoaded : false
query: messageQuery(), const offset = (options.refetch ? 0 : this.messagePage) * this.messagePageSize
variables: { try {
roomId: room.id, const {
}, data: { Message },
fetchPolicy: 'no-cache', } = await this.$apollo.query({
}) query: messageQuery(),
this.messages = Message variables: {
} catch (error) { roomId: room.id,
this.messages = [] first: this.messagePageSize,
this.$toast.error(error.message) offset,
} },
this.messagesLoaded = true fetchPolicy: 'no-cache',
})
this.selectedRoom = room const msgs = []
}) ;[...this.messages, ...Message].forEach((m) => {
msgs[m.indexId] = m
})
this.messages = msgs.filter(Boolean)
if (Message.length < this.messagePageSize) {
this.messagesLoaded = true
}
this.messagePage += 1
} catch (error) {
this.messages = []
this.$toast.error(error.message)
}
}, },
refetchMessage(roomId) { refetchMessage(roomId) {
this.fetchMessages({ room: this.rooms.find((r) => r.roomId === roomId) }) this.fetchMessages({
room: this.rooms.find((r) => r.roomId === roomId),
options: { refetch: true },
})
}, },
async sendMessage(message) { async sendMessage(message) {
@ -234,6 +258,12 @@ export default {
query() { query() {
return roomQuery() return roomQuery()
}, },
variables() {
return {
first: this.roomPageSize,
offset: this.roomPage * this.roomPageSize,
}
},
update({ Room }) { update({ Room }) {
if (!Room) { if (!Room) {
this.rooms = [] this.rooms = []

View File

@ -2,10 +2,11 @@ import gql from 'graphql-tag'
export const messageQuery = () => { export const messageQuery = () => {
return gql` return gql`
query ($roomId: ID!) { query ($roomId: ID!, $first: Int, $offset: Int) {
Message(roomId: $roomId) { Message(roomId: $roomId, first: $first, offset: $offset, orderBy: createdAt_desc) {
_id _id
id id
indexId
senderId senderId
content content
author { author {

View File

@ -1,8 +1,8 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
export const roomQuery = () => gql` export const roomQuery = () => gql`
query { query Room($first: Int, $offset: Int) {
Room { Room(first: $first, offset: $offset) {
id id
roomId roomId
roomName roomName