Skip to content

Commit

Permalink
enable 1country subdomain activation feature for localuse
Browse files Browse the repository at this point in the history
  • Loading branch information
fegloff committed Sep 13, 2024
1 parent 02af912 commit 63eb084
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
27 changes: 22 additions & 5 deletions src/modules/1country/api/relayApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios'
import axios, { AxiosError } from 'axios'

import config from '../../../config'

Expand All @@ -8,7 +8,7 @@ const base = axios.create({
})

export const relayApi = (): {
enableSubdomains: (domainName: string) => Promise<void>
enableSubdomains: (domainName: string) => Promise<boolean>
checkDomain: ({ sld }: { sld: string }) => Promise<{ isAvailable: any, renewPrice: any, responseText: any, isReserved: any, isRegistered: any, error: string, regPrice: any, restorePrice: any, transferPrice: any } | { error: string }>
createCert: ({
domain,
Expand All @@ -18,9 +18,26 @@ export const relayApi = (): {
genNFT: ({ domain }: { domain: string }) => Promise<{ metadata: any, generated: any }>
} => {
return {
enableSubdomains: async (domainName: string) => {
const { data } = await base.post('/enable-subdomains', { domain: `${domainName}${config.country.tld}` })
console.log('enableSubdomains', data)
enableSubdomains: async (domainName: string): Promise<boolean> => {
try {
const { data } = await base.post('dns/enable-subdomains', { domain: `${domainName}${config.country.tld}` })
console.log('enableSubdomains', data)
return true
} catch (e) {
if (e instanceof AxiosError) {
if (e.code === AxiosError.ERR_BAD_REQUEST) {
const data = e.response?.data
if (data && data.error === 'already enabled') {
console.log('Axios response:', data.error)
return true
}
}
console.log('Axios Error:', e.message)
return false
}
console.log(e)
return false
}
},
checkDomain: async ({ sld }: { sld: string }) => {
try {
Expand Down
56 changes: 55 additions & 1 deletion src/modules/1country/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export enum SupportedCommands {
check = 'check',
cert = 'cert',
nft = 'nft',
set = 'set'
set = 'set',
subdomain = 'subdomain'
}

const COUNTRY_PREFIX_LIST = ['+', '%']
Expand Down Expand Up @@ -113,6 +114,11 @@ export class OneCountryBot implements PayableBot {
return
}

if (ctx.hasCommand(SupportedCommands.subdomain)) {
await this.onEnableSubdomain(ctx)
return
}

// if (ctx.hasCommand(SupportedCommands.RENEW)) {
// this.onRenewCmd(ctx);
// return;
Expand Down Expand Up @@ -385,6 +391,54 @@ export class OneCountryBot implements PayableBot {
}
}

onEnableSubdomain = async (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> => {
try {
if (this.botSuspended) {
ctx.transient.analytics.sessionState = RequestState.Error
await sendMessage(ctx, 'The bot is suspended').catch(async (e) => {
await this.onError(ctx, e)
})
ctx.transient.analytics.actualResponseTime = now()
return
}
if (!ctx.match) {
await ctx.reply(appText.setParameterError, {
message_thread_id: ctx.message?.message_thread_id,
parse_mode: 'Markdown'
}).catch(async (e) => {
await this.onError(ctx, e)
})
return
}
const params = (ctx.match as string).split(' ')
const [domain] = params
const isDomain = await isDomainAvailable(domain)
if (isDomain.isAvailable) {
await ctx.reply(`The domain ${domain} doesn't exist`, {
message_thread_id: ctx.message?.message_thread_id,
parse_mode: 'Markdown'
}).catch(async (e) => {
await this.onError(ctx, e)
})
ctx.transient.analytics.actualResponseTime = now()
ctx.transient.analytics.sessionState = RequestState.Error
return
}
const response = await relayApi().enableSubdomains(domain)
if (response) {
await ctx.reply('Subdomain feature enabled')
} else {
await ctx.reply('Failed to enable subdomain feature')
}
ctx.transient.analytics.actualResponseTime = now()
ctx.transient.analytics.sessionState = RequestState.Success
} catch (e) {
ctx.transient.analytics.sessionState = RequestState.Error
ctx.transient.analytics.actualResponseTime = now()
await this.onError(ctx, e)
}
}

onCertCmd = async (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> => {
if (await isAdmin(ctx, false, true)) {
if (!ctx.match) {
Expand Down

0 comments on commit 63eb084

Please sign in to comment.