mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Merge branch 'master' into 6536-chat-remove-message-actions
This commit is contained in:
commit
acf45ace4f
@ -6,6 +6,9 @@ export const createMessageMutation = () => {
|
||||
CreateMessage(roomId: $roomId, content: $content) {
|
||||
id
|
||||
content
|
||||
saved
|
||||
distributed
|
||||
seen
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -22,7 +25,18 @@ export const messageQuery = () => {
|
||||
username
|
||||
avatar
|
||||
date
|
||||
saved
|
||||
distributed
|
||||
seen
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
export const markMessagesAsSeen = () => {
|
||||
return gql`
|
||||
mutation ($messageIds: [String!]) {
|
||||
MarkMessagesAsSeen(messageIds: $messageIds)
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
@ -463,6 +463,7 @@ export default shield(
|
||||
saveCategorySettings: isAuthenticated,
|
||||
CreateRoom: isAuthenticated,
|
||||
CreateMessage: isAuthenticated,
|
||||
MarkMessagesAsSeen: isAuthenticated,
|
||||
},
|
||||
User: {
|
||||
email: or(isMyOwn, isAdmin),
|
||||
|
||||
@ -2,7 +2,7 @@ import { createTestClient } from 'apollo-server-testing'
|
||||
import Factory, { cleanDatabase } from '../../db/factories'
|
||||
import { getNeode, getDriver } from '../../db/neo4j'
|
||||
import { createRoomMutation } from '../../graphql/rooms'
|
||||
import { createMessageMutation, messageQuery } from '../../graphql/messages'
|
||||
import { createMessageMutation, messageQuery, markMessagesAsSeen } from '../../graphql/messages'
|
||||
import createServer from '../../server'
|
||||
|
||||
const driver = getDriver()
|
||||
@ -122,6 +122,9 @@ describe('Message', () => {
|
||||
CreateMessage: {
|
||||
id: expect.any(String),
|
||||
content: 'Some nice message to other chatting user',
|
||||
saved: true,
|
||||
distributed: false,
|
||||
seen: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
@ -217,6 +220,9 @@ describe('Message', () => {
|
||||
username: 'Chatting User',
|
||||
avatar: expect.any(String),
|
||||
date: expect.any(String),
|
||||
saved: true,
|
||||
distributed: true,
|
||||
seen: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
@ -261,6 +267,9 @@ describe('Message', () => {
|
||||
username: 'Chatting User',
|
||||
avatar: expect.any(String),
|
||||
date: expect.any(String),
|
||||
saved: true,
|
||||
distributed: true,
|
||||
seen: false,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
@ -269,6 +278,9 @@ describe('Message', () => {
|
||||
username: 'Other Chatting User',
|
||||
avatar: expect.any(String),
|
||||
date: expect.any(String),
|
||||
saved: true,
|
||||
distributed: true,
|
||||
seen: false,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: expect.any(String),
|
||||
@ -277,6 +289,9 @@ describe('Message', () => {
|
||||
username: 'Chatting User',
|
||||
avatar: expect.any(String),
|
||||
date: expect.any(String),
|
||||
saved: true,
|
||||
distributed: false,
|
||||
seen: false,
|
||||
}),
|
||||
]),
|
||||
},
|
||||
@ -308,4 +323,74 @@ describe('Message', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('marks massges as seen', () => {
|
||||
describe('unauthenticated', () => {
|
||||
beforeAll(() => {
|
||||
authenticatedUser = null
|
||||
})
|
||||
|
||||
it('throws authorization error', async () => {
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: markMessagesAsSeen(),
|
||||
variables: {
|
||||
messageIds: ['some-id'],
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
errors: [{ message: 'Not Authorized!' }],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated', () => {
|
||||
const messageIds: string[] = []
|
||||
beforeAll(async () => {
|
||||
authenticatedUser = await otherChattingUser.toJson()
|
||||
const msgs = await query({
|
||||
query: messageQuery(),
|
||||
variables: {
|
||||
roomId,
|
||||
},
|
||||
})
|
||||
msgs.data.Message.forEach((m) => messageIds.push(m.id))
|
||||
})
|
||||
|
||||
it('returns true', async () => {
|
||||
await expect(
|
||||
mutate({
|
||||
mutation: markMessagesAsSeen(),
|
||||
variables: {
|
||||
messageIds,
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
errors: undefined,
|
||||
data: {
|
||||
MarkMessagesAsSeen: true,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it('has seen prop set to true', async () => {
|
||||
await expect(
|
||||
query({
|
||||
query: messageQuery(),
|
||||
variables: {
|
||||
roomId,
|
||||
},
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
data: {
|
||||
Message: [
|
||||
expect.objectContaining({ seen: true }),
|
||||
expect.objectContaining({ seen: false }),
|
||||
expect.objectContaining({ seen: true }),
|
||||
],
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -14,9 +14,37 @@ export default {
|
||||
},
|
||||
}
|
||||
const resolved = await neo4jgraphql(object, params, context, resolveInfo)
|
||||
|
||||
if (resolved) {
|
||||
const undistributedMessagesIds = resolved
|
||||
.filter((msg) => !msg.distributed && msg.senderId !== context.user.id)
|
||||
.map((msg) => msg.id)
|
||||
if (undistributedMessagesIds.length > 0) {
|
||||
const session = context.driver.session()
|
||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
||||
const setDistributedCypher = `
|
||||
MATCH (m:Message) WHERE m.id IN $undistributedMessagesIds
|
||||
SET m.distributed = true
|
||||
RETURN m { .* }
|
||||
`
|
||||
const setDistributedTxResponse = await transaction.run(setDistributedCypher, {
|
||||
undistributedMessagesIds,
|
||||
})
|
||||
const messages = await setDistributedTxResponse.records.map((record) => record.get('m'))
|
||||
return messages
|
||||
})
|
||||
try {
|
||||
await writeTxResultPromise
|
||||
} finally {
|
||||
session.close()
|
||||
}
|
||||
// send subscription to author to updated the messages
|
||||
}
|
||||
resolved.forEach((message) => {
|
||||
message._id = message.id
|
||||
if (message.senderId !== context.user.id) {
|
||||
message.distributed = true
|
||||
}
|
||||
})
|
||||
}
|
||||
return resolved
|
||||
@ -35,7 +63,10 @@ export default {
|
||||
CREATE (currentUser)-[:CREATED]->(message:Message {
|
||||
createdAt: toString(datetime()),
|
||||
id: apoc.create.uuid(),
|
||||
content: $content
|
||||
content: $content,
|
||||
saved: true,
|
||||
distributed: false,
|
||||
seen: false
|
||||
})-[:INSIDE]->(room)
|
||||
RETURN message { .* }
|
||||
`
|
||||
@ -58,6 +89,32 @@ export default {
|
||||
session.close()
|
||||
}
|
||||
},
|
||||
MarkMessagesAsSeen: async (_parent, params, context, _resolveInfo) => {
|
||||
const { messageIds } = params
|
||||
const currentUserId = context.user.id
|
||||
const session = context.driver.session()
|
||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
||||
const setSeenCypher = `
|
||||
MATCH (m:Message)<-[:CREATED]-(user:User)
|
||||
WHERE m.id IN $messageIds AND NOT user.id = $currentUserId
|
||||
SET m.seen = true
|
||||
RETURN m { .* }
|
||||
`
|
||||
const setSeenTxResponse = await transaction.run(setSeenCypher, {
|
||||
messageIds,
|
||||
currentUserId,
|
||||
})
|
||||
const messages = await setSeenTxResponse.records.map((record) => record.get('m'))
|
||||
return messages
|
||||
})
|
||||
try {
|
||||
await writeTxResultPromise
|
||||
// send subscription to author to updated the messages
|
||||
return true
|
||||
} finally {
|
||||
session.close()
|
||||
}
|
||||
},
|
||||
},
|
||||
Message: {
|
||||
...Resolver('Message', {
|
||||
|
||||
@ -16,6 +16,10 @@ type Message {
|
||||
username: String! @cypher(statement: "MATCH (this)<-[:CREATED]-(user:User) RETURN user.name")
|
||||
avatar: String @cypher(statement: "MATCH (this)<-[:CREATED]-(:User)-[:AVATAR_IMAGE]->(image:Image) RETURN image.url")
|
||||
date: String! @cypher(statement: "RETURN this.createdAt")
|
||||
|
||||
saved: Boolean
|
||||
distributed: Boolean
|
||||
seen: Boolean
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
@ -23,6 +27,8 @@ type Mutation {
|
||||
roomId: ID!
|
||||
content: String!
|
||||
): Message
|
||||
|
||||
MarkMessagesAsSeen(messageIds: [String!]): Boolean
|
||||
}
|
||||
|
||||
type Query {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user