async Task UpdateFollowStatus(IEntity entity, IUser user) { // The follow type var followType = FollowTypes.Doc; // Get any existing follow var existingFollow = await _followStore.SelectByNameThingIdAndCreatedUserId( followType.Name, entity.Id, user.Id); // Add the follow if (FollowPostedValue()) { // If we didn't find an existing follow create a new one if (existingFollow == null) { // Add follow await _followManager.CreateAsync(new Follows.Models.Follow() { Name = followType.Name, ThingId = entity.Id, CreatedUserId = user.Id, CreatedDate = DateTime.UtcNow }); } } else { if (existingFollow != null) { await _followManager.DeleteAsync(existingFollow); } } }
public override async Task <IViewProviderResult> BuildUpdateAsync(Reply reply, IViewProviderContext context) { if (reply == null) { return(await BuildIndexAsync(new Reply(), context)); } // Get authenticated user var user = await _contextFacade.GetAuthenticatedUserAsync(context.Controller.HttpContext.User?.Identity); // We must be authenticated to automatically follow entities if (user == null) { return(await BuildEditAsync(reply, context)); } // Get entity for reply var entity = await _entityStore.GetByIdAsync(reply.EntityId); // We always need an entity if (entity == null) { return(await BuildEditAsync(reply, context)); } // Are we authorized to automatically follow entities we participate in? if (!await _authorizationService.AuthorizeAsync(context.Controller.HttpContext.User, entity.CategoryId, Permissions.AutoFollowTopicReplies)) { return(await BuildEditAsync(reply, context)); } // --------- // Automatically follow entities upon a reply // --------- var followType = FollowTypes.Topic; // Are we already following the entity? var entityFollow = await _followStore.SelectByNameThingIdAndCreatedUserId( followType.Name, entity.Id, user.Id); // Ensure we are not already following the entity if (entityFollow == null) { // Add follow await _followManager.CreateAsync(new Follows.Models.Follow() { Name = followType.Name, ThingId = entity.Id, CreatedUserId = user.Id, CreatedDate = DateTime.UtcNow }); } return(await BuildEditAsync(reply, context)); }
public async Task <IActionResult> Post([FromBody] Models.Follow follow) { // We need a user to subscribe to the thing var user = await base.GetAuthenticatedUserAsync(); if (user == null) { return(base.UnauthorizedException()); } // Is the user already following the thing? var existingFollow = await _followStore.SelectByNameThingIdAndCreatedUserId( follow.Name, follow.ThingId, user.Id); if (existingFollow != null) { return(base.Result(HttpStatusCode.OK, $"Authenticated user already following object with id '{follow.ThingId}'")); } // Build a new subscription var followToAdd = new Models.Follow() { Name = follow.Name, ThingId = follow.ThingId, CreatedUserId = user.Id, CreatedDate = DateTime.UtcNow }; // Add and return result var result = await _followManager.CreateAsync(followToAdd); if (result != null) { return(base.Result(result)); } // We should not reach here return(base.InternalServerError()); }
public override async Task <IViewProviderResult> BuildUpdateAsync(Question question, IViewProviderContext updater) { // Ensure entity exists before attempting to update var entity = await _entityStore.GetByIdAsync(question.Id); if (entity == null) { return(await BuildEditAsync(question, updater)); } // Get the follow checkbox value var follow = false; foreach (var key in _request.Form.Keys) { if (key == FollowHtmlName) { var values = _request.Form[key]; if (!String.IsNullOrEmpty(values)) { follow = true; break; } } } // We need to be authenticated to follow var user = await _contextFacade.GetAuthenticatedUserAsync(); if (user == null) { return(await BuildEditAsync(question, updater)); } // The follow type var followType = FollowTypes.Question; // Get any existing follow var existingFollow = await _followStore.SelectByNameThingIdAndCreatedUserId( followType.Name, entity.Id, user.Id); // Add the follow if (follow) { // If we didn't find an existing follow create a new one if (existingFollow == null) { // Add follow await _followManager.CreateAsync(new Follows.Models.Follow() { Name = followType.Name, ThingId = entity.Id, CreatedUserId = user.Id, CreatedDate = DateTime.UtcNow }); } } else { if (existingFollow != null) { await _followManager.DeleteAsync(existingFollow); } } return(await BuildEditAsync(question, updater)); }