Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link-only posts #52

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class PostResponseContract
[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("access_key")]
public string AccessKey { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

Expand Down
47 changes: 44 additions & 3 deletions backend/CircleForms/Controllers/V1/PostsController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using CircleForms.Contracts.V1;
using CircleForms.Contracts.V1.ContractModels.Request;
using CircleForms.Contracts.V1.ContractModels.Response;
using CircleForms.Models.Enums;
using CircleForms.Models.Posts;
using CircleForms.Models.Posts.Questions;
using CircleForms.Models.Posts.Questions.Submissions;
Expand Down Expand Up @@ -125,6 +128,25 @@ public async Task<IActionResult> Answer(string id, [FromBody] List<SubmissionCon
return Ok();
}

private static string GenerateAccessKey(byte size)
JustRoxy marked this conversation as resolved.
Show resolved Hide resolved
{
const string chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var data = new byte[size];
using (var crypto = RandomNumberGenerator.Create())
{
crypto.GetBytes(data);
}

var result = new StringBuilder(size);
foreach (var b in data)
{
result.Append(chars[b % chars.Length]);
}

return result.ToString();
}

/// <summary>
/// Add a new post. (Requires auth)
/// </summary>
Expand All @@ -148,6 +170,10 @@ public async Task<IActionResult> Post(PostRequestContract postContract)
_logger.LogInformation("User {User} posts a post {PostId}", claim, post.ID);

post.AuthorId = claim;
if (post.Accessibility == Accessibility.Link)
{
post.AccessKey = GenerateAccessKey(6);
}

for (var i = 0; i < post.Questions.Count; i++)
{
Expand Down Expand Up @@ -181,22 +207,37 @@ public async Task<IActionResult> Post(PostRequestContract postContract)
[ProducesResponseType(typeof(PostResponseContract), StatusCodes.Status200OK, "application/json")]
[ProducesResponseType(typeof(PostDetailedResponseContract), StatusCodes.Status200OK, "application/json")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetDetailed(string id)
public async Task<IActionResult> GetDetailed(string id, [FromQuery] string key = "")
{
var claim = HttpContext.User.Identity?.Name;
var post = await _postRepository.Get(id);
var post = await _postRepository.GetCached(id);
if (post is null)
{
return NotFound();
}

Post forceRequestedPost = null;
if (post.Accessibility == Accessibility.Link)
{
if (string.IsNullOrEmpty(key) || key.Length != 6)
JustRoxy marked this conversation as resolved.
Show resolved Hide resolved
{
return NotFound();
}

forceRequestedPost = await _postRepository.Get(id);
if (forceRequestedPost.AccessKey != key)
{
return NotFound();
}
}

if (string.IsNullOrEmpty(claim) || !long.TryParse(claim, out _))
{
return Ok(_mapper.Map<PostDetailedResponseContract>(post));
}

return post.AuthorId == claim
? Ok(_mapper.Map<PostResponseContract>(await _postRepository.Get(id)))
? Ok(_mapper.Map<PostResponseContract>(forceRequestedPost ?? await _postRepository.Get(id)))
: Ok(_mapper.Map<PostDetailedResponseContract>(post));
}

Expand Down
3 changes: 3 additions & 0 deletions backend/CircleForms/Models/Posts/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public Post()
[JsonProperty("title")]
public string Title { get; set; }

[JsonProperty("access_key")]
public string AccessKey { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

Expand Down
6 changes: 6 additions & 0 deletions backend/CircleForms/Services/Database/PostRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using AutoMapper;
using CircleForms.Models;
using CircleForms.Models.Enums;
using CircleForms.Models.Posts;
using CircleForms.Services.Database.Interfaces;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -56,6 +57,11 @@ await DB.Update<User>()
return post;
}

if (post.Accessibility != Accessibility.Public)
{
return post;
}

_logger.LogInformation("Adding {PostId} to the cached posts set", postId);
if (!await redisDb.SortedSetAddAsync("posts", postId, publishUnixTime))
{
Expand Down