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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ data/*
# IDE
############################
.vscode
.worktrees/
.strapi/
11 changes: 3 additions & 8 deletions src/api/resource/documentation/1.0.0/resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,12 @@
{
"name": "fields",
"in": "query",
"description": "Fields to return (ex: fields[]=slug&fields[]=campaign)",
"description": "Fields to return (ex: title,author)",
"deprecated": false,
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
}
},
"style": "form",
"explode": true
"type": "string"
}
},
{
"name": "populate",
Expand Down
96 changes: 36 additions & 60 deletions src/api/telegram-subscription/controllers/telegram-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,64 @@

import { factories } from '@strapi/strapi'
import { errors } from '@strapi/utils'
import { TelegramData } from '../types'

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

export default factories.createCoreController(MODULE_ID, ({strapi}) => {
return {
async addSubscription(context) {
const {account, data} : { account: string, data: TelegramData } = context.request.body

const service = strapi.service(MODULE_ID)

const isAlreadySubscribed = await this.checkSubscription(context)
async getSubscriptions(context) {
const { accounts } = context.query

if (isAlreadySubscribed) {
return true
}
const accountsArray = accounts ? accounts.split(',') : []

await service.addSubscription(account, data)
if (!accountsArray.length) return []

return true
return strapi.service(MODULE_ID).getSubscriptions(accountsArray)
},
async removeSubscription(context) {
const {account} : { account: string, data: TelegramData } = context.request.body

const service = strapi.service(MODULE_ID)

const isAlreadySubscribed = await this.checkSubscription(context)
async getAccountSubscriptions(context) {
const account = context.params.account

if (!isAlreadySubscribed) {
return true
return strapi.service(MODULE_ID).getAccountSubscriptions(account)
},
async linkViaBot(context) {
const { account, chatId, firstName, username } = context.request.body as {
account?: string
chatId?: number
firstName?: string
username?: string
}

await service.removeSubscriptions(account)

return true
},
async checkSubscription(context) {
const {account, data} : { account: string, data: TelegramData } = context.request.body
if (!account || !chatId) {
throw new errors.ValidationError('account and chatId are required')
}

const service = strapi.service(MODULE_ID)

const result = await service.verifyTgAuthentication(data)

/**
* Verify if the Telegram authentication data is valid
* Which proves that the data belongs to the user
*/
if (!result) {
throw new errors.ValidationError('Invalid telegram authentication data')
}
await service.linkSubscriptionViaBot(account, { chatId, firstName, username })

const existing = await strapi.entityService.findMany(MODULE_ID, {
filters: {
account: {
$eqi: account
}, chatId: data.id
}
})

/**
* We will only return true if the subscription belongs to the Telegram owner
* And the account is already subscribed
* So, it's not possible to check another account's subscription without owning Telegram account
*/
return existing.length > 0
return { success: true }
},
async getSubscriptions(context) {
const { accounts } = context.query
async unlinkViaBot(context) {
const { account } = context.request.body as { account?: string }

const accountsArray = accounts ? accounts.split(',') : []
if (!account) {
throw new errors.ValidationError('account is required')
}

if (!accountsArray.length) return []
const service = strapi.service(MODULE_ID)

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

return strapi.service(MODULE_ID).getAccountSubscriptions(account)
return { success: true }
},
async sendNotifications(context) {
return strapi.service(MODULE_ID).sendNotifications()
async getAccountsByChatViaBot(context) {
const { chatId } = context.request.body as { chatId?: number }

if (!chatId) {
throw new errors.ValidationError('chatId is required')
}

return strapi.service(MODULE_ID).getAccountsByChatId(chatId)
}
}
});
Loading
Loading