Skip to content

Commit

Permalink
feat: include filters in query params for version page, closes #1320
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed May 11, 2024
1 parent c090a6b commit 775cb8d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -68,8 +69,10 @@ public Version getVersion(final String slug, final String name) {
@Override
@VisibilityRequired(type = VisibilityRequired.Type.PROJECT, args = "{#slug}")
@ApplicableFilters({VersionChannelFilter.class, VersionPlatformFilter.class, VersionPlatformVersionFilter.class})
public PaginatedResult<Version> getVersions(final String slug, @ConfigurePagination(defaultLimitString = "@hangarConfig.projects.initVersionLoad", maxLimit = 25) final @NotNull RequestPagination pagination) {
return this.versionsApiService.getVersions(slug, pagination);
public PaginatedResult<Version> getVersions(final String slug,
@ConfigurePagination(defaultLimitString = "@hangarConfig.projects.initVersionLoad", maxLimit = 25) final @NotNull RequestPagination pagination,
@RequestParam(required = false, defaultValue = "true") final boolean includeHiddenChannels) {
return this.versionsApiService.getVersions(slug, pagination, includeHiddenChannels);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ default Version getVersion(@Parameter(description = "The author of the project t
})
@GetMapping("/projects/{slug}/versions")
PaginatedResult<Version> getVersions(@Parameter(description = "The slug of the project to return versions for") @PathVariable String slug,
@Parameter(description = "Pagination information") @NotNull RequestPagination pagination);
@Parameter(description = "Pagination information") @NotNull RequestPagination pagination,
@Parameter(description = "Whether to include hidden-by-default channels in the result, defaults to try") boolean includeHiddenChannels);

@GetMapping("/projects/{author}/{slug}/versions")
@Deprecated(forRemoval = true)
default PaginatedResult<Version> getVersions(@Parameter(description = "The author of the project to return versions for") @PathVariable String author,
@Parameter(description = "The slug of the project to return versions for") @PathVariable String slug,
@Parameter(description = "Pagination information") @NotNull RequestPagination pagination) {
return this.getVersions(slug, pagination);
return this.getVersions(slug, pagination, true);
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.papermc.hangar.model.api.project.version.Version;
import io.papermc.hangar.model.api.project.version.VersionStats;
import io.papermc.hangar.model.api.requests.RequestPagination;
import io.papermc.hangar.model.common.ChannelFlag;
import io.papermc.hangar.model.common.Permission;
import io.papermc.hangar.model.common.Platform;
import io.papermc.hangar.model.db.projects.ProjectTable;
Expand All @@ -26,6 +27,7 @@
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Stream;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -162,20 +164,24 @@ public Version getVersion(final String slug, final String versionString) {
}

@Transactional(readOnly = true)
public PaginatedResult<Version> getVersions(final String slug, final RequestPagination pagination) {
public PaginatedResult<Version> getVersions(final String slug, final RequestPagination pagination, final boolean includeHiddenChannels) {
//TODO Squash queries
final boolean canSeeHidden = this.getGlobalPermissions().has(Permission.SeeHidden);
final ProjectTable projectTable = this.projectService.getProjectTable(slug);
if (projectTable == null) {
throw new HangarApiException(HttpStatus.NOT_FOUND);
}

final List<Version> versions = this.versionsApiDAO.getVersions(projectTable.getSlug(), canSeeHidden, this.getHangarUserId(), pagination).entrySet().parallelStream()
Stream<Version> versions = this.versionsApiDAO.getVersions(projectTable.getSlug(), canSeeHidden, this.getHangarUserId(), pagination).entrySet().parallelStream()
.map(entry -> this.versionDependencyService.addDownloadsAndDependencies(projectTable.getOwnerName(), projectTable.getSlug(), entry.getValue().getName(), entry.getKey()).applyTo(entry.getValue()))
.sorted((v1, v2) -> v2.getCreatedAt().compareTo(v1.getCreatedAt()))
.toList();
.sorted((v1, v2) -> v2.getCreatedAt().compareTo(v1.getCreatedAt()));

if (!includeHiddenChannels) {
versions = versions.filter(version -> !version.getChannel().getFlags().contains(ChannelFlag.HIDE_BY_DEFAULT));
}

final Long versionCount = this.versionsApiDAO.getVersionCount(projectTable.getSlug(), canSeeHidden, this.getHangarUserId(), pagination);
return new PaginatedResult<>(new Pagination(versionCount == null ? 0 : versionCount, pagination), versions);
return new PaginatedResult<>(new Pagination(versionCount == null ? 0 : versionCount, pagination), versions.toList());
}

public Map<String, VersionStats> getVersionStats(final String slug, final String versionString, final OffsetDateTime fromDate, final OffsetDateTime toDate) {
Expand Down
30 changes: 23 additions & 7 deletions frontend/src/pages/[user]/[project]/versions/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ import { NamedPermission, Visibility, ChannelFlag } from "~/types/backend";
import type { Platform, HangarProject, Version } from "~/types/backend";
const i18n = useI18n();
const router = useRouter();
const route = useRoute("user-project-versions");
const toArray = <T,>(input: unknown): T => (Array.isArray(input) ? input : input ? [input] : []) as T;
const filter = reactive({
channels: [] as string[],
platforms: [] as Platform[],
channels: toArray<string[]>(route.query.channel),
platforms: toArray<Platform[]>(route.query.platform),
allChecked: {
channels: true,
platforms: true,
},
});
console.log("loaded filters", filter);
const props = defineProps<{
project: HangarProject;
}>();
const platforms = computed(() => [...(useBackendData.platforms?.values() || [])]);
const pagination = ref();
const page = ref(0);
const page = ref(route.query.page ? Number(route.query.page) : 0);
const requestParams = computed(() => {
const limit = 7;
return {
Expand All @@ -31,17 +34,25 @@ const requestParams = computed(() => {
};
});
const results = await Promise.all([useProjectChannels(route.params.project), useProjectVersions(route.params.project, requestParams.value)]);
const results = await Promise.all([
useProjectChannels(route.params.project),
useProjectVersions(route.params.project, { ...requestParams.value, includeHiddenChannels: route.query.channel != null }),
]);
const channels = results[0].data;
const versions = results[1];
filter.channels.push(...channels.value.filter((c) => !c.flags.includes(ChannelFlag.HIDE_BY_DEFAULT)).map((c) => c.name));
filter.platforms.push(...platforms.value.map((p) => p.enumName));
if (!route.query.channel) {
filter.channels.push(...channels.value.filter((c) => !c.flags.includes(ChannelFlag.HIDE_BY_DEFAULT)).map((c) => c.name));
}
if (!route.query.platform) {
filter.platforms.push(...platforms.value.map((p) => p.enumName));
}
useHead(useSeo("Versions | " + props.project.name, props.project.description, route, props.project.avatarUrl));
const pageChangeScrollAnchor = ref<Element>();
async function update(newPage: number) {
console.log("update", requestParams.value);
page.value = newPage;
versions.value = (await useProjectVersions(route.params.project, requestParams.value))?.value;
}
Expand All @@ -56,7 +67,12 @@ watch(
versions.value.result = [];
return;
}
// dont want limit in url, its hardcoded in frontend
// offset we dont want, we set page instead
const { limit, offset, ...paramsWithoutLimit } = requestParams.value;
// set the request params
await router.replace({ query: { page: page.value, ...paramsWithoutLimit } });
// do the update
await update(0);
},
{ deep: true }
Expand Down

0 comments on commit 775cb8d

Please sign in to comment.