version deletion/restoring backend

This commit is contained in:
Jake Potrebic 2021-04-08 00:08:51 -07:00
parent 15eee40823
commit dce793d3a2
No known key found for this signature in database
GPG Key ID: 7C58557EC9C421F8
15 changed files with 177 additions and 323 deletions

View File

@ -382,6 +382,9 @@ const msgs: LocaleMessageObject = {
channels: 'Channels',
editChannels: 'Edit Channels',
platforms: 'Platforms',
error: {
onlyOnePublic: 'You only have 1 public version left',
},
},
channel: {
modal: {

View File

@ -32,6 +32,7 @@
<!--TODO set recommended button -->
<v-btn v-if="$perms.canDeleteVersion" color="error" @click="deleteVersion">{{ $t('version.page.delete') }}</v-btn>
<!--TODO hard delete button if version is already soft-deleted-->
<v-btn v-if="!projectVersion.externalUrl" color="primary" :to="$route.path + '/download'">{{ $t('version.page.download') }}</v-btn>
<v-btn v-else color="primary" :to="$route.path + '/download'">{{ $t('version.page.downloadExternal') }}</v-btn>
<v-menu v-if="$perms.canViewLogs || $perms.isReviewer || $perms.canHardDeleteVersion" offset-y open-on-hover>

View File

@ -0,0 +1,45 @@
package io.papermc.hangar.controller.extras.resolvers;
import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.dao.internal.table.projects.ProjectsDAO;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.db.projects.ProjectTable;
import io.papermc.hangar.service.internal.projects.ProjectService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver;
@Component
public class ProjectTableResolver extends PathVariableMethodArgumentResolver {
private final ProjectsDAO projectsDAO;
private final ProjectService projectService;
@Autowired
public ProjectTableResolver(HangarDao<ProjectsDAO> projectsDAO, ProjectService projectService) {
this.projectsDAO = projectsDAO.get();
this.projectService = projectService;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return super.supportsParameter(parameter) && parameter.getParameterType().equals(ProjectTable.class);
}
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
Long projectId = NumberUtils.createLong((String) super.resolveName(name, parameter, request));
if (projectId == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND);
}
if (parameter.hasParameterAnnotation(NoCache.class)) {
return projectsDAO.getById(projectId);
} else {
return projectService.getProjectTable(projectId);
}
}
}

View File

@ -0,0 +1,46 @@
package io.papermc.hangar.controller.extras.resolvers;
import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.dao.internal.table.versions.ProjectVersionsDAO;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.db.UserTable;
import io.papermc.hangar.service.internal.versions.VersionService;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver;
@Component
public class ProjectVersionTableResolver extends PathVariableMethodArgumentResolver {
private final ProjectVersionsDAO projectVersionsDAO;
private final VersionService versionService;
@Autowired
public ProjectVersionTableResolver(HangarDao<ProjectVersionsDAO> projectVersionsDAO, VersionService versionService) {
this.projectVersionsDAO = projectVersionsDAO.get();
this.versionService = versionService;
}
@Override
public boolean supportsParameter(@NotNull MethodParameter parameter) {
return super.supportsParameter(parameter) && parameter.getParameterType().equals(UserTable.class);
}
@Override
protected Object resolveName(@NotNull String name, @NotNull MethodParameter parameter, @NotNull NativeWebRequest request) throws Exception {
Long versionId = NumberUtils.createLong((String) super.resolveName(name, parameter, request));
if (versionId == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND);
}
if (parameter.hasParameterAnnotation(NoCache.class)) {
return projectVersionsDAO.getProjectVersionTable(versionId);
} else {
return versionService.getProjectVersionTable(versionId);
}
}
}

View File

@ -5,6 +5,7 @@ import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.common.NamedPermission;
import io.papermc.hangar.model.common.PermissionType;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.model.db.projects.ProjectTable;
import io.papermc.hangar.model.db.versions.ProjectVersionTable;
import io.papermc.hangar.model.internal.api.requests.StringContent;
import io.papermc.hangar.model.internal.api.requests.versions.UpdatePlatformVersions;
@ -120,4 +121,29 @@ public class VersionController extends HangarComponent {
public void savePluginDependencies(@PathVariable long projectId, @PathVariable long versionId, @Valid @RequestBody UpdatePluginDependencies updatePluginDependencies) {
versionDependencyService.updateVersionPluginDependencies(projectId, versionId, updatePluginDependencies);
}
@Unlocked
@ResponseStatus(HttpStatus.NO_CONTENT)
@PermissionRequired(type = PermissionType.PROJECT, perms = NamedPermission.DELETE_VERSION, args = "{#projectId}")
@PostMapping(path = "/version/{projectId}/{versionId}/delete", consumes = MediaType.APPLICATION_JSON_VALUE)
public void softDeleteVersion(@PathVariable long projectId, @PathVariable("versionId") ProjectVersionTable version, @RequestBody @Valid StringContent commentContent) {
versionService.softDeleteVersion(projectId, version, commentContent.getContent());
}
@Unlocked
@ResponseStatus(HttpStatus.NO_CONTENT)
@PermissionRequired(NamedPermission.HARD_DELETE_VERSION)
@PostMapping(path = "/version/{projectId}/{versionId}/hardDelete", consumes = MediaType.APPLICATION_JSON_VALUE)
public void hardDeleteVersion(@PathVariable("projectId") ProjectTable projectTable, @PathVariable("versionId") ProjectVersionTable projectVersionTable, @RequestBody @Valid StringContent commentContent) {
versionService.hardDeleteVersion(projectTable, projectVersionTable, commentContent.getContent());
}
@Unlocked
@ResponseStatus(HttpStatus.CREATED)
@PermissionRequired(type = PermissionType.PROJECT, perms = NamedPermission.DELETE_VERSION, args = "{#projectId}")
@PostMapping("/version/{projectId}/{versionId}/restore")
public void restoreVersion(@PathVariable long projectId, @PathVariable("versionId") ProjectVersionTable version) {
versionService.restoreVersion(projectId, version);
}
}

View File

@ -3,8 +3,6 @@ package io.papermc.hangar.controllerold;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.papermc.hangar.config.hangar.HangarConfig;
import io.papermc.hangar.db.customtypes.LoggedActionType;
import io.papermc.hangar.db.customtypes.LoggedActionType.VersionContext;
import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.daoold.ProjectDao;
import io.papermc.hangar.db.daoold.ProjectVersionDownloadWarningDao;
@ -20,7 +18,6 @@ import io.papermc.hangar.model.common.projects.ReviewState;
import io.papermc.hangar.model.common.projects.Visibility;
import io.papermc.hangar.modelold.DownloadType;
import io.papermc.hangar.modelold.viewhelpers.ProjectData;
import io.papermc.hangar.modelold.viewhelpers.VersionData;
import io.papermc.hangar.securityold.annotations.GlobalPermission;
import io.papermc.hangar.securityold.annotations.ProjectPermission;
import io.papermc.hangar.securityold.annotations.UserLock;
@ -33,7 +30,6 @@ import io.papermc.hangar.serviceold.project.ChannelService;
import io.papermc.hangar.serviceold.project.ProjectFactory;
import io.papermc.hangar.util.AlertUtil;
import io.papermc.hangar.util.AlertUtil.AlertType;
import io.papermc.hangar.util.FileUtils;
import io.papermc.hangar.util.RequestUtil;
import io.papermc.hangar.util.Routes;
import org.springframework.beans.factory.annotation.Autowired;
@ -50,12 +46,10 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;
import javax.servlet.http.Cookie;
@ -87,13 +81,12 @@ public class VersionsController extends HangarController {
private final HttpServletRequest request;
private final HttpServletResponse response;
private final Supplier<ProjectVersionsTable> projectVersionsTable;
private final Supplier<VersionData> versionData;
private final Supplier<ProjectsTable> projectsTable;
private final Supplier<ProjectData> projectData;
@Autowired
public VersionsController(VersionService versionService, RecommendedVersionService recommendedVersionService, ProjectFactory projectFactory, ChannelService channelService, DownloadsService downloadsService, UserActionLogService userActionLogService, HangarConfig hangarConfig, HangarDao<ProjectDao> projectDao, ProjectFiles projectFiles, HangarDao<ProjectVersionDownloadWarningDao> downloadWarningDao, MessageSource messageSource, ObjectMapper mapper, HttpServletRequest request, HttpServletResponse response, Supplier<ProjectVersionsTable> projectVersionsTable, Supplier<VersionData> versionData, Supplier<ProjectsTable> projectsTable, Supplier<ProjectData> projectData) {
public VersionsController(VersionService versionService, RecommendedVersionService recommendedVersionService, ProjectFactory projectFactory, ChannelService channelService, DownloadsService downloadsService, UserActionLogService userActionLogService, HangarConfig hangarConfig, HangarDao<ProjectDao> projectDao, ProjectFiles projectFiles, HangarDao<ProjectVersionDownloadWarningDao> downloadWarningDao, MessageSource messageSource, ObjectMapper mapper, HttpServletRequest request, HttpServletResponse response, Supplier<ProjectVersionsTable> projectVersionsTable, Supplier<ProjectsTable> projectsTable, Supplier<ProjectData> projectData) {
this.versionService = versionService;
this.recommendedVersionService = recommendedVersionService;
this.projectFactory = projectFactory;
@ -109,7 +102,6 @@ public class VersionsController extends HangarController {
this.request = request;
this.response = response;
this.projectVersionsTable = projectVersionsTable;
this.versionData = versionData;
this.projectsTable = projectsTable;
this.projectData = projectData;
}
@ -267,25 +259,6 @@ public class VersionsController extends HangarController {
}
}
@ProjectPermission(NamedPermission.DELETE_VERSION)
@UserLock(route = Routes.PROJECTS_SHOW, args = "{#author, #slug}")
@Secured("ROLE_USER")
@PostMapping(value = "/{author}/{slug}/versions/{version}/delete", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView softDelete(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam String comment, RedirectAttributes ra) {
VersionData vData = versionData.get();
try {
projectFactory.prepareDeleteVersion(vData);
} catch (HangarException e) {
AlertUtil.showAlert(ra, AlertUtil.AlertType.ERROR, e.getMessage());
return Routes.VERSIONS_SHOW.getRedirect(author, slug, version);
}
Visibility oldVisibility = vData.getV().getVisibility();
versionService.changeVisibility(vData, Visibility.SOFTDELETE, comment, getCurrentUser().getId());
userActionLogService.version(request, LoggedActionType.VERSION_DELETED.with(VersionContext.of(vData.getP().getProject().getId(), vData.getV().getId())), "SoftDelete: " + comment, oldVisibility.getName());
return Routes.VERSIONS_SHOW_LIST.getRedirect(author, slug);
}
@GetMapping(value = "/{author}/{slug}/versions/{version}/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object download(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(required = false) String token, @RequestParam(defaultValue = "false") boolean confirm) {
@ -346,25 +319,6 @@ public class VersionsController extends HangarController {
return new FileSystemResource(path);
}
@GlobalPermission(NamedPermission.HARD_DELETE_PROJECT)
@Secured("ROLE_USER")
@PostMapping("/{author}/{slug}/versions/{version}/hardDelete")
public ModelAndView delete(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestBody String comment, RedirectAttributes ra) {
VersionData vData = versionData.get();
try {
projectFactory.prepareDeleteVersion(vData);
} catch (HangarException e) {
AlertUtil.showAlert(ra, AlertUtil.AlertType.ERROR, e.getMessage());
return Routes.VERSIONS_SHOW_LIST.getRedirect(author, slug);
}
Path versionDir = projectFiles.getVersionDir(vData.getP().getOwnerName(), vData.getP().getProject().getSlug(), vData.getV().getVersionString());
FileUtils.deleteDirectory(versionDir);
versionService.deleteVersion(vData.getV().getId());
userActionLogService.version(request, LoggedActionType.VERSION_DELETED.with(VersionContext.of(vData.getV().getProjectId(), vData.getV().getId())), "Deleted: " + comment, vData.getV().getVisibility().getName());
// Ore deletes the channel if no more versions are left, I don't think that is a good idea, easy enough to delete the channel manually.
return Routes.VERSIONS_SHOW_LIST.getRedirect(author, slug);
}
@GetMapping(value = "/{author}/{slug}/versions/{version}/jar", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public Object downloadJar(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam(required = false) String token) {
@ -416,14 +370,5 @@ public class VersionsController extends HangarController {
return Routes.VERSIONS_SHOW.getRedirect(author, slug, version);
}
@GlobalPermission(NamedPermission.REVIEWER)
@Secured("ROLE_USER")
@PostMapping(value = "/{author}/{slug}/versions/{version}/restore", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView restore(@PathVariable String author, @PathVariable String slug, @PathVariable String version, @RequestParam String comment) {
VersionData vData = versionData.get();
versionService.changeVisibility(vData, Visibility.PUBLIC, comment, getCurrentUser().getId());
userActionLogService.version(request, LoggedActionType.VERSION_DELETED.with(VersionContext.of(vData.getP().getProject().getId(), vData.getV().getId())), "Restore: " + comment, "");
return Routes.VERSIONS_SHOW.getRedirect(author, slug, version);
}
}

View File

@ -14,8 +14,6 @@ public class LoggedActionType<C extends AbstractContext> {
public static final LoggedActionType<ProjectContext> PROJECT_VISIBILITY_CHANGE = new LoggedActionType<>(PGLoggedAction.PROJECT_VISIBILITY_CHANGED, "ProjectVisibilityChange", "The project visibility state was changed");
public static final LoggedActionType<ProjectContext> PROJECT_SETTINGS_CHANGED = new LoggedActionType<>(PGLoggedAction.PROJECT_SETTINGS_CHANGED, "ProjectSettingsChanged", "The project settings were changed");
public static final LoggedActionType<VersionContext> VERSION_DELETED = new LoggedActionType<>(PGLoggedAction.VERSION_DELETED, "VersionDeleted", "The version was deleted");
private final PGLoggedAction value;
private final String name;
private C actionContext;
@ -73,30 +71,6 @@ public class LoggedActionType<C extends AbstractContext> {
}
}
public static class VersionContext extends AbstractContext {
private final long projectId;
private final long versionId;
private VersionContext(long projectId, long versionId) {
super(1);
this.projectId = projectId;
this.versionId = versionId;
}
public long getProjectId() {
return projectId;
}
public long getVersionId() {
return versionId;
}
public static VersionContext of(long projectId, long versionId) {
return new VersionContext(projectId, versionId);
}
}
public abstract static class AbstractContext {
protected int value;

View File

@ -1,55 +0,0 @@
package io.papermc.hangar.db.modelold;
import io.papermc.hangar.model.common.Platform;
import org.jdbi.v3.core.enums.EnumByOrdinal;
import java.time.OffsetDateTime;
@Deprecated(forRemoval = true)
public class PlatformVersionsTable {
private long id;
private OffsetDateTime createdAt;
private Platform platform;
private String version;
public PlatformVersionsTable(Platform platform, String version) {
this.platform = platform;
this.version = version;
}
public PlatformVersionsTable() { }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public OffsetDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(OffsetDateTime createdAt) {
this.createdAt = createdAt;
}
@EnumByOrdinal
public Platform getPlatform() {
return platform;
}
@EnumByOrdinal
public void setPlatform(Platform platform) {
this.platform = platform;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}

View File

@ -1,61 +0,0 @@
package io.papermc.hangar.modelold.viewhelpers;
import io.papermc.hangar.db.modelold.PlatformVersionsTable;
import io.papermc.hangar.db.modelold.ProjectChannelsTable;
import io.papermc.hangar.db.modelold.ProjectVersionsTable;
import io.papermc.hangar.model.api.project.version.PluginDependency;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.modelold.generated.PlatformDependency;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class VersionData {
private final ProjectData p;
private final ProjectVersionsTable v;
private final ProjectChannelsTable c;
private final String approvedBy;
private final Map<Platform, Map<PluginDependency, String>> dependencies;
private final Map<Platform, List<PlatformVersionsTable>> versionPlatformDependencyTables;
public VersionData(ProjectData p, ProjectVersionsTable v, ProjectChannelsTable c, String approvedBy, Map<Platform, Map<PluginDependency, String>> dependencies, Map<Platform, List<PlatformVersionsTable>> versionPlatformDependencyTables) {
this.p = p;
this.v = v;
this.c = c;
this.approvedBy = approvedBy;
this.dependencies = dependencies;
this.versionPlatformDependencyTables = versionPlatformDependencyTables;
}
public ProjectData getP() {
return p;
}
public ProjectVersionsTable getV() {
return v;
}
public ProjectChannelsTable getC() {
return c;
}
public String getApprovedBy() {
return approvedBy;
}
public Map<Platform, Map<PluginDependency, String>> getDependencies() {
return dependencies;
}
public Map<PlatformDependency, Map<PluginDependency, String>> getFormattedDependencies() {
return versionPlatformDependencyTables.entrySet().stream().collect(HashMap::new, (hashMap, platformListEntry) -> hashMap.put(new PlatformDependency(io.papermc.hangar.modelold.Platform.valueOf(platformListEntry.getKey().name()), platformListEntry.getValue().stream().map(PlatformVersionsTable::getVersion).collect(Collectors.toList())), this.dependencies.get(platformListEntry.getKey())), HashMap::putAll);
}
public boolean isRecommended() {
final Long recommendedVersionId = p.getProject().getRecommendedVersionId();
return recommendedVersionId != null && recommendedVersionId == v.getId();
}
}

View File

@ -7,13 +7,21 @@ import io.papermc.hangar.db.dao.internal.versions.HangarVersionsDAO;
import io.papermc.hangar.exceptions.HangarApiException;
import io.papermc.hangar.model.common.Permission;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.model.common.projects.Visibility;
import io.papermc.hangar.model.db.projects.ProjectTable;
import io.papermc.hangar.model.db.versions.ProjectVersionTable;
import io.papermc.hangar.model.internal.logs.LogAction;
import io.papermc.hangar.model.internal.logs.contexts.VersionContext;
import io.papermc.hangar.model.internal.versions.HangarVersion;
import io.papermc.hangar.service.internal.uploads.ProjectFiles;
import io.papermc.hangar.service.internal.visibility.ProjectVersionVisibilityService;
import io.papermc.hangar.service.internal.visibility.ProjectVisibilityService;
import io.papermc.hangar.util.FileUtils;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@ -23,15 +31,19 @@ public class VersionService extends HangarComponent {
private final ProjectVersionsDAO projectVersionsDAO;
private final HangarVersionsDAO hangarVersionsDAO;
private final ProjectVisibilityService projectVisibilityService;
private final ProjectVersionVisibilityService projectVersionVisibilityService;
private final VersionDependencyService versionDependencyService;
private final ProjectFiles projectFiles;
@Autowired
public VersionService(HangarDao<ProjectVersionsDAO> projectVersionDAO, HangarDao<HangarVersionsDAO> hangarProjectsDAO, ProjectVersionVisibilityService projectVersionVisibilityService, VersionDependencyService versionDependencyService) {
public VersionService(HangarDao<ProjectVersionsDAO> projectVersionDAO, HangarDao<HangarVersionsDAO> hangarProjectsDAO, ProjectVisibilityService projectVisibilityService, ProjectVersionVisibilityService projectVersionVisibilityService, VersionDependencyService versionDependencyService, ProjectFiles projectFiles) {
this.projectVersionsDAO = projectVersionDAO.get();
this.hangarVersionsDAO = hangarProjectsDAO.get();
this.projectVisibilityService = projectVisibilityService;
this.projectVersionVisibilityService = projectVersionVisibilityService;
this.versionDependencyService = versionDependencyService;
this.projectFiles = projectFiles;
}
@Nullable
@ -67,4 +79,45 @@ public class VersionService extends HangarComponent {
}
return versions.stream().map(v -> versionDependencyService.addDependenciesAndTags(v.getId(), v)).collect(Collectors.toList());
}
@Transactional
public void softDeleteVersion(long projectId, ProjectVersionTable pvt, String comment) {
if (pvt.getVisibility() != Visibility.SOFTDELETE) {
List<ProjectVersionTable> projectVersionTables = projectVersionsDAO.getProjectVersions(projectId);
// TODO should this disallow? or just reset project visibility to NEW
if (projectVersionTables.stream().filter(pv -> pv.getVisibility() == Visibility.PUBLIC).count() <= 1 && pvt.getVisibility() == Visibility.PUBLIC) {
throw new HangarApiException("version.error.onlyOnePublic");
}
// TODO previously, if this was a recommended version, it would auto assign a new one, but I don't think that's a good idea. easy enough to just set another, no requirement saying there has to be a recommended version
Visibility oldVisibility = pvt.getVisibility();
projectVersionVisibilityService.changeVisibility(pvt, Visibility.SOFTDELETE, comment);
userActionLogService.version(LogAction.VERSION_DELETED.create(VersionContext.of(projectId, pvt.getId()), "Soft Delete: " + comment, oldVisibility.getTitle()));
}
}
@Transactional
public void hardDeleteVersion(ProjectTable pt, ProjectVersionTable pvt, String comment) {
List<ProjectVersionTable> projectVersionTables = projectVersionsDAO.getProjectVersions(pt.getId());
boolean hasOtherPublicVersion = projectVersionTables.stream().filter(pv -> pv.getId() != pvt.getId()).anyMatch(pv -> pv.getVisibility() == Visibility.PUBLIC);
if (!hasOtherPublicVersion && pt.getVisibility() == Visibility.PUBLIC) {
projectVisibilityService.changeVisibility(pt, Visibility.NEW, "Visibility reset to new because no public versions exist");
}
userActionLogService.version(LogAction.VERSION_DELETED.create(VersionContext.of(pt.getId(), pvt.getId()), "Deleted: " + comment, pvt.getVisibility().getTitle()));
List<Platform> versionPlatforms = projectVersionsDAO.getVersionPlatforms(pvt.getId());
for (Platform platform : versionPlatforms) {
FileUtils.deleteDirectory(projectFiles.getVersionDir(pt.getOwnerName(), pt.getName(), pvt.getVersionString(), platform));
}
projectVersionsDAO.delete(pvt);
}
@Transactional
public void restoreVersion(long projectId, ProjectVersionTable pvt) {
if (pvt.getVisibility() == Visibility.SOFTDELETE) {
projectVersionVisibilityService.changeVisibility(pvt, Visibility.PUBLIC, "Version Restored");
userActionLogService.version(LogAction.VERSION_DELETED.create(VersionContext.of(projectId, pvt.getId()), "Version Restored", Visibility.SOFTDELETE.getTitle()));
}
}
}

View File

@ -33,19 +33,6 @@ public class UserActionLogService extends HangarService {
// actionsDao.get().insertProjectLog(log);
}
public void version(HttpServletRequest request, LoggedActionType<LoggedActionType.VersionContext> loggedActionType, String newState, String oldState) {
// LoggedActionsVersionTable log = new LoggedActionsVersionTable(
// getCurrentUser().getId(),
// RequestUtil.getRemoteInetAddress(request),
// loggedActionType.getValue(),
// Objects.toString(newState, ""),
// Objects.toString(oldState, ""),
// loggedActionType.getActionContext().getProjectId(),
// loggedActionType.getActionContext().getVersionId()
// );
// actionsDao.get().insertVersionLog(log);
}
public List<HangarLoggedAction> getLog(Integer oPage, String userFilter, String projectFilter, String versionFilter, String pageFilter, String actionFilter, String subjectFilter) {
long pageSize = 50L;
long offset;

View File

@ -117,10 +117,6 @@ public class UserService extends HangarService {
);
}
public UserData getUserData(long userId) {
return getUserData(userDao.get().getById(userId));
}
public UserData getUserData(String userName) {
return getUserData(userDao.get().getByName(userName));
}

View File

@ -4,23 +4,12 @@ import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.daoold.ProjectDao;
import io.papermc.hangar.db.daoold.ProjectVersionDao;
import io.papermc.hangar.db.daoold.VisibilityDao;
import io.papermc.hangar.db.modelold.ProjectChannelsTable;
import io.papermc.hangar.db.modelold.ProjectVersionVisibilityChangesTable;
import io.papermc.hangar.db.modelold.ProjectVersionsTable;
import io.papermc.hangar.db.modelold.ProjectsTable;
import io.papermc.hangar.model.api.project.version.PluginDependency;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.model.common.projects.Visibility;
import io.papermc.hangar.modelold.viewhelpers.ProjectData;
import io.papermc.hangar.modelold.viewhelpers.UserData;
import io.papermc.hangar.modelold.viewhelpers.VersionData;
import io.papermc.hangar.service.internal.versions.VersionDependencyService;
import io.papermc.hangar.service.internal.visibility.ProjectVersionVisibilityService;
import io.papermc.hangar.serviceold.project.ChannelService;
import io.papermc.hangar.serviceold.project.ProjectService;
import io.papermc.hangar.util.RequestUtil;
import io.papermc.hangar.util.StringUtils;
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
@ -30,8 +19,6 @@ import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.server.ResponseStatusException;
import javax.servlet.http.HttpServletRequest;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
@ -43,22 +30,14 @@ public class VersionService extends HangarService {
private final HangarDao<ProjectVersionDao> versionDao;
private final HangarDao<ProjectDao> projectDao;
private final HangarDao<VisibilityDao> visibilityDao;
private final ProjectService projectService;
private final ChannelService channelService;
private final UserService userService;
private final VersionDependencyService versionDependencyService;
private final HttpServletRequest request;
@Autowired
public VersionService(HangarDao<ProjectVersionDao> versionDao, HangarDao<ProjectDao> projectDao, HangarDao<VisibilityDao> visibilityDao, ProjectService projectService, ChannelService channelService, UserService userService, VersionDependencyService versionDependencyService, HttpServletRequest request) {
public VersionService(HangarDao<ProjectVersionDao> versionDao, HangarDao<ProjectDao> projectDao, HangarDao<VisibilityDao> visibilityDao, HttpServletRequest request) {
this.versionDao = versionDao;
this.projectDao = projectDao;
this.visibilityDao = visibilityDao;
this.projectService = projectService;
this.channelService = channelService;
this.userService = userService;
this.versionDependencyService = versionDependencyService;
this.request = request;
}
@ -77,14 +56,6 @@ public class VersionService extends HangarService {
}
}
@Bean
@RequestScope
@Autowired
public Supplier<VersionData> versionData(Supplier<ProjectData> projectDataSupplier) {
//noinspection SpringConfigurationProxyMethods
return () -> this.getVersionData(projectDataSupplier.get(), projectVersionsTable().get());
}
public ProjectVersionsTable getVersion(long projectId, long versionId) {
return null;
@ -96,56 +67,6 @@ public class VersionService extends HangarService {
return getVersion(projectsTable.getId(), versionId);
}
public void deleteVersion(long versionId) {
versionDao.get().deleteVersion(versionId);
}
public void changeVisibility(VersionData versionData, Visibility visibility, String comment, long userId) {
if (versionData.getV().getVisibility() == visibility) return; // No change
visibilityDao.get().updateLatestVersionChange(userId, versionData.getV().getId());
visibilityDao.get().insert(new ProjectVersionVisibilityChangesTable(userId, versionData.getV().getId(), comment, visibility));
versionData.getV().setVisibility(visibility);
versionDao.get().update(versionData.getV());
}
public VersionData getVersionData(ProjectData projectData, ProjectVersionsTable projectVersion) {
ProjectChannelsTable projectChannel = channelService.getProjectChannel(projectData.getProject().getId(), projectVersion.getChannelId());
String approvedBy = null;
if (projectVersion.getReviewerId() != null) {
UserData approveUser = userService.getUserData(projectVersion.getReviewerId());
if (approveUser == null) {
approvedBy = "Unknown";
} else {
approvedBy = approveUser.getUser().getName();
}
}
Map<Platform, Map<PluginDependency, String>> dependencies = new EnumMap<>(Platform.class);
versionDependencyService.getProjectVersionDependencyTables(projectVersion.getId()).forEach(pvdt -> {
dependencies.computeIfAbsent(pvdt.getPlatform(), platform -> new HashMap<>());
String path;
if (pvdt.getExternalUrl() != null) {
path = pvdt.getExternalUrl();
} else if (pvdt.getProjectId() != null) {
ProjectsTable projectsTable = projectService.getProjectsTable(pvdt.getProjectId());
path = "/" + projectsTable.getOwnerName() + "/" + projectsTable.getSlug();
} else {
path = null;
}
// dependencies.get(pvdt.getPlatform()).put(new PluginDependency(pvdt.getName(), pvdt.isRequired(), pvdt.getProjectId(), pvdt.getExternalUrl()), path);
});
if (true) throw new NotImplementedException();
return new VersionData(
projectData,
projectVersion,
projectChannel,
approvedBy,
dependencies, null
/*versionDependencyService.getProjectVersionPlatformDependencies(projectVersion.getId())*/);
}
public Map<ProjectVersionVisibilityChangesTable, String> getVersionVisibilityChanges(long versionId) {
return visibilityDao.get().getProjectVersionVisibilityChanges(versionId);
}

View File

@ -17,10 +17,6 @@ public class ChannelService {
this.channelDao = channelDao;
}
public ProjectChannelsTable getProjectChannel(long projectId, long channelId) {
return channelDao.get().getProjectChannel(projectId, null, channelId);
}
public ProjectChannelsTable getVersionsChannel(long projectId, long versionId) {
return channelDao.get().getVersionsChannel(projectId, versionId);
}

View File

@ -3,17 +3,11 @@ package io.papermc.hangar.serviceold.project;
import io.papermc.hangar.db.dao.HangarDao;
import io.papermc.hangar.db.daoold.ProjectDao;
import io.papermc.hangar.db.daoold.ProjectVersionDao;
import io.papermc.hangar.db.modelold.ProjectVersionsTable;
import io.papermc.hangar.db.modelold.ProjectsTable;
import io.papermc.hangar.exceptions.HangarException;
import io.papermc.hangar.model.common.projects.Visibility;
import io.papermc.hangar.modelold.viewhelpers.VersionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
@Component("oldProjectFactory")
@Deprecated(forRemoval = true)
public class ProjectFactory {
@ -41,21 +35,4 @@ public class ProjectFactory {
projectDao.get().delete(projectsTable);
}
public void prepareDeleteVersion(VersionData versionData) {
if (versionData.getP().getVisibility() == Visibility.SOFTDELETE) return;
List<ProjectVersionsTable> projectVersions = projectVersionDao.get().getProjectVersions(versionData.getP().getProject().getId());
if (projectVersions.stream().filter(p -> p.getVisibility() == Visibility.PUBLIC).count() <= 1 && versionData.getV().getVisibility() == Visibility.PUBLIC) {
throw new HangarException("error.version.onlyOnePublic");
}
ProjectVersionsTable recommended = versionData.getP().getRecommendedVersion();
if (recommended != null && versionData.getV().getId() == recommended.getId()) { // pick new recommended
Optional<ProjectVersionsTable> tableOptional = projectVersions.stream().filter(v -> v.getId() != versionData.getV().getId() && v.getVisibility() != Visibility.SOFTDELETE).findFirst();
tableOptional.ifPresent(projectVersionsTable -> {
versionData.getP().getProject().setRecommendedVersionId(projectVersionsTable.getId());
projectDao.get().update(versionData.getP().getProject());
});
}
}
}