-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using NexusMods.Abstractions.Games.DTO; | ||
using NexusMods.Abstractions.NexusModsLibrary; | ||
using NexusMods.Abstractions.NexusModsLibrary.Models; | ||
using NexusMods.Abstractions.NexusWebApi.Types; | ||
using NexusMods.MnemonicDB.Abstractions; | ||
using NexusMods.Paths; | ||
|
||
namespace NexusMods.Networking.NexusWebApi.Extensions; | ||
|
||
/// <summary> | ||
/// Extensions to GraphQL fragments. | ||
/// </summary> | ||
public static class FragmentExtensions | ||
{ | ||
/// <summary> | ||
/// Resolves the IUserFragment to an entity in the database, inserting or updating as necessary. | ||
/// </summary> | ||
public static async Task<EntityId> Resolve(this IUserFragment userFragment, IDb db, ITransaction tx, HttpClient client, CancellationToken token) | ||
{ | ||
var userResolver = GraphQLResolver.Create(db, tx, User.NexusId, (ulong)userFragment.MemberId); | ||
userResolver.Add(User.Name, userFragment.Name); | ||
userResolver.Add(User.Avatar, new Uri(userFragment.Avatar)); | ||
|
||
var avatarImage = await DownloadImage(client, userFragment.Avatar, token); | ||
userResolver.Add(User.AvatarImage,avatarImage); | ||
return userResolver.Id; | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the IModFragment to an entity in the database, inserting or updating as necessary. | ||
/// </summary> | ||
public static EntityId Resolve(this IModFileFragment modFileFragment, IDb db, ITransaction tx, EntityId modEId) | ||
{ | ||
var nexusFileResolver = GraphQLResolver.Create(db, tx, (NexusModsFileMetadata.FileId, FileId.From((ulong)modFileFragment.FileId)), (NexusModsFileMetadata.ModPageId, modEId)); | ||
Check failure on line 34 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build
Check failure on line 34 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (ubuntu-latest)
Check failure on line 34 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (ubuntu-latest)
Check failure on line 34 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (macos-latest)
|
||
nexusFileResolver.Add(NexusModsFileMetadata.ModPageId, modEId); | ||
nexusFileResolver.Add(NexusModsFileMetadata.Name, modFileFragment.Name); | ||
nexusFileResolver.Add(NexusModsFileMetadata.Version, modFileFragment.Version); | ||
if (ulong.TryParse(modFileFragment.SizeInBytes, out var size)) | ||
nexusFileResolver.Add(NexusModsFileMetadata.Size, Size.From(size)); | ||
return nexusFileResolver.Id; | ||
} | ||
|
||
/// <summary> | ||
/// Resolves the IModFragment to an entity in the database, inserting or updating as necessary. | ||
/// </summary> | ||
public static EntityId Resolve(this IModFragment modFragment, IDb db, ITransaction tx) | ||
{ | ||
var nexusModResolver = GraphQLResolver.Create(db, tx, NexusModsModPageMetadata.ModId, | ||
ModId.From((ulong)modFragment.ModId)); | ||
Check failure on line 49 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build
Check failure on line 49 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (ubuntu-latest)
Check failure on line 49 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (ubuntu-latest)
Check failure on line 49 in src/Networking/NexusMods.Networking.NexusWebApi/Extensions/FragmentExtensions.cs GitHub Actions / build-and-test / Build and Test (macos-latest)
|
||
|
||
nexusModResolver.Add(NexusModsModPageMetadata.Name, modFragment.Name); | ||
nexusModResolver.Add(NexusModsModPageMetadata.GameDomain, GameDomain.From(modFragment.Game.DomainName)); | ||
|
||
if (Uri.TryCreate(modFragment.PictureUrl, UriKind.Absolute, out var fullSizedPictureUri)) | ||
nexusModResolver.Add(NexusModsModPageMetadata.FullSizedPictureUri, fullSizedPictureUri); | ||
|
||
if (Uri.TryCreate(modFragment.ThumbnailUrl, UriKind.Absolute, out var thumbnailUri)) | ||
nexusModResolver.Add(NexusModsModPageMetadata.ThumbnailUri, thumbnailUri); | ||
return nexusModResolver.Id; | ||
} | ||
|
||
private static async Task<byte[]> DownloadImage(HttpClient client, string? uri, CancellationToken token) | ||
{ | ||
if (uri is null) return []; | ||
if (!Uri.TryCreate(uri, UriKind.Absolute, out var imageUri)) return []; | ||
|
||
return await client.GetByteArrayAsync(imageUri, token); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
fragment UserFragment on User { | ||
name | ||
avatar | ||
memberId | ||
} | ||
|
||
fragment ModFileFragment on ModFile { | ||
name, | ||
modId, | ||
fileId, | ||
version, | ||
sizeInBytes | ||
} | ||
|
||
fragment ModFragment on Mod { | ||
modId | ||
name | ||
game { | ||
domainName | ||
} | ||
thumbnailUrl | ||
pictureUrl | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include { ModFragment } from './CommonFragments.graphql' | ||
|
||
query ModInfo($gameDomain: String!, $modId: Int!) | ||
{ | ||
legacyModsByDomain(ids: [{gameDomain: $gameDomain, modId: $modId}]) | ||
{ | ||
nodes | ||
{ | ||
...ModFragment | ||
} | ||
} | ||
} |