public static async Task <bool?> GetEndorsementStatusForFile(string gamedomain, int fileid, int currentuserid)
        {
            if (!NexusModsUtilities.HasAPIKey)
            {
                return(false);
            }
            var client  = NexusModsUtilities.GetClient();
            var modinfo = await client.Mods.GetMod(gamedomain, fileid);

            if (modinfo.User.MemberID == currentuserid)
            {
                return(null); //cannot endorse your own mods
            }
            var endorsementstatus = modinfo.Endorsement;

            if (endorsementstatus != null)
            {
                if (endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Undecided || endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Abstained)
                {
                    return(false);
                }

                if (endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Endorsed)
                {
                    return(true);
                }
            }
            return(null); //Cannot endorse this (could not get endorsement status)
        }
Beispiel #2
0
 /// <summary>
 /// Gets a list of download links for the specified file
 /// </summary>
 /// <param name="domain"></param>
 /// <param name="modid"></param>
 /// <param name="fileid"></param>
 /// <returns></returns>
 public static async Task <ModFileDownloadLink[]> GetDownloadLinkForFile(string domain, int modid, int fileid, string nxmkey = null, int expiry = 0)
 {
     if (nxmkey == null)
     {
         return(await NexusModsUtilities.GetClient().ModFiles.GetDownloadLinks(domain, modid, fileid));
     }
     return(await NexusModsUtilities.GetClient().ModFiles.GetDownloadLinks(domain, modid, fileid, nxmkey, expiry));
 }
Beispiel #3
0
        /// <summary>
        /// Gets the content preview for a mod file. This is a blocking call
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static ContentPreview GetFileListing(ModFile file)
        {
            if (NexusModsUtilities.UserInfo == null)
            {
                return(null);
            }
            var client = NexusModsUtilities.GetClient();

            return(client.ModFiles.GetContentPreview(file.ContentPreviewLink).Result);
        }
Beispiel #4
0
        /// <summary>
        /// Asynchronously endorses a file. This call does not wait for a result of the operation.
        /// </summary>
        /// <param name="gamedomain"></param>
        /// <param name="endorse"></param>
        /// <param name="fileid"></param>
        /// <param name="currentuserid"></param>
        public static void EndorseFile(string gamedomain, bool endorse, int fileid,
                                       Action <bool> newEndorsementStatusCallback = null)
        {
            if (NexusModsUtilities.UserInfo == null)
            {
                return;
            }
            NamedBackgroundWorker nbw = new NamedBackgroundWorker(@"EndorseMod");

            nbw.DoWork += (a, b) =>
            {
                var    client            = NexusModsUtilities.GetClient();
                string telemetryOverride = null;
                try
                {
                    if (endorse)
                    {
                        client.Mods.Endorse(gamedomain, fileid, @"1.0").Wait();
                    }
                    else
                    {
                        client.Mods.Unendorse(gamedomain, fileid, @"1.0").Wait();
                    }
                }
                catch (Exception e)
                {
                    Log.Error(@"Error endorsing/unendorsing: " + e.ToString());
                    telemetryOverride = e.ToString();
                }

                var newStatus = GetEndorsementStatusForFile(gamedomain, fileid).Result;

                Analytics.TrackEvent(@"Set endorsement for mod", new Dictionary <string, string>
                {
                    { @"Endorsed", endorse.ToString() },
                    { @"Succeeded", telemetryOverride ?? (endorse == newStatus).ToString() }
                });
                b.Result = newStatus;
            };
            nbw.RunWorkerCompleted += (a, b) =>
            {
                if (b.Error != null)
                {
                    Log.Error($@"Exception occurred in {nbw.Name} thread: {b.Error.Message}");
                }

                if (b.Result is bool val)
                {
                    newEndorsementStatusCallback?.Invoke(val);
                }
            };
            nbw.RunWorkerAsync();
        }
Beispiel #5
0
        public static async Task <bool?> GetEndorsementStatusForFile(string gamedomain, int fileid)
        {
            if (UserInfo == null)
            {
                return(false);
            }
            var client = NexusModsUtilities.GetClient();

            try
            {
                var modinfo = await client.Mods.GetMod(gamedomain, fileid);

                if (modinfo.User.MemberID == UserInfo.UserID)
                {
                    return(null); //cannot endorse your own mods
                }

                var endorsementstatus = modinfo.Endorsement;
                if (endorsementstatus != null)
                {
                    if (endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Undecided ||
                        endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Abstained)
                    {
                        return(false);
                    }

                    if (endorsementstatus.EndorseStatus == Pathoschild.FluentNexus.Models.EndorsementStatus.Endorsed)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(@"Error getting endorsement status for mod: " + e.Message);
            }

            return(null); //Cannot endorse this (could not get endorsement status)
        }
Beispiel #6
0
        /// <summary>
        /// Gets user information from NexusMods using their API key. This should only be called when setting API key or app boot
        /// </summary>
        /// <param name="apiKey"></param>
        /// <returns></returns>
        public static async Task <User> AuthToNexusMods(string apiKey = null)
        {
            try
            {
                if (apiKey == null)
                {
                    if (NexusModsUtilities.HasAPIKey)
                    {
                        apiKey = NexusModsUtilities.DecryptNexusmodsAPIKeyFromDisk();
                    }

                    if (apiKey == null)
                    {
                        return(null);
                    }
                }

                var nexus = GetClient(apiKey);
                Log.Information("Getting user information from NexusMods");
                var userinfo = await nexus.Users.ValidateAsync();

                if (userinfo.Name != null)
                {
                    Log.Information("NexusMods API call returned valid data. API key is valid");
                    UserInfo = userinfo;
                    //Authorized OK.

                    //Track how many users authenticate to nexusmods, but don't track who.
                    return(userinfo);
                }
            }
            catch (Exception e)
            {
                Log.Error(@"Exception while authenticating to nexusmods: " + e.Message);
            }

            return(null);
        }