public ActionResult PostFeedComment(FormCollection formCollection) { try { var comment = formCollection["comment"]; if (string.IsNullOrWhiteSpace(comment)) { return(RedirectToAction("Index")); } comment = comment.TrimStart(','); OsbideWebService client = new OsbideWebService(); Authentication auth = new Authentication(); string key = auth.GetAuthenticationKey(); if (string.IsNullOrEmpty(comment) == false) { EventLog log = new EventLog(); log.SenderId = CurrentUser.Id; log.LogType = FeedPostEvent.Name; FeedPostEvent commentEvent = new FeedPostEvent(); commentEvent.Comment = comment; log.Data.BinaryData = EventFactory.ToZippedBinary(commentEvent); log = client.SubmitLog(log, CurrentUser); //find all of this user's subscribers and send them an email List <OsbideUser> observers = new List <OsbideUser>(); observers = (from subscription in Db.UserSubscriptions join user in Db.Users on new { InstitutionId = subscription.ObserverInstitutionId, SchoolId = subscription.ObserverSchoolId } equals new { InstitutionId = user.InstitutionId, SchoolId = user.SchoolId } where subscription.SubjectSchoolId == CurrentUser.SchoolId && subscription.SubjectInstitutionId == CurrentUser.InstitutionId && user.ReceiveEmailOnNewFeedPost == true select user).ToList(); if (observers.Count > 0) { string url = StringConstants.GetActivityFeedDetailsUrl(log.Id); string body = "Greetings,<br />{0} posted a new item to the activity feed:<br />\"{1}\"<br />To view this " + "conversation online, please visit {2} or visit your OSBIDE user profile.<br /><br />Thanks,\nOSBIDE<br /><br />" + "These automated messages can be turned off by editing your user profile."; body = string.Format(body, CurrentUser.FirstAndLastName, comment, url); List <MailAddress> to = new List <MailAddress>(); foreach (OsbideUser user in observers) { to.Add(new MailAddress(user.Email)); } Email.Send("[OSBIDE] New Activity Post", body, to); } } } catch (Exception ex) { LogErrorMessage(ex); } return(RedirectToAction("Index")); }
public EventLog SubmitLog(EventLog log, OsbideUser user) { LocalErrorLog errorLogger = new LocalErrorLog(); errorLogger.SenderId = user.Id; errorLogger.Content = "About to save log " + log.LogType + " to DB. "; errorLogger.LogDate = DateTime.Now; log.Sender = null; log.SenderId = user.Id; log.DateReceived = DateTime.UtcNow; log.EventTypeId = Convert.ToInt32(Enum.Parse(typeof(EventTypes), log.LogType)); //insert into the DB Db.EventLogs.Add(log); try { Db.SaveChanges(); errorLogger.Content += "Item saved. "; } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { errorLogger.Content += "Error saving: "; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); errorLogger.Content += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } Db.LocalErrorLogs.Add(errorLogger); Db.SaveChanges(); return(null); } catch (Exception ex) { errorLogger.Content += "Error saving: " + ex.Message; System.Diagnostics.Trace.TraceInformation(ex.Message); Db.LocalErrorLogs.Add(errorLogger); Db.SaveChanges(); return(null); } //Tease apart log information and insert into the appropriate DB table IOsbideEvent evt = null; try { errorLogger.Content += "About to unzip event."; evt = EventFactory.FromZippedBinary(log.Data.BinaryData, new OsbideDeserializationBinder()); evt.EventLogId = log.Id; } catch (Exception) { errorLogger.Content += "Error unzipping event."; Db.LocalErrorLogs.Add(errorLogger); Db.SaveChanges(); return(null); } var hashtags = string.Empty; var usertags = string.Empty; if (log.LogType == AskForHelpEvent.Name) { Db.AskForHelpEvents.Add((AskForHelpEvent)evt); AskForHelpEvent ask = evt as AskForHelpEvent; //send email to interested parties //find all of this user's subscribers and send them an email List <OsbideUser> observers = new List <OsbideUser>(); observers = (from subscription in Db.UserSubscriptions join dbUser in Db.Users on new { InstitutionId = subscription.ObserverInstitutionId, SchoolId = subscription.ObserverSchoolId } equals new { InstitutionId = user.InstitutionId, SchoolId = user.SchoolId } where subscription.SubjectSchoolId == user.SchoolId && subscription.SubjectInstitutionId == user.InstitutionId && dbUser.ReceiveEmailOnNewFeedPost == true select dbUser).ToList(); if (observers.Count > 0) { string url = StringConstants.GetActivityFeedDetailsUrl(log.Id); string body = "Greetings,<br />{0} asked for help regarding the following item:<br />\"{1}\"<br />To view this " + "conversation online, please visit {2} or visit your OSBIDE user profile.<br /><br />Thanks,<br />OSBIDE<br /><br />" + "These automated messages can be turned off by editing your user profile."; body = string.Format(body, user.FirstAndLastName, ask.UserComment, url); List <MailAddress> to = new List <MailAddress>(); foreach (OsbideUser observer in observers) { to.Add(new MailAddress(observer.Email)); } Email.Send("[OSBIDE] Someone has asked for help!", body, to); } } else if (log.LogType == BuildEvent.Name) { BuildEvent build = (BuildEvent)evt; Db.BuildEvents.Add(build); string pattern = "error ([^:]+)"; //strip out non-critical errors List <BuildEventErrorListItem> errorItems = build.ErrorItems.Where(e => e.ErrorListItem.CriticalErrorName.Length > 0).ToList(); build.ErrorItems.Clear(); build.ErrorItems = errorItems; //log all errors in their own DB for faster search List <string> errors = new List <string>(); Dictionary <string, ErrorType> errorTypes = new Dictionary <string, ErrorType>(); foreach (BuildEventErrorListItem item in build.ErrorItems) { Match match = Regex.Match(item.ErrorListItem.Description, pattern); //ignore bad matches if (match.Groups.Count == 2) { string errorCode = match.Groups[1].Value.ToLower().Trim(); ErrorType type = Db.ErrorTypes.Where(t => t.Name == errorCode).FirstOrDefault(); if (type == null) { if (errorTypes.ContainsKey(errorCode) == false) { type = new ErrorType() { Name = errorCode }; Db.ErrorTypes.Add(type); } else { type = errorTypes[errorCode]; } } if (errorCode.Length > 0 && errors.Contains(errorCode) == false) { errors.Add(errorCode); } errorTypes[errorCode] = type; } } Db.SaveChanges(); foreach (string errorType in errors) { Db.BuildErrors.Add(new BuildError() { BuildErrorTypeId = errorTypes[errorType].Id, LogId = log.Id }); } } else if (log.LogType == CutCopyPasteEvent.Name) { Db.CutCopyPasteEvents.Add((CutCopyPasteEvent)evt); } else if (log.LogType == DebugEvent.Name) { Db.DebugEvents.Add((DebugEvent)evt); } else if (log.LogType == EditorActivityEvent.Name) { Db.EditorActivityEvents.Add((EditorActivityEvent)evt); } else if (log.LogType == ExceptionEvent.Name) { Db.ExceptionEvents.Add((ExceptionEvent)evt); } else if (log.LogType == FeedPostEvent.Name) { hashtags = string.Join(",", ParseHashtags(((FeedPostEvent)evt).Comment)); usertags = string.Join(",", ParseUserTags(((FeedPostEvent)evt).Comment)); Db.FeedPostEvents.Add((FeedPostEvent)evt); } else if (log.LogType == HelpfulMarkGivenEvent.Name) { Db.HelpfulMarkGivenEvents.Add((HelpfulMarkGivenEvent)evt); } else if (log.LogType == LogCommentEvent.Name) { Db.LogCommentEvents.Add((LogCommentEvent)evt); } else if (log.LogType == SaveEvent.Name) { Db.SaveEvents.Add((SaveEvent)evt); } else if (log.LogType == SubmitEvent.Name) { errorLogger.Content += "Submit event detected. "; Db.SubmitEvents.Add((SubmitEvent)evt); } try { errorLogger.Content += "Attempting to save to DB. "; Db.SaveChanges(); /* * if(hashtags.Length >= 0 || usertags.Length >= 0 ) * * using (var context = new OsbideProcs()) * { * context.InsertPostTags(log.Id, usertags, hashtags); * } * */ } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { errorLogger.Content += "Error saving to DB: "; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); errorLogger.Content += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); Db.LocalErrorLogs.Add(errorLogger); Db.SaveChanges(); } } return(null); } catch (Exception ex) { errorLogger.Content += "Error saving to DB: " + ex.Message; Db.LocalErrorLogs.Add(errorLogger); Db.SaveChanges(); System.Diagnostics.Trace.TraceInformation(ex.Message); return(null); } //Db.LocalErrorLogs.Add(errorLogger); //Db.SaveChanges(); return(log); }
/// <summary> /// Helper method for posting a comment. Will return true if everything went OK, false otherwise /// </summary> /// <param name="logId"></param> /// <param name="comment"></param> /// <returns></returns> protected bool PostComment(string logId, string comment) { int id = -1; if (Int32.TryParse(logId, out id) == true) { //comments made on comments or mark helpful events need to be routed back to the original source IOsbideEvent checkEvent = Db.LogCommentEvents.Where(l => l.EventLogId == id).FirstOrDefault(); if (checkEvent != null) { id = (checkEvent as LogCommentEvent).SourceEventLogId; } else { checkEvent = Db.HelpfulMarkGivenEvents.Where(l => l.EventLogId == id).FirstOrDefault(); if (checkEvent != null) { id = (checkEvent as HelpfulMarkGivenEvent).LogCommentEvent.SourceEventLogId; } } LogCommentEvent logComment = new LogCommentEvent() { Content = comment, SourceEventLogId = id, SolutionName = "OSBIDE" }; OsbideWebService client = new OsbideWebService(); Authentication auth = new Authentication(); string key = auth.GetAuthenticationKey(); EventLog log = null; if (string.IsNullOrEmpty(comment) == false) { log = new EventLog(logComment, CurrentUser); log = client.SubmitLog(log, CurrentUser); } else { return(false); } logComment = Db.LogCommentEvents.Where(l => l.EventLogId == log.Id).FirstOrDefault(); //the code below performs two functions: // 1. Send interested parties email notifications // 2. Log the comment in the social activity log (displayed on an individual's profile page) //find others that have posted on this same thread List <OsbideUser> interestedParties = Db.LogCommentEvents .Where(l => l.SourceEventLogId == id) .Where(l => l.EventLog.SenderId != CurrentUser.Id) .Select(l => l.EventLog.Sender) .ToList(); //(email only) find those that are subscribed to this thread List <OsbideUser> subscribers = (from logSub in Db.EventLogSubscriptions join user in Db.Users on logSub.UserId equals user.Id where logSub.LogId == id && logSub.UserId != CurrentUser.Id && user.ReceiveNotificationEmails == true select user).ToList(); //check to see if the author wants to be notified of posts OsbideUser eventAuthor = Db.EventLogs.Where(l => l.Id == id).Select(l => l.Sender).FirstOrDefault(); //master list shared between email and social activity log Dictionary <int, OsbideUser> masterList = new Dictionary <int, OsbideUser>(); if (eventAuthor != null) { masterList.Add(eventAuthor.Id, eventAuthor); } foreach (OsbideUser user in interestedParties) { if (masterList.ContainsKey(user.Id) == false) { masterList.Add(user.Id, user); } } //add the current user for activity log tracking, but not for emails OsbideUser creator = new OsbideUser(CurrentUser); creator.ReceiveNotificationEmails = false; //force no email send on the current user if (masterList.ContainsKey(creator.Id) == true) { masterList.Remove(creator.Id); } masterList.Add(creator.Id, creator); //update social activity foreach (OsbideUser user in masterList.Values) { CommentActivityLog social = new CommentActivityLog() { TargetUserId = user.Id, LogCommentEventId = logComment.Id }; Db.CommentActivityLogs.Add(social); } Db.SaveChanges(); //form the email list SortedDictionary <int, OsbideUser> emailList = new SortedDictionary <int, OsbideUser>(); //add in interested parties from our master list foreach (OsbideUser user in masterList.Values) { if (user.ReceiveNotificationEmails == true) { if (emailList.ContainsKey(user.Id) == false) { emailList.Add(user.Id, user); } } } //add in subscribers to email list foreach (OsbideUser user in subscribers) { if (emailList.ContainsKey(user.Id) == false) { emailList.Add(user.Id, user); } } //send emails if (emailList.Count > 0) { //send email string url = StringConstants.GetActivityFeedDetailsUrl(id); string body = "Greetings,<br />{0} has commented on a post that you have previously been involved with:<br />\"{1}\"<br />To view this " + "conversation online, please visit {2} or visit your OSBIDE user profile.<br /><br />Thanks,<br />OSBIDE<br /><br />" + "These automated messages can be turned off by editing your user profile."; body = string.Format(body, logComment.EventLog.Sender.FirstAndLastName, logComment.Content, url); List <MailAddress> to = new List <MailAddress>(); foreach (OsbideUser user in emailList.Values) { to.Add(new MailAddress(user.Email)); } Email.Send("[OSBIDE] Activity Notification", body, to); } } return(true); }