/// <summary> /// Sends a message to a dm conversation /// </summary> /// <param name="order">The info to send the message</param> /// See <see cref="Areas.DirectMessages.Models.SendDMMessage"/> to know the param structure /// <returns>IActionResult of the send message action</returns> /// See <see cref="Areas.DirectMessages.Models.DMRoom"/> to know the response structure public async Task <IActionResult> sendMsg([FromBody] SendDMMessage order) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); DirectMessageTitle title = new DirectMessageTitle(); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (!checkOrder(ref title, order.dmId, user)) { return(BadRequest()); } try { bool isAdmin = AdminPolicy.isAdmin(user, _context); DirectMessageMessages msg = new DirectMessageMessages { message = order.message, isAdmin = isAdmin, DirectMessageTitle = title }; addUnreadMessages(title, isAdmin); _context.Add(msg); _context.SaveChanges(); await sendMailAndSendNotification(title, user); using (var scope = _scopeFactory.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>(); DirectMessageTitle dbTitle = dbContext.DirectMessagesTitle.Where(t => t.id == title.id).First(); User dbuser = dbContext.User.Where(u => u.id == user.id).First(); DMRoom room = new DMRoom(dbTitle, dbuser, dbContext); return(Ok(room)); } } catch (Exception) { return(StatusCode(500)); } }
/// <summary> /// Closes or open a dm conversation /// </summary> /// <param name="id">The id of the dm conversation</param> /// <param name="openOrder">"1" to open de conversation, "0" to clse it</param> /// <returns>The IActionResult of the open/close action</returns> /// See <see cref="Areas.DirectMessages.Models.DMRoom"/> to know the response structure public async Task <IActionResult> close(string id, string openOrder) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); DirectMessageTitle title = new DirectMessageTitle(); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (!AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } if (openOrder != "1" && openOrder != "0") { return(BadRequest()); } bool open = openOrder == "1"; if (!checkOrder(ref title, id, open)) { return(BadRequest()); } if (!checkAdminInDM(title, user)) { return(BadRequest()); } try { sendClosedMessage(title, open); title.closed = !open; _context.SaveChanges(); await sendMailAndNotification(title, user, open); DMRoom room = new DMRoom(title, user, _context); return(Ok(room)); } catch (Exception) { return(StatusCode(500)); } }