Пример #1
0
        /// <summary>
        /// Добавление указанного пользователя в группу
        /// </summary>
        /// <param name="userId">идентификатор пользователя</param>
        /// <param name="groupId">идентификатор группы</param>
        /// <returns>экземпляр участника группы</returns>
        public GroupMember AddMember(Guid userId, Guid groupId, bool saveChanges = true)
        {
            var group = DataService.PerThread.GroupSet.SingleOrDefault(g => g.Id == groupId);

            if (group == null)
            {
                throw new BusinessLogicException("Не найдена группа с указанным идентификатором");
            }

            var gm = DataService.PerThread.GroupMemberSet.SingleOrDefault(m => m.GroupId == groupId && m.UserId == userId);

            if (gm == null)
            {
                gm = new GroupMember
                {
                    EntryDate = DateTime.Now,
                    GroupId   = groupId,
                    State     = (byte)GroupMemberState.NotApproved,
                    UserId    = userId
                };

                if (!group.PrivacyEnum.HasFlag(GroupPrivacy.MemberModeration) && group.State != (byte)GroupState.Blank || group.State == (byte)GroupState.Archive)
                {
                    gm.State = (byte)GroupMemberState.Approved;
                }

                DataService.PerThread.GroupMemberSet.AddObject(gm);
                DataService.PerThread.SaveChanges();

                if (group.GroupMembers.Count(x => x.State == (byte)GroupMemberState.Moderator) == 0)
                {
                    GroupService.ChangeMemberStatus(userId, group.Id, GroupMemberState.Moderator);
                }

                SubscriptionService.SubscribeToGroup(group.Url, gm.UserId);
            }
            else if (gm.State != (byte)GroupMemberState.Approved && gm.State != (byte)GroupMemberState.Moderator)
            {
                if (gm.State == (byte)GroupMemberState.Banned && DateTime.Now < gm.ExitDate.Value.AddMonths(2))
                {
                    throw new BusinessLogicException("Вы не можете вступать в эту группу еще " + (gm.ExitDate.Value.AddMonths(2) - DateTime.Now).Days + ", так как были заблокированы");
                }

                if (group.GroupMembers.Count(x => x.State == (byte)GroupMemberState.Moderator) == 0)
                {
                    GroupService.ChangeMemberStatus(userId, group.Id, GroupMemberState.Moderator);
                }
                else if (!group.PrivacyEnum.HasFlag(GroupPrivacy.MemberModeration) && group.State != (byte)GroupState.Blank)
                {
                    gm.State = (byte)GroupMemberState.Approved;
                }
                else
                {
                    gm.State = (byte)GroupMemberState.NotApproved;
                }
            }

            UpdateGroupState(group.Id);

            return(gm);
        }