temporary invite

This commit is contained in:
xiaomlove
2022-12-13 13:51:39 +08:00
parent 6fa604ce56
commit f413c61901
42 changed files with 727 additions and 47 deletions
+5
View File
@@ -153,6 +153,11 @@ class ClaimRepository extends BaseRepository
public function settleUser($uid, $force = false, $test = false): bool
{
$shouldDoSettle = apply_filter('user_should_do_claim_settle', true, $uid);
if (!$shouldDoSettle) {
do_log("uid: $uid, filter: user_should_do_claim_settle => false");
return false;
}
$user = User::query()->with('language')->findOrFail($uid);
$list = Claim::query()->where('uid', $uid)->with(['snatch', 'torrent' => fn ($query) => $query->select(Torrent::$commentFields)])->get();
$now = Carbon::now();
+23
View File
@@ -1,6 +1,7 @@
<?php
namespace App\Repositories;
use App\Models\Invite;
use App\Models\Message;
use App\Models\News;
use App\Models\Poll;
@@ -11,6 +12,7 @@ use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Nexus\Database\NexusDB;
use Nexus\Plugin\Plugin;
use NexusPlugin\Permission\PermissionRepository;
@@ -416,4 +418,25 @@ class ToolRepository extends BaseRepository
$uidPermissionsCached[$uid] = $result;
return $result;
}
public function generateUniqueInviteHash(array $hashArr, int $total, int $left, int $deep = 0): array
{
do_log("total: $total, left: $left, deep: $deep");
if ($deep > 10) {
throw new \RuntimeException("deep: $deep > 10");
}
if (count($hashArr) >= $total) {
return array_slice(array_values($hashArr), 0, $total);
}
for ($i = 0; $i < $left; $i++) {
$hash = Str::random(32);
$hashArr[$hash] = $hash;
}
$exists = Invite::query()->whereIn('hash', array_values($hashArr))->get(['id', 'hash']);
foreach($exists as $value) {
unset($hashArr[$value->hash]);
}
return $this->generateUniqueInviteHash($hashArr, $total, $total - count($hashArr), ++$deep);
}
}
+78
View File
@@ -6,6 +6,7 @@ use App\Exceptions\NexusException;
use App\Http\Resources\ExamUserResource;
use App\Http\Resources\UserResource;
use App\Models\ExamUser;
use App\Models\Invite;
use App\Models\Message;
use App\Models\Setting;
use App\Models\User;
@@ -566,4 +567,81 @@ class UserRepository extends BaseRepository
return true;
}
public function addTemporaryInvite(User $operator, int $uid, string $action, int $count, int|null $days, string|null $reason = '')
{
do_log("uid: $uid, action: $action, count: $count, days: $days, reason: $reason");
$action = strtolower($action);
if ($count <= 0 || ($action == 'increment' && $days <= 0)) {
throw new \InvalidArgumentException("days or count lte 0");
}
$targetUser = User::query()->findOrFail($uid, User::$commonFields);
$this->checkPermission($operator, $targetUser);
$toolRep = new ToolRepository();
$locale = $targetUser->locale;
$changeType = nexus_trans("nexus.$action", [], $locale);
$subject = nexus_trans('message.temporary_invite_change.subject', ['change_type' => $changeType], $locale);
$body = nexus_trans('message.temporary_invite_change.body', [
'change_type' => $changeType,
'count' => $count,
'operator' => $operator->username,
'reason' => $reason,
], $locale);
$message = [
'sender' => 0,
'receiver' => $targetUser->id,
'subject' => $subject,
'msg' => $body,
'added' => Carbon::now(),
];
$inviteData = [];
if ($action == 'increment') {
$hashArr = $toolRep->generateUniqueInviteHash([], $count, $count);
foreach ($hashArr as $hash) {
$inviteData[] = [
'inviter' => $uid,
'invitee' => '',
'hash' => $hash,
'valid' => 0,
'expired_at' => Carbon::now()->addDays($days),
'created_at' => Carbon::now(),
];
}
}
NexusDB::transaction(function () use ($uid, $message, $inviteData, $count) {
if (!empty($inviteData)) {
Invite::query()->insert($inviteData);
do_log("[INSERT TEMPORARY INVITE] to $uid, count: $count");
} else {
Invite::query()->where('inviter', $uid)
->where('invitee', '')
->orderBy('expired_at', 'asc')
->limit($count)
->delete()
;
do_log("[DELETE TEMPORARY INVITE] of $uid, count: $count");
}
Message::add($message);
});
return true;
}
public function getInviteBtnText(int $uid)
{
if (Setting::get('main.invitesystem') != 'yes') {
throw new NexusException(nexus_trans('invite.send_deny_reasons.invite_system_closed'));
}
$permission = 'sendinvite';
if (!user_can($permission, false, $uid)) {
$requireClass = get_setting("authority.$permission");
throw new NexusException(nexus_trans('invite.send_deny_reasons.no_permission', ['class' => User::getClassText($requireClass)]));
}
$userInfo = User::query()->findOrFail($uid, User::$commonFields);
$temporaryInviteCount = $userInfo->temporary_invites()->count();
if ($userInfo->invites + $temporaryInviteCount < 1) {
throw new NexusException(nexus_trans('invite.send_deny_reasons.invite_not_enough'));
}
return nexus_trans('invite.send_allow_text');
}
}