Skip to content

Commit

Permalink
Added the fedban.
Browse files Browse the repository at this point in the history
  • Loading branch information
mleandrojr committed Jul 5, 2023
1 parent b84d5ed commit 25f57b9
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/action/AdaShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default class AdaShield extends Action {
.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
2 changes: 1 addition & 1 deletion src/action/AskToAsk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class AskToAsk extends Action {
this.context.chat.getId()
);

if (!chat.warn_ask_to_ask) {
if (!chat?.warn_ask_to_ask) {
return;
}
if (this.context.message.getReplyToMessage()) {
Expand Down
4 changes: 2 additions & 2 deletions src/action/SaveMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export default class saveUserAndChat extends Action {
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
2 changes: 1 addition & 1 deletion src/action/SaveUserAndChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default class saveUserAndChat extends Action {
const userId = user === null ? await UserHelper.createUser(contextUser) : user.id;

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

UserHelper.updateUser(contextUser);
ChatHelper.updateChat(this.context.chat);
Expand Down
2 changes: 1 addition & 1 deletion src/command/Ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class Ask extends Command {
this.context.message.delete();

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

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage) {
Expand Down
193 changes: 193 additions & 0 deletions src/command/federation/Ban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Ada Lovelace Telegram Bot
*
* This file is part of Ada Lovelace Telegram Bot.
* You are free to modify and share this project or its files.
*
* @package mslovelace_bot
* @author Marcos Leandro <mleandrojr@yggdrasill.com.br>
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.en.html>
*/

import Federation from "./Federation.js";
import Context from "../../library/telegram/context/Context.js";
import Message from "../../library/telegram/context/Message.js";
import User from "../../library/telegram/context/User.js";
import UserHelper from "../../helper/User.js";
import ChatHelper from "../../helper/Chat.js";
import FederationHelper from "../../helper/Federation.js";
import Lang from "../../helper/Lang.js";
import Log from "../../helper/Log.js";
import Bans from "../../model/Bans.js";

export default class Ban extends Federation {

/**
* The constructor.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param app App instance.
*/
public constructor(context: Context) {
super(context);
this.setCommands(["fban"]);
}

/**
* Bans an user in the federation.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @return
*/
private async ban(): Promise<void> {

if (!await this.context.user.isAdmin()) {
return;
}

const isUserAdmin = await FederationHelper.isUserAdmin(Number(this.user!.id), this.federation!);
if (!isUserAdmin) {
this.context.message.reply(Lang.get("fedBanOnlyAdminError"));
return;
}

this.context.message.delete();
let params = this.command!.getParams() || [];

const replyToMessage = this.context.message.getReplyToMessage();
if (replyToMessage) {
this.banByReply(replyToMessage!, params.join(" ").trim());
return;
}

const mentions = await this.context.message.getMentions();
if (mentions.length) {
params = params.filter((param) => param.indexOf("@") !== 0);
mentions.forEach((mention) => {
this.banByMention(mention, params.join(" ").trim());
});
}

const userId = parseInt(params[0]);
if (userId === Number(params[0])) {
params.shift();
this.banByUserId(userId, params.join(" ").trim());
}
}

/**
* Bans an user by message reply.
*
* @author Marcos Leandro
* @since 2023-06-07
*
* @return void
*/
private async banByReply(replyToMessage: Message, reason: string): Promise<void> {

const user = UserHelper.getByTelegramId(replyToMessage.getUser().getId());
const federationChats = await FederationHelper.getChats(this.federation!);

for (const chat of federationChats) {
const context = this.getContext(user, chat);
this.saveBan(context, reason);
context.user.ban();
}
}

/**
* Bans an user by mention reply.
*
* @author Marcos Leandro
* @since 2023-06-07
*
* @return void
*/
private async banByMention(mention: User, reason: string): Promise<void> {

const user = await UserHelper.getByTelegramId(mention.getId());
const federationChats = await FederationHelper.getChats(this.federation!);

for (const chat of federationChats) {
const context = this.getContext(user, chat);
this.saveBan(context, reason);
context.user.ban();
}
}

/**
* Bans the user by Telegram ID.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param userId
* @param reason
*
* @return {Promise<Record<string, any>|undefined>}
*/
private async banByUserId(userId: number, reason: string): Promise<void> {

const user = await UserHelper.getByTelegramId(userId);
const federationChats = await FederationHelper.getChats(this.federation!);

for (const chat of federationChats) {
const context = this.getContext(user, chat);
this.saveBan(context, reason);
context.user.ban();
}
}

/**
* Saves the ban.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param context
* @param reason
*
* @return void
*/
private async saveBan(context: Context, reason: string): Promise<void> {

const user = await UserHelper.getByTelegramId(context.user.getId());
const chat = await ChatHelper.getByTelegramId(context.chat.getId());

if (!user || !chat) {
return;
}

Lang.set(chat.language || "us");

const ban = new Bans();
const insert = ban.insert();
insert
.set("user_id", user.id)
.set("chat_id", chat.id)
.set("federation_id", chat.federation_id)
.set("date", Math.floor(Date.now() / 1000));

if (reason.length) {
insert.set("reason", reason);
}

try {

await ban.execute();
const message = Lang.get("fedBannedMessage")
.replace("{userId}", context.user.getId())
.replace("{username}", context.user.getFirstName() || context.user.getUsername())
.replace("{reason}", reason.length ? reason : "Unknown");

this.context.chat.sendMessage(message, { parseMode: "HTML" });

} catch (err: any) {
Log.error(err.toString());
}
}
}
63 changes: 62 additions & 1 deletion src/command/federation/Federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import Context from "../../library/telegram/context/Context.js";
import CommandContext from "../../library/telegram/context/Command.js";
import Lang from "../../helper/Lang.js";
import UserHelper from "../../helper/User.js";
import FederationHelper from "../../helper/Federation.js";
import { Message as MessageType } from "../../library/telegram/type/Message.js";
import { Chat as ChatType } from "../../library/telegram/type/Chat.js";
import { User as UserType } from "../../library/telegram/type/User.js";

export default class Federation extends Command {

Expand All @@ -34,6 +38,14 @@ export default class Federation extends Command {
*/
protected chat?: Record<string, any>;

/**
* Federation object.
*
* @author Marcos Leandro
* @since 2023-07-04
*/
protected federation?: Record<string, any>;

/**
* Command context.
*
Expand Down Expand Up @@ -71,10 +83,59 @@ export default class Federation extends Command {
return;
}

if (!this.chat?.federation_id) {
return;
}

this.federation = await FederationHelper.getById(Number(this.chat?.federation_id));
if (!this.federation) {
return;
}

Lang.set(this.chat!.language || "us");

this.command = command;
const action = this.command.getCommand().substring(1);
const action = this.command!.getCommand().substring(1);
this[action as keyof typeof Federation.prototype](true as never);
}

/**
* Creates a context.
*
* @author Marcos Leandro
* @since 2023-07-04
*
* @param user
* @param chat
*
* @return {Message}
*/
protected getContext(user: Record<string, any>, chat: Record<string, any>): Context {

const userType: UserType = {
id: user.user_id,
isBot: user.is_bot,
firstName: user.first_name,
lastName: user.last_name,
username: user.username
};

const chatType: ChatType = {
id: chat.chat_id,
type: chat.type,
title: chat.title,
username: chat.username,
firstName: chat.first_name,
lastName: chat.last_name
};

const messageType: MessageType = {
messageId: 0,
from: userType,
chat: chatType,
date: Math.floor(Date.now() / 1000)
};

return new Context({ message: messageType });
}
}
8 changes: 6 additions & 2 deletions src/command/federation/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export default class User extends Federation {
this.setCommands([
"fpromote",
"fdemote",
"fban",
"funban"
]);
}

private async promote(): Promise<void> {
}

private async demote(): Promise<void> {
}
}
4 changes: 2 additions & 2 deletions src/config/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import AdaShield from "../command/AdaShield.js";
import Ask from "../command/Ask.js";
import Ban from "../command/Ban.js";
import Federation from "../command/federation/Federation.js";
import FederationBan from "../command/federation/Ban.js";
import FederationGroup from "../command/federation/Group.js";
import FederationManage from "../command/federation/Manage.js";
import FederationUser from "../command/federation/User.js";
Expand All @@ -31,7 +31,7 @@ export const commands = [
AdaShield,
Ask,
Ban,
Federation,
FederationBan,
FederationGroup,
FederationManage,
FederationUser,
Expand Down
Loading

0 comments on commit 25f57b9

Please sign in to comment.