2020-12-18 19:23:21 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const slugify = require('slugify')
|
2021-09-25 10:48:28 +02:00
|
|
|
const axios = require('axios')
|
|
|
|
|
|
|
|
|
|
const TELEGRAM_API_URL = 'https://api.telegram.org'
|
|
|
|
|
const TELEGRAM_CHAN_ID = process.env.TELEGRAM_CHAN_ID || null
|
|
|
|
|
const TELEGRAM_API_TOKEN = process.env.TELEGRAM_API_TOKEN || null
|
|
|
|
|
const MESSAGE_URL = `${TELEGRAM_API_URL}/bot${TELEGRAM_API_TOKEN}/sendMessage?chat_id=${TELEGRAM_CHAN_ID}&parse_mode=html`
|
2020-12-18 19:23:21 +01:00
|
|
|
|
|
|
|
|
const jwennAwtisEpiId = async data => {
|
|
|
|
|
const awtis = await strapi.query('awtis').find({_id: data})
|
|
|
|
|
return awtis.map(a => a.alias).join('-')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
lifecycles: {
|
|
|
|
|
beforeCreate: async data => {
|
|
|
|
|
if (data.tit) {
|
|
|
|
|
const awtis = await jwennAwtisEpiId(data.awtis)
|
|
|
|
|
data.slug = slugify(`${awtis}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
beforeUpdate: async (params, data) => {
|
|
|
|
|
if (data.tit) {
|
|
|
|
|
const awtis = await jwennAwtisEpiId(data.awtis)
|
|
|
|
|
data.slug = slugify(`${awtis}-${data.tit}`, {lower: true, remove: /[*#+~.()'"!:@]/g})
|
|
|
|
|
}
|
2021-05-30 12:00:39 +02:00
|
|
|
if (data.published_at != null) {
|
|
|
|
|
const {_id} = params
|
|
|
|
|
const previousData = await strapi.query('teks').findOne({_id})
|
|
|
|
|
|
|
|
|
|
const previousPublishedAt = previousData.published_at
|
|
|
|
|
const currentPublished_at = data.published_at
|
|
|
|
|
if (currentPublished_at != previousPublishedAt) {
|
2021-09-25 10:48:28 +02:00
|
|
|
const message = `<b>Nouvelle publication</b> \xF0\x9F\x8E\xB6 \xF0\x9F\x94\xA5
|
2022-03-13 02:27:54 +04:00
|
|
|
\n${process.env.WEBSITE_URL}/paroles/${previousData.slug}`
|
2021-05-30 12:00:39 +02:00
|
|
|
if (previousData.user) {
|
|
|
|
|
strapi.services.email.send(
|
|
|
|
|
process.env.SMTP_FROM,
|
|
|
|
|
previousData.user.email,
|
|
|
|
|
`Publication de "${previousData.tit}"`,
|
|
|
|
|
`Le titre que vous avez soumis, "${previousData.tit}" a été publié sur le site.
|
2022-03-13 02:27:54 +04:00
|
|
|
Vous pouvez le trouver à l'adresse ${process.env.WEBSITE_URL}/paroles/${previousData.slug}`
|
2021-05-30 12:00:39 +02:00
|
|
|
)
|
|
|
|
|
}
|
2021-09-25 10:48:28 +02:00
|
|
|
|
|
|
|
|
await axios.post(`${MESSAGE_URL}&text=${message}`)
|
2021-05-30 12:00:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-24 18:57:12 +02:00
|
|
|
},
|
|
|
|
|
afterCreate: async data => {
|
|
|
|
|
if (data.user) {
|
|
|
|
|
strapi.services.email.send(
|
|
|
|
|
process.env.SMTP_FROM,
|
|
|
|
|
process.env.SMTP_SEND_TO,
|
|
|
|
|
`Nouveau texte de ${data.user.username} : "${data.tit}"`,
|
|
|
|
|
`Le titre "${data.tit}" a été soumis depuis le site.`
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-12-18 19:23:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|