Skip to content

Commit

Permalink
bot group - membership - sync child groups
Browse files Browse the repository at this point in the history
  • Loading branch information
grandmotivator committed Jun 11, 2024
1 parent 570bd2a commit 577e3a1
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 10 deletions.
3 changes: 3 additions & 0 deletions messages/ru/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@
'Child groups' => 'Дочерние группы',
'Child groups associated with the group' => 'Дочерние группы, связанные с этой группой',
'Membership and verification dates are automatically copied to active child groups' => 'Даты членства и верификации автоматически копируются в активные дочерние группы',
'Membership information has been copied to child groups' => 'Информация о членстве скопирована в дочерние группы',
'No child groups' => 'Нет дочерних групп',
'list of premium members' => 'список премиум участников',
'premium members with intro' => 'премиум участники с интро',
'Note' => 'Заметка',
Expand All @@ -248,6 +250,7 @@
'Balance, price' => 'Баланс, цена',
'Tariff, days' => 'Тариф, дни',
'Balance, days' => 'Баланс, дни',
'No premium members' => 'Нет премиум участников',
'Select a currency in the group settings' => 'Выберите валюту в настройках группы',
'FAQ' => 'ЧАВО',
'Help Center' => 'Центр помощи',
Expand Down
1 change: 1 addition & 0 deletions modules/bot/components/helpers/Emoji.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ class Emoji
public const EYE = '👁‍🗨';
public const THANKS = '🙏';
public const SWITCH = '🔀';
public const SYNC = '🔂';
}
1 change: 1 addition & 0 deletions modules/bot/config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@
'pass_captcha',
'child_groups',
'set_child_group',
'sync_child_groups',
];
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function actionIndex($page = 1)
$chat = $this->getTelegramChat();

if ($chat->isMembershipOn()) {
$query = $chat->getPremiumChatMembers();
$query = $chat->getActivePremiumChatMembers();

$pagination = new Pagination([
'totalCount' => $query->count(),
Expand Down
2 changes: 1 addition & 1 deletion modules/bot/controllers/privates/GroupGuestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function actionPremiumMembers($id = null, $page = 1)

$this->getState()->clearInputRoute();

$query = $chat->getPremiumChatMembers();
$query = $chat->getActivePremiumChatMembers();

$pagination = new Pagination([
'totalCount' => $query->count(),
Expand Down
63 changes: 55 additions & 8 deletions modules/bot/controllers/privates/GroupMembershipController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,7 @@ public function actionMembers($id = null, $page = 1)
'id' => $chat->id,
]));

$query = ChatMember::find()
->where([
'chat_id' => $chat->id,
])
->andWhere([
'not', ['membership_date' => null],
])
$query = $chat->getPremiumChatMembers()
->orderBy([
'membership_date' => SORT_ASC,
'user_id' => SORT_ASC,
Expand Down Expand Up @@ -1015,7 +1009,7 @@ public function actionSetMemberTariffDaysBalance($id = null, $oid = null)
}

/**
* @param int|null $id Chat->id
* @param int|null $id Chat->id
* @param int $page
* @return array
*/
Expand Down Expand Up @@ -1078,6 +1072,12 @@ public function actionChildGroups($id = null, $page = 1)
]),
'text' => Emoji::BACK,
],
[
'callback_data' => self::createRoute('sync-child-groups', [
'id' => $chat->id,
]),
'text' => Emoji::SYNC,
],
[
'callback_data' => MenuController::createRoute(),
'text' => Emoji::MENU,
Expand Down Expand Up @@ -1133,4 +1133,51 @@ public function actionSetChildGroup($id = null, $cid = null)
'id' => $chat->id,
]);
}

/**
* @param int|null $id Chat->id
* @return array
*/
public function actionSyncChildGroups($id = null)
{
$chat = Yii::$app->cache->get('chat');

if (!$premiumChatMembers = $chat->premiumChatMembers) {
return $this->getResponseBuilder()
->answerCallbackQuery(
$this->render('alert-no-premium-members')
)
->build();
}

if (!$childChats = $chat->childGroups) {
return $this->getResponseBuilder()
->answerCallbackQuery(
$this->render('alert-no-child-groups')
)
->build();
}

$updatedCount = 0;

foreach ($childChats as $childChat) {
foreach ($premiumChatMembers as $premiumChatMember) {
if ($childChatMember = $childChat->getChatMemberByUserId($premiumChatMember->getUserId())) {
$childChatMember->membership_date = $premiumChatMember->membership_date;
$childChatMember->limiter_date = $premiumChatMember->limiter_date;
$childChatMember->save();

$updatedCount++;
}
}
}

return $this->getResponseBuilder()
->answerCallbackQuery(
$this->render('alert-sync-child-groups', [
'updatedCount' => $updatedCount,
])
)
->build();
}
}
8 changes: 8 additions & 0 deletions modules/bot/models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ public function getChatMemberCreator()
}

public function getPremiumChatMembers()
{
return $this->hasMany(ChatMember::class, ['chat_id' => 'id'])
->andWhere([
'not', [ChatMember::tableName() . '.membership_date' => null],
]);
}

public function getActivePremiumChatMembers()
{
$today = new DateTime('@' . (time() + ($this->timezone * 60)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= Yii::t('bot', 'No child groups');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= Yii::t('bot', 'No premium members');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= Yii::t('bot', 'Membership information has been copied to child groups'); ?>. <?= Yii::t('bot', 'Members') ?>: <?= $updatedCount ?><br/>

0 comments on commit 577e3a1

Please sign in to comment.