public Message ExchangeMessage(Message message)
        {
            Message toDeliver = new NullNormalizeFactory <Message>(m =>
            {
                m.WriteDate = DateTime.Now;
                m.ToId      = message.To.Id;
                m.FromId    = message.From.Id;
                m.StateId   = M_SENT;
            }).Instance;

            //throws if one person of the relation has blocked the other one
            CheckBlocking(toDeliver);

            Message found = GetMessage(message);

            Reflector.Merge(toDeliver, message);

            toDeliver.MessageId = found != null ? found.MessageId + 1 : 0;

            Create(toDeliver);
            TrySave();

            Wrapper.Notificaton.CreateMessageReceived(message.To, message.From);

            return(GetMessage(toDeliver));
        }
        private Notification GetNewNotificaion(Action <Notification> action)
        {
            Notification data = new NullNormalizeFactory <Notification>(action)
                                .Instance;

            Notification newNotif = new Notification()
            {
                KindId    = (int)Kind.NotificationKind.Social,
                NotifId   = Guid.NewGuid(),
                WriteDate = DateTime.Now,
                Seen      = false
            };

            Reflector.Merge(newNotif, data);

            return(newNotif);
        }
        public Message Answer(Message message)
        {
            Message answer = GetMessage(message.Answer);
            Message found  = GetMessage(new Message()
            {
                ToId   = answer.From.Id,
                FromId = answer.To.Id
            });

            if (FindByCondition(m =>
                                m.AnswerFromId.Equals(answer.From.Id) &&
                                m.AnswerId.Equals(answer.MessageId) &&
                                m.AnswerToId.Equals(answer.To.Id)).FirstOrDefault()
                != null)
            {
                DbEx.Instance.Throw <AnsweredTwiceException>(message);
            }

            Message toDeliver = new NullNormalizeFactory <Message>(m =>
            {
                m.WriteDate = DateTime.Now;

                m.AnswerFromId = answer.From.Id;
                m.AnswerToId   = answer.To.Id;

                m.ToId   = answer.From.Id;
                m.FromId = answer.To.Id;

                m.StateId = M_SENT;
            }).Instance;

            //throws if one person of the relation has blocked the other one
            CheckBlocking(toDeliver);

            Reflector.Merge(toDeliver, message);

            toDeliver.MessageId = found != null ? found.MessageId + 1 : 0;

            Create(toDeliver);
            TrySave();

            return(GetMessage(toDeliver));
        }
        private Follow GetFreshReciprocal(Follow follow)
        {
            Follow copy = Util.GetCopyOf(follow);

            // the reciproquity
            Follow fresReciprocal = new NullNormalizeFactory <Follow>(f => {
                f.Followed  = copy.Following;
                f.Following = copy.Followed;

                f.StatusId     = F_FRIEND;
                f.ImportanceId = DEFAULT;
            }).Instance;


            // remove references from the copy so they will be skipped from the
            // merging process
            copy.Followed = copy.Following = null;

            Reflector.Merge(fresReciprocal, copy);

            return(fresReciprocal);
        }