Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Sep 8, 2023
1 parent ccae4bc commit b775e5a
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 174 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.2.0.1</Version>
<Version>2.2.1.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
24 changes: 18 additions & 6 deletions XinjingdailyBot.Command/AdminCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,24 @@ async Task<string> exec()
post.ModifyAt = DateTime.Now;
await _postService.Updateable(post).UpdateColumns(static x => new { x.Status, x.ModifyAt }).ExecuteCommandAsync();

var chat = post.Status == EPostStatus.Accepted ? _channelService.AcceptChannel : _channelService.SecondChannel;
if (chat == null)
{
return "第二频道未设定";
}

if (post.WarnTextID != -1)
{
try
{
await _botClient.DeleteMessageAsync(chat, (int)post.WarnTextID);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除消息出错");
}
}

if (post.IsMediaGroup)
{
var mediaGroups = await _mediaGroupService.QueryMediaGroup(post.PublishMediaGroupID);
Expand All @@ -1571,12 +1589,6 @@ async Task<string> exec()
}
else
{
var chat = post.Status == EPostStatus.Accepted ? _channelService.AcceptChannel : _channelService.SecondChannel;
if (chat == null)
{
return "第二频道未设定";
}

try
{
await _botClient.DeleteMessageAsync(chat, (int)post.PublicMsgID);
Expand Down
4 changes: 4 additions & 0 deletions XinjingdailyBot.Model/Models/NewPosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public sealed record NewPosts : BaseModel, IModifyAt, ICreateAt
/// </summary>
public long PublicMsgID { get; set; } = -1;

/// <summary>
/// 警告消息Id
/// </summary>
public long WarnTextID { get; set; } = -1;
/// <summary>
/// 是否为直接投稿
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion XinjingdailyBot.Repository/Base/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using SqlSugar;
using SqlSugar.IOC;
using System.Data;
using System.Linq.Expressions;
using XinjingdailyBot.Model.Base;
Expand Down
13 changes: 9 additions & 4 deletions XinjingdailyBot.Service/Data/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ public async Task AcceptPost(NewPosts post, Users dbUser, bool inPlan, bool seco
string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(acceptChannel, warnText, allowSendingWithoutReply: true);
var warnMsg = await _botClient.SendTextMessageAsync(acceptChannel, warnText, allowSendingWithoutReply: true);
post.WarnTextID = warnMsg.MessageId;
}

Message? postMessage = null;
Expand Down Expand Up @@ -772,7 +773,8 @@ public async Task AcceptPost(NewPosts post, Users dbUser, bool inPlan, bool seco
string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(acceptChannel, warnText, allowSendingWithoutReply: true);
var warnMsg = await _botClient.SendTextMessageAsync(acceptChannel, warnText, allowSendingWithoutReply: true);
post.WarnTextID = warnMsg.MessageId;
}

var postMessages = await _botClient.SendMediaGroupAsync(acceptChannel, group);
Expand Down Expand Up @@ -814,6 +816,7 @@ public async Task AcceptPost(NewPosts post, Users dbUser, bool inPlan, bool seco
x.PublicMsgID,
x.PublishMediaGroupID,
x.ReviewerUID,
x.WarnTextID,
x.Status,
x.ModifyAt
}).ExecuteCommandAsync();
Expand Down Expand Up @@ -877,7 +880,8 @@ public async Task<bool> PublicInPlanPost(NewPosts post)
string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(_channelService.AcceptChannel, warnText, allowSendingWithoutReply: true);
var warnMsg = await _botClient.SendTextMessageAsync(_channelService.AcceptChannel, warnText, allowSendingWithoutReply: true);
post.WarnTextID = warnMsg.MessageId;
}

Message? postMessage = null;
Expand Down Expand Up @@ -936,7 +940,8 @@ public async Task<bool> PublicInPlanPost(NewPosts post)
string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(_channelService.AcceptChannel, warnText, allowSendingWithoutReply: true);
var warnMsg = await _botClient.SendTextMessageAsync(_channelService.AcceptChannel, warnText, allowSendingWithoutReply: true);
post.WarnTextID = warnMsg.MessageId;
}

var postMessages = await _botClient.SendMediaGroupAsync(_channelService.AcceptChannel, group);
Expand Down
2 changes: 1 addition & 1 deletion XinjingdailyBot.Service/Data/UserTokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class UserTokenService : BaseService<UserTokens>, IUserTokenServ
public UserTokenService(
ILogger<UserTokenService> logger,
ISqlSugarClient context) : base(context)
{
{
_logger = logger;
}

Expand Down
141 changes: 4 additions & 137 deletions XinjingdailyBot.Tasks/PlanedPostsTask.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Infrastructure.Enums;
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Data;
using XinjingdailyBot.Interface.Helper;
using XinjingdailyBot.Model.Models;
using XinjingdailyBot.Repository;

namespace XinjingdailyBot.Tasks;

Expand All @@ -20,42 +13,18 @@ internal class PlanedPostsTask : IJob
{
private readonly ILogger<PlanedPostsTask> _logger;
private readonly IPostService _postService;
private readonly IUserService _userService;
private readonly ITelegramBotClient _botClient;
private readonly TagRepository _tagRepository;
private readonly IAttachmentService _attachmentService;
private readonly IChannelService _channelService;
private readonly IChannelOptionService _channelOptionService;
private readonly ITextHelperService _textHelperService;
private readonly IMediaGroupService _mediaGroupService;

public PlanedPostsTask(
ILogger<PlanedPostsTask> logger,
IPostService postService,
IUserService userService,
ITelegramBotClient botClient,
TagRepository tagRepository,
IAttachmentService attachmentService,
IChannelService channelService,
IChannelOptionService channelOptionService,
ITextHelperService textHelperService,
IMediaGroupService mediaGroupService)
IPostService postService)
{
_logger = logger;
_postService = postService;
_userService = userService;
_botClient = botClient;
_tagRepository = tagRepository;
_attachmentService = attachmentService;
_channelService = channelService;
_channelOptionService = channelOptionService;
_textHelperService = textHelperService;
_mediaGroupService = mediaGroupService;
}

public async Task Execute(IJobExecutionContext context)
{
_logger.LogInformation("开始定时任务, 发布定时任务");
_logger.LogInformation("开始定时任务, 发布定时稿件");

var post = await _postService.Queryable()
.Where(static x => x.Status == EPostStatus.InPlan).FirstAsync();
Expand All @@ -66,109 +35,7 @@ public async Task Execute(IJobExecutionContext context)
return;
}

var poster = await _userService.Queryable().FirstAsync(x => x.UserID == post.PosterUID);
if (post.IsDirectPost)
{
poster.PostCount++;
}

ChannelOptions? channel = null;
if (post.IsFromChannel)
{
channel = await _channelOptionService.FetchChannelByChannelId(post.ChannelID);
}
string postText = _textHelperService.MakePostText(post, poster, channel);
bool hasSpoiler = post.HasSpoiler;

try
{
//发布频道发布消息
if (!post.IsMediaGroup)
{
string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(_channelService.AcceptChannel.Id, warnText, allowSendingWithoutReply: true);
}

Message? postMessage = null;
if (post.PostType == MessageType.Text)
{
postMessage = await _botClient.SendTextMessageAsync(_channelService.AcceptChannel.Id, postText, parseMode: ParseMode.Html, disableWebPagePreview: true);
}
else
{
var attachment = await _attachmentService.Queryable().FirstAsync(x => x.PostID == post.Id);

var inputFile = new InputFileId(attachment.FileID);
var handler = post.PostType switch {
MessageType.Photo => _botClient.SendPhotoAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html, hasSpoiler: hasSpoiler),
MessageType.Audio => _botClient.SendAudioAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html, title: attachment.FileName),
MessageType.Video => _botClient.SendVideoAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html, hasSpoiler: hasSpoiler),
MessageType.Voice => _botClient.SendVoiceAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html),
MessageType.Document => _botClient.SendDocumentAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html),
MessageType.Animation => _botClient.SendAnimationAsync(_channelService.AcceptChannel.Id, inputFile, caption: postText, parseMode: ParseMode.Html, hasSpoiler: hasSpoiler),
_ => null,
};

if (handler == null)
{
_logger.LogError("不支持的稿件类型: {postType}", post.PostType);
return;
}

postMessage = await handler;
}
post.PublicMsgID = postMessage?.MessageId ?? -1;
}
else
{
var attachments = await _attachmentService.Queryable().Where(x => x.PostID == post.Id).ToListAsync();
var group = new IAlbumInputMedia[attachments.Count];
for (int i = 0; i < attachments.Count; i++)
{
var attachmentType = attachments[i].Type;
if (attachmentType == MessageType.Unknown)
{
attachmentType = post.PostType;
}

var inputFile = new InputFileId(attachments[i].FileID);
group[i] = attachmentType switch {
MessageType.Photo => new InputMediaPhoto(inputFile) { Caption = i == 0 ? postText : null, ParseMode = ParseMode.Html, HasSpoiler = hasSpoiler },
MessageType.Audio => new InputMediaAudio(inputFile) { Caption = i == 0 ? postText : null, ParseMode = ParseMode.Html },
MessageType.Video => new InputMediaVideo(inputFile) { Caption = i == 0 ? postText : null, ParseMode = ParseMode.Html, HasSpoiler = hasSpoiler },
MessageType.Voice => new InputMediaVideo(inputFile) { Caption = i == 0 ? postText : null, ParseMode = ParseMode.Html },
MessageType.Document => new InputMediaDocument(inputFile) { Caption = i == attachments.Count - 1 ? postText : null, ParseMode = ParseMode.Html },
_ => throw new Exception("未知的稿件类型"),
};
}

string? warnText = _tagRepository.GetActivedTagWarnings(post.Tags);
if (!string.IsNullOrEmpty(warnText))
{
await _botClient.SendTextMessageAsync(_channelService.AcceptChannel.Id, warnText, allowSendingWithoutReply: true);
}

var postMessages = await _botClient.SendMediaGroupAsync(_channelService.AcceptChannel.Id, group);
post.PublicMsgID = postMessages.First().MessageId;
post.PublishMediaGroupID = postMessages.First().MediaGroupId ?? "";

//记录媒体组消息
await _mediaGroupService.AddPostMediaGroup(postMessages);
}
}
finally
{
post.Status = EPostStatus.Accepted;
post.ModifyAt = DateTime.Now;

await _postService.Updateable(post).UpdateColumns(static x => new {
x.PublicMsgID,
x.PublishMediaGroupID,
x.Status,
x.ModifyAt
}).ExecuteCommandAsync();
}
var result = await _postService.PublicInPlanPost(post);
_logger.LogInformation("发布定时稿件 {status}", result ? "成功" : "失败");
}
}
1 change: 0 additions & 1 deletion XinjingdailyBot.Tasks/PostAdvertiseTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Telegram.Bot;
using Telegram.Bot.Types;
using XinjingdailyBot.Infrastructure.Attribute;
Expand Down
4 changes: 0 additions & 4 deletions XinjingdailyBot.Tasks/ReviewStatusTask.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Infrastructure.Enums;
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Data;
using XinjingdailyBot.Interface.Helper;
using XinjingdailyBot.Model.Models;
using XinjingdailyBot.Repository;

namespace XinjingdailyBot.Tasks;
Expand Down
6 changes: 0 additions & 6 deletions XinjingdailyBot.WebAPI/Authorization/AppendAuthorizeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;

namespace XinjingdailyBot.WebAPI.Authorization;

//public class AppendAuthorizeFilter : IOperationFilter
Expand Down
9 changes: 0 additions & 9 deletions XinjingdailyBot.WebAPI/Authorization/VerifyAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Server.IIS;
using NLog;
using System.DirectoryServices.Protocols;

namespace ZR.Admin.WebApi.Filters;

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions XinjingdailyBot.WebAPI/Extensions/DatabaseExtension.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using NLog.Fluent;
using SqlSugar;
using SqlSugar.IOC;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using XinjingdailyBot.Infrastructure;
using XinjingdailyBot.Service.Bot.Common;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace XinjingdailyBot.WebAPI.Extensions;

Expand Down

0 comments on commit b775e5a

Please sign in to comment.