Skip to content

Commit

Permalink
Merge pull request #329 from n0th1ng-else/types
Browse files Browse the repository at this point in the history
  • Loading branch information
n0th1ng-else authored Jun 21, 2023
2 parents 88e4a76 + 4930082 commit e85179b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ NEW_RELIC_NO_CONFIG_FILE=true
NEW_RELIC_APP_NAME=voicetotext-local
NEW_RELIC_LOG=stdout
NEW_RELIC_LICENSE_KEY=
STRIPE_TOKEN=
SENTRY_DSN=
18 changes: 5 additions & 13 deletions e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ export class TelegramMessageModel {
prefixId: string
): this {
this.messageId = messageId;
const data = new TelegramButtonModel<LanguageCode>(
TelegramButtonType.Language,
langId,
prefixId
);
const data = new TelegramButtonModel<LanguageCode>("l", langId, prefixId);

this.callbackData = data.getDtoString();
return this;
Expand All @@ -113,11 +109,7 @@ export class TelegramMessageModel {
prefixId: string
): this {
this.messageId = messageId;
const data = new TelegramButtonModel(
TelegramButtonType.Donation,
String(price),
prefixId
);
const data = new TelegramButtonModel("d", String(price), prefixId);

this.callbackData = data.getDtoString();
return this;
Expand Down Expand Up @@ -195,7 +187,7 @@ export class TelegramMessageMetaItem {
public readonly type: TelegramMessageMetaType,
public readonly title: LabelId | string,
public readonly data: string,
public readonly btnType = TelegramButtonType.Donation
public readonly btnType: TelegramButtonType = "d"
) {}
}

Expand Down Expand Up @@ -251,15 +243,15 @@ export const getLangButtons = (): TelegramMessageMetaItem[][] => {
TelegramMessageMetaType.Button,
LabelId.BtnRussian,
"ru-RU",
TelegramButtonType.Language
"l"
),
],
[
new TelegramMessageMetaItem(
TelegramMessageMetaType.Button,
LabelId.BtnEnglish,
"en-US",
TelegramButtonType.Language
"l"
),
],
];
Expand Down
3 changes: 1 addition & 2 deletions src/telegram/actions/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
BotCommand,
BotMessageModel,
TelegramButtonModel,
TelegramButtonType,
TelegramMessagePrefix,
} from "../types.js";
import { getDonationDtoString, isFundMessage } from "../helpers.js";
Expand Down Expand Up @@ -165,7 +164,7 @@ export class FundAction extends GenericAction {
return {
text: TextModel.toCurrency(price, emoji),
callback_data: new TelegramButtonModel(
TelegramButtonType.Donation,
"d",
`${price}`,
logId
).getDtoString(),
Expand Down
6 changes: 3 additions & 3 deletions src/telegram/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DbClient } from "../../db/index.js";
import { PaymentService } from "../../donate/types.js";
import { TgCallbackQuery, TgCheckoutQuery } from "../api/types.js";
import { AnalyticsData } from "../../analytics/ga/types.js";
import { TelegramButtonModel, TelegramButtonType } from "../types.js";
import { TelegramButtonModel } from "../types.js";
import { Logger } from "../../logger/index.js";
import { collectAnalytics } from "../../analytics/index.js";
import { CheckoutAction } from "./checkout.js";
Expand Down Expand Up @@ -77,9 +77,9 @@ export class BotActions {
const button = TelegramButtonModel.fromDto(data);

switch (button.id) {
case TelegramButtonType.Donation:
case "d":
return this.fund.runCallback(message, button, analytics);
case TelegramButtonType.Language:
case "l":
return this.lang.runCallback(message, button, analytics, msg);
default:
throw new Error("Unknown type passed in callback query");
Expand Down
13 changes: 2 additions & 11 deletions src/telegram/actions/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
BotLangData,
BotMessageModel,
TelegramButtonModel,
TelegramButtonType,
TelegramMessagePrefix,
} from "../types.js";
import {
Expand Down Expand Up @@ -170,16 +169,8 @@ export class LangAction extends GenericAction {

return this.getChatLanguage(model, prefix)
.then((lang) => {
const EnData = new TelegramButtonModel(
TelegramButtonType.Language,
"en-US",
prefix.id
);
const RuData = new TelegramButtonModel(
TelegramButtonType.Language,
"ru-RU",
prefix.id
);
const EnData = new TelegramButtonModel("l", "en-US", prefix.id);
const RuData = new TelegramButtonModel("l", "ru-RU", prefix.id);

return this.sendMessage(
model.id,
Expand Down
17 changes: 3 additions & 14 deletions src/telegram/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
BotMessageModel,
DonationDto,
DonationPayload,
TelegramButtonType,
DonationSchema,
VoiceContentReason,
VoiceContentReasonModel,
} from "./types.js";
Expand Down Expand Up @@ -196,17 +196,6 @@ export const getLanguageByText = (
}
};

export const getButtonTypeByText = (type: string): TelegramButtonType => {
switch (type) {
case TelegramButtonType.Donation:
return TelegramButtonType.Donation;
case TelegramButtonType.Language:
return TelegramButtonType.Language;
default:
return TelegramButtonType.Unknown;
}
};

export const getDonationDtoString = (
donationId: number,
chatId: number,
Expand All @@ -221,9 +210,9 @@ export const getDonationDtoString = (
return JSON.stringify(dto);
};

export const parseDonationPayload = (text = ""): DonationPayload => {
export const parseDonationPayload = (dtoString = ""): DonationPayload => {
try {
const obj: DonationDto = JSON.parse(text);
const obj = DonationSchema.parse(JSON.parse(dtoString));
return {
donationId: obj.d || 0,
chatId: obj.c || 0,
Expand Down
57 changes: 36 additions & 21 deletions src/telegram/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import { nanoid } from "nanoid";
import {
getButtonTypeByText,
getChatId,
parseDonationPayload,
getFullUserName,
Expand All @@ -13,7 +14,6 @@ import {
isVideoMessage,
} from "./helpers.js";
import { LanguageCode } from "../recognition/types.js";
import { nanoid } from "nanoid";
import { Logger } from "../logger/index.js";
import { MenuLabel } from "../text/labels.js";
import { TextModel } from "../text/index.js";
Expand Down Expand Up @@ -115,17 +115,32 @@ export class BotCommandOption {
}
}

export enum TelegramButtonType {
Donation = "d",
Language = "l",
Unknown = "u",
}
const TelegramButtonTypeSchema = z
.union([z.literal("d"), z.literal("l"), z.literal("u")])
.describe("Button type schema. d is Donation, l is Language, u is Unknown");

export type TelegramButtonType = z.infer<typeof TelegramButtonTypeSchema>;

const ButtonSchema = z
.object({
i: TelegramButtonTypeSchema,
l: z.string(),
v: z.string(),
})
.describe(
"Button schema used in Telegram callback. i is Button Type, l is Log prefix, v is Value"
);

export type BotButtonDto = z.infer<typeof ButtonSchema>;

export class TelegramButtonModel<V extends string = string> {
public static fromDto(dtoString: string): TelegramButtonModel {
const dto: BotButtonDto = JSON.parse(dtoString);
const type = getButtonTypeByText(dto.i);
return new TelegramButtonModel(type, dto.v, dto.l);
try {
const obj = ButtonSchema.parse(JSON.parse(dtoString));
return new TelegramButtonModel(obj.i, obj.v, obj.l);
} catch (err) {
return new TelegramButtonModel("u", "", "");
}
}

constructor(
Expand All @@ -145,20 +160,20 @@ export class TelegramButtonModel<V extends string = string> {
}
}

interface BotButtonDto {
i: string; // type Identifier
l: string; // log prefix
v: string; // value
}

export interface DonationPayload {
donationId: number;
chatId: number;
prefix: string;
}

export interface DonationDto {
d: number; // donationId
c: number; // chatId
l: string; // log prefix
}
export const DonationSchema = z
.object({
d: z.number(),
c: z.number(),
l: z.string(),
})
.describe(
"Donation schema used in Telegram callback. d is DonationId, c is ChatId, l is Log prefix"
);

export type DonationDto = z.infer<typeof DonationSchema>;

0 comments on commit e85179b

Please sign in to comment.