fix: initialize the executor the same for the filter programs and the sort program.

This commit is contained in:
redxef 2024-12-22 18:16:45 +01:00
parent b73f65829d
commit b441a09831
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921

View file

@ -94,11 +94,9 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
return playlistGuid; return playlistGuid;
} }
private IEnumerable<Guid> FilterPlaylistItems(IEnumerable<BaseItem> items, User user, SmartPlaylistDto smartPlaylist) { private Executor SetupExecutor() {
List<BaseItem> results = new List<BaseItem>(); var env = new DefaultEnvironment();
Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); // parse here, so that we don't repeat the work for each item var executor = new Executor(env);
Executor executor = new Executor(new DefaultEnvironment());
executor.builtins["logd"] = (x) => { executor.builtins["logd"] = (x) => {
_logger.LogDebug(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray()); _logger.LogDebug(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray());
return Lisp_Boolean.TRUE; return Lisp_Boolean.TRUE;
@ -115,13 +113,20 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
_logger.LogError(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray()); _logger.LogError(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray());
return Lisp_Boolean.TRUE; return Lisp_Boolean.TRUE;
}; };
executor.environment.Set("user", Lisp_Object.FromBase(user));
if (Plugin.Instance is not null) { if (Plugin.Instance is not null) {
executor.eval(Plugin.Instance.Configuration.InitialProgram); executor.eval(Plugin.Instance.Configuration.InitialProgram);
} else { } else {
throw new ApplicationException("Plugin Instance is not yet initialized"); throw new ApplicationException("Plugin Instance is not yet initialized");
} }
return executor;
}
private IEnumerable<Guid> FilterPlaylistItems(IEnumerable<BaseItem> items, User user, SmartPlaylistDto smartPlaylist) {
List<BaseItem> results = new List<BaseItem>();
Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); // parse here, so that we don't repeat the work for each item
Executor executor = SetupExecutor();
executor.environment.Set("user", Lisp_Object.FromBase(user));
foreach (var i in items) { foreach (var i in items) {
executor.environment.Set("item", Lisp_Object.FromBase(i)); executor.environment.Set("item", Lisp_Object.FromBase(i));
var r = executor.eval(expression); var r = executor.eval(expression);
@ -131,7 +136,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
results.Add(i); results.Add(i);
} }
} }
executor = new Executor(new DefaultEnvironment()); executor = SetupExecutor();
executor.environment.Set("user", Lisp_Object.FromBase(user)); executor.environment.Set("user", Lisp_Object.FromBase(user));
executor.environment.Set("items", Lisp_Object.FromBase(results)); executor.environment.Set("items", Lisp_Object.FromBase(results));
results = new List<BaseItem>(); results = new List<BaseItem>();