コード例 #1
0
        public async Task should_get_messages_in_chat()
        {
            var messages = await _normalChatyApiService.GetMessagesInChat("some_wx_id", "1556867241");

            Assert.Equal(1, _httpClient.RequestsSent.Count);
            Assert.Contains(_httpClient.RequestsSent, req => req.RequestUri.PathAndQuery == "/chat/detail/some_wx_id/1556867241");

            Assert.NotNull(messages);
            Assert.Equal(2, messages.Length);

            var firstMsg = messages[0];

            Assert.Equal("Wx_879LJJKJGS", firstMsg.SourceWxId);
            Assert.Equal(1547552933, firstMsg.SourceTimestamp);

            var fileMessage = firstMsg.Content as FileChatMessageContent;

            Assert.NotNull(fileMessage);
            Assert.Equal("89257293", fileMessage.FileId);
            Assert.Equal("SomeFile.jpg", fileMessage.FileName);

            var secondMsg = messages[1];

            Assert.Equal("Wx_879LKJGSJJ", secondMsg.SourceWxId);
            Assert.Equal(1547552934, secondMsg.SourceTimestamp);

            var urlMessage = secondMsg.Content as UrlChatMessageContent;

            Assert.NotNull(urlMessage);
            Assert.Equal("http://dotnetclub", urlMessage.Link);
        }
コード例 #2
0
        public async Task <ActionResult> ImportFromWeChat(ChatHistoryImportingModel model)
        {
            var user          = HttpContext.DiscussionUser();
            var weChatAccount = _wechatAccountRepo.All().FirstOrDefault(wxa => wxa.UserId == user.Id);

            if (weChatAccount == null)
            {
                _logger.LogWarning("导入对话失败:{@ImportAttempt}", new { UserId = user.Id, user.UserName, Result = "未绑定微信号" });
                return(BadRequest());
            }

            var messages = await _chatyApiService.GetMessagesInChat(weChatAccount.WxId, model.ChatId);

            if (messages == null)
            {
                return(new StatusCodeResult((int)HttpStatusCode.InternalServerError));
            }

            var replies = await _chatHistoryImporter.Import(messages);

            var actionResult = CreateTopic(model);

            if (!(actionResult is RedirectToActionResult redirectResult))
            {
                return(actionResult);
            }

            var topicId = (int)redirectResult.RouteValues["Id"];
            var topic   = _topicRepo.Get(topicId);

            replies.ForEach(r =>
            {
                r.TopicId = topicId;
                _replyRepo.Save(r);
            });

            topic.ReplyCount = replies.Count;
            topic.LastRepliedByWeChatAccount = replies.Last().CreatedByWeChatAccount;
            topic.LastRepliedAt = replies.Last().CreatedAtUtc;
            _topicRepo.Update(topic);

            _logger.LogInformation("导入对话成功:{@ImportAttempt}",
                                   new { TopicId = topic.Id, model.ChatId, topic.ReplyCount, UserId = user.Id, user.UserName });
            return(redirectResult);
        }