Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Apr 15, 2023
1 parent 7eba481 commit 4b2a119
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 44 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.0.25.0</Version>
<Version>2.0.25.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion XinjingdailyBot.Infrastructure/Extensions/ChatExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static string GetMessageLink(this Chat chat, long messageId)
{
string link = !string.IsNullOrEmpty(chat.Username) ? $"https://t.me/{chat.Username}/{messageId}" : $"https://t.me/c/{chat.Id}/{messageId}";
return link;

}
}
}
13 changes: 13 additions & 0 deletions XinjingdailyBot.Interface/Data/IMediaGroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,18 @@ public interface IMediaGroupService
/// <param name="message"></param>
/// <returns></returns>
Task<MediaGroups?> QueryMediaGroup(Message message);
/// <summary>
/// 查询媒体组消息
/// </summary>
/// <param name="mediaGroupId"></param>
/// <returns></returns>
Task<List<MediaGroups>> QueryMediaGroup(string? mediaGroupId);
/// <summary>
/// 查询媒体组消息
/// </summary>
/// <param name="chat"></param>
/// <param name="msgId"></param>
/// <returns></returns>
Task<MediaGroups?> QueryMediaGroup(Chat chat, long msgId);
}
}
105 changes: 67 additions & 38 deletions XinjingdailyBot.Service/Bot/Handler/ForwardMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using XinjingdailyBot.Interface.Data;
using XinjingdailyBot.Interface.Helper;
using XinjingdailyBot.Model.Models;
using XinjingdailyBot.Service.Bot.Common;

namespace XinjingdailyBot.Service.Bot.Handler
{
Expand All @@ -24,71 +25,99 @@ public class ForwardMessageHandler : IForwardMessageHandler
private readonly IPostService _postService;
private readonly IMarkupHelperService _markupHelperService;
private readonly IUserService _userService;
private readonly IMediaGroupService _mediaGroupService;

public ForwardMessageHandler(
ILogger<ForwardMessageHandler> logger,
ITelegramBotClient botClient,
IChannelService channelService,
IPostService postService,
IMarkupHelperService markupHelperService,
IUserService userService)
IMarkupHelperService markupHelperService,
IUserService userService,
IMediaGroupService mediaGroupService)
{
_logger = logger;
_botClient = botClient;
_channelService = channelService;
_postService = postService;
_markupHelperService = markupHelperService;
_userService = userService;
_mediaGroupService = mediaGroupService;
}

public async Task<bool> OnForwardMessageReceived(Users dbUser, Message message)
{
if (!dbUser.Right.HasFlag(UserRights.AdminCmd))
if (dbUser.Right.HasFlag(UserRights.AdminCmd))
{
return false;
}

var forwardFrom = message.ForwardFrom!;
var forwardFromChat = message.ForwardFromChat;

if (forwardFromChat != null
&& (forwardFromChat.Id == _channelService.AcceptChannel.Id || forwardFromChat.Id == _channelService.RejectChannel.Id))
{
var post = await _postService.GetFirstAsync(x => x.PublicMsgID == message.ForwardFromMessageId);
var poster = await _userService.FetchUserByUserID(post.PosterUID);
var forwardFrom = message.ForwardFrom!;
var forwardFromChat = message.ForwardFromChat;
var foreardMsgId = message.ForwardFromMessageId;

if (post != null && poster != null)
if (forwardFromChat != null && foreardMsgId != null
&& (_channelService.IsChannelMessage(forwardFromChat.Id) || _channelService.IsGroupMessage(forwardFromChat.Id)))
{
if (post.Status == PostStatus.Reviewing)
Posts? post = null;

bool isMediaGroup = !string.IsNullOrEmpty(message.MediaGroupId);
if (!isMediaGroup)
{
await _botClient.AutoReplyAsync("无法操作审核中的稿件", message);
return false;
if (forwardFromChat.Id == _channelService.AcceptChannel.Id)
{
post = await _postService.GetFirstAsync(x => x.PublicMsgID == foreardMsgId);
}
else if (forwardFromChat.Id == _channelService.ReviewGroup.Id)
{
post = await _postService.GetFirstAsync(x => x.ReviewMsgID == foreardMsgId || x.ManageMsgID == foreardMsgId);
}
}
else
{
var groupMsgs = await _mediaGroupService.QueryMediaGroup(message.MediaGroupId);
var msgIds = groupMsgs.Select(x => x.MessageID).ToList();

var keyboard = _markupHelperService.QueryPostMenuKeyboard(dbUser, post);
if (forwardFromChat.Id == _channelService.AcceptChannel.Id)
{
post = await _postService.GetFirstAsync(x => msgIds.Contains(x.PublicMsgID));
}
else if (forwardFromChat.Id == _channelService.ReviewGroup.Id)
{
post = await _postService.GetFirstAsync(x => msgIds.Contains(x.ReviewMsgID) || msgIds.Contains(x.ManageMsgID));
}
}

string postStatus = post.Status switch
if (post != null)
{
PostStatus.ConfirmTimeout => "投递超时",
PostStatus.ReviewTimeout => "审核超时",
PostStatus.Rejected => "已拒绝",
PostStatus.Accepted => "已发布",
_ => "未知",
};
string postMode = post.IsDirectPost ? "直接发布" : (post.Anonymous ? "匿名投稿" : "保留来源");
string posterLink = poster.HtmlUserLink();
var poster = await _userService.FetchUserByUserID(post.PosterUID);
if (poster != null)
{
if (post.Status == PostStatus.Reviewing)
{
await _botClient.AutoReplyAsync("无法操作审核中的稿件", message);
return false;
}

StringBuilder sb = new();
sb.AppendLine($"投稿人: {posterLink}");
sb.AppendLine($"模式: {postMode}");
sb.AppendLine($"状态: {postStatus}");
var keyboard = _markupHelperService.QueryPostMenuKeyboard(dbUser, post);

await _botClient.SendTextMessageAsync(message.Chat, sb.ToString(), parseMode: ParseMode.Html, disableWebPagePreview: true, replyMarkup: keyboard, replyToMessageId: message.MessageId, allowSendingWithoutReply: true);
return true;
}
else
{
return false;
string postStatus = post.Status switch
{
PostStatus.ConfirmTimeout => "投递超时",
PostStatus.ReviewTimeout => "审核超时",
PostStatus.Rejected => "已拒绝",
PostStatus.Accepted => "已发布",
_ => "未知",
};
string postMode = post.IsDirectPost ? "直接发布" : (post.Anonymous ? "匿名投稿" : "保留来源");
string posterLink = poster.HtmlUserLink();

StringBuilder sb = new();
sb.AppendLine($"投稿人: {posterLink}");
sb.AppendLine($"模式: {postMode}");
sb.AppendLine($"状态: {postStatus}");

await _botClient.SendTextMessageAsync(message.Chat, sb.ToString(), parseMode: ParseMode.Html, disableWebPagePreview: true, replyMarkup: keyboard, replyToMessageId: message.MessageId, allowSendingWithoutReply: true);
return true;
}
}
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion XinjingdailyBot.Service/Bot/Handler/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task OnMediaMessageReceived(Users dbUser, Message message)
if (message.ForwardFromChat != null)
{
var handled = await _forwardMessageHandler.OnForwardMessageReceived(dbUser, message);
if (!handled)
if (handled)
{
return;
}
Expand Down
12 changes: 12 additions & 0 deletions XinjingdailyBot.Service/Data/MediaGroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ public async Task AddPostMediaGroup(IEnumerable<Message> messages)
var mediaGroup = await Queryable().FirstAsync(x => x.ChatID == message.Chat.Id && x.MessageID == message.MessageId);
return mediaGroup;
}

public async Task<List<MediaGroups>> QueryMediaGroup(string? mediaGroupId)
{
var mediaGroups = await Queryable().Where(x => x.MediaGroupID == mediaGroupId).ToListAsync();
return mediaGroups;
}

public async Task<MediaGroups?> QueryMediaGroup(Chat chat,long msgId)
{
var mediaGroup = await Queryable().FirstAsync(x => x.ChatID == chat.Id && x.MessageID == msgId);
return mediaGroup;
}
}
}
6 changes: 3 additions & 3 deletions XinjingdailyBot.Service/Data/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public async Task HandleMediaPosts(Users dbUser, Message message)
/// <summary>
/// mediaGroupID字典
/// </summary>
private ConcurrentDictionary<string, long> MediaGroupIDs { get; } = new();
private ConcurrentDictionary<string, int> MediaGroupIDs { get; } = new();

public async Task HandleMediaGroupPosts(Users dbUser, Message message)
{
Expand All @@ -381,7 +381,7 @@ public async Task HandleMediaGroupPosts(Users dbUser, Message message)
}

string mediaGroupId = message.MediaGroupId!;
if (!MediaGroupIDs.TryGetValue(mediaGroupId, out long postID)) //如果mediaGroupId不存在则创建新Post
if (!MediaGroupIDs.TryGetValue(mediaGroupId, out int postID)) //如果mediaGroupId不存在则创建新Post
{
MediaGroupIDs.TryAdd(mediaGroupId, -1);

Expand Down Expand Up @@ -469,7 +469,7 @@ public async Task HandleMediaGroupPosts(Users dbUser, Message message)
newPost.ManageMsgID = msg.MessageId;
}

postID = await Insertable(newPost).ExecuteReturnBigIdentityAsync();
postID = await Insertable(newPost).ExecuteReturnIdentityAsync();

MediaGroupIDs[mediaGroupId] = postID;

Expand Down

0 comments on commit 4b2a119

Please sign in to comment.