public async Task <JsonResult> GetHistoryBetweenAsync(string id1, string id2)
        {
            var messages = await _messageStore.GetMessagesBetween(id1, id2);

            List <MessageViewModel> messagesModel = new List <MessageViewModel>();

            foreach (var item in messages)
            {
                messagesModel.Add(
                    new MessageViewModel {
                    MessageId    = item.MessageId,
                    SenderId     = item.SenderId,
                    SenderName   = item.Sender.UserNameStr,
                    ReceiverId   = item.ReceiverId,
                    ReceiverName = item.Receiver.UserNameStr,
                    Body         = item.Body,
                    Date         = item.DateSent,
                    hasBeenRead  = item.Read
                });
            }
            return(Json(messagesModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Download(int?id)
        {
            //if (!ModelState.IsValid)
            //{
            //    return View(model);
            //}

            var user = await _manager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_manager.GetUserId(User)}'.");
            }

            var buddiesIDs = _store.myChatBuddies(user.Id);
            var buddiesAU  = _manager.Users.Where(x => buddiesIDs.Contains(x.Id)).ToList();
            var buddies    = new List <MapUserModel>();

            foreach (var u in buddiesAU)
            {
                buddies.Add(
                    new MapUserModel
                {
                    Id          = u.Id,
                    UserNameStr = u.UserNameStr,
                });
            }

            List <string> txt = new List <string>();

            txt.Add("LIST OF MESSAGES");
            foreach (var u in buddies)
            {
                txt.Add(" ");
                txt.Add(" ");
                txt.Add("-->>" + u.UserNameStr + "<<--");
                txt.Add(" ");

                var messages = await _store.GetMessagesBetween(user.Id, u.Id);

                foreach (var item in messages)
                {
                    txt.Add("From: " + item.Sender.UserNameStr);
                    txt.Add("To: " + item.Receiver.UserNameStr);
                    txt.Add("Message: " + item.Body);
                    txt.Add("Date: " + item.DateSent.ToString("yyyy-dd-M--HH-mm-ss"));
                    txt.Add(" ");
                    //txt.Add("Read: " + item.Read);
                }
            }

            string        str          = string.Empty;
            IFileProvider fileProvider = _hostingEnvironment.ContentRootFileProvider;
            string        fileName     = user.UserNameStr + "_" + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + "_" + "Messagess.txt";
            string        content      = (txt.Aggregate((i, j) => i + Environment.NewLine + j)).ToString();
            //return File(content, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

            MemoryStream memoryStream = new MemoryStream();
            TextWriter   tw           = new StreamWriter(memoryStream);

            tw.WriteLine(content);
            tw.Flush();
            tw.Close();

            return(File(memoryStream.GetBuffer(), "text/plain", fileName));
        }