Add explicit helper methods to get/edit main page

This commit is contained in:
Nassim Jahnke 2023-03-30 08:57:57 +02:00
parent cb9e388699
commit 3d8fd2b877
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 63 additions and 8 deletions

View File

@ -7,6 +7,7 @@
</list>
</option>
<JavaCodeStyleSettings>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="999" />
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="" withSubpackages="true" static="false" />

View File

@ -6,6 +6,7 @@ import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.api.project.PageEditForm;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.common.PermissionType;
import io.papermc.hangar.model.internal.api.requests.StringContent;
import io.papermc.hangar.model.internal.projects.ExtendedProjectPage;
import io.papermc.hangar.security.annotations.Anyone;
import io.papermc.hangar.security.annotations.permission.PermissionRequired;
@ -30,6 +31,15 @@ public class PagesController extends HangarComponent implements IPagesController
this.projectPageService = projectPageService;
}
@Override
@RateLimit(overdraft = 5, refillTokens = 1, refillSeconds = 5)
@PermissionRequired(type = PermissionType.PROJECT, perms = NamedPermission.VIEW_PUBLIC_INFO, args = "{#author, #slug}")
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public String getMainPage(final String author, final String slug) {
return this.getPage(author, slug, "");
}
@Override
@RateLimit(overdraft = 5, refillTokens = 1, refillSeconds = 5)
@PermissionRequired(type = PermissionType.PROJECT, perms = NamedPermission.VIEW_PUBLIC_INFO, args = "{#author, #slug}")
@ -48,7 +58,16 @@ public class PagesController extends HangarComponent implements IPagesController
@RateLimit(overdraft = 10, refillTokens = 1, refillSeconds = 20)
@PermissionRequired(perms = NamedPermission.EDIT_PAGE, type = PermissionType.PROJECT, args = "{#author, #slug}")
@ResponseStatus(HttpStatus.OK)
public void changePage(final String author, final String slug, final PageEditForm pageEditForm) {
public void editMainPage(final String author, final String slug, final StringContent pageEditForm) {
this.editPage(author, slug, new PageEditForm("", pageEditForm.getContent()));
}
@Unlocked
@Override
@RateLimit(overdraft = 10, refillTokens = 1, refillSeconds = 20)
@PermissionRequired(perms = NamedPermission.EDIT_PAGE, type = PermissionType.PROJECT, args = "{#author, #slug}")
@ResponseStatus(HttpStatus.OK)
public void editPage(final String author, final String slug, final PageEditForm pageEditForm) {
final ExtendedProjectPage projectPage = this.projectPageService.getProjectPage(author, slug, pageEditForm.path());
if (projectPage == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND, "Project page not found");

View File

@ -1,6 +1,7 @@
package io.papermc.hangar.controller.api.v1.interfaces;
import io.papermc.hangar.model.api.project.PageEditForm;
import io.papermc.hangar.model.internal.api.requests.StringContent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@ -19,6 +20,21 @@ import org.springframework.web.bind.annotation.RequestParam;
@RequestMapping(path = "/api/v1", produces = MediaType.APPLICATION_JSON_VALUE)
public interface IPagesController {
@Operation(
summary = "Returns the main page of a project",
operationId = "getMainPage",
description = "Returns the main page of a project. Requires visibility of the page.",
tags = "Pages"
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Ok"),
@ApiResponse(responseCode = "401", description = "Api session missing, invalid or expired"),
@ApiResponse(responseCode = "403", description = "Not enough permissions to use this endpoint")
})
@GetMapping(value = "/pages/main/{author}/{slug}", produces = MediaType.TEXT_PLAIN_VALUE)
String getMainPage(@Parameter(description = "The author of the project to return the page for") @PathVariable String author,
@Parameter(description = "The slug of the project to return the page for") @PathVariable String slug);
@Operation(
summary = "Returns a page of a project",
operationId = "getPage",
@ -36,9 +52,9 @@ public interface IPagesController {
@Parameter(description = "The path of the page") @RequestParam String path);
@Operation(
summary = "Changes a page of a project",
operationId = "changePage",
description = "Returns a page of a project. Requires the `edit_page` permission in the project or owning organization.",
summary = "Edits the main page of a project",
operationId = "editMainPage",
description = "Edits the main page of a project. Requires the `edit_page` permission in the project or owning organization.",
security = @SecurityRequirement(name = "HangarAuth", scopes = "edit_page"),
tags = "Pages"
)
@ -48,7 +64,24 @@ public interface IPagesController {
@ApiResponse(responseCode = "403", description = "Not enough permissions to use this endpoint")
})
@PatchMapping(path = "/pages/edit/{author}/{slug}", consumes = MediaType.APPLICATION_JSON_VALUE)
void changePage(@Parameter(description = "The author of the project to change the page for") @PathVariable String author,
@Parameter(description = "The slug of the project to change the page for") @PathVariable String slug,
@Parameter(description = "The path and new contents of the page") @RequestBody PageEditForm pageEditForm);
void editMainPage(@Parameter(description = "The author of the project to change the page for") @PathVariable String author,
@Parameter(description = "The slug of the project to change the page for") @PathVariable String slug,
@Parameter(description = "The path and new contents of the page") @RequestBody StringContent pageEditForm);
@Operation(
summary = "Edits a page of a project",
operationId = "editPage",
description = "Edits a page of a project. Requires the `edit_page` permission in the project or owning organization.",
security = @SecurityRequirement(name = "HangarAuth", scopes = "edit_page"),
tags = "Pages"
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Ok"),
@ApiResponse(responseCode = "401", description = "Api session missing, invalid or expired"),
@ApiResponse(responseCode = "403", description = "Not enough permissions to use this endpoint")
})
@PatchMapping(path = "/pages/edit/{author}/{slug}", consumes = MediaType.APPLICATION_JSON_VALUE)
void editPage(@Parameter(description = "The author of the project to change the page for") @PathVariable String author,
@Parameter(description = "The slug of the project to change the page for") @PathVariable String slug,
@Parameter(description = "The path and new contents of the page") @RequestBody PageEditForm pageEditForm);
}

View File

@ -78,7 +78,9 @@ public class ProjectFactory extends HangarComponent {
if (newPageContent == null) {
newPageContent = "# " + projectTable.getName() + "\n\n" + this.config.pages.home().message();
}
this.projectPageService.createPage(projectTable.getId(), this.config.pages.home().name(), StringUtils.slugify(this.config.pages.home().name()), newPageContent, false, null, true);
final String defaultName = this.config.pages.home().name();
this.projectPageService.createPage(projectTable.getId(), defaultName, StringUtils.slugify(defaultName), newPageContent, false, null, true);
this.jobService.save(new UpdateDiscourseProjectTopicJob(projectTable.getId()));
if (newProject.getAvatarUrl() != null) {
this.avatarService.importProjectAvatar(projectTable.getId(), newProject.getAvatarUrl());