public MakerReturn Post() { MakerReturn r = new MakerReturn(); #region look for duplicate Query qDup = new Query(); qDup.TopRecords = 1; qDup.Columns = new ColumnSet(Bobs.Comment.Columns.K, Bobs.Comment.Columns.ThreadK); qDup.QueryCondition = new Q(Bobs.Comment.Columns.DuplicateGuid, (Guid)DuplicateGuid); CommentSet csDup = new CommentSet(qDup); if (csDup.Count > 0) { r.Success = false; r.MessageHtml = "Duplicate thread detected"; r.Thread = csDup[0].Thread; r.Duplicate = true; return r; } #endregion #region ensure two parents are different types... if (ParentType.Equals(Model.Entities.ObjectType.Group) && GroupK != ParentK) { r.Success = false; r.MessageHtml = "Posting to group, but GroupK!=ParentK"; return r; } #endregion #region Make sure Group and ParentType are syncronised if (GroupK > 0 && ParentType.Equals(Model.Entities.ObjectType.None)) { ParentType = Model.Entities.ObjectType.Group; ParentK = GroupK; } if (GroupK == 0 && ParentType.Equals(Model.Entities.ObjectType.Group)) { GroupK = ParentK; } #endregion #region if we're posting in a group, make sure we are a member if (GroupK > 0) { GroupUsr gu = ParentGroup.GetGroupUsr(PostingUsr); if (!PostingUsr.CanGroupMember(gu)) { r.Success = false; r.MessageHtml = "You have tried to post a message in the " + ParentGroup.Link() + " group, but you're not currently a member of this group. Check out the group homepage for more information."; return r; } } #endregion #region ensure both parents map to real objects if (!ParentType.Equals(Model.Entities.ObjectType.None)) { if (ParentBob == null) { r.Success = false; r.MessageHtml = "You've tried to post a message in a non-existant chat. Maybe it's been deleted."; return r; } } if (GroupK > 0) { if (ParentGroup == null) { r.Success = false; r.MessageHtml = "You've tried to post a message in a non-existant group. Maybe it's been deleted."; return r; } } #endregion #region Private bool tPrivate = false; bool gPrivate = false; if (Private) { if (GroupK == 0) tPrivate = true; else gPrivate = true; } #endregion #region News tNews = false; tNewsStatus = NewsStatusEnum.None; int tNewsLevel = 0; int tNewsUsrK = 0; int tNewsModeratorUsrK = 0; if (News) { if (tPrivate) { r.Success = false; r.MessageHtml = "You've tried to post private news! How did this happen?"; return r; } else if (GroupK == 0) { r.Success = false; r.MessageHtml = "You've tried to post public non-group news. How did this happen?"; return r; //if (PostingUsr.NewsPermissionLevel >= 10 || PostingUsr.IsSuper) //{ // tNews = true; // tNewsStatus = NewsStatusEnum.Recommended; // tNewsModeratorUsrK = Usr.GetNewsModeratorUsrK(); // tNewsLevel = 10; // tNewsUsrK = PostingUsr.K; //} //else //{ // tNews = false; // tNewsStatus = NewsStatusEnum.Recommended; // tNewsModeratorUsrK = Usr.GetNewsModeratorUsrK(); // tNewsLevel = 0; // tNewsUsrK = PostingUsr.K; //} } else { GroupUsr gu = ParentGroup.GetGroupUsr(PostingUsr); if (PostingUsr.CanGroupNewsAdmin(gu)) { tNews = true; tNewsStatus = NewsStatusEnum.Done; tNewsLevel = 10; tNewsUsrK = PostingUsr.K; } else { tNews = false; tNewsStatus = NewsStatusEnum.Recommended; tNewsLevel = 10; tNewsUsrK = PostingUsr.K; } } } #endregion #region Sealed bool tSealed = false; if (Sealed) { if (tPrivate) tSealed = true; } #endregion #region Closed bool tClosed = false; if (Closed) { tClosed = true; } #endregion #region Review bool tReview = false; if (Review) { if (ParentType.Equals(Model.Entities.ObjectType.Event) && GroupK == 0) { Query qReviews = new Query(); qReviews.QueryCondition = new And( new Q(Columns.UsrK, PostingUsr.K), new Q(Columns.ParentObjectType, Model.Entities.ObjectType.Event), new Q(Columns.ParentObjectK, ParentK), new Q(Columns.IsReview, true) ); qReviews.TopRecords = 1; qReviews.Columns = new ColumnSet(Thread.Columns.K); ThreadSet tsReviews = new ThreadSet(qReviews); if (tsReviews.Count > 0) { r.Success = false; r.MessageHtml = "You've already posted a review. You can only post one review per event."; r.Duplicate = true; r.Thread = tsReviews[0]; return r; } else tReview = true; } else { r.Success = false; r.MessageHtml = "You must post a review in an event chat board."; return r; } } #endregion if (GroupK > 0) CurrentGroupUsr = ParentGroup.GetGroupUsr(PostingUsr); Transaction trans = null;//new Transaction(); NewThread = new Thread(); try { #region Create thread NewThread.DateTime = DateTime.Now; NewThread.Enabled = true; NewThread.UsrK = PostingUsr.K; NewThread.LastPostUsrK = PostingUsr.K; NewThread.Subject = Helpers.Strip(Subject, true, true, true, true); NewThread.ParentObjectType = ParentType; NewThread.ParentObjectK = ParentK; if (GroupK > 0) NewThread.GroupK = ParentGroup.K; else NewThread.GroupK = 0; NewThread.Private = tPrivate; NewThread.GroupPrivate = gPrivate; if (GroupK > 0) NewThread.PrivateGroup = ParentGroup.PrivateChat; else NewThread.PrivateGroup = false; NewThread.IsNews = tNews; NewThread.NewsStatus = tNewsStatus; NewThread.NewsLevel = tNewsLevel; NewThread.NewsModeratorUsrK = tNewsModeratorUsrK; NewThread.NewsUsrK = tNewsUsrK; NewThread.Sealed = tSealed; NewThread.Closed = tClosed; NewThread.IsReview = tReview; NewThread.UpdateAncestorLinksNoUpdate(); NewThread.Update(trans); #endregion #region Add PostingUsr ThreadUsr CurrentThreadUsr = new ThreadUsr(); CurrentThreadUsr.DateTime = DateTime.Now; CurrentThreadUsr.InvitingUsrK = PostingUsr.K; CurrentThreadUsr.UsrK = PostingUsr.K; CurrentThreadUsr.ThreadK = NewThread.K; CurrentThreadUsr.ChangeStatus(ThreadUsr.StatusEnum.Archived, NewThread.DateTime); CurrentThreadUsr.StatusChangeObjectK = PostingUsr.K; CurrentThreadUsr.StatusChangeObjectType = Model.Entities.ObjectType.Usr; CurrentThreadUsr.Update(trans); #endregion #region Make the comment Comment.Maker cMaker = new Comment.Maker(); cMaker.Body = Body; cMaker.ParentThread = NewThread; cMaker.DuplicateGuid = DuplicateGuid; cMaker.PostingUsr = PostingUsr; cMaker.InviteKs = InviteKs; cMaker.AlertedUsrs = AlertedUsrs; cMaker.NewThread = true; cMaker.CurrentThreadUsr = CurrentThreadUsr; cMaker.CurrentGroupUsr = CurrentGroupUsr; cMaker.RunAsync = RunAsync; cMaker.DisableLiveChatMessage = DisableLiveChatMessage; Comment.MakerReturn cReturn = null; try { cReturn = cMaker.Post(trans); } catch (Exception ex) { NewThread.DeleteAll(null); throw ex; } #endregion //SendThreadAlertsJob sendThreadsAlert = new SendThreadAlertsJob(NewThread.K, AlertedUsrs, PostingUsr.K, Subject, InviteKs); //if (this.RunAsync) //{ // sendThreadsAlert.ExecuteAsynchronously(); //} //else //{ // sendThreadsAlert.ExecuteSynchronously(); //} r.Success = true; r.Thread = NewThread; r.Comment = cReturn.Comment; return r; } catch (Exception ex) { //trans.Rollback(); NewThread.DeleteAll(null); throw ex; } finally { //trans.Close(); } }
public MakerReturn Post(Transaction transaction) { if (HttpContext.Current!=null) HttpContext.Current.Items["VisitComments"]=1; MakerReturn r = new MakerReturn(); #region Look for duplicates Query qDuplicate = new Query(); qDuplicate.QueryCondition=new Q(Comment.Columns.DuplicateGuid,(Guid)DuplicateGuid); qDuplicate.Columns=new ColumnSet(Comment.Columns.K, Comment.Columns.IndexInThread, Comment.Columns.ThreadK); CommentSet csDuplicate = new CommentSet(qDuplicate); if (csDuplicate.Count>0) { r.Success = false; r.Duplicate = true; r.Comment = csDuplicate[0]; return r; } #endregion #region Check permission if (!NewThread && !ParentThread.CheckPermissionPost(PostingUsr, CurrentThreadUsr, CurrentGroupUsr)) { r.Success=false; r.MessageHtml="You can't post in this topic."; return r; } #endregion #region Check for closed thread if (!NewThread && ParentThread.Closed) { r.Success=false; r.MessageHtml="This topic is closed - posting is disabled."; return r; } #endregion #region Create comment NewComment = new Comment(); NewComment.DateTime = DateTime.Now; if (HttpContext.Current!=null) NewComment.Ip = Utilities.TruncateIp(HttpContext.Current.Request.ServerVariables["REMOTE_HOST"]); if (ChatItemGuid == null) NewComment.ChatItemGuid = Guid.NewGuid(); else NewComment.ChatItemGuid = ChatItemGuid; NewComment.Enabled = true; NewComment.Text = Body; NewComment.ThreadK = ParentThread.K; NewComment.UsrK = PostingUsr.K; if (!ParentThread.Private) { PostingUsr.CommentCount++; PostingUsr.Update(transaction); } NewComment.DuplicateGuid = (Guid)DuplicateGuid; NewComment.IndexInThread = ParentThread.TotalComments; NewComment.Update(transaction); #endregion #region Update thread ParentThread.LastPost=NewComment.DateTime; ParentThread.LastPostUsrK=NewComment.UsrK; ParentThread.Update(transaction); ParentThread.UpdateTotalComments(transaction); #endregion #region Update / create ThreadUsr CurrentThreadUsr.ChangeStatus(ThreadUsr.StatusEnum.Archived, NewComment.DateTime); CurrentThreadUsr.ViewDateTime = NewComment.DateTime; CurrentThreadUsr.ViewDateTimeLatest = NewComment.DateTime; CurrentThreadUsr.ViewComments = ParentThread.TotalComments; CurrentThreadUsr.ViewCommentsLatest = ParentThread.TotalComments; CurrentThreadUsr.Update(transaction); #endregion string chatMessageBody = ""; bool alreadySentChatMessage = false; bool commentIsPrivate = ParentThread.Private || ParentThread.GroupPrivate || ParentThread.PrivateGroup; if (!DisableLiveChatMessage) { chatMessageBody = Chat.GetMessageFromCommentBody(Body); //If the thread is subject to group privacy and we are inviting people, we should wait until after all the invites have been processed before sending the //message, or we might sent the chat message to people who don't have permission to see the thread bool commentIsSubjectToGroupPrivacy = ParentThread.GroupPrivate || ParentThread.PrivateGroup; bool canPostImmediatly = !commentIsSubjectToGroupPrivacy || InviteKs.Count == 0; if (!NewThread && canPostImmediatly) { //If we're NOT posting a new thread, we should post the chat message immediatly... //if we are posting a new thread, we should wait until after all the invites have been done, so we have a better list of participants. sendChatMessageToParticipantsNow(InviteKs, NewComment, ParentThread, PostingUsr, chatMessageBody); alreadySentChatMessage = true; } if (!commentIsPrivate) { //Since we don't invite anyone to this room, we can post immediatly to the random chat room. //CommentMessageStub ms = getChatMessage( // //new Chat.RoomSpec(RoomType.RandomChat).Guid, // new Chat.RoomSpec(RoomType.PublicStream).Guid, // ParentThread.GetRoomSpec().Guid, // NewComment, // ParentThread, // PostingUsr, // chatMessageBody); //Chat.SendJsonChatItem(ms); CommentMessageStub ms1 = getChatMessage( new Chat.RoomSpec(RoomType.PublicStream).Guid, ParentThread.GetRoomSpec().Guid, NewComment, ParentThread, PostingUsr, chatMessageBody); Chat.SendJsonChatItem(ms1); } } try { if (NewThread && !commentIsPrivate && PostingUsr.FacebookConnected) { bool sentToFacebook = false; if (NewComment.Thread.IsReview && PostingUsr.FacebookStoryEventReview) { FacebookPost.CreateEventReview(PostingUsr, NewComment.Thread, NewComment); sentToFacebook = true; } if (!sentToFacebook && NewComment.Thread.IsNews && PostingUsr.FacebookStoryPostNews) { FacebookPost.CreateEventReview(PostingUsr, NewComment.Thread, NewComment); sentToFacebook = true; } if (!sentToFacebook && PostingUsr.FacebookStoryNewTopic) { FacebookPost.CreateNewTopic(PostingUsr, NewComment.Thread, NewComment); } } } catch { } SendCommentAlertsJob job = new SendCommentAlertsJob( this.ParentThread, this.PostingUsr, this.NewComment, this.NewThread, this.AlertedUsrs, this.InviteKs, DisableLiveChatMessage, alreadySentChatMessage, chatMessageBody); if (Vars.DevEnv) { job.ExecuteSynchronously(); } else { if (this.RunAsync) job.ExecuteAsynchronously(); else job.ExecuteSynchronously(); } if (Usr.Current != null && PostingUsr.K != Usr.DsiUsrK) { Usr.IncrementSpamBotDefeaterCounter(Usr.SpamBotDefeaterCounter.Comments, Usr.Current.K); } r.Success = true; r.Comment = NewComment; return r; }