Beispiel #1
0
 public IEnumerable <PMMessage> getMessages(Diapasone diapasone, UserContext context, bool isAscending)
 {
     diapasone.total = this.totalMessages;
     return(PMMessage.LoadByIds(
                from stringId in Config.instance.mainConnection.LoadIdsByConditions(
                    PMMessage.TableSpec.instance,
                    new ComplexCondition(
                        ConditionsJoinType.AND,
                        new ComparisonCondition(
                            PMMessage.TableSpec.instance.getColumnSpec(PMMessage.TableSpec.FIELD_OWNERID),
                            ComparisonType.EQUAL,
                            this.ownerId.ToString()
                            ),
                        new ComparisonCondition(
                            PMMessage.TableSpec.instance.getColumnSpec(PMMessage.TableSpec.FIELD_INTERLOCUTORID),
                            ComparisonType.EQUAL,
                            this.interlocutorId.ToString()
                            )
                        ),
                    diapasone,
                    new JoinSpec[0],
                    new SortSpec[] {
         new SortSpec(
             PMMessage.TableSpec.instance.getIdSpec(),
             isAscending
             ),
     }
                    ) select int.Parse(stringId)
                ));
 }
Beispiel #2
0
        public static PMMessage SendPMMessage(Account sender, Account receiver, string title, string bodyUBB)
        {
            string         bodyIntermediate = UBBParser.UBBToIntermediate(bodyUBB);
            AbstractChange insertPmReceiver = new InsertChange(
                PMMessage.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { PMMessage.TableSpec.FIELD_OWNERID, new ScalarFieldValue(receiver.id.ToString()) },
                { PMMessage.TableSpec.FIELD_INTERLOCUTORID, new ScalarFieldValue(sender.id.ToString()) },
                { PMMessage.TableSpec.FIELD_DIRECTION, new ScalarFieldValue(PMMessage.ENUM_DIRECTION_INCOMING) },
                { PMMessage.TableSpec.FIELD_POSTDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
                { PMMessage.TableSpec.FIELD_TITLE, new ScalarFieldValue(title) },
                { PMMessage.TableSpec.FIELD_BODY, new ScalarFieldValue(bodyIntermediate) },
                { PMMessage.TableSpec.FIELD_BODYUBB, new ScalarFieldValue(bodyUBB) },
                { PMMessage.TableSpec.FIELD_INCOMINGPMID, new ScalarFieldValue(null) },
                { PMMessage.TableSpec.FIELD_ISREAD, new ScalarFieldValue("0") },
            }
                );
            AbstractChange insertPmSender = new InsertChange(
                PMMessage.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { PMMessage.TableSpec.FIELD_OWNERID, new ScalarFieldValue(sender.id.ToString()) },
                { PMMessage.TableSpec.FIELD_INTERLOCUTORID, new ScalarFieldValue(receiver.id.ToString()) },
                { PMMessage.TableSpec.FIELD_DIRECTION, new ScalarFieldValue(PMMessage.ENUM_DIRECTION_OUTGOING) },
                { PMMessage.TableSpec.FIELD_POSTDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
                { PMMessage.TableSpec.FIELD_TITLE, new ScalarFieldValue(title) },
                { PMMessage.TableSpec.FIELD_BODY, new ScalarFieldValue(bodyIntermediate) },
                { PMMessage.TableSpec.FIELD_BODYUBB, new ScalarFieldValue(bodyUBB) },
                { PMMessage.TableSpec.FIELD_INCOMINGPMID, new ReferenceFieldValue(insertPmReceiver) },
                { PMMessage.TableSpec.FIELD_ISREAD, new ScalarFieldValue("1") },
            }
                );
            AbstractChange updateConversationSender = new InsertOrUpdateChange(
                TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { TableSpec.FIELD_OWNERID, new ScalarFieldValue(sender.id.ToString()) },
                { TableSpec.FIELD_INTERLOCUTORID, new ScalarFieldValue(receiver.id.ToString()) },
                { TableSpec.FIELD_TOTALMESSAGES, new ScalarFieldValue("1") },
                { TableSpec.FIELD_LASTMESSAGEID, new ReferenceFieldValue(insertPmSender) },
                { TableSpec.FIELD_LASTMESSAGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
                { TableSpec.FIELD_LASTREADMESSAGEID, new ReferenceFieldValue(insertPmSender) },
            },
                new Dictionary <string, AbstractFieldValue> {
                { TableSpec.FIELD_TOTALMESSAGES, new IncrementFieldValue() },
                { TableSpec.FIELD_LASTMESSAGEID, new TwoWayReferenceFieldValue(insertPmSender, TwoWayReferenceFieldValue.GREATEST) },
                { TableSpec.FIELD_LASTMESSAGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
            },
                new ComplexCondition(
                    ConditionsJoinType.AND,
                    new ComparisonCondition(
                        TableSpec.instance.getColumnSpec(TableSpec.FIELD_OWNERID),
                        ComparisonType.EQUAL,
                        sender.id.ToString()
                        ),
                    new ComparisonCondition(
                        TableSpec.instance.getColumnSpec(TableSpec.FIELD_INTERLOCUTORID),
                        ComparisonType.EQUAL,
                        receiver.id.ToString()
                        )
                    )
                );
            AbstractChange updateConversationReceiver = new InsertOrUpdateChange(
                TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { TableSpec.FIELD_OWNERID, new ScalarFieldValue(receiver.id.ToString()) },
                { TableSpec.FIELD_INTERLOCUTORID, new ScalarFieldValue(sender.id.ToString()) },
                { TableSpec.FIELD_TOTALMESSAGES, new ScalarFieldValue("1") },
                { TableSpec.FIELD_LASTMESSAGEID, new ReferenceFieldValue(insertPmReceiver) },
                { TableSpec.FIELD_LASTMESSAGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
                { TableSpec.FIELD_LASTREADMESSAGEID, new ScalarFieldValue(null) },
            },
                new Dictionary <string, AbstractFieldValue> {
                { TableSpec.FIELD_TOTALMESSAGES, new IncrementFieldValue() },
                { TableSpec.FIELD_LASTMESSAGEID, new TwoWayReferenceFieldValue(insertPmReceiver, TwoWayReferenceFieldValue.GREATEST) },
                { TableSpec.FIELD_LASTMESSAGEDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
            },
                new ComplexCondition(
                    ConditionsJoinType.AND,
                    new ComparisonCondition(
                        TableSpec.instance.getColumnSpec(TableSpec.FIELD_OWNERID),
                        ComparisonType.EQUAL,
                        receiver.id.ToString()
                        ),
                    new ComparisonCondition(
                        TableSpec.instance.getColumnSpec(TableSpec.FIELD_INTERLOCUTORID),
                        ComparisonType.EQUAL,
                        sender.id.ToString()
                        )
                    )
                );
            AbstractChange updateIndicatorReceiver = new UpdateChange(
                AccountIndicator.TableSpec.instance,
                new Dictionary <string, AbstractFieldValue> {
                { AccountIndicator.TableSpec.FIELD_PRIVATEMESSAGES, new IncrementFieldValue() },
                { AccountIndicator.TableSpec.FIELD_UNREADPRIVATEMESSAGES, new IncrementFieldValue() },
            },
                AccountIndicator.LoadByAccount(receiver).id
                );

            ChangeSetUtil.ApplyChanges(
                insertPmReceiver,
                insertPmSender,
                updateConversationReceiver,
                updateConversationSender,
                updateIndicatorReceiver
                );
            return(PMMessage.LoadById(insertPmSender.getId().Value));
        }
Beispiel #3
0
 public void markAsRead(Account account, PMMessage minMessage, PMMessage maxMessage)
 {
     if (this.ownerId != account.id)
     {
         throw new AccessDeniedException();
     }
     ChangeSetUtil.ApplyChanges(new AbstractChange[] {
         new UpdateChange(
             TableSpec.instance,
             new Dictionary <string, AbstractFieldValue> {
             {
                 TableSpec.FIELD_LASTREADMESSAGEID,
                 new IncrementFieldValue(
                     s => {
                     if ((s == null) || (s == ""))
                     {
                         s = "0";                                                 //workaround
                     }
                     if (maxMessage.id < int.Parse(s))
                     {
                         return((s == "0") ? null : s);                                                //if some newer posts were already read
                     }
                     long count = Config.instance.mainConnection.GetCountByConditions(
                         PMMessage.TableSpec.instance,
                         new ComplexCondition(
                             ConditionsJoinType.AND,
                             new ComparisonCondition(
                                 PMMessage.TableSpec.instance.getColumnSpec(PMMessage.TableSpec.FIELD_OWNERID),
                                 ComparisonType.EQUAL,
                                 this.ownerId.ToString()
                                 ),
                             new ComparisonCondition(
                                 PMMessage.TableSpec.instance.getColumnSpec(PMMessage.TableSpec.FIELD_INTERLOCUTORID),
                                 ComparisonType.EQUAL,
                                 this.interlocutorId.ToString()
                                 ),
                             new ComparisonCondition(
                                 PMMessage.TableSpec.instance.getIdSpec(),
                                 ComparisonType.GREATERTHAN,
                                 s
                                 ),
                             new ComparisonCondition(
                                 PMMessage.TableSpec.instance.getIdSpec(),
                                 ComparisonType.LESSTHAN,
                                 minMessage.id.ToString()
                                 )
                             )
                         );
                     if (count > 0)
                     {
                         return((s == "0") ? null : s);                                                //if there are some unread posts earlier than minPost
                     }
                     else
                     {
                         return(maxMessage.id.ToString());
                     }
                 }
                     )
             }
         },
             this.id
             )
     });
 }
Beispiel #4
0
        public void Punish(Account account, PunishmentType type, string comment, PunishmentTransfer.NewTransferInfo?transferInfo, PunishmentLayerChange.NewLayerChangeInfo?layerChangeInfo)
        {
            if (string.IsNullOrEmpty(comment))
            {
                throw new FLocalException("Comment is empty");
            }

            if (!Moderator.isModerator(account, this.thread))
            {
                throw new FLocalException(account.id + " is not a moderator in board " + this.thread.board.id);
            }

            if (!Moderator.isTrueModerator(account, this.thread.board))
            {
                if (type.weight != 0)
                {
                    throw new FLocalException("You cannot set punishments with weight != 0");
                }
                if (transferInfo.HasValue && !transferInfo.Value.newBoard.isTransferTarget)
                {
                    throw new FLocalException("You cannot transfer in '" + transferInfo.Value.newBoard.name + "'");
                }
            }

            lock (this.Punish_Locker) {
                lock (this.thread.locker) {
                    IEnumerable <AbstractChange> changes = (
                        from punishment in this.punishments
                        select(AbstractChange) new UpdateChange(
                            Punishment.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                        { Punishment.TableSpec.FIELD_ISWITHDRAWED, new ScalarFieldValue("1") },
                    },
                            punishment.id
                            )
                        );

                    InsertChange layerChangeInsert = null;
                    if (layerChangeInfo.HasValue)
                    {
                        var _layerChangeInfo = layerChangeInfo.Value;

                        if (_layerChangeInfo.newLayer.name == PostLayer.NAME_HIDDEN)
                        {
                            throw new FLocalException("You cannot hide posts");
                        }

                        layerChangeInsert = new InsertChange(
                            PunishmentLayerChange.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                            { PunishmentLayerChange.TableSpec.FIELD_OLDLAYERID, new ScalarFieldValue(this.layerId.ToString()) },
                            { PunishmentLayerChange.TableSpec.FIELD_NEWLAYERID, new ScalarFieldValue(_layerChangeInfo.newLayerId.ToString()) },
                            { PunishmentLayerChange.TableSpec.FIELD_ISSUBTHREADCHANGE, new ScalarFieldValue(_layerChangeInfo.isSubthreadChange.ToDBString()) },
                        }
                            );
                        changes.Union(layerChangeInsert);

                        List <Post> postsAffected;
                        if (_layerChangeInfo.isSubthreadChange)
                        {
                            postsAffected = this.ToSequence(post => post.subPosts).OrderBy(post => post.id).ToList();
                        }
                        else
                        {
                            postsAffected = new List <Post>();
                            postsAffected.Add(this);
                        }

                        changes = changes.Union(
                            from post in postsAffected
                            select(AbstractChange) new UpdateChange(
                                Post.TableSpec.instance,
                                new Dictionary <string, AbstractFieldValue> {
                            { Post.TableSpec.FIELD_LAYERID, new ScalarFieldValue(_layerChangeInfo.newLayerId.ToString()) },
                        },
                                post.id
                                )
                            );
                    }

                    InsertChange transferInsert = null;
                    if (transferInfo.HasValue)
                    {
                        var _transferInfo = transferInfo.Value;

                        transferInsert = new InsertChange(
                            PunishmentTransfer.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                            { PunishmentTransfer.TableSpec.FIELD_OLDBOARDID, new ScalarFieldValue(this.thread.boardId.ToString()) },
                            { PunishmentTransfer.TableSpec.FIELD_NEWBOARDID, new ScalarFieldValue(_transferInfo.newBoardId.ToString()) },
                            { PunishmentTransfer.TableSpec.FIELD_ISSUBTHREADTRANSFER, new ScalarFieldValue(_transferInfo.isSubthreadTransfer.ToDBString()) },
                            { PunishmentTransfer.TableSpec.FIELD_OLDPARENTPOSTID, new ScalarFieldValue(this.parentPostId.HasValue ? this.parentPostId.ToString() : null) },
                        }
                            );
                        changes = changes.Union(transferInsert);

                        Post lastAffectedPost;
                        int  totalAffectedPosts;

                        if (!this.parentPostId.HasValue)
                        {
                            if (!_transferInfo.isSubthreadTransfer)
                            {
                                throw new FLocalException("You cannot move the first post in thread");
                            }
                            else
                            {
                                lastAffectedPost   = this.thread.lastPost;
                                totalAffectedPosts = this.thread.totalPosts;
                                changes            = changes.Union(
                                    new UpdateChange(
                                        Thread.TableSpec.instance,
                                        new Dictionary <string, AbstractFieldValue> {
                                    { Thread.TableSpec.FIELD_BOARDID, new ScalarFieldValue(_transferInfo.newBoardId.ToString()) },
                                },
                                        this.thread.id
                                        )
                                    );
                            }
                        }
                        else
                        {
                            List <Post> postsAffected;
                            if (_transferInfo.isSubthreadTransfer)
                            {
                                postsAffected = this.ToSequence(post => post.subPosts).OrderBy(post => post.id).ToList();
                            }
                            else
                            {
                                postsAffected = new List <Post>();
                                postsAffected.Add(this);
                            }

                            lastAffectedPost   = postsAffected.Last();
                            totalAffectedPosts = postsAffected.Count;

                            InsertChange threadCreate = new InsertChange(
                                Thread.TableSpec.instance,
                                new Dictionary <string, AbstractFieldValue> {
                                { Thread.TableSpec.FIELD_BOARDID, new ScalarFieldValue(_transferInfo.newBoardId.ToString()) },
                                { Thread.TableSpec.FIELD_FIRSTPOSTID, new ScalarFieldValue(this.id.ToString()) },
                                { Thread.TableSpec.FIELD_ISANNOUNCEMENT, new ScalarFieldValue("0") },
                                { Thread.TableSpec.FIELD_ISLOCKED, new ScalarFieldValue("0") },
                                { Thread.TableSpec.FIELD_LASTPOSTDATE, new ScalarFieldValue(lastAffectedPost.postDate.ToUTCString()) },
                                { Thread.TableSpec.FIELD_LASTPOSTID, new ScalarFieldValue(lastAffectedPost.id.ToString()) },
                                { Thread.TableSpec.FIELD_TITLE, new ScalarFieldValue(this.title) },
                                { Thread.TableSpec.FIELD_TOPICSTARTERID, new ScalarFieldValue(this.posterId.ToString()) },
                                { Thread.TableSpec.FIELD_TOTALPOSTS, new ScalarFieldValue(totalAffectedPosts.ToString()) },
                                { Thread.TableSpec.FIELD_TOTALVIEWS, new ScalarFieldValue("0") },
                            }
                                );
                            changes = changes.Union(threadCreate);

                            changes = changes.Union(
                                from post in postsAffected
                                select(AbstractChange) new UpdateChange(
                                    TableSpec.instance,
                                    new Dictionary <string, AbstractFieldValue> {
                                { TableSpec.FIELD_THREADID, new ReferenceFieldValue(threadCreate) },
                            },
                                    post.id
                                    )
                                );

                            if (!_transferInfo.isSubthreadTransfer)
                            {
                                changes = changes.Union(
                                    from post in this.subPosts
                                    select(AbstractChange) new UpdateChange(
                                        TableSpec.instance,
                                        new Dictionary <string, AbstractFieldValue> {
                                    { TableSpec.FIELD_PARENTPOSTID, new ScalarFieldValue(this.parentPostId.ToString()) },
                                },
                                        post.id
                                        )
                                    );
                            }
                        }

                        changes = changes.Union(
                            from board in this.thread.board.boardAndParents
                            select(AbstractChange) new UpdateChange(
                                Board.TableSpec.instance,
                                new Dictionary <string, AbstractFieldValue> {
                            { Board.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue(IncrementFieldValue.DECREMENTOR_CUSTOM(totalAffectedPosts)) },
                            { Board.TableSpec.FIELD_TOTALTHREADS, new IncrementFieldValue(IncrementFieldValue.DECREMENTOR_CUSTOM(this.parentPostId.HasValue ? 0 : 1)) },
                        },
                                board.id
                                )
                            );

                        changes = changes.Union(
                            from board in _transferInfo.newBoard.boardAndParents
                            select(AbstractChange) new UpdateChange(
                                Board.TableSpec.instance,
                                new Dictionary <string, AbstractFieldValue> {
                            { Board.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue(IncrementFieldValue.INCREMENTOR_CUSTOM(totalAffectedPosts)) },
                            { Board.TableSpec.FIELD_TOTALTHREADS, new IncrementFieldValue() },
                            { Board.TableSpec.FIELD_LASTPOSTID, new IncrementFieldValue(IncrementFieldValue.GREATEST(lastAffectedPost.id)) },
                        },
                                board.id
                                )
                            );

                        changes = changes.Union(
                            new UpdateChange(
                                TableSpec.instance,
                                new Dictionary <string, AbstractFieldValue> {
                            { TableSpec.FIELD_PARENTPOSTID, new ScalarFieldValue(null) },
                        },
                                this.id
                                )
                            );

                        if (this.parentPostId.HasValue)
                        {
                            changes = changes.Union(
                                new UpdateChange(
                                    Thread.TableSpec.instance,
                                    new Dictionary <string, AbstractFieldValue> {
                                { Thread.TableSpec.FIELD_TOTALPOSTS, new IncrementFieldValue(IncrementFieldValue.DECREMENTOR_CUSTOM(totalAffectedPosts)) },
                            },
                                    this.threadId
                                    )
                                );
                        }
                    }

                    changes = changes.Union(
                        new UpdateChange(
                            TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                        { TableSpec.FIELD_TOTALPUNISHMENTS, new IncrementFieldValue() },
                    },
                            this.id
                            ),
                        new InsertChange(
                            Punishment.TableSpec.instance,
                            new Dictionary <string, AbstractFieldValue> {
                        { Punishment.TableSpec.FIELD_POSTID, new ScalarFieldValue(this.id.ToString()) },
                        { Punishment.TableSpec.FIELD_OWNERID, new ScalarFieldValue(this.poster.id.ToString()) },
                        { Punishment.TableSpec.FIELD_ORIGINALBOARDID, new ScalarFieldValue(this.thread.board.id.ToString()) },
                        { Punishment.TableSpec.FIELD_MODERATORID, new ScalarFieldValue(account.id.ToString()) },
                        { Punishment.TableSpec.FIELD_PUNISHMENTDATE, new ScalarFieldValue(DateTime.Now.ToUTCString()) },
                        { Punishment.TableSpec.FIELD_PUNISHMENTTYPE, new ScalarFieldValue(type.id.ToString()) },
                        { Punishment.TableSpec.FIELD_ISWITHDRAWED, new ScalarFieldValue("0") },
                        { Punishment.TableSpec.FIELD_COMMENT, new ScalarFieldValue(comment) },
                        { Punishment.TableSpec.FIELD_EXPIRES, new ScalarFieldValue(DateTime.Now.Add(type.timeSpan).ToUTCString()) },
                        { Punishment.TableSpec.FIELD_TRANSFERID, (transferInsert != null) ? (AbstractFieldValue) new ReferenceFieldValue(transferInsert) : (AbstractFieldValue) new ScalarFieldValue(null) },
                        { Punishment.TableSpec.FIELD_LAYERCHANGEID, (layerChangeInsert != null) ? (AbstractFieldValue) new ReferenceFieldValue(layerChangeInsert) : (AbstractFieldValue) new ScalarFieldValue(null) },
                    }
                            )
                        );

                    ChangeSetUtil.ApplyChanges(changes.ToArray());

                    this.punishments_Reset();

                    Account posterAccount = null;
                    try {
                        posterAccount = Account.LoadByUser(this.poster);
                    } catch (NotFoundInDBException) {
                    }

                    if ((posterAccount != null) && (posterAccount.id != account.id) && !posterAccount.needsMigration)
                    {
                        PMMessage newMessage = PMConversation.SendPMMessage(
                            account,
                            posterAccount,
                            this.title,
                            String.Format("{0}{3}[post]{2}[/post]{3}{1}", type.description, comment, this.id, Util.EOL)
                            );
                        newMessage.conversation.markAsRead(account, newMessage, newMessage);
                    }

                    HashSet <int> punishmentsBoards = new HashSet <int>(from punishment in this.punishments select punishment.originalBoardId);
                    foreach (int boardId in punishmentsBoards)
                    {
                        Restriction.RecalculateRestrictions(Board.LoadById(boardId), this.poster);
                    }
                }
            }
        }