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;
}
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 = new Executor(new DefaultEnvironment());
private Executor SetupExecutor() {
var env = new DefaultEnvironment();
var executor = new Executor(env);
executor.builtins["logd"] = (x) => {
_logger.LogDebug(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray());
return Lisp_Boolean.TRUE;
@ -115,13 +113,20 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
_logger.LogError(((Lisp.String)x.First()).Value(), x.Skip(1).ToArray());
return Lisp_Boolean.TRUE;
};
executor.environment.Set("user", Lisp_Object.FromBase(user));
if (Plugin.Instance is not null) {
executor.eval(Plugin.Instance.Configuration.InitialProgram);
} else {
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) {
executor.environment.Set("item", Lisp_Object.FromBase(i));
var r = executor.eval(expression);
@ -131,7 +136,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
results.Add(i);
}
}
executor = new Executor(new DefaultEnvironment());
executor = SetupExecutor();
executor.environment.Set("user", Lisp_Object.FromBase(user));
executor.environment.Set("items", Lisp_Object.FromBase(results));
results = new List<BaseItem>();