<commit_before>'use strict';

const { getUser } = require('../../stores/user');

const syncStatusHandler = (ctx, next) => {
	const { message } = ctx;
	const { new_chat_members } = message;

	new_chat_members.forEach(async newMember => {
		if (newMember.is_bot) {
			return null;
		}

		const dbUser = await getUser({ id: newMember.id });
		// this handler runs after messages/addUser
		// so, it should always be able to obtain the user from db
		console.assert(dbUser);

		switch (dbUser.status) {
		case 'admin':
			return ctx.telegram.promoteChatMember(ctx.chat.id, dbUser.id, {
				can_change_info: false,
				can_delete_messages: true,
				can_invite_users: true,
				can_pin_messages: true,
				can_promote_members: true,
				can_restrict_members: true,
			});
		case 'banned':
			return ctx.telegram.kickChatMember(ctx.chat.id, dbUser.id);
		case 'member':
			// do nothing
			return null;
		default:
			throw new Error(`Unexpected member status: ${dbUser.status}`);
		}
	});

	return next();
};

module.exports = syncStatusHandler;<commit_msg>don't let synced admins promote members<commit_after>'use strict';

const { getUser } = require('../../stores/user');

const syncStatusHandler = (ctx, next) => {
	const { message } = ctx;
	const { new_chat_members } = message;

	new_chat_members.forEach(async newMember => {
		if (newMember.is_bot) {
			return null;
		}

		const dbUser = await getUser({ id: newMember.id });
		// this handler runs after messages/addUser
		// so, it should always be able to obtain the user from db
		console.assert(dbUser);

		switch (dbUser.status) {
		case 'admin':
			return ctx.telegram.promoteChatMember(ctx.chat.id, dbUser.id, {
				can_change_info: false,
				can_delete_messages: true,
				can_invite_users: true,
				can_pin_messages: true,
				can_promote_members: false,
				can_restrict_members: true,
			});
		case 'banned':
			return ctx.telegram.kickChatMember(ctx.chat.id, dbUser.id);
		case 'member':
			// do nothing
			return null;
		default:
			throw new Error(`Unexpected member status: ${dbUser.status}`);
		}
	});

	return next();
};

module.exports = syncStatusHandler;
{'fraction_non_alphanumeric': 0.12916862376702679, 'fraction_numerical': 0.0, 'mean_word_length': 3.6710526315789473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3726852', 'n_tokens_mistral': 804, 'n_tokens_neox': 705, 'n_words': 176}