public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } #if !DEBUG if (!await validator.Validate()) { ModelState.AddModelError(string.Empty, "Recaptcha nicht valide"); return(Page()); } #endif if (!Input.Consent) { ModelState.AddModelError(string.Empty, "Bitte stimme der Veröffentlichung deiner Frage zu."); return(Page()); } string dirty = Markdown.ToHtml(Input.Question, markdown); string html = sanitizer.Sanitize(dirty); string text = Markdown.ToPlainText(Input.Question, markdown); List <string> tags = Input.Tags?.Split(',').Select(s => s.Trim()).Select(s => s.Substring(0, Math.Min(10, s.Length))).ToList() ?? new List <string>(); string shortName = Regex.Replace(Input.Title, @"[^\u0000-\u007F]+", string.Empty); // Strip non-ascii characters shortName = Regex.Replace(shortName.ToLower(), @"\s+", "-"); const int maxLength = 50; const int minLength = 20; // Find the highest cut point that lies under maxlength and on a word boundary int cut = Math.Min(maxLength, shortName.Length); int c = 0; while ((c = shortName.IndexOf('-', c + 1)) != -1) { if (c <= maxLength) { cut = c; } else { break; } } shortName = shortName.Substring(0, cut); while (shortName.Length < minLength) { shortName += "-" + Guid.NewGuid().ToString().Substring(0, minLength - shortName.Length); } while (await database.Questions.AnyAsync(q => q.ShortName == shortName)) { shortName += "-" + Guid.NewGuid().ToString().Substring(0, 6); } string id = await database.GetNewID(); Question question; database.Questions.Add(question = new() { Name = Input.Name, Title = Input.Title, TagString = string.Join(',', Input.Tags?.Split(',').Select(s => s.Trim()) ?? Array.Empty <string>()), Email = Input.Email, QuestionHtml = html, QuestionText = text, QuestionSource = Input.Question, QuestionDate = DateTime.Now, Identifier = id, QuestionState = Question.State.Asked, ShortName = shortName }); await database.SaveChangesAsync(); if (!string.IsNullOrWhiteSpace(Input.Email)) { notificationBuilder.PushForQuestion("NewQuestionUser", question); } notificationBuilder.PushForQuestion("NewQuestionAdmin", question, false, true); homeAssistant.NotifyForQuestion(question); return(Redirect($"/QuestionConfirm?id={id}&email={!string.IsNullOrWhiteSpace(Input.Email)}" + (Input.SaveId ? $"&save=1&name=" + HttpUtility.UrlEncode(Input.Title) : ""))); }