public static MessageVm Create(ConversationMsg msg) { var model = new MessageVm { Id = msg.Id, SendTime = msg.Time, Type = msg.Type, SenderId = msg.SenderId, ConversationId = msg.ConversationId }; switch (msg.Type) { case ConversationMsgType.Text: model.Message = JsonConvert.SerializeObject(new TextMessageVm { Content = msg.Content }); break; default: model.Message = msg.Content; break; } return(model); }
private static async Task SaveMsgAsync(IReliableStateManager stateManager, ITransaction tx, ConversationMsg item) { try { var listDict = await Service.GetMessageListDictByIdAsync(stateManager, item.Id); await listDict.AddAsync(tx, item.Id, item); var indexDict = await Service.GetMessageIndexDictByIdAsync(stateManager, item.ConversationId); var lstMessageIndex = await indexDict.GetOrAddAsync(tx, item.ConversationId, new List <Guid>()); lstMessageIndex.Add(item.Id); await indexDict.SetAsync(tx, item.ConversationId, lstMessageIndex); await tx.CommitAsync(); } catch (Exception ex) { Log.Error("ConversationMsgQueueProcessor SaveMsgAsync Error: " + $"ItemId: {item.Id}; ConversationId: {item.ConversationId}; SenderId: {item.SenderId}; Message: {ex.Message}"); if (IsInUnitTest) { throw; } } }
private async Task <SendMessageResult> SendAsync(ConversationMsg msg, ConversationType conversationType) { var entity = await _conversationCtrlAppService.GetByIdAsync(msg.ConversationId); var conversationMsgAppService = _conversationMsgAppServiceFactory(msg.ConversationId); if (entity.HasValue && !entity.Value.IsDeleted) { if (entity.Value.Participants.Contains(msg.SenderId)) { await conversationMsgAppService.SendMessageAsync(msg); return(new SendMessageResult() { Success = true, Message = "发送消息成功", MessageId = msg.Id, SendTime = msg.Time }); } else { return new SendMessageResult() { Success = false, Message = conversationType == ConversationType.P2P ? "用户不属于此会话..." : "用户不在群组中..." } } } ;
private async Task <MessageVm> BuildMessageVmAsync(ConversationMsg msg) { var dtos = await _employeeMappingService.ByUserIdAsync(msg.SenderId); var name = (dtos == null || !dtos.Any()) ? "未知用户" : dtos.First().Name; var gender = (dtos == null || !dtos.Any()) ? 0 : dtos.First().Gender; var message = MessageVm.Create(msg).SetSenderInfo(name, gender); return(message); }
private async Task SendMessageAsync(Guid groupId, Guid senderId, string fileName, AttachmentItem attachment) { var fileMsg = new FileMessageVm { FileGuid = attachment.Id, FileName = fileName, FileSize = attachment.Size, }; var msg = ConversationMsg.Create(groupId, senderId, fileMsg.GetJsonContent(), ConversationMsgType.File); var conversationMsgAppService = _conversationMsgAppServiceFactory(msg.ConversationId); await conversationMsgAppService?.SendMessageAsync(msg); }
public async Task SendMessageAsync(ConversationMsg msg) { var queue = await Service.GetMessageProcessQueue(StateManager); using (var tx = StateManager.CreateTransaction()) { _logger.Debug("Enqueue ConversationMsg: {msgId}", msg.Id); await queue.EnqueueAsync(tx, msg); await tx.CommitAsync(); } }
private static async Task PushNotifyAsync( IConversationCtrlAppService conversationCtrlAppService, Func <Guid, INotifySessionActor> notifySessionActorFactory, ConversationMsg item) { try { var entityConversationWrap = await conversationCtrlAppService.GetByIdAsync(item.ConversationId); if (entityConversationWrap.HasValue) { var entityConversation = entityConversationWrap.Value; var tasks = new List <Task>(); foreach (var userId in entityConversation.Participants) { var actorSession = notifySessionActorFactory(userId); tasks.Add(actorSession.PushMsgNotifyAsync(new MessageNotifyDto { Target = NotifyTargetType.Conversation, TargetId = item.ConversationId.ToString(), TargetCategory = (int)entityConversation.Type, LatestMsgId = item.Id, })); } await Task.WhenAll(tasks); } } catch (Exception ex) { //如果PushMsgNotifyAsync过程出错,也实现保存消息的过程,只是会出现接收方接收不到消息 Log.Error("ConversationMsgQueueProcessor PushMsgNotifyAsync Message: " + $"ItemId is {item.Id}, ConversationId is {item.ConversationId}, SenderId is {item.SenderId}, Message is {ex.Message}"); if (IsInUnitTest) { throw; } } }