Esempio n. 1
0
        public Thread CreateThread(User poster, string title, string body, PostLayer desiredLayer, DateTime date, int?forcedPostId)
        {
            PostLayer      actualLayer  = poster.getActualLayer(this, desiredLayer);
            AbstractChange threadInsert = new InsertChange(
                Thread.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { Thread.TableSpec.FIELD_BOARDID, new ScalarFieldValue(this.id.ToString()) },
                { Thread.TableSpec.FIELD_ISANNOUNCEMENT, new ScalarFieldValue("0") },
                { Thread.TableSpec.FIELD_ISLOCKED, new ScalarFieldValue("0") },
                { Thread.TableSpec.FIELD_TITLE, new ScalarFieldValue(title) },
                { Thread.TableSpec.FIELD_TOPICSTARTERID, new ScalarFieldValue(poster.id.ToString()) },
                { Thread.TableSpec.FIELD_TOTALPOSTS, new ScalarFieldValue("0") },
                { Thread.TableSpec.FIELD_TOTALVIEWS, new ScalarFieldValue("0") },
                { Thread.TableSpec.FIELD_FIRSTPOSTID, new ScalarFieldValue(null) },
                { Thread.TableSpec.FIELD_LASTPOSTID, new ScalarFieldValue(null) },
                { Thread.TableSpec.FIELD_LASTPOSTDATE, new ScalarFieldValue(date.ToUTCString()) },
            }
                );

            using (ChangeSet threadInsertSet = new ChangeSet(), dataInsertSet = new ChangeSet()) {
                Config.Transactional(transaction => {
                    threadInsertSet.Add(threadInsert);
                    threadInsertSet.Apply(transaction);
                    foreach (AbstractChange change in Thread.getNewPostChanges(this, threadInsert.getId().Value, null, poster, actualLayer, title, body, date, forcedPostId).Value)
                    {
                        dataInsertSet.Add(change);
                    }
                    dataInsertSet.Apply(transaction);
                });
            }

            return(Thread.LoadById(threadInsert.getId().Value));
        }
Esempio n. 2
0
        public PostLayer getActualLayer(Board board, PostLayer desiredLayer)
        {
            Dictionary <int, DateTime> restrictionData = Restriction.GetRestrictionData(this, board);

            if (restrictionData.ContainsKey(desiredLayer.id) && (restrictionData[desiredLayer.id].CompareTo(DateTime.Now) >= 0))
            {
                throw new FLocalException("You're restricted from posting in this layer until " + restrictionData[desiredLayer.id].ToString());
            }
            return(desiredLayer);
        }
Esempio n. 3
0
 public XElement exportToXml(UserContext context)
 {
     return(new XElement("restriction",
                         this.user.exportToXmlForViewing(context),
                         this.board.exportToXmlSimple(context, Board.SubboardsOptions.None),
                         new XElement("layers",
                                      from kvp in this.data
                                      select PostLayer.LoadById(kvp.Key).exportToXml(
                                          context,
                                          new XElement("restrictionExpires", kvp.Value.ToXml())
                                          )
                                      )
                         ));
 }
Esempio n. 4
0
        public Post Reply(User poster, string title, string body, PostLayer desiredLayer, DateTime date, int?forcedPostId)
        {
            if (this.thread.isLocked && poster.name != Config.instance.AdminUserName)
            {
                throw new FLocalException("thread locked");
            }

            PostLayer actualLayer = poster.getActualLayer(this.thread.board, desiredLayer);

            Post newPost;

            lock (this.thread.locker) {
                var changes = Thread.getNewPostChanges(this.thread.board, this.threadId, this, poster, actualLayer, title, body, date, forcedPostId);
                ChangeSetUtil.ApplyChanges(changes.Value.ToArray());

                newPost = Post.LoadById(changes.Key.getId().Value);
            }
            return(newPost);
        }
Esempio n. 5
0
 public Thread CreateThread(User poster, string title, string body, PostLayer desiredLayer)
 {
     return(this.CreateThread(poster, title, body, desiredLayer, DateTime.Now, null));
 }
Esempio n. 6
0
        internal static KeyValuePair <AbstractChange, IEnumerable <AbstractChange> > getNewPostChanges(Board board, int threadId, Post parentPost, User poster, PostLayer layer, string title, string body, DateTime date, int?forcedPostId)
        {
            string parentPostId = null;

            if (parentPost != null)
            {
                parentPostId = parentPost.id.ToString();
            }
            bool          isNewThread       = (parentPost == null);
            HashSet <int> mentionedUsersIds = new HashSet <int>();

            if (parentPost != null && parentPost.poster.id != poster.id)
            {
                mentionedUsersIds.Add(parentPost.poster.id);
            }
            string bodyIntermediate;

            if (forcedPostId.HasValue)
            {
                //dirty hack
                bodyIntermediate = body;
            }
            else
            {
                bodyIntermediate = UBBParser.UBBToIntermediate(
                    new DelegatePostParsingContext(mentionedUser => mentionedUsersIds.Add(mentionedUser.id)),
                    body
                    );
            }
            var postInsertData = new Dictionary <string, AbstractFieldValue> {
                { Post.TableSpec.FIELD_THREADID, new ScalarFieldValue(threadId.ToString()) },
                { Post.TableSpec.FIELD_PARENTPOSTID, new ScalarFieldValue(parentPostId) },
                { Post.TableSpec.FIELD_POSTERID, new ScalarFieldValue(poster.id.ToString()) },
                { Post.TableSpec.FIELD_POSTDATE, new ScalarFieldValue(date.ToUTCString()) },
                //{ Post.TableSpec.FIELD_REVISION, new ScalarFieldValue("0") },
                { Post.TableSpec.FIELD_LASTCHANGEDATE, new ScalarFieldValue(date.ToUTCString()) },
                { Post.TableSpec.FIELD_LAYERID, new ScalarFieldValue(layer.id.ToString()) },
                { Post.TableSpec.FIELD_TITLE, new ScalarFieldValue(title) },
                { Post.TableSpec.FIELD_BODY, new ScalarFieldValue(bodyIntermediate) },
                { Post.TableSpec.FIELD_TOTALPUNISHMENTS, new ScalarFieldValue("0") },
            };

            if (!forcedPostId.HasValue)
            {
                postInsertData[Post.TableSpec.FIELD_REVISION] = new ScalarFieldValue("0");
            }
            else
            {
                postInsertData[Post.TableSpec.FIELD_ID] = new ScalarFieldValue(forcedPostId.Value.ToString());
            }
            AbstractChange postInsert = new InsertChange(
                Post.TableSpec.instance,
                postInsertData
                );
            AbstractFieldValue postReference      = new ReferenceFieldValue(postInsert);
            AbstractFieldValue postIndexReference = new TwoWayReferenceFieldValue(postInsert, TwoWayReferenceFieldValue.GREATEST);
            AbstractChange     revisionInsert     = new InsertChange(
                Revision.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { Revision.TableSpec.FIELD_POSTID, postReference },
                { Revision.TableSpec.FIELD_NUMBER, new ScalarFieldValue("0") },
                { Revision.TableSpec.FIELD_CHANGEDATE, new ScalarFieldValue(date.ToUTCString()) },
                { Revision.TableSpec.FIELD_TITLE, new ScalarFieldValue(title) },
                { Revision.TableSpec.FIELD_BODY, new ScalarFieldValue(body) },
            }
                );
            Dictionary <string, AbstractFieldValue> threadData = new Dictionary <string, AbstractFieldValue> {
                { Thread.TableSpec.FIELD_LASTPOSTDATE, new ScalarFieldValue(date.ToUTCString()) },
                { Thread.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue() },
                {
                    Thread.TableSpec.FIELD_LASTPOSTID,
                    postIndexReference
                }
            };

            if (isNewThread)
            {
                threadData[Thread.TableSpec.FIELD_FIRSTPOSTID] = postReference;
            }
            AbstractChange threadUpdate = new UpdateChange(
                Thread.TableSpec.instance,
                threadData,
                threadId
                );
            AbstractChange userUpdate = new UpdateChange(
                User.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { User.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue() },
            },
                poster.id
                );

            List <AbstractChange> changes = new List <AbstractChange>();

            changes.Add(postInsert);
            if (!forcedPostId.HasValue)
            {
                //dirty hack
                changes.Add(revisionInsert);
            }
            changes.Add(threadUpdate);
            changes.Add(userUpdate);

            foreach (var mentionedUserId in mentionedUsersIds)
            {
                changes.Add(
                    new InsertChange(
                        Mention.TableSpec.instance,
                        new Dictionary <string, AbstractFieldValue> {
                    { Mention.TableSpec.FIELD_MENTIONEDUSERID, new ScalarFieldValue(mentionedUserId.ToString()) },
                    { Mention.TableSpec.FIELD_POSTID, new ReferenceFieldValue(postInsert) },
                    { Mention.TableSpec.FIELD_DATE, new ScalarFieldValue(date.ToUTCString()) },
                }
                        )
                    );
            }

            Dictionary <string, AbstractFieldValue> boardData = new Dictionary <string, AbstractFieldValue> {
                { Board.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue() },
                {
                    Board.TableSpec.FIELD_LASTPOSTID,
                    postIndexReference
                }
            };

            if (isNewThread)
            {
                boardData[Board.TableSpec.FIELD_TOTALTHREADS] = new IncrementFieldValue();
            }
            int?boardId = board.id;

            do
            {
                Board _board = Board.LoadById(boardId.Value);
                changes.Add(
                    new UpdateChange(
                        Board.TableSpec.instance,
                        boardData,
                        _board.id
                        )
                    );
                boardId = _board.parentBoardId;
            } while(boardId.HasValue);

            return(new KeyValuePair <AbstractChange, IEnumerable <AbstractChange> >(postInsert, changes));
        }
 public NewLayerChangeInfo(PostLayer newLayer, bool isSubthreadChange)
 {
     this.newLayerId        = newLayer.id;
     this.isSubthreadChange = isSubthreadChange;
 }
Esempio n. 8
0
        private readonly object Edit_locker = new object();         //TODO: move locking to DB
        public void Edit(User user, string newTitle, string newBody, PostLayer newDesiredLayer)
        {
            if (this.poster.id != user.id)
            {
                throw new AccessDeniedException();
            }
            PostLayer actualLayer = poster.getActualLayer(this.thread.board, newDesiredLayer);

            if (actualLayer.id < this.layer.id)
            {
                actualLayer = this.layer;
            }
            lock (this.Edit_locker) {
                DateTime date = DateTime.Now;

                HashSet <int> newMentionedUsersIds = new HashSet <int>();
                if (this.parentPostId != null && this.parentPost.poster.id != this.poster.id)
                {
                    newMentionedUsersIds.Add(this.parentPost.poster.id);
                }
                string newBodyIntermediate = UBBParser.UBBToIntermediate(
                    new DelegatePostParsingContext(mentionedUser => newMentionedUsersIds.Add(mentionedUser.id)),
                    newBody
                    );

                List <AbstractChange> changes = new List <AbstractChange> {
                    new InsertChange(
                        Revision.TableSpec.instance,
                        new Dictionary <string, AbstractFieldValue> {
                        { Revision.TableSpec.FIELD_POSTID, new ScalarFieldValue(this.id.ToString()) },
                        { Revision.TableSpec.FIELD_CHANGEDATE, new ScalarFieldValue(date.ToUTCString()) },
                        { Revision.TableSpec.FIELD_TITLE, new ScalarFieldValue(newTitle) },
                        { Revision.TableSpec.FIELD_BODY, new ScalarFieldValue(newBody) },
                        { Revision.TableSpec.FIELD_NUMBER, new ScalarFieldValue((this.revision + 1).ToString()) },
                    }
                        ),
                    new UpdateChange(
                        TableSpec.instance,
                        new Dictionary <string, AbstractFieldValue> {
                        { TableSpec.FIELD_TITLE, new ScalarFieldValue(newTitle) },
                        { TableSpec.FIELD_BODY, new ScalarFieldValue(newBodyIntermediate) },
                        { TableSpec.FIELD_LASTCHANGEDATE, new ScalarFieldValue(date.ToUTCString()) },
                        { TableSpec.FIELD_REVISION, new IncrementFieldValue() },
                        { TableSpec.FIELD_LAYERID, new ScalarFieldValue(actualLayer.id.ToString()) },
                    },
                        this.id
                        )
                };
                if (this.thread.firstPost.id == this.id)
                {
                    changes.Add(
                        new UpdateChange(
                            Thread.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                        { TableSpec.FIELD_TITLE, new ScalarFieldValue(newTitle) },
                    },
                            this.thread.id
                            )
                        );
                }
                foreach (var mentionedUserId in newMentionedUsersIds.Except(this.mentionedUsersIds))
                {
                    changes.Add(
                        new InsertChange(
                            Mention.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                        { Mention.TableSpec.FIELD_MENTIONEDUSERID, new ScalarFieldValue(mentionedUserId.ToString()) },
                        { Mention.TableSpec.FIELD_POSTID, new ScalarFieldValue(this.id.ToString()) },
                        { Mention.TableSpec.FIELD_DATE, new ScalarFieldValue(date.ToUTCString()) },
                    }
                            )
                        );
                }
                ChangeSetUtil.ApplyChanges(changes.ToArray());
            }
        }
Esempio n. 9
0
 public Post Reply(User poster, string title, string body, PostLayer desiredLayer)
 {
     return(this.Reply(poster, title, body, desiredLayer, DateTime.Now, null));
 }