Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 3 additions & 39 deletions src/api/notification/controllers/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import { factories } from '@strapi/strapi'

const MODULE_ID = 'api::notification.notification'
const GLOBAL_MODULE_ID = 'api::notifications-consumer.notifications-consumer'
const SINGLETON_ID = 1

export default factories.createCoreController(MODULE_ID, ({ strapi }) => {
return {
Expand All @@ -17,44 +15,10 @@ export default factories.createCoreController(MODULE_ID, ({ strapi }) => {
},

async getPushNotifications() {
const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, {
populate: ['id', 'lastConsumedNotificationDate']
})
const service = strapi.service(MODULE_ID)
const notifications = service.getPushNotifications()

const lastConsumedNotificationDate = global?.lastConsumedNotificationDate

const notifications = await strapi.entityService.findMany(
MODULE_ID,
{
limit: 200,
filters: {
notification_template: { push: true },
...(lastConsumedNotificationDate ? {
createdAt: {$gt: lastConsumedNotificationDate}
} : undefined)
},
populate: {
notification_template: {
fields: ['id', 'title', 'description', 'url', 'push'],
populate: {
thumbnail: {
fields: ['url']
}
}
}
}
}
)

if (notifications.length) {
await strapi.entityService.update(
GLOBAL_MODULE_ID,
SINGLETON_ID,
{
data: { lastConsumedNotificationDate: new Date() }
}
)
}
await service.updateLastConsumedNotificationDate()

return notifications
},
Expand Down
43 changes: 42 additions & 1 deletion src/api/notification/services/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import { factories } from '@strapi/strapi';

const MODULE_ID = 'api::notification.notification'
const GLOBAL_MODULE_ID = 'api::notifications-consumer.notifications-consumer'
const SINGLETON_ID = 1

export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
return {
Expand Down Expand Up @@ -41,10 +43,49 @@ export default factories.createCoreService(MODULE_ID, ({ strapi }) => {
thumbnail: notification.notification_template.thumbnail.url
}))
},
async getPushNotifications() {
const global = await strapi.entityService.findOne(GLOBAL_MODULE_ID, SINGLETON_ID, {
populate: ['id', 'lastConsumedNotificationDate']
})

const lastConsumedNotificationDate = global?.lastConsumedNotificationDate

return strapi.entityService.findMany(
MODULE_ID,
{
limit: 200,
filters: {
notification_template: { push: true },
...(lastConsumedNotificationDate ? {
createdAt: {$gt: lastConsumedNotificationDate}
} : undefined)
},
populate: {
notification_template: {
fields: ['id', 'title', 'description', 'url', 'push'],
populate: {
thumbnail: {
fields: ['url']
}
}
}
}
}
)
},
updateLastConsumedNotificationDate() {
return strapi.entityService.update(
GLOBAL_MODULE_ID,
SINGLETON_ID,
{
data: { lastConsumedNotificationDate: new Date() }
}
)
}
}
});

function templateNotification(description: string, data: {[key: string]: string}): string {
export function templateNotification(description: string, data: {[key: string]: string}): string {
let result = description

if (!data) return result
Expand Down
13 changes: 10 additions & 3 deletions src/api/telegram-subscription/controllers/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ export default factories.createCoreController(MODULE_ID, ({strapi}) => {
return true
},
async getSubscriptions(context) {
const { accounts } = context.query

const accountsArray = accounts ? accounts.split(',') : []

if (!accountsArray.length) return []

return strapi.service(MODULE_ID).getSubscriptions(accountsArray)
},
async getAccountSubscriptions(context) {
const account = context.params.account

return strapi.service(MODULE_ID).getAccountSubscriptions(account)
},
async sendNotifications(context) {
const account = context.params.account

return strapi.service(MODULE_ID).sendNotifications(account)
return strapi.service(MODULE_ID).sendNotifications()
}
}
});
13 changes: 11 additions & 2 deletions src/api/telegram-subscription/routes/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ const customRouter = (innerRouter, extraRoutes = []) => {
const myExtraRoutes = [
{
method: 'GET',
path: '/tg-subscriptions/:account',
path: '/tg-subscriptions',
handler: 'telegram-subscription.getSubscriptions',
config: {
policies: [],
middlewares: [],
},
},
{
method: 'GET',
path: '/tg-subscriptions/:account',
handler: 'telegram-subscription.getAccountSubscriptions',
config: {
policies: [],
middlewares: [],
},
},
{
method: 'POST',
path: '/add-tg-subscription',
Expand All @@ -40,7 +49,7 @@ const myExtraRoutes = [
},
{
method: 'GET',
path: '/send-tg-notifications/:account',
path: '/send-tg-notifications',
handler: 'telegram-subscription.sendNotifications',
config: {
policies: [],
Expand Down
22 changes: 18 additions & 4 deletions src/api/telegram-subscription/services/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { env } from '@strapi/utils'
import fetch from 'node-fetch'
import crypto from 'crypto'
import { TelegramData } from '../types'
import { templateNotification } from '../../notification/services/notification'

const MODULE_ID = 'api::telegram-subscription.telegram-subscription'

Expand Down Expand Up @@ -42,6 +43,19 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
}
})
},
async getSubscriptions(accounts: string[]) {
return strapi.entityService.findMany(
MODULE_ID,
{
filters: {
account: {
$in: accounts
}
},
fields: ['id', 'account', 'chat_id']
}
)
},
async getAccountSubscriptions(account: string) {
return strapi.entityService.findMany(
MODULE_ID,
Expand All @@ -54,12 +68,12 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
)
},
// TODO: temporary implementation
async sendNotifications(account: string): Promise<number> {
const notifications = await strapi.service('api::notification.notification').getNotificationList(account, true)
async sendNotifications(): Promise<number> {
const notifications = await strapi.service('api::notification.notification').getPushNotifications()

if (notifications.length === 0) return 0

const subscriptions = await this.getAccountSubscriptions(account)
const subscriptions = await this.getSubscriptions(notifications.map(n => n.account))

const requests = subscriptions.map(subscription => {
return notifications.map(notification => {
Expand All @@ -70,7 +84,7 @@ export default factories.createCoreService(MODULE_ID, ({strapi}) => {
},
body: JSON.stringify({
chat_id: subscription.chat_id,
text: notification.description
text: templateNotification(notification.notification_template.description, notification.data)
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "2024-05-08T15:28:37.728Z"
"x-generation-date": "2024-05-09T06:57:04.271Z"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down