public void DeleteMessage(Guid userId, Guid msgId) { var message = DataService.PerThread.MessageSet.SingleOrDefault(x => x.Id == msgId); if (message == null) { throw new BusinessLogicException("Указан неверный идентификатор сообщения"); } if (message.AuthorId != userId && message.RecipientId != userId) { throw new BusinessLogicException("Пользователь не имеет права удалять данное сообщение"); } if (message.AuthorId == userId) { message.IsDeletedByAuthor = true; } else if (message.RecipientId == userId) { message.IsRead = true; message.IsDeletedByRecipient = true; } DataService.PerThread.SaveChanges(); if (message.RecipientId == userId) { UserContextService.Abandon(userId); } }
public void SendToGroup(Group group, MessageStruct messageInfo, GroupMessageRecipientType recipientType) { var recipientIdList = new List <Guid>(); if (recipientType.HasFlag(GroupMessageRecipientType.Members)) { recipientIdList.AddRange(DataService.PerThread.GroupMemberSet.Where( x => x.GroupId == group.Id && x.State == (byte)GroupMemberState.Approved).Select(uig => uig.UserId) .ToList()); } if (recipientType.HasFlag(GroupMessageRecipientType.Moderators)) { recipientIdList.AddRange(DataService.PerThread.GroupMemberSet.Where( x => x.GroupId == group.Id && x.State == (byte)GroupMemberState.Moderator).Select(uig => uig.UserId) .ToList()); } if (recipientType.HasFlag(GroupMessageRecipientType.NotApprovedUsers)) { recipientIdList.AddRange(DataService.PerThread.GroupMemberSet.Where( x => x.GroupId == group.Id && x.State == (byte)GroupMemberState.NotApproved).Select(uig => uig.UserId) .ToList()); } recipientIdList = recipientIdList.Distinct().ToList(); if (recipientIdList.Count > 0) { foreach (var userId in recipientIdList) { var message = new MessageStruct { AuthorId = messageInfo.AuthorId, RecipientId = userId, Text = messageInfo.Text, Type = messageInfo.Type, Date = messageInfo.Date }; Send(message); } DataService.PerThread.SaveChanges(); } UserContextService.GroupMembersAbandon(group.Id); }
public void MarkAsRead(Guid userId, Guid?contactId) { var messages = DataService.PerThread.MessageSet.Where(x => x.RecipientId == userId); if (contactId.HasValue) { messages = messages.Where(x => x.AuthorId == contactId.Value); } else { messages = messages.Where(x => !x.AuthorId.HasValue); } foreach (var msg in messages) { msg.IsRead = true; } DataService.PerThread.SaveChanges(); UserContextService.Abandon(userId); }
public Message Send(MessageStruct messageInfo) { if (DataService.PerThread.BaseUserSet.Count(u => u.Id == messageInfo.RecipientId) == 0) { throw new BusinessLogicException("Получатель не найден!"); } if (messageInfo.AuthorId.HasValue) { if (DataService.PerThread.BaseUserSet.SingleOrDefault(u => u.Id == messageInfo.AuthorId) == null) { throw new BusinessLogicException("Отправитель не найден!"); } if (messageInfo.AuthorId == messageInfo.RecipientId) { throw new BusinessLogicException("Нельзя посылать самому себе!"); } } var message = new Message { AuthorId = messageInfo.AuthorId, Date = messageInfo.Date, RecipientId = messageInfo.RecipientId, Text = messageInfo.Text, Type = messageInfo.Type, IsDeletedByAuthor = false, IsDeletedByRecipient = false }; DataService.PerThread.MessageSet.AddObject(message); DataService.PerThread.SaveChanges(); UserContextService.Abandon(message.RecipientId); return(message); }
public override void Execute() { UserContextService.ClearUnused(); }
public Group UpdateGroup(EditGroupStruct newGroupData, bool saveChanges = true) { //var parentGroup = DataService.PerThread.GroupSet.Where(g => g.Id == groupData.ParentGroupId).SingleOrDefault(); var parentGroup = DataService.PerThread.GroupSet.SingleOrDefault(g => g.Id == ConstHelper.RootGroupId); //TODO: пока нет вложенности, будет так if (parentGroup == null) { throw new BusinessLogicException("В качестве родительской указана не существующая группа"); } if (ExistAnotherGroupWithLabel(newGroupData.Label, newGroupData.GroupId)) { throw new BusinessLogicException("Существует другая группа с указанным label (ЧПУ)"); } var group = DataService.PerThread.GroupSet.SingleOrDefault(g => g.Id == newGroupData.GroupId); if (group == null) { throw new BusinessLogicException("Такой группы не существует"); } if (newGroupData.ElectionFrequency > 12) { throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в 12 месяцев"); } if (newGroupData.ElectionFrequency < 1) { throw new BusinessLogicException("Выборы в группе не могут происходить реже чем раз в месяц"); } if (newGroupData.ModeratorsCount < 3) { throw new BusinessLogicException("В группе должно быть не менее 3х модераторов"); } group.Name = newGroupData.Name; group.Label = newGroupData.Label; group.Summary = newGroupData.Summary; group.Categories.Clear(); foreach (var cid in newGroupData.Categories) { var category = DataService.PerThread.GroupCategorySet.SingleOrDefault(x => x.Id == cid); if (category != null) { group.Categories.Add(category); } } //IList<Tag> tagsToDelete = new List<Tag>(); //IList<ExpertVote> evToDelete = new List<ExpertVote>(); //foreach (var tag in group.Tags) // if (!newTagsList.Contains(tag)) // { // tagsToDelete.Add(tag); // foreach (var gm in group.GroupMembers) // if (gm.Expert != null) // gm.Expert.Tags.Remove(tag); // foreach (var ev in DataService.PerThread.ExpertVoteSet.Where(x => x.GroupMember.GroupId == group.Id && x.TagId == tag.Id)) // evToDelete.Add(ev); // } //foreach (var ev in evToDelete) // DataService.PerThread.ExpertVoteSet.DeleteObject(ev); //foreach (var tag in tagsToDelete) // group.Tags.Remove(tag); //foreach (var tag in newTagsList) // if (!group.Tags.Contains(tag)) // group.Tags.Add(tag); group.ModeratorsCount = newGroupData.ModeratorsCount; group.ElectionFrequency = newGroupData.ElectionFrequency; group.ParentGroup = parentGroup; group.PollQuorum = newGroupData.PollQuorum; group.ElectionQuorum = newGroupData.ElectionQuorum; if (saveChanges) { DataService.PerThread.SaveChanges(); } CachService.DropViewModelByModel(group.Id); UserContextService.GroupMembersAbandon(group.Id); return(group); }