feat: avatarurl for users

This commit is contained in:
MiniDigger | Martin 2023-10-28 21:39:04 +02:00
parent b228e1f30c
commit f2b57843d6
2 changed files with 27 additions and 14 deletions

View File

@ -1,6 +1,8 @@
package io.papermc.hangar.components.query;
import graphql.schema.DataFetchingEnvironment;
import io.papermc.hangar.components.images.service.AvatarService;
import io.papermc.hangar.service.internal.file.FileService;
import java.util.List;
public final class QueryHelper {
@ -30,4 +32,22 @@ public final class QueryHelper {
queryBuilder.joins.add(STR."JOIN \{table} \{parentAlias}\{alias} ON \{parentAlias}\{alias}.\{fieldA} = \{parentTable}\{fieldB}");
return EMPTY_LIST;
}
public static Object avatarUrl(final DataFetchingEnvironment environment, final FileService fileService, final String avatarType) {
final String idVar = avatarType.equals(AvatarService.USER) ? "userid" : "projectid";
final String idField = avatarType.equals(AvatarService.USER) ? "uuid" : "id";
final QueryBuilder queryBuilder = environment.getGraphQlContext().get("queryBuilder");
final String parentTable = PrefixUtil.getParentTable(environment.getExecutionStepInfo(), queryBuilder);
final String parentAlias = PrefixUtil.getParentAlias(environment.getExecutionStepInfo(), queryBuilder);
final String avatarVersion = STR."ext_\{parentAlias.replace("_", "")}avatarversion";
final String id = STR."ext_\{parentAlias.replace("_", "")}\{idVar}";
queryBuilder.fields.add(STR."\{parentAlias}avatar.version AS \{avatarVersion}");
queryBuilder.fields.add(STR."\{parentTable}\{idField} AS \{id}");
queryBuilder.joins.add(STR."JOIN avatars \{parentAlias}avatar ON \{parentAlias}avatar.type = '\{avatarType}' AND \{parentAlias}avatar.subject = \{parentTable}\{idField}::varchar");
queryBuilder.resolver.put(parentAlias + "avatarUrl", (r) -> fileService.getAvatarUrl(avatarType, String.valueOf(r.get(id)), (Integer) r.get(avatarVersion)));
return EMPTY;
}
}

View File

@ -7,7 +7,7 @@ import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.stereotype.Controller;
import static io.papermc.hangar.components.query.QueryHelper.EMPTY;
import static io.papermc.hangar.components.query.QueryHelper.avatarUrl;
import static io.papermc.hangar.components.query.QueryHelper.join;
import static io.papermc.hangar.components.query.QueryHelper.query;
@ -21,7 +21,6 @@ public class QueryMappings {
}
// queries
@QueryMapping
public Object projectBySlug(final DataFetchingEnvironment environment) {
return query(environment, "projects", "WHERE projects.slug = :slug");
@ -38,7 +37,6 @@ public class QueryMappings {
}
// joins
@SchemaMapping(typeName = "User", field = "projects")
public Object userProjects(final DataFetchingEnvironment environment) {
return join(environment, "projects", "projects", "owner_id", "id");
@ -50,19 +48,14 @@ public class QueryMappings {
}
// special schemas
@SchemaMapping(typeName = "Project", field = "avatarUrl")
public Object projectAvatarUrl(final DataFetchingEnvironment environment) {
final QueryBuilder queryBuilder = environment.getGraphQlContext().get("queryBuilder");
final String parentTable = PrefixUtil.getParentTable(environment.getExecutionStepInfo(), queryBuilder);
final String parentAlias = PrefixUtil.getParentAlias(environment.getExecutionStepInfo(), queryBuilder);
final String avatarVersion = STR."ext_\{parentAlias.replace("_", "")}avatarversion";
final String projectId = STR."ext_\{parentAlias.replace("_", "")}projectid";
queryBuilder.fields.add(STR."\{parentAlias}avatar.version AS \{avatarVersion}");
queryBuilder.fields.add(STR."\{parentTable}id AS \{projectId}");
queryBuilder.joins.add(STR."JOIN avatars \{parentAlias}avatar ON \{parentAlias}avatar.type = 'project' AND \{parentAlias}avatar.subject = \{parentTable}id::varchar");
queryBuilder.resolver.put(parentAlias + "avatarUrl", (r) -> this.fileService.getAvatarUrl(AvatarService.PROJECT, String.valueOf(r.get(projectId)), (Integer) r.get(avatarVersion)));
return EMPTY;
return avatarUrl(environment, this.fileService, AvatarService.PROJECT);
}
@SchemaMapping(typeName = "User", field = "avatarUrl")
public Object userUrl(final DataFetchingEnvironment environment) {
return avatarUrl(environment, this.fileService, AvatarService.USER);
}
@SchemaMapping(typeName = "Project", field = "namespace")