Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: notify admin to update solvers in the community #618

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions bot/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
attemptCommunitiesPendingPayments,
deleteCommunity,
nodeInfo,
checkSolvers,
} from '../jobs';
import { logger } from "../logger";
import { ICommunity, IUsernameId } from '../models/community';
Expand Down Expand Up @@ -199,6 +200,10 @@ const initialize = (botToken: string, options: Partial<Telegraf.Options<MainCont
await nodeInfo(bot);
});

schedule.scheduleJob(`0 0 * * *`, async () => {
await checkSolvers(bot);
});

bot.start(async (ctx: MainContext) => {
try {
if (!('message' in ctx.update) || !('text' in ctx.update.message)){
Expand Down
49 changes: 49 additions & 0 deletions jobs/check_solvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Telegraf } from 'telegraf';
import { MainContext } from '../bot/start';

import { Community, User, } from '../models';
import { ICommunity } from '../models/community';
import { logger } from '../logger';

const MAX_MESSAGES = 5; // Number of messages before disabling the community

const checkSolvers = async (bot: Telegraf<MainContext>) => {
try {
const communities = await Community.find({ is_disabled: false });

for (const community of communities) {
if (community.solvers.length > 0) {
continue;
} else {
await notifyAdmin(community, bot);
}
}
} catch (error) {
const message = String(error);
logger.error(`checkSolvers catch error: ${message}`);
}
};

const notifyAdmin = async (community: ICommunity, bot: Telegraf<MainContext>) => {
community.messages_sent_count += 1;
// The community is disabled if the admin has received the maximum notification message to add a solver
if (community.messages_sent_count >= MAX_MESSAGES) {
community.is_disabled = true;
await community.save();

logger.info(`Community: ${community.name} has been disabled due to lack of solvers.`);
return;
}

await community.save();
const admin = await User.findOne({ tg_id: community.creator_id });

if (admin) {
await bot.telegram.sendMessage(
admin.tg_id,
`Your community ${community.name} doesn't have any solvers. Please add at least one solver to avoid being disabled.`
);
}
}

export default checkSolvers;
2 changes: 2 additions & 0 deletions jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import deleteOrders from "./delete_published_orders";
import calculateEarnings from './calculate_community_earnings'
import deleteCommunity from './communities'
import nodeInfo from './node_info'
import checkSolvers from "./check_solvers";

export {
attemptPendingPayments,
Expand All @@ -16,4 +17,5 @@ export {
attemptCommunitiesPendingPayments,
deleteCommunity,
nodeInfo,
checkSolvers,
};
4 changes: 4 additions & 0 deletions models/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface ICommunity extends Document {
currencies: Array<string>;
created_at: Date;
nostr_public_key: string;
messages_sent_count: number;
is_disabled: boolean;
}

const CommunitySchema = new Schema<ICommunity>({
Expand Down Expand Up @@ -81,6 +83,8 @@ const CommunitySchema = new Schema<ICommunity>({
},
created_at: { type: Date, default: Date.now },
nostr_public_key: { type: String },
messages_sent_count: { type: Number, default: 0 },
is_disabled: { type: Boolean, default: false },
});


Expand Down