public async Task <CommandResponse <bool> > ToggleNSFW(int submissionID) { DemandAuthentication(); // get model for selected submission var submission = _db.Submission.Find(submissionID); var response = CommandResponse.FromStatus(Status.Error); if (submission == null || submission.IsDeleted) { return(CommandResponse.FromStatus(false, Status.Error, "Submission is missing or deleted")); } var subverse = submission.Subverse; if (!User.Identity.Name.IsEqual(submission.UserName)) { // check if caller is subverse moderator, if not, deny change if (!ModeratorPermission.HasPermission(User, subverse, Domain.Models.ModeratorAction.AssignFlair)) { return(CommandResponse.FromStatus(false, Status.Denied, "Moderator Permissions are not satisfied")); } } try { submission.IsAdult = !submission.IsAdult; await _db.SaveChangesAsync(); return(CommandResponse.FromStatus(submission.IsAdult, Status.Success)); } catch (Exception ex) { return(CommandResponse.Error <CommandResponse <bool> >(ex)); } }
public async Task <CommandResponse <string> > RegenerateThumbnail(int submissionID) { DemandAuthentication(); // get model for selected submission var submission = _db.Submission.Find(submissionID); var response = CommandResponse.FromStatus(Status.Error); if (submission == null || submission.IsDeleted) { return(CommandResponse.FromStatus("", Status.Error, "Submission is missing or deleted")); } var subverse = submission.Subverse; // check if caller is subverse moderator, if not, deny change if (!ModeratorPermission.HasPermission(User, subverse, Domain.Models.ModeratorAction.AssignFlair)) { return(CommandResponse.FromStatus("", Status.Denied, "Moderator Permissions are not satisfied")); } try { throw new NotImplementedException(); await _db.SaveChangesAsync(); return(CommandResponse.FromStatus("", Status.Success)); } catch (Exception ex) { return(CommandResponse.Error <CommandResponse <string> >(ex)); } }
private object Execute(string commandName, string commandJson) { try { return(_commander.Execute(commandName, commandJson)); } catch (Exception ex) { return(CommandResponse.Error(ex.ToString())); } }
public async Task <CommandResponse <bool?> > DeleteSet(DomainReference setReference) { DemandAuthentication(); var set = GetSet(setReference.Name, setReference.OwnerName); if (set == null) { return(CommandResponse.FromStatus <bool?>(null, Status.Denied, "Set cannot be found")); } //Check Perms var perms = SetPermission.GetPermissions(set.Map(), User.Identity); if (!perms.Delete) { return(CommandResponse.FromStatus <bool?>(null, Status.Denied, "User not authorized to delete set")); } var param = new DynamicParameters(); param.Add("ID", set.ID); var conn = _db.Connection; //var tran = conn.BeginTransaction(System.Data.IsolationLevel.Serializable); try { var d = new DapperDelete(); d.Delete = SqlFormatter.DeleteBlock(SqlFormatter.Table("SubverseSetSubscription")); d.Where = "\"SubverseSetID\" = @ID"; await conn.ExecuteAsync(d.ToString(), param); d.Delete = SqlFormatter.DeleteBlock(SqlFormatter.Table("SubverseSetList")); d.Where = "\"SubverseSetID\" = @ID"; await conn.ExecuteAsync(d.ToString(), param); d.Delete = SqlFormatter.DeleteBlock(SqlFormatter.Table("SubverseSet")); d.Where = "\"ID\" = @ID"; await conn.ExecuteAsync(d.ToString(), param); //tran.Commit(); return(CommandResponse.FromStatus <bool?>(true, Status.Success)); } catch (Exception ex) { //tran.Rollback(); return(CommandResponse.Error <CommandResponse <bool?> >(ex)); } }
public async Task <CommandResponse> ToggleSticky(int submissionID, string subverse = null, bool clearExisting = false, int stickyLimit = 3) { DemandAuthentication(); // get model for selected submission var submission = _db.Submission.Find(submissionID); var response = CommandResponse.FromStatus(Status.Error); if (submission == null || submission.IsDeleted) { return(CommandResponse.FromStatus(Status.Error, "Submission is missing or deleted")); } //Eventually we want users to be able to sticky other subs posts, but for now make sure we don't allow this subverse = submission.Subverse; // check if caller is subverse moderator, if not, deny change if (!ModeratorPermission.HasPermission(User, subverse, Domain.Models.ModeratorAction.AssignStickies)) { return(CommandResponse.FromStatus(Status.Denied, "Moderator Permissions are not satisfied")); } int affectedCount = 0; try { // find and clear current sticky if toggling var existingSticky = _db.StickiedSubmission.FirstOrDefault(s => s.SubmissionID == submissionID); if (existingSticky != null) { _db.StickiedSubmission.Remove(existingSticky); affectedCount += -1; } else { if (clearExisting) { // remove all stickies for subverse matching submission subverse _db.StickiedSubmission.RemoveRange(_db.StickiedSubmission.Where(s => s.Subverse == subverse)); affectedCount = 0; } // set new submission as sticky var stickyModel = new Data.Models.StickiedSubmission { SubmissionID = submissionID, CreatedBy = User.Identity.Name, CreationDate = Repository.CurrentDate, Subverse = subverse }; _db.StickiedSubmission.Add(stickyModel); affectedCount += 1; } //limit sticky counts var currentCount = _db.StickiedSubmission.Count(x => x.Subverse == subverse); if ((currentCount + affectedCount) > stickyLimit) { return(CommandResponse.FromStatus(Status.Denied, $"Stickies are limited to {stickyLimit}")); } await _db.SaveChangesAsync(); StickyHelper.ClearStickyCache(submission.Subverse); return(CommandResponse.FromStatus(Status.Success)); } catch (Exception ex) { return(CommandResponse.Error <CommandResponse>(ex)); } }
public async Task <CommandResponse <Domain.Models.Set> > CreateOrUpdateSet(Set set) { DemandAuthentication(); set.Name = set.Name.TrimSafe(); //Evaulate Rules var context = new VoatRuleContext(User); context.PropertyBag.Set = set; var outcome = VoatRulesEngine.Instance.EvaluateRuleSet(context, RuleScope.CreateSet); if (!outcome.IsAllowed) { return(MapRuleOutCome <Set>(outcome, null)); } var existingSet = _db.SubverseSet.FirstOrDefault(x => x.ID == set.ID); if (existingSet != null) { var perms = SetPermission.GetPermissions(existingSet.Map(), User.Identity); if (!perms.EditProperties) { return(CommandResponse.FromStatus <Set>(null, Status.Denied, "User does not have permission to edit this set")); } //HACK: Need to clear this entry out of cache if name changes and check name if (!existingSet.Name.IsEqual(set.Name)) { if (_db.SubverseSet.Any(x => x.Name.ToLower() == set.Name.ToLower() && x.UserName.ToLower() == UserName.ToLower())) { return(CommandResponse.FromStatus <Set>(null, Status.Denied, "A set with this name already exists")); } CacheHandler.Instance.Remove(CachingKey.Set(existingSet.Name, existingSet.UserName)); } existingSet.Name = set.Name; existingSet.Title = set.Title; existingSet.Description = set.Description; existingSet.IsPublic = set.IsPublic; await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT); return(CommandResponse.FromStatus <Set>(existingSet.Map(), Status.Success)); } else { //Validation - MOVE TO RULES SYSTEM MAYBE if (!VoatSettings.Instance.SetCreationEnabled || VoatSettings.Instance.MaximumOwnedSets <= 0) { return(CommandResponse.FromStatus <Set>(null, Status.Denied, "Set creation is currently disabled")); } if (VoatSettings.Instance.MaximumOwnedSets > 0) { var d = new DapperQuery(); d.Select = $"SELECT COUNT(*) FROM {SqlFormatter.Table("SubverseSet", "subSet")}"; d.Where = "subSet.\"Type\" = @Type AND subSet.\"UserName\" = @UserName"; d.Parameters.Add("Type", (int)SetType.Normal); d.Parameters.Add("UserName", UserName); var setCount = _db.Connection.ExecuteScalar <int>(d.ToString(), d.Parameters); if (setCount >= VoatSettings.Instance.MaximumOwnedSets) { return(CommandResponse.FromStatus <Set>(null, Status.Denied, $"Sorry, Users are limited to {VoatSettings.Instance.MaximumOwnedSets} sets and you currently have {setCount}")); } } //Create new set try { var setCheck = GetSet(set.Name, UserName); if (setCheck != null) { return(CommandResponse.FromStatus <Set>(null, Status.Denied, "A set with same name and owner already exists")); } var newSet = new SubverseSet { Name = set.Name, Title = set.Title, Description = set.Description, UserName = UserName, Type = (int)SetType.Normal, IsPublic = set.IsPublic, CreationDate = Repository.CurrentDate, SubscriberCount = 1, //Owner is a subscriber. Reminds me of that hair club commercial: I"m not only the Set Owner, I'm also a subscriber. }; _db.SubverseSet.Add(newSet); await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT); _db.SubverseSetSubscription.Add(new SubverseSetSubscription() { SubverseSetID = newSet.ID, UserName = UserName, CreationDate = CurrentDate }); await _db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT); return(CommandResponse.Successful(newSet.Map())); } catch (Exception ex) { return(CommandResponse.Error <CommandResponse <Set> >(ex)); } } }
public static async Task <CommandResponse <string> > GenerateThumbnail(Uri uri, bool purgeTempFile = true) { if (VoatSettings.Instance.OutgoingTraffic.Enabled) { var url = uri.ToString(); if (!String.IsNullOrEmpty(url) && UrlUtility.IsUriValid(url)) { try { //Ok this all needs to be centralized, we should only make 1 request to a remote resource using (var httpResource = new HttpResource( new Uri(url), new HttpResourceOptions() { AllowAutoRedirect = true }, VoatSettings.Instance.OutgoingTraffic.Proxy.ToWebProxy())) { var result = await httpResource.GiddyUp(); if (httpResource.IsImage) { var fileManager = FileManager.Instance; var fileCheck = fileManager.IsUploadPermitted(url, FileType.Thumbnail, httpResource.Response.Content.Headers.ContentType.MediaType, httpResource.Stream.Length); if (fileCheck.Success) { var key = new FileKey(); key.FileType = FileType.Thumbnail; key.ID = await GenerateRandomFilename(".jpg", FileType.Thumbnail); var stream = httpResource.Stream; await GenerateImageThumbnail(fileManager, key, stream, VoatSettings.Instance.ThumbnailSize, true); return(CommandResponse.Successful(key.ID)); } } else if (httpResource.Image != null) { //just do it. again. return(await GenerateThumbnail(httpResource.Image)); } } } catch (Exception ex) { EventLogger.Instance.Log(ex, new { url = url, type = FileType.Thumbnail }); var response = CommandResponse.Error <CommandResponse <string> >(ex); response.Response = ""; //Make sure this returns string.empty as other failures do. return(response); } } EventLogger.Instance.Log(new LogInformation() { Type = LogType.Debug, Category = "Thumbnail Diag", Message = "Default Response", Data = new { url = url }, Origin = VoatSettings.Instance.Origin }); } return(CommandResponse.FromStatus <string>("", Status.Invalid)); }