Skip to content

Commit

Permalink
Merge pull request Desenvolvimento-de-Software#15 from mleandrojr/master
Browse files Browse the repository at this point in the history
Commands refactor.
  • Loading branch information
mleandrojr authored Aug 1, 2023
2 parents f4ea084 + 5739f49 commit cf81b47
Show file tree
Hide file tree
Showing 40 changed files with 1,487 additions and 159 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"ts-node": "^10.9.1",
"tsc-watch": "^4.5.0",
"tsconfig-paths": "^3.12.0",
"typescript": "^5.1.3"
"typescript": "^5.1.6"
}
}
51 changes: 51 additions & 0 deletions sql/update_2022-05-04_02.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
CREATE TABLE `ada`.`federations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`hash` varchar(32) NOT NULL,
`description` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_hash` (`hash`),
KEY `fk_federations_user_id_idx` (`user_id`),
CONSTRAINT `fk_federations_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4

ALTER TABLE `ada`.`chats`
ADD COLUMN `federation_id` INT UNSIGNED NULL AFTER `id`,
ADD INDEX `idx_federation_id` (`federation_id` ASC) VISIBLE;

ALTER TABLE `ada`.`chats`
ADD CONSTRAINT `fk_chats_federation_id`
FOREIGN KEY (`federation_id`)
REFERENCES `ada`.`federations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE `ada`.`bans`
ADD COLUMN `federation_id` INT UNSIGNED NULL AFTER `chat_id`,
ADD INDEX `fk_bans_federation_id_idx` (`federation_id` ASC) VISIBLE;

ALTER TABLE `ada`.`bans`
ADD CONSTRAINT `fk_bans_federation_id`
FOREIGN KEY (`federation_id`)
REFERENCES `ada`.`federations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

CREATE TABLE `ada`.`rel_users_federations` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`federation_id` INT UNSIGNED NOT NULL,
`date` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_uq_user_id_federation_id` (`user_id` ASC, `federation_id` ASC) VISIBLE,
INDEX `fk_rel_users_federations_federation_id_idx` (`federation_id` ASC) VISIBLE,
CONSTRAINT `fk_rel_users_federations_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ada`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_rel_users_federations_federation_id`
FOREIGN KEY (`federation_id`)
REFERENCES `ada`.`federations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
19 changes: 19 additions & 0 deletions sql/update_2023-07-04_01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE `ada`.`bans` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`chat_id` INT UNSIGNED NOT NULL,
`reason` VARCHAR(50) NULL,
`date` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_user_id_chat_id` (`chat_id` ASC, `user_id` ASC) VISIBLE,
INDEX `fk_bans_user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `fk_bans_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `ada`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_bans_chat_id`
FOREIGN KEY (`chat_id`)
REFERENCES `ada`.`chats` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
10 changes: 5 additions & 5 deletions src/action/AdaShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class AdaShield extends Action {
*
* @var string
*/
private banMessage: string = "adaShield";
private banMessage: string = "adaShieldMessage";

/**
* The constructor.
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class AdaShield extends Action {
.replace("{userid}", userId)
.replace("{username}", username);

this.context.chat.sendMessage(lang);
this.context.chat.sendMessage(lang, { parseMode: "HTML" });
await this.updateRelationship();
}

Expand Down Expand Up @@ -142,14 +142,14 @@ export default class AdaShield extends Action {
*/
private async updateRelationship(): Promise<void> {

const user = await UserHelper.getUserByTelegramId(this.context.newChatMember!.getId());
const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const user = await UserHelper.getByTelegramId(this.context.newChatMember!.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
const relUserChat = new RelUsersChats();
relUserChat
.update()
.set("joined", 0)
.where("user_id").equal(user.id)
.and("chat_id").equal(chat.id);
.and("chat_id").equal(chat!.id);

return relUserChat.execute();
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/AskToAsk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export default class AskToAsk extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

if (!chat.warn_ask_to_ask) {
if (!chat?.warn_ask_to_ask) {
return;
}
if (this.context.message.getReplyToMessage()) {
Expand Down
2 changes: 1 addition & 1 deletion src/action/Captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Captcha extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat?.id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/Greetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class Greetings extends Action {
return;
}

this.chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
this.chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!this.chat || !this.chat?.id) {
return;
}
Expand All @@ -81,7 +81,7 @@ export default class Greetings extends Action {
return;
}

this.user = await UserHelper.getUserByTelegramId(this.context.user.getId());
this.user = await UserHelper.getByTelegramId(this.context.user.getId());
if (!await this.isUserJoined()) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/action/LeftChatMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export default class LeftChatMember extends Action {
return;
}

const user = await UserHelper.getUserByTelegramId(
const user = await UserHelper.getByTelegramId(
this.context.user.getId()
);

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

Expand Down
2 changes: 1 addition & 1 deletion src/action/NewChatMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class NewChatMember extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat?.id) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/action/Ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Ping extends Action {
return;
}

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/action/SaveMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ export default class saveUserAndChat extends Action {
}

const contextUser = this.context.newChatMember || this.context.leftChatMember || this.context.user;
const user = await UserHelper.getUserByTelegramId(contextUser.getId());
const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const user = await UserHelper.getByTelegramId(contextUser.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());

switch (this.context.type) {

case "editedMessage":
this.updateMessage(user, chat);
this.updateMessage(user, chat!);
break;

default:
this.saveNewMessage(user, chat);
this.saveNewMessage(user, chat!);
}

return Promise.resolve();
Expand Down
6 changes: 3 additions & 3 deletions src/action/SaveUserAndChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export default class saveUserAndChat extends Action {
public async run(): Promise<void> {

const contextUser = this.context.newChatMember || this.context.leftChatMember || this.context.user;
const user = await UserHelper.getUserByTelegramId(contextUser.getId());
const user = await UserHelper.getByTelegramId(contextUser.getId());
const userId = user === null ? await UserHelper.createUser(contextUser) : user.id;

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chatId = chat === null ? await ChatHelper.createChat(this.context.chat) : chat.id;
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
const chatId = chat === null ? await ChatHelper.createChat(this.context.chat) : chat!.id;

UserHelper.updateUser(contextUser);
ChatHelper.updateChat(this.context.chat);
Expand Down
4 changes: 2 additions & 2 deletions src/callback/CaptchaConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export default class CaptchaConfirmation extends Callback {
return;
}

const user = await UserHelper.getUserByTelegramId(
const user = await UserHelper.getByTelegramId(
this.context.user.getId()
);

const chat = await ChatHelper.getChatByTelegramId(
const chat = await ChatHelper.getByTelegramId(
this.context.chat.getId()
);

Expand Down
4 changes: 2 additions & 2 deletions src/command/AdaShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class AdaShield extends Command {
*/
public async index(): Promise<void> {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class AdaShield extends Command {
*/
public async change(status: number) {

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
if (!chat || !chat.id) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/Ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export default class Ask extends Command {

this.context.message.delete();

const chat = await ChatHelper.getChatByTelegramId(this.context.chat.getId());
Lang.set(chat.language || "us");
const chat = await ChatHelper.getByTelegramId(this.context.chat.getId());
Lang.set(chat?.language || "us");

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage) {
Expand Down
Loading

0 comments on commit cf81b47

Please sign in to comment.