public void AddLog(CommentActivityLog activityLog) { DateTime dateKey = activityLog.LogCommentEvent.EventDate.Date; int logKey = activityLog.LogCommentEvent.SourceEventLogId; int userKey = activityLog.LogCommentEvent.EventLog.SenderId; //organized first by date if (_activites.ContainsKey(dateKey) == false) { _activites.Add(dateKey, new SortedDictionary <int, SortedDictionary <int, CommentActivityLog> >()); } //then the ID of the event that they track if (_activites[dateKey].ContainsKey(logKey) == false) { _activites[dateKey].Add(logKey, new SortedDictionary <int, CommentActivityLog>()); } //then the user the generated the event if (_activites[dateKey][logKey].ContainsKey(userKey) == false) { _activites[dateKey][logKey][userKey] = activityLog; } else { //we want the most recent post by the user, replace if we are storing an older item CommentActivityLog other = _activites[dateKey][logKey][userKey]; if (activityLog.LogCommentEvent.EventDate > other.LogCommentEvent.EventDate) { _activites[dateKey][logKey][userKey] = activityLog; } } }
public ActionResult Index(int?id, int timestamp = -1) { if (null != ActiveCourseUser) { ViewBag.HideMail = OSBLE.Utility.DBHelper.GetAbstractCourseHideMailValue(ActiveCourseUser.AbstractCourseID); } else { ViewBag.HideMail = true; ViewBag.errorMessage = "No user information yet available. Please sign up for a course first."; ViewBag.errorName = "No Courses"; return(View("Error")); } try { var query = new ActivityFeedQuery(ActiveCourseUser.AbstractCourseID); var subscriptionsQuery = new ActivityFeedQuery(ActiveCourseUser.AbstractCourseID); ProfileViewModel vm = new ProfileViewModel(); vm.User = CurrentUser; if (id != null) { UserProfile user = DBHelper.GetUserProfile((int)id); if (user != null && ActiveCourseUser.AbstractCourseID != (int)CourseRole.CourseRoles.Observer) { vm.User = user; } } if (timestamp > 0) { DateTime pullDate = new DateTime(timestamp); query.StartDate = pullDate; } //Only show social events foreach (var evt in ActivityFeedQuery.GetSocialEvents()) { query.AddEventType(evt); subscriptionsQuery.AddEventType(evt); } //add in the list of users that the current person cares about query.AddSubscriptionSubject(vm.User); //build the feed view model vm.Feed = new FeedViewModel(); vm.Feed.Feed = AggregateFeedItem.FromFeedItems(query.Execute().ToList()); vm.Feed.LastLogId = -1; vm.Feed.SingleUserId = vm.User.ID; vm.Feed.LastPollDate = query.StartDate; //vm.Score = Db.UserScores.Where(s => s.UserId == vm.User.Id).FirstOrDefault(); using (SqlConnection conn = DBHelper.GetNewConnection()) { int AskForHelpValue = conn.Query <int>("SELECT e.EventTypeId " + "FROM EventTypes e " + "WHERE e.EventTypeName = 'AskForHelpEvent' ").FirstOrDefault(); int FeedPostValue = conn.Query <int>("SELECT e.EventTypeId " + "FROM EventTypes e " + "WHERE e.EventTypeName = 'FeedPostEvent'").FirstOrDefault(); int LogCommentValue = conn.Query <int>("SELECT e.EventTypeId " + "FROM EventTypes e " + "WHERE e.EventTypeName = 'LogCommentEvent'").FirstOrDefault(); var posts = conn.Query <EventLog>( "SELECT * " + "FROM EventLogs " + "WHERE EventTypeId = @fpe " + "AND SenderId = @UserId " + "ORDER BY EventDate DESC", new { fpe = FeedPostValue, afhe = AskForHelpValue, UserId = vm.User.ID }).ToList(); vm.NumberOfPosts = posts.Count; var comments = DBHelper.GetCommentsForUserID(vm.User.ID, conn); //var comments = conn.Query<EventLog>( // "SELECT * " + // "FROM EventLogs e " + // "WHERE (e.EventTypeId = @lcv " + // "AND e.SenderId = @UserId) " + // "ORDERBY e.EventDate DESCENDING", new { lcv = LogCommentValue, UserId = CurrentUser.ID }).ToList(); vm.NumberOfComments = comments.Count(); //vm.NumberOfPosts = (from e in Db.EventLogs // where (e.LogType == FeedPostEvent.Name || e.LogType == AskForHelpEvent.Name) // && e.SenderId == vm.User.Id // select e // ).Count(); //vm.NumberOfComments = Db.LogCommentEvents.Where(c => c.EventLog.SenderId == vm.User.Id).Count(); //if (vm.Score == null) //{ // vm.Score = new UserScore(); //} ////// need to figure this out //////var maxQuery = Db.EventLogs.Where(e => e.SenderId == vm.User.Id).Select(e => e.Id); //////if (maxQuery.Count() > 0) //////{ ////// vm.Feed.LastLogId = maxQuery.Max(); //////} // Build a catalog of recent commenting activity: // 1. Find all comments that the user has made // 2. Find all comments made by others on posts authored by the current user // 3. Find all comments made by others on posts on which the current user has written a comment DateTime maxLookback = DateTime.UtcNow.AddDays(-14); //1. find recent comments //c.LogCommentEvent //c.Id //c.LogCommentEventId //c.UserProfile //c.UserProfileId int i = 0; foreach (LogCommentEvent lce in comments) { lce.SourceEvent = DBHelper.GetActivityEvent(lce.SourceEventLogId, conn); lce.Sender = vm.User; CommentActivityLog cal = new CommentActivityLog() { Id = i, LogCommentEvent = lce, LogCommentEventId = lce.EventLogId, UserProfile = vm.User, UserProfileId = vm.User.ID }; if (ActiveCourseUser.AbstractCourseID == (int)CourseRole.CourseRoles.Observer) { cal.UserProfileId = 0; } i++; vm.SocialActivity.AddLog(cal); } //List<CommentActivityLog> socialLogs = (from social in Db.CommentActivityLogs // .Include("TargetUser") // .Include("LogCommentEvent") // .Include("LogCommentEvent.SourceEventLog") // .Include("LogCommentEvent.SourceEventLog.Sender") // .Include("LogCommentEvent") // .Include("LogCommentEvent.EventLog") // .Include("LogCommentEvent.EventLog.Sender") // where 1 == 1 // && social.LogCommentEvent.EventDate >= maxLookback // && // (social.TargetUserId == vm.User.Id || // social.LogCommentEvent.SourceEventLog.SenderId == vm.User.Id) // orderby social.LogCommentEvent.EventDate descending // select social // ).ToList(); } //show subscriptions only if the user is accessing his own page //if (vm.User.Id == CurrentUser.Id) //{ // List<int> eventLogIds = Db.EventLogSubscriptions.Where(s => s.UserId == vm.User.Id).Select(s => s.LogId).ToList(); // if (eventLogIds.Count > 0) // { // foreach (int logId in eventLogIds) // { // subscriptionsQuery.AddEventId(logId); // } // vm.EventLogSubscriptions = AggregateFeedItem.FromFeedItems(subscriptionsQuery.Execute().ToList()); // } //} ViewBag.IsInstructor = ActiveCourseUser.AbstractRoleID == (int)CourseRole.CourseRoles.Instructor; ViewBag.IsSelf = ActiveCourseUser != null ? ActiveCourseUser.UserProfileID == id || id == null : false; ViewBag.UploadedImages = GetImageFilesForCurrentUser(); return(View(vm)); } catch (Exception ex) { //LogErrorMessage(ex); ViewBag.errorMessage = ex.Message; ViewBag.errorName = ex.GetType().ToString(); return(View("Error")); } }
/// <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); }