mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
refactor(backend): new chat message notification email (#8357)
* new chat message notification email - new mail features name in subject and text * fix english version * typo * fix typos * adjust tests
This commit is contained in:
parent
fcc99ab58e
commit
ab6fe37c3e
@ -39,7 +39,12 @@ const resetPasswordTemplateData = () => ({
|
|||||||
const chatMessageTemplateData = {
|
const chatMessageTemplateData = {
|
||||||
email: 'test@example.org',
|
email: 'test@example.org',
|
||||||
variables: {
|
variables: {
|
||||||
name: 'Mr Example',
|
senderUser: {
|
||||||
|
name: 'Sender',
|
||||||
|
},
|
||||||
|
recipientUser: {
|
||||||
|
name: 'Recipient',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const wrongAccountTemplateData = () => ({
|
const wrongAccountTemplateData = () => ({
|
||||||
@ -174,10 +179,10 @@ describe('templateBuilder', () => {
|
|||||||
describe('chatMessageTemplate', () => {
|
describe('chatMessageTemplate', () => {
|
||||||
describe('multi language', () => {
|
describe('multi language', () => {
|
||||||
it('e-mail is build with all data', () => {
|
it('e-mail is build with all data', () => {
|
||||||
const subject = 'Neue Chatnachricht | New chat message'
|
const subject = `Neue Chat-Nachricht | New chat message - ${chatMessageTemplateData.variables.senderUser.name}`
|
||||||
const actionUrl = new URL('/chat', CONFIG.CLIENT_URI).toString()
|
const actionUrl = new URL('/chat', CONFIG.CLIENT_URI).toString()
|
||||||
const enContent = 'You have received a new chat message.'
|
const enContent = `You have received a new chat message from <b>${chatMessageTemplateData.variables.senderUser.name}</b>.`
|
||||||
const deContent = 'Du hast eine neue Chatnachricht erhalten.'
|
const deContent = `Du hast eine neue Chat-Nachricht von <b>${chatMessageTemplateData.variables.senderUser.name}</b> erhalten.`
|
||||||
testEmailData(null, chatMessageTemplate, chatMessageTemplateData, [
|
testEmailData(null, chatMessageTemplate, chatMessageTemplateData, [
|
||||||
...textsStandard,
|
...textsStandard,
|
||||||
{
|
{
|
||||||
@ -187,7 +192,8 @@ describe('templateBuilder', () => {
|
|||||||
},
|
},
|
||||||
englishHint,
|
englishHint,
|
||||||
actionUrl,
|
actionUrl,
|
||||||
chatMessageTemplateData.variables.name,
|
chatMessageTemplateData.variables.senderUser,
|
||||||
|
chatMessageTemplateData.variables.recipientUser,
|
||||||
enContent,
|
enContent,
|
||||||
deContent,
|
deContent,
|
||||||
supportUrl,
|
supportUrl,
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
/* eslint-disable import/no-namespace */
|
/* eslint-disable import/no-namespace */
|
||||||
import mustache from 'mustache'
|
import mustache from 'mustache'
|
||||||
|
|
||||||
import logosWebapp from '@config//logos'
|
|
||||||
import metadata from '@config//metadata'
|
|
||||||
import CONFIG from '@config/index'
|
import CONFIG from '@config/index'
|
||||||
|
import logosWebapp from '@config/logos'
|
||||||
|
import metadata from '@config/metadata'
|
||||||
|
|
||||||
import * as templates from './templates'
|
import * as templates from './templates'
|
||||||
import * as templatesDE from './templates/de'
|
import * as templatesDE from './templates/de'
|
||||||
@ -73,10 +73,17 @@ export const resetPasswordTemplate = ({ email, variables: { nonce, name } }) =>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const chatMessageTemplate = ({ email, variables: { name } }) => {
|
export const chatMessageTemplate = ({ email, variables: { senderUser, recipientUser } }) => {
|
||||||
const subject = 'Neue Chatnachricht | New chat message'
|
const subject = `Neue Chat-Nachricht | New chat message - ${senderUser.name}`
|
||||||
const actionUrl = new URL('/chat', CONFIG.CLIENT_URI)
|
const actionUrl = new URL('/chat', CONFIG.CLIENT_URI)
|
||||||
const renderParams = { ...defaultParams, englishHint, actionUrl, name, subject }
|
const renderParams = {
|
||||||
|
...defaultParams,
|
||||||
|
subject,
|
||||||
|
englishHint,
|
||||||
|
actionUrl,
|
||||||
|
senderUser,
|
||||||
|
recipientUser,
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from,
|
from,
|
||||||
|
|||||||
@ -23,8 +23,8 @@
|
|||||||
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
|
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
|
||||||
<h1
|
<h1
|
||||||
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
|
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
|
||||||
Hallo {{ name }}!</h1>
|
Hallo {{ recipientUser.name }}!</h1>
|
||||||
<p style="margin: 0;">Du hast eine neue Chatnachricht erhalten.</p>
|
<p style="margin: 0;">Du hast eine neue Chat-Nachricht von <b>{{ senderUser.name }}</b> erhalten.</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -78,8 +78,8 @@
|
|||||||
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
|
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
|
||||||
<h1
|
<h1
|
||||||
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
|
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
|
||||||
Hello {{ name }}!</h1>
|
Hello {{ recipientUser.name }}!</h1>
|
||||||
<p style="margin: 0;">You have received a new chat message.</p>
|
<p style="margin: 0;">You have received a new chat message from <b>{{ senderUser.name }}</b>.</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
@ -5,35 +5,33 @@ let user
|
|||||||
describe('isUserOnline', () => {
|
describe('isUserOnline', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
user = {
|
user = {
|
||||||
properties: {
|
lastActiveAt: null,
|
||||||
lastActiveAt: null,
|
awaySince: null,
|
||||||
awaySince: null,
|
lastOnlineStatus: null,
|
||||||
lastOnlineStatus: null,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
describe('user has lastOnlineStatus `online`', () => {
|
describe('user has lastOnlineStatus `online`', () => {
|
||||||
it('returns true if he was active within the last 90 seconds', () => {
|
it('returns true if he was active within the last 90 seconds', () => {
|
||||||
user.properties.lastOnlineStatus = 'online'
|
user.lastOnlineStatus = 'online'
|
||||||
user.properties.lastActiveAt = new Date()
|
user.lastActiveAt = new Date()
|
||||||
expect(isUserOnline(user)).toBe(true)
|
expect(isUserOnline(user)).toBe(true)
|
||||||
})
|
})
|
||||||
it('returns false if he was not active within the last 90 seconds', () => {
|
it('returns false if he was not active within the last 90 seconds', () => {
|
||||||
user.properties.lastOnlineStatus = 'online'
|
user.lastOnlineStatus = 'online'
|
||||||
user.properties.lastActiveAt = new Date().getTime() - 90001
|
user.lastActiveAt = new Date().getTime() - 90001
|
||||||
expect(isUserOnline(user)).toBe(false)
|
expect(isUserOnline(user)).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('user has lastOnlineStatus `away`', () => {
|
describe('user has lastOnlineStatus `away`', () => {
|
||||||
it('returns true if he went away less then 180 seconds ago', () => {
|
it('returns true if he went away less then 180 seconds ago', () => {
|
||||||
user.properties.lastOnlineStatus = 'away'
|
user.lastOnlineStatus = 'away'
|
||||||
user.properties.awaySince = new Date()
|
user.awaySince = new Date()
|
||||||
expect(isUserOnline(user)).toBe(true)
|
expect(isUserOnline(user)).toBe(true)
|
||||||
})
|
})
|
||||||
it('returns false if he went away more then 180 seconds ago', () => {
|
it('returns false if he went away more then 180 seconds ago', () => {
|
||||||
user.properties.lastOnlineStatus = 'away'
|
user.lastOnlineStatus = 'away'
|
||||||
user.properties.awaySince = new Date().getTime() - 180001
|
user.awaySince = new Date().getTime() - 180001
|
||||||
expect(isUserOnline(user)).toBe(false)
|
expect(isUserOnline(user)).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
export const isUserOnline = (user) => {
|
export const isUserOnline = (user) => {
|
||||||
// Is Recipient considered online
|
// Is Recipient considered online
|
||||||
const lastActive = new Date(user.properties.lastActiveAt).getTime()
|
const lastActive = new Date(user.lastActiveAt).getTime()
|
||||||
const awaySince = new Date(user.properties.awaySince).getTime()
|
const awaySince = new Date(user.awaySince).getTime()
|
||||||
const now = new Date().getTime()
|
const now = new Date().getTime()
|
||||||
const status = user.properties.lastOnlineStatus
|
const status = user.lastOnlineStatus
|
||||||
if (
|
if (
|
||||||
// online & last active less than 1.5min -> online
|
// online & last active less than 1.5min -> online
|
||||||
(status === 'online' && now - lastActive < 90000) ||
|
(status === 'online' && now - lastActive < 90000) ||
|
||||||
|
|||||||
@ -355,12 +355,12 @@ const handleCreateMessage = async (resolve, root, args, context, resolveInfo) =>
|
|||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
const messageRecipient = session.readTransaction(async (transaction) => {
|
const messageRecipient = session.readTransaction(async (transaction) => {
|
||||||
const messageRecipientCypher = `
|
const messageRecipientCypher = `
|
||||||
MATCH (currentUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
MATCH (senderUser:User { id: $currentUserId })-[:CHATS_IN]->(room:Room { id: $roomId })
|
||||||
MATCH (room)<-[:CHATS_IN]-(recipientUser:User)-[:PRIMARY_EMAIL]->(emailAddress:EmailAddress)
|
MATCH (room)<-[:CHATS_IN]-(recipientUser:User)-[:PRIMARY_EMAIL]->(emailAddress:EmailAddress)
|
||||||
WHERE NOT recipientUser.id = $currentUserId
|
WHERE NOT recipientUser.id = $currentUserId
|
||||||
AND NOT (recipientUser)-[:BLOCKED]-(currentUser)
|
AND NOT (recipientUser)-[:BLOCKED]-(senderUser)
|
||||||
AND NOT recipientUser.emailNotificationsChatMessage = false
|
AND NOT recipientUser.emailNotificationsChatMessage = false
|
||||||
RETURN recipientUser, emailAddress {.email}
|
RETURN senderUser {.*}, recipientUser {.*}, emailAddress {.email}
|
||||||
`
|
`
|
||||||
const txResponse = await transaction.run(messageRecipientCypher, {
|
const txResponse = await transaction.run(messageRecipientCypher, {
|
||||||
currentUserId,
|
currentUserId,
|
||||||
@ -368,18 +368,19 @@ const handleCreateMessage = async (resolve, root, args, context, resolveInfo) =>
|
|||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
user: await txResponse.records.map((record) => record.get('recipientUser'))[0],
|
senderUser: await txResponse.records.map((record) => record.get('senderUser'))[0],
|
||||||
|
recipientUser: await txResponse.records.map((record) => record.get('recipientUser'))[0],
|
||||||
email: await txResponse.records.map((record) => record.get('emailAddress'))[0]?.email,
|
email: await txResponse.records.map((record) => record.get('emailAddress'))[0]?.email,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Execute Query
|
// Execute Query
|
||||||
const { user, email } = await messageRecipient
|
const { senderUser, recipientUser, email } = await messageRecipient
|
||||||
|
|
||||||
// Send EMail if we found a user(not blocked) and he is not considered online
|
// Send EMail if we found a user(not blocked) and he is not considered online
|
||||||
if (user && !isUserOnline(user)) {
|
if (recipientUser && !isUserOnline(recipientUser)) {
|
||||||
void sendMail(chatMessageTemplate({ email, variables: { name: user.properties.name } }))
|
void sendMail(chatMessageTemplate({ email, variables: { senderUser, recipientUser } }))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return resolver result to client
|
// Return resolver result to client
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user