Skip to content

Commit

Permalink
v6.0.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Nov 15, 2024
1 parent 7ffd15c commit 8c70347
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 30 deletions.
143 changes: 133 additions & 10 deletions src/Midjourney.API/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
using MailKit.Search;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Caching.Memory;
using Midjourney.Infrastructure;
using Midjourney.Infrastructure.Data;
using Midjourney.Infrastructure.Dto;
using Midjourney.Infrastructure.LoadBalancer;
using Midjourney.Infrastructure.Models;
using Midjourney.Infrastructure.Services;
using Midjourney.Infrastructure.StandardTable;
using Midjourney.Infrastructure.Storage;
Expand Down Expand Up @@ -663,11 +665,30 @@ public async Task<Result> AccountAction(string id, [FromQuery] string customId,
[HttpPost("account")]
public Result AccountAdd([FromBody] DiscordAccountConfig accountConfig)
{
if (_isAnonymous)
var setting = GlobalConfiguration.Setting;
var user = _workContext.GetUser();

if (user == null)
{
return Result.Fail("演示模式,禁止操作");
}

if (!setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
return Result.Fail("未开启赞助功能,禁止操作");
}

// 同一个用户每天最多只能赞助 10 个账号
var limitKey = $"{DateTime.Now:yyyyMMdd}:sponsor:{user.Id}";
var sponsorCount = 0;
if (setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
if (_memoryCache.TryGetValue(limitKey, out sponsorCount) && sponsorCount > 10)
{
Result.Fail("每天最多只能赞助 10 个账号");
}
}

var model = DbHelper.AccountStore.GetCollection()
.Query()
.Where(c => c.ChannelId == accountConfig.ChannelId)
Expand All @@ -679,11 +700,39 @@ public Result AccountAdd([FromBody] DiscordAccountConfig accountConfig)
}

var account = DiscordAccount.Create(accountConfig);

// 赞助账号
if (account.IsSponsor)
{
account.SponsorUserId = user.Id;

// 赞助者禁止配置的选项
if (user.Role != EUserRole.ADMIN)
{
account.Sort = 0;
account.SubChannels.Clear();
account.WorkTime = null;
account.FishingTime = null;
}

// 赞助者参数校验
account.SponsorValidate();
}

DbHelper.AccountStore.Add(account);

// 后台执行
_ = _discordAccountInitializer.StartCheckAccount(account);


// 更新缓存
if (setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
sponsorCount++;

_memoryCache.Set(limitKey, sponsorCount, TimeSpan.FromDays(1));
}

return Result.Ok();
}

Expand All @@ -695,17 +744,47 @@ public Result AccountAdd([FromBody] DiscordAccountConfig accountConfig)
[HttpPut("account/{id}")]
public async Task<Result> AccountEdit([FromBody] DiscordAccount param)
{
if (_isAnonymous)
var setting = GlobalConfiguration.Setting;
var user = _workContext.GetUser();

if (user == null)
{
return Result.Fail("演示模式,禁止操作");
}

if (!setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
return Result.Fail("未开启赞助功能,禁止操作");
}

//if (_isAnonymous)
//{
// return Result.Fail("演示模式,禁止操作");
//}

var model = DbHelper.AccountStore.Get(param.Id);
if (model == null)
{
throw new LogicException("账号不存在");
}

if (user.Role != EUserRole.ADMIN && model.SponsorUserId != user.Id)
{
return Result.Fail("无权限操作");
}

// 赞助者禁止配置的选项
if (user.Role != EUserRole.ADMIN)
{
param.Sort = model.Sort;
param.SubChannels = model.SubChannels;
param.WorkTime = model.WorkTime;
param.FishingTime = model.WorkTime;

// 赞助者参数校验
param.SponsorValidate();
}

model.NijiBotChannelId = param.NijiBotChannelId;
model.PrivateChannelId = param.PrivateChannelId;
model.RemixAutoSubmit = param.RemixAutoSubmit;
Expand Down Expand Up @@ -742,22 +821,36 @@ public async Task<Result> AccountEdit([FromBody] DiscordAccount param)
[HttpPut("account-reconnect/{id}")]
public async Task<Result> AccountReconnect(string id, [FromBody] DiscordAccount param)
{
if (_isAnonymous)
if (id != param.Id)
{
throw new LogicException("参数错误");
}

var setting = GlobalConfiguration.Setting;
var user = _workContext.GetUser();

if (user == null)
{
return Result.Fail("演示模式,禁止操作");
}

if (id != param.Id)
if (!setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
throw new LogicException("参数错误");
return Result.Fail("未开启赞助功能,禁止操作");
}

var model = DbHelper.AccountStore.Get(param.Id);

var model = DbHelper.AccountStore.Get(id);
if (model == null)
{
throw new LogicException("账号不存在");
}

if (user.Role != EUserRole.ADMIN && model.SponsorUserId != user.Id)
{
return Result.Fail("无权限操作");
}

// 不可修改频道 ID
if (param.GuildId != model.GuildId || param.ChannelId != model.ChannelId)
{
Expand All @@ -776,11 +869,36 @@ public async Task<Result> AccountReconnect(string id, [FromBody] DiscordAccount
[HttpDelete("account/{id}")]
public Result AccountDelete(string id)
{
if (_isAnonymous)
var setting = GlobalConfiguration.Setting;
var user = _workContext.GetUser();

if (user == null)
{
return Result.Fail("演示模式,禁止操作");
}

if (!setting.EnableAccountSponsor && user.Role != EUserRole.ADMIN)
{
return Result.Fail("未开启赞助功能,禁止操作");
}


var model = DbHelper.AccountStore.Get(id);
if (model == null)
{
throw new LogicException("账号不存在");
}

if (user.Role != EUserRole.ADMIN && model.SponsorUserId != user.Id)
{
return Result.Fail("无权限操作");
}

//if (_isAnonymous)
//{
// return Result.Fail("演示模式,禁止操作");
//}

_discordAccountInitializer.DeleteAccount(id);

return Result.Ok();
Expand All @@ -793,6 +911,8 @@ public Result AccountDelete(string id)
[HttpGet("accounts")]
public ActionResult<List<DiscordAccount>> List()
{
var user = _workContext.GetUser();

var list = DbHelper.AccountStore.GetCollection().Query().Where(c => c.Enable == true)
.ToList()
.OrderBy(c => c.Sort).ThenBy(c => c.DateCreated).ToList();
Expand All @@ -805,7 +925,7 @@ public ActionResult<List<DiscordAccount>> List()
item.QueueCount = inc?.GetQueueTasks().Count ?? 0;
item.Running = inc?.IsAlive ?? false;

if (_isAnonymous)
if (user == null || (user.Role != EUserRole.ADMIN && user.Id != item.SponsorUserId))
{
// Token 加密
item.UserToken = item.UserToken?.Substring(0, item.UserToken.Length / 5) + "****";
Expand Down Expand Up @@ -836,6 +956,9 @@ public ActionResult<List<DiscordAccount>> List()
[HttpPost("accounts")]
public ActionResult<StandardTableResult<DiscordAccount>> Accounts([FromBody] StandardTableParam<DiscordAccount> request)
{
var user = _workContext.GetUser();


var page = request.Pagination;
if (page.PageSize > 100)
{
Expand Down Expand Up @@ -884,7 +1007,7 @@ public ActionResult<StandardTableResult<DiscordAccount>> Accounts([FromBody] Sta
item.QueueCount = inc?.GetQueueTasks().Count ?? 0;
item.Running = inc?.IsAlive ?? false;

if (_isAnonymous)
if (user == null || (user.Role != EUserRole.ADMIN && user.Id != item.SponsorUserId))
{
// Token 加密
item.UserToken = item.UserToken?.Substring(0, item.UserToken.Length / 5) + "****";
Expand Down Expand Up @@ -1476,7 +1599,7 @@ public Result<Setting> GetSetting()
model.Replicate.Token = "****";
}

if(model.TencentCos!=null)
if (model.TencentCos != null)
{
model.TencentCos.SecretId = "****";
model.TencentCos.SecretKey = "****";
Expand Down
Loading

0 comments on commit 8c70347

Please sign in to comment.