Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions Simkl-Emby/API/ServerEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ public class GetPinStatus : IReturn <CodeStatusResponse>
public string user_code { get; set; }
}

[Route("/Simkl/users/settings/{userId}", "GET")]
public class GetUserSettings : IReturn<UserSettings>
{
// Note: In the future, when we'll have config for more than one user, we'll use a parameter
[ApiMember(Name = "id", Description = "emby's user id", IsRequired = true, DataType = "Guid", ParameterType = "path", Verb = "GET")]
public string userId { get; set; }
}

class ServerEndpoint : IService, IHasResultFactory
{
public IHttpResultFactory ResultFactory { get; set; }
Expand All @@ -55,11 +47,5 @@ public CodeStatusResponse Get(GetPinStatus request)
{
return _api.getCodeStatus(request.user_code).Result;
}

public UserSettings Get(GetUserSettings request)
{
_logger.Debug(_json.SerializeToString(request));
return _api.getUserSettings(Plugin.Instance.Configuration.getByGuid(request.userId).userToken).Result;
}
}
}
22 changes: 16 additions & 6 deletions Simkl-Emby/API/SimklApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using Simkl.Api.Responses;
using Simkl.Api.Exceptions;
using MediaBrowser.Model.Dto;
using Simkl.Configuration;
using Emby.Web.GenericEdit.Validation;
using MediaBrowser.Controller.Library;

namespace Simkl.Api
{
Expand All @@ -20,6 +23,7 @@ public class SimklApi
private readonly IJsonSerializer _json;
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private readonly IUserManager _userManager;

/* BASIC API THINGS */
public const string BASE_URL = @"https://api.simkl.com";
Expand Down Expand Up @@ -50,11 +54,12 @@ private HttpRequestOptions GetOptions(string userToken = null)
return options;
}

public SimklApi(IJsonSerializer json, ILogger logger, IHttpClient httpClient)
public SimklApi(IJsonSerializer json, ILogger logger, IHttpClient httpClient, IUserManager userManager)
{
_json = json;
_logger = logger;
_httpClient = httpClient;
_userManager = userManager;
}

public async Task<CodeResponse> getCode()
Expand Down Expand Up @@ -131,10 +136,10 @@ private static SimklHistory createHistoryFromItem(BaseItemDto item) {
}

/* NOW EVERYTHING RELATED TO SCROBBLING */
public async Task<(bool success, BaseItemDto item)> markAsWatched(BaseItemDto item, string userToken)
public async Task<(bool success, BaseItemDto item)> markAsWatched(BaseItemDto item, UserConfig userConfig, long embyUserId)
{
SimklHistory history = createHistoryFromItem(item);
SyncHistoryResponse r = await SyncHistoryAsync(history, userToken);
SyncHistoryResponse r = await SyncHistoryAsync(history, userConfig, embyUserId);
_logger.Debug("Response: " + _json.SerializeToString(r));
if (history.movies.Count == r.added.movies && history.shows.Count == r.added.shows) return (true, item);

Expand All @@ -148,7 +153,7 @@ private static SimklHistory createHistoryFromItem(BaseItemDto item) {
(history, item) = await getHistoryFromFileName(item, false);
}

r = await SyncHistoryAsync(history, userToken);
r = await SyncHistoryAsync(history, userConfig, embyUserId);
_logger.Debug("Response: " + _json.SerializeToString(r));

return (history.movies.Count == r.added.movies && history.shows.Count == r.added.shows, item);
Expand All @@ -160,14 +165,19 @@ private static SimklHistory createHistoryFromItem(BaseItemDto item) {
/// <param name="history">History object</param>
/// <param name="userToken">User token</param>
/// <returns></returns>
public async Task<SyncHistoryResponse> SyncHistoryAsync(SimklHistory history, string userToken)
public async Task<SyncHistoryResponse> SyncHistoryAsync(SimklHistory history, UserConfig userConfig, long embyUserId)
{
var userToken = userConfig.userToken;

try {
_logger.Info("Syncing History: " + _json.SerializeToString(history));
return _json.DeserializeFromStream<SyncHistoryResponse>(await _post("/sync/history", userToken, history));
} catch (MediaBrowser.Model.Net.HttpException e) when (e.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
_logger.Error("Invalid user token " + userToken + ", deleting");
Plugin.Instance.deleteUserToken(userToken);

userConfig.userToken = null;

_userManager.SetTypedUserSetting(embyUserId, ConfigurationFactory.ConfigKey, userConfig);
throw new InvalidTokenException("Invalid user token " + userToken);
}
}
Expand Down
29 changes: 23 additions & 6 deletions Simkl-Emby/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Plugins;
using System.Linq;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using System.Collections.Generic;

namespace Simkl.Configuration
{
Expand All @@ -10,15 +14,28 @@ namespace Simkl.Configuration
/// </summary>
public class PluginConfiguration : BasePluginConfiguration
{
// TODO: Delete this a couple weeks after the plugin has gotten updated, to avoid any erroneous upgrades from happening when they shouldn't, and to avoid accidentally writing new code that touches it.
public UserConfig[] userConfigs { get; set; }
}

public PluginConfiguration() {
userConfigs = new UserConfig[]{};
}

public UserConfig getByGuid(string guid)
public class ConfigurationFactory : IUserConfigurationFactory
{
public IEnumerable<ConfigurationStore> GetConfigurations()
{
return userConfigs.Where(c => c.guid == guid).FirstOrDefault();
return new[]
{
new SimklConfigStore
{
ConfigurationType = typeof(UserConfig),
Key = ConfigKey
}
};
}

public static string ConfigKey = "simkl";
}

public class SimklConfigStore : ConfigurationStore
{
}
}
Loading