/// <summary> /// Creates a new message in the given thread /// Caller should validate input parameters. /// </summary> /// <param name="threadID">Thread wherein the new message will be placed</param> /// <param name="userID">User who posted this message</param> /// <param name="messageText">Message text</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="userIDIPAddress">IP address of user calling this method</param> /// <param name="messageAsXML">Message text as XML, which is the result of the parse action on MessageText.</param> /// <param name="subscribeToThread">if set to <c>true</c> [subscribe to thread].</param> /// <param name="threadUpdatedNotificationTemplate">The thread updated notification template.</param> /// <param name="emailData">The email data.</param> /// <param name="sendReplyNotifications">Flag to signal to send reply notifications. If set to false no notifications are mailed, /// otherwise a notification is mailed to all subscribers to the thread the new message is posted in</param> /// <returns>MessageID if succeeded, 0 if not.</returns> public static int CreateNewMessageInThread(int threadID, int userID, string messageText, string messageAsHTML, string userIDIPAddress, string messageAsXML, bool subscribeToThread, string threadUpdatedNotificationTemplate, Dictionary <string, string> emailData, bool sendReplyNotifications) { Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "InsertNewMessage"); int messageID = 0; try { DateTime postingDate = DateTime.Now; messageID = InsertNewMessage(threadID, userID, messageText, messageAsHTML, userIDIPAddress, messageAsXML, trans, postingDate); MessageManager.UpdateStatisticsAfterMessageInsert(threadID, userID, trans, postingDate, true, subscribeToThread); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } finally { trans.Dispose(); } if (sendReplyNotifications) { // send notification email to all subscribers. Do this outside the transaction so a failed email send action doesn't terminate the save process // of the message. ThreadManager.SendThreadReplyNotifications(threadID, userID, threadUpdatedNotificationTemplate, emailData); } return(messageID); }
/// <summary> /// Creates a new message in the given thread. Caller should validate input parameters. It potentially closes the thread if ordered to do so. /// </summary> /// <param name="threadId">Thread wherein the new message will be placed</param> /// <param name="userId">User who posted this message</param> /// <param name="messageText">Message text</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="userIdIPAddress">IP address of user calling this method</param> /// <param name="subscribeToThread">if set to <c>true</c> [subscribe to thread].</param> /// <param name="emailData">The email data.</param> /// <param name="sendReplyNotifications">Flag to signal to send reply notifications. If set to false no notifications are mailed, /// otherwise a notification is mailed to all subscribers to the thread the new message is posted in</param> /// <returns> /// MessageID if succeeded, 0 if not. /// </returns> private static async Task <int> CreateNewMessageInThreadAndPotentiallyCloseThread(int threadId, int userId, string messageText, string messageAsHTML, string userIdIPAddress, bool subscribeToThread, Dictionary <string, string> emailData, bool sendReplyNotifications) { var messageId = 0; using (var adapter = new DataAccessAdapter()) { await adapter.StartTransactionAsync(IsolationLevel.ReadCommitted, "InsertNewMessage").ConfigureAwait(false); try { var postingDate = DateTime.Now; var message = new MessageEntity { MessageText = messageText, MessageTextAsHTML = messageAsHTML, PostedByUserID = userId, PostingDate = postingDate, ThreadID = threadId, PostedFromIP = userIdIPAddress, }; messageId = await adapter.SaveEntityAsync(message).ConfigureAwait(false) ? message.MessageID : 0; if (messageId > 0) { await MessageManager.UpdateStatisticsAfterMessageInsert(threadId, userId, messageId, adapter, postingDate, true, subscribeToThread); } adapter.Commit(); } catch { adapter.Rollback(); throw; } } if (sendReplyNotifications) { // send notification email to all subscribers. Do this outside the transaction so a failed email send action doesn't terminate the save process // of the message. await ThreadManager.SendThreadReplyNotifications(threadId, userId, emailData).ConfigureAwait(false); } return(messageId); }
/// <summary> /// Creates a new message in the given thread and closes the thread right after the addition of the message, /// which makes the just added message the 'close' message of the thread. Close messages are handy when the /// closure of a thread is not obvious. /// Caller should validate input parameters. /// </summary> /// <param name="threadID">Thread wherein the new message will be placed</param> /// <param name="userID">User who posted this message</param> /// <param name="messageText">Message text</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="userIDIPAddress">IP address of user calling this method</param> /// <param name="messageAsXML">Message text as XML, which is the result of the parse action on MessageText.</param> /// <param name="threadUpdatedNotificationTemplate">The thread updated notification template.</param> /// <param name="emailData">The email data.</param> /// <param name="sendReplyNotifications">Flag to signal to send reply notifications. If set to false no notifications are mailed, /// otherwise a notification is mailed to all subscribers to the thread the new message is posted in</param> /// <returns>MessageID if succeeded, 0 if not.</returns> public static int CreateNewMessageInThreadAndCloseThread(int threadID, int userID, string messageText, string messageAsHTML, string userIDIPAddress, string messageAsXML, string threadUpdatedNotificationTemplate, Dictionary <string, string> emailData, bool sendReplyNotifications) { Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "InsertNewMessage"); int messageID = 0; try { DateTime postingDate = DateTime.Now; messageID = InsertNewMessage(threadID, userID, messageText, messageAsHTML, userIDIPAddress, messageAsXML, trans, postingDate); MessageManager.UpdateStatisticsAfterMessageInsert(threadID, userID, trans, postingDate, false, false); ThreadEntity thread = new ThreadEntity(); trans.Add(thread); thread.FetchUsingPK(threadID); thread.IsClosed = true; thread.IsSticky = false; thread.MarkedAsDone = true; bool result = thread.Save(); if (result) { // save succeeded, so remove from queue, pass the current transaction to the method so the action takes place inside this transaction. SupportQueueManager.RemoveThreadFromQueue(threadID, trans); } trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } finally { trans.Dispose(); } if (sendReplyNotifications) { // send notification email to all subscribers. Do this outside the transaction so a failed email send action doesn't terminate the save process // of the message. ThreadManager.SendThreadReplyNotifications(threadID, userID, threadUpdatedNotificationTemplate, emailData); } return(messageID); }
/// <summary> /// Creates a new thread in the given forum and places the passed in message as the first message in the thread. /// Caller should validate input parameters. /// </summary> /// <param name="forumID">Forum wherein the new thread will be placed</param> /// <param name="userID">User who started this thread</param> /// <param name="subject">Subject of the thread</param> /// <param name="messageText">First message of the thread</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="isSticky">Flag if the thread is sticky / pinned (true) or not (false)</param> /// <param name="userIDIPAddress">IP address of user calling this method</param> /// <param name="defaultSupportQueueID">The ID of the default support queue for this forum. If not null, the thread created will be /// added to the support queue with the ID specified.</param> /// <param name="messageID">The message ID of the new message, which is the start message in the thread.</param> /// <returns>ThreadID if succeeded, 0 if not.</returns> public static int CreateNewThreadInForum(int forumID, int userID, string subject, string messageText, string messageAsHTML, bool isSticky, string userIDIPAddress, int?defaultSupportQueueID, bool subscribeToThread, out int messageID) { Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "NewThread"); try { DateTime postDate = DateTime.Now; ThreadEntity newThread = new ThreadEntity(); newThread.ForumID = forumID; newThread.IsClosed = false; newThread.IsSticky = isSticky; newThread.StartedByUserID = userID; newThread.Subject = subject; newThread.ThreadLastPostingDate = postDate; if (defaultSupportQueueID.HasValue) { // a support queue has been specified as the default support queue for this forum. Add the new thread to this support queue. SupportQueueThreadEntity supportQueueThread = new SupportQueueThreadEntity(); supportQueueThread.QueueID = defaultSupportQueueID.Value; supportQueueThread.PlacedInQueueByUserID = userID; supportQueueThread.PlacedInQueueOn = DateTime.Now; // assign the Thread property to the newly created thread entity, so this supportqueuethreadentity will be part of the graph // of objects which will be saved. supportQueueThread.Thread = newThread; } DateTime postingDate = DateTime.Now; MessageEntity newMessage = new MessageEntity(); newMessage.MessageText = messageText; newMessage.MessageTextAsHTML = messageAsHTML; newMessage.PostedByUserID = userID; newMessage.PostingDate = postingDate; newMessage.PostedFromIP = userIDIPAddress; newMessage.Thread = newThread; // add the newMessage entity to the transaction object. All entities saved recursively will be added automatically trans.Add(newMessage); // save the complete graph newMessage.Save(true); messageID = newMessage.MessageID; int threadID = newMessage.ThreadID; // update thread statistics, this is the task for the message manager, and we pass the transaction object so the actions will run in // the same transaction. MessageManager.UpdateStatisticsAfterMessageInsert(threadID, userID, trans, postingDate, false, subscribeToThread); trans.Commit(); return(newThread.ThreadID); } catch (Exception) { trans.Rollback(); throw; } finally { trans.Dispose(); } }
/// <summary> /// Creates a new thread in the given forum and places the passed in message as the first message in the thread. /// Caller should validate input parameters. /// </summary> /// <param name="forumId">Forum wherein the new thread will be placed</param> /// <param name="userId">User who started this thread</param> /// <param name="subject">Subject of the thread</param> /// <param name="messageText">First message of the thread</param> /// <param name="messageAsHTML">Message text as HTML</param> /// <param name="isSticky">Flag if the thread is sticky / pinned (true) or not (false)</param> /// <param name="userIdIPAddress">IP address of user calling this method</param> /// <param name="defaultSupportQueueId">The ID of the default support queue for this forum. If not null, the thread created will be /// added to the support queue with the ID specified.</param> /// <param name="subscribeToThread">If true, the user with userid is automatically subscribed to the new thread created</param> /// <returns>tuple with ThreadID and messageid. ThreadId, if succeeded, is set to the threadid of the new thread, or 0 if failed. /// The message ID is the id of the new message, which is the start message in the thread.</returns> public static async Task <(int threadId, int messageId)> CreateNewThreadInForumAsync(int forumId, int userId, string subject, string messageText, string messageAsHTML, bool isSticky, string userIdIPAddress, int?defaultSupportQueueId, bool subscribeToThread) { var newThread = new ThreadEntity { ForumID = forumId, IsClosed = false, IsSticky = isSticky, StartedByUserID = userId, Subject = subject, }; if (defaultSupportQueueId.HasValue) { // a support queue has been specified as the default support queue for this forum. Add the new thread to this support queue. newThread.SupportQueueThread = new SupportQueueThreadEntity { QueueID = defaultSupportQueueId.Value, PlacedInQueueByUserID = userId, PlacedInQueueOn = DateTime.Now, // No need to set the Thread property, as it's auto-assigned due to the assignment of newThread.SupportQueueThread. }; } var newMessage = new MessageEntity { MessageText = messageText, MessageTextAsHTML = messageAsHTML, PostedByUserID = userId, PostingDate = DateTime.Now, PostedFromIP = userIdIPAddress, Thread = newThread }; // Assign a new statistics entity so we can track what the last message is. newThread.Statistics = new ThreadStatisticsEntity() { LastMessage = newMessage, NumberOfMessages = 1, NumberOfViews = 1 }; using (var adapter = new DataAccessAdapter()) { await adapter.StartTransactionAsync(IsolationLevel.ReadCommitted, "NewThread").ConfigureAwait(false); try { // save the complete graph await adapter.SaveEntityAsync(newMessage, true).ConfigureAwait(false); var messageId = newMessage.MessageID; var threadId = newMessage.ThreadID; // update thread statistics, this is the task for the message manager, and we pass the adapter so the actions will run in // the same transaction. Pass -1 as newMessageId as we already have inserted this. await MessageManager.UpdateStatisticsAfterMessageInsert(threadId, userId, -1, adapter, DateTime.Now, false, subscribeToThread); adapter.Commit(); return(newThread.ThreadID, messageId); } catch { adapter.Rollback(); throw; } } }