Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Aug 8, 2023
1 parent d1b727c commit 16fd7ef
Show file tree
Hide file tree
Showing 24 changed files with 205 additions and 112 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.1.3.1</Version>
<Version>2.1.5.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
16 changes: 8 additions & 8 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="MySql.Data" Version="8.0.33" />
<PackageVersion Include="NLog" Version="5.2.0" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.0" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.0" />
<PackageVersion Include="Quartz" Version="3.6.2" />
<PackageVersion Include="Quartz.AspNetCore" Version="3.6.2" />
<PackageVersion Include="MySql.Data" Version="8.1.0" />
<PackageVersion Include="NLog" Version="5.2.3" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.2" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.2" />
<PackageVersion Include="Quartz" Version="3.7.0" />
<PackageVersion Include="Quartz.AspNetCore" Version="3.7.0" />
<PackageVersion Include="SqlSugar.IOC" Version="2.0.0" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.82" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.94" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="Telegram.Bot" Version="19.0.0" />
Expand Down
41 changes: 40 additions & 1 deletion XinjingdailyBot.Command/SuperCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class SuperCommand
private readonly IUserService _userService;
private readonly IHttpHelperService _httpHelperService;
private readonly ITextHelperService _textHelperService;
private readonly IAdvertiseService _advertiseService;

[Obsolete("迁移使用")]
public SuperCommand(
Expand All @@ -50,7 +51,8 @@ public SuperCommand(
ICommandHandler commandHandler,
IUserService userService,
IHttpHelperService httpHelperService,
ITextHelperService textHelperService)
ITextHelperService textHelperService,
IAdvertiseService advertiseService)
{
_logger = logger;
_botClient = botClient;
Expand All @@ -63,6 +65,7 @@ public SuperCommand(
_userService = userService;
_httpHelperService = httpHelperService;
_textHelperService = textHelperService;
_advertiseService = advertiseService;
}

/// <summary>
Expand Down Expand Up @@ -683,4 +686,40 @@ async Task<string> exec()
string text = await exec();
await _botClient.SendCommandReply(text, message, false, ParseMode.Html);
}

/// <summary>
/// 新建广告
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
/// <returns></returns>
[TextCmd("CREATEAD", EUserRights.SuperCmd, Description = "新建广告")]
public async Task ResponseCreateAd(Message message)
{
var replyMsg = message.ReplyToMessage;

if (replyMsg == null)
{
await _botClient.SendCommandReply("请回复广告消息", message, false);
return;
}

var newAd = new Advertises {
ChatID = replyMsg.Chat.Id,
MessageID = replyMsg.MessageId,
Enable = false,
PinMessage = false,
Mode = EAdMode.All,
Weight = 100,
LastPostAt = DateTime.MinValue,
ShowCount = 0,
MaxShowCount = 0,
CreateAt = DateTime.Now,
ExpiredAt = DateTime.Now.AddDays(90),
};

await _advertiseService.Insertable(newAd).ExecuteCommandAsync();

await _botClient.SendCommandReply("创建广告成功, 请在数据库中修改广告配置并启用该广告", message, false, parsemode: ParseMode.Html);
}
}
14 changes: 14 additions & 0 deletions XinjingdailyBot.Interface/Bot/Common/IUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ namespace XinjingdailyBot.Interface.Bot.Common;
/// </summary>
public interface IUpdateService
{
/// <summary>
/// 处理Polling错误
/// </summary>
/// <param name="_"></param>
/// <param name="exception"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task HandlePollingErrorAsync(ITelegramBotClient _, Exception exception, CancellationToken cancellationToken);
/// <summary>
/// 处理更新
/// </summary>
/// <param name="_"></param>
/// <param name="update"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task HandleUpdateAsync(ITelegramBotClient _, Update update, CancellationToken cancellationToken);
}
10 changes: 5 additions & 5 deletions XinjingdailyBot.Model/Models/Advertises.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace XinjingdailyBot.Model.Models;
/// 广告投放
/// </summary>
[SugarTable("ad", TableDescription = "广告投放")]
public sealed record Advertises : BaseModel, ICreateAt
public sealed record Advertises : BaseModel, ICreateAt, IExpiredAt
{
/// <summary>
/// 主键
Expand Down Expand Up @@ -59,11 +59,11 @@ public sealed record Advertises : BaseModel, ICreateAt
/// </summary>
public uint MaxShowCount { get; set; }

/// <summary>
/// 过期时间, 系统时间大于过期时间自动禁用
/// </summary>
public DateTime ExpireAt { get; set; } = DateTime.MaxValue;

/// <inheritdoc cref="ICreateAt"/>
public DateTime CreateAt { get; set; } = DateTime.Now;

/// <inheritdoc cref="IExpiredAt"/>
[SugarColumn(OldColumnName = "ExpireAt")]
public DateTime ExpiredAt { get; set; } = DateTime.MaxValue;
}
1 change: 1 addition & 0 deletions XinjingdailyBot.Repository/GroupRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class GroupRepository : BaseRepository<Groups>
/// 用户组仓储类
/// </summary>
/// <param name="logger"></param>
/// <param name="context"></param>
public GroupRepository(
ILogger<GroupRepository> logger,
ISqlSugarClient context) : base(context)
Expand Down
1 change: 1 addition & 0 deletions XinjingdailyBot.Repository/LevelRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class LevelRepository : BaseRepository<Levels>
/// 用户等级仓储类
/// </summary>
/// <param name="logger"></param>
/// <param name="context"></param>
public LevelRepository(
ILogger<LevelRepository> logger,
ISqlSugarClient context) : base(context)
Expand Down
4 changes: 4 additions & 0 deletions XinjingdailyBot.Repository/OldPostRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace XinjingdailyBot.Repository;
[Obsolete("废弃的仓储类")]
public class OldPostRepository : BaseRepository<OldPosts>
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public OldPostRepository(ISqlSugarClient context) : base(context)
{
}
Expand Down
4 changes: 4 additions & 0 deletions XinjingdailyBot.Repository/PostRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace XinjingdailyBot.Repository;
[AppService(LifeTime.Transient)]
public class PostRepository : BaseRepository<NewPosts>
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public PostRepository(ISqlSugarClient context) : base(context)
{
}
Expand Down
1 change: 1 addition & 0 deletions XinjingdailyBot.Repository/RejectReasonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class RejectReasonRepository : BaseRepository<RejectReasons>
/// 拒绝理由仓储类
/// </summary>
/// <param name="logger"></param>
/// <param name="context"></param>
public RejectReasonRepository(
ILogger<RejectReasonRepository> logger,
ISqlSugarClient context) : base(context)
Expand Down
1 change: 1 addition & 0 deletions XinjingdailyBot.Repository/TagRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TagRepository : BaseRepository<Tags>
/// 标签仓储类
/// </summary>
/// <param name="logger"></param>
/// <param name="context"></param>
public TagRepository(
ILogger<GroupRepository> logger,
ISqlSugarClient context) : base(context)
Expand Down
10 changes: 5 additions & 5 deletions XinjingdailyBot.Service/Bot/Common/PollingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ private async Task DoWork(CancellationToken stoppingToken)
using var scope = _serviceProvider.CreateScope();
var updateService = scope.ServiceProvider.GetRequiredService<IUpdateService>();

bool skip = false;
//bool skip = false;
while (!stoppingToken.IsCancellationRequested)
{
try
{
var receiverOptions = new ReceiverOptions {
AllowedUpdates = Array.Empty<UpdateType>(),
ThrowPendingUpdates = _throwPendingUpdates,
Offset = skip ? 1 : 0,
Limit = 10,
//Offset = skip ? -1 : 0,
Limit = 100,
};

skip = false;
//skip = false;

_logger.LogInformation("接收服务运行中...");

Expand All @@ -123,7 +123,7 @@ await _botClient.ReceiveAsync(
_logger.LogError(ex, "Telegram API 调用出错");
if (ex.Message == "Bad Request: query is too old and response timeout expired or query ID is invalid")
{
skip = true;
//skip = true;
}
}
catch (Exception ex)
Expand Down
9 changes: 8 additions & 1 deletion XinjingdailyBot.Service/Bot/Common/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public async Task HandleUpdateAsync(ITelegramBotClient _, Update update, Cancell

if (handler != null)
{
await handler;
try
{
await handler;
}
catch (Exception ex)
{
_logger.LogError(ex, "处理轮询出错 {0}", update);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions XinjingdailyBot.Service/Data/AdvertiseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public AdvertiseService(
foreach (var ad in ads)
{
if ((ad.MaxShowCount > 0 && ad.ShowCount >= ad.MaxShowCount) ||
now >= ad.ExpireAt || ad.Weight == 0)
now >= ad.ExpiredAt || ad.Weight == 0)
{
ad.Enable = false;
await UpdateAsync(ad);
await _advertisePostService.DeleteOldAdPosts(ad, false);
}
}
var validAds = ads.Where(static x => x.Enable);

now = now.AddSeconds(-now.Second).AddMinutes(-now.Minute).AddHours(-now.Hour);
var validAds = ads.Where(x => x.Enable && x.LastPostAt < now);

if (!validAds.Any())
{
Expand Down
4 changes: 4 additions & 0 deletions XinjingdailyBot.Service/Data/Base/BaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace XinjingdailyBot.Service.Data.Base;
/// <typeparam name="T"></typeparam>
public abstract class BaseService<T> : BaseRepository<T> where T : BaseModel, new()
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
protected BaseService(ISqlSugarClient context) : base(context)
{
}
Expand Down
8 changes: 1 addition & 7 deletions XinjingdailyBot.Tasks/PostAdvertiseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ public async Task Execute(IJobExecutionContext context)
return;
}

var channelService = _serviceProvider.GetService<IChannelService>();

if (channelService == null)
{
_logger.LogError("获取服务 {type} 失败", nameof(IChannelService));
return;
}
var channelService = _serviceProvider.GetRequiredService<IChannelService>();

var operates = new List<(EAdMode, ChatId)>
{
Expand Down
40 changes: 20 additions & 20 deletions XinjingdailyBot.WebAPI/Authorization/AppendAuthorizeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

namespace XinjingdailyBot.WebAPI.Authorization;

public class AppendAuthorizeFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizationAttribute);
//public class AppendAuthorizeFilter : IOperationFilter
//{
// public void Apply(OpenApiOperation operation, OperationFilterContext context)
// {
// var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
// var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Filter).Any(filter => filter is AuthorizationAttribute);

if (isAuthorized)
{
if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();
// if (isAuthorized)
// {
// if (operation.Parameters == null)
// operation.Parameters = new List<OpenApiParameter>();

operation.Parameters.Add(new OpenApiParameter
{
Name = "SYSTOKEN",
In = ParameterLocation.Header,
Description = "身份验证",
Required = false
});
}
}
}
// operation.Parameters.Add(new OpenApiParameter
// {
// Name = "SYSTOKEN",
// In = ParameterLocation.Header,
// Description = "身份验证",
// Required = false
// });
// }
// }
//}
Loading

0 comments on commit 16fd7ef

Please sign in to comment.