Skip to content

Commit

Permalink
Improve marketplace remote handling
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K authored and wborn committed Oct 16, 2024
1 parent af35487 commit 6d0a3b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.openhab.core.common.ThreadPoolManager.THREAD_POOL_NAME_COMMON;

import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -95,7 +94,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
this::getRemoteAddons);
protected final AddonInfoRegistry addonInfoRegistry;
protected List<Addon> cachedAddons = List.of();
protected List<String> installedAddons = List.of();
protected List<String> installedAddonIds = List.of();

private final Logger logger = LoggerFactory.getLogger(AbstractRemoteAddonService.class);
private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME_COMMON);
Expand Down Expand Up @@ -137,7 +136,10 @@ public void refreshSource() {
// this is safe, because the {@link AddonHandler}s only report ready when they installed everything from the
// cache
try {
installedAddonStorage.stream().map(this::convertFromStorage).peek(this::setInstalled).forEach(addons::add);
installedAddonStorage.stream().map(this::convertFromStorage).forEach(addon -> {
setInstalled(addon);
addons.add(addon);
});
} catch (JsonSyntaxException e) {
List.copyOf(installedAddonStorage.getKeys()).forEach(installedAddonStorage::remove);
logger.error(
Expand All @@ -152,13 +154,15 @@ public void refreshSource() {
addons.removeIf(addon -> missingAddons.contains(addon.getUid()));

// create lookup list to make sure installed addons take precedence
List<String> installedAddons = addons.stream().map(Addon::getUid).toList();
List<String> currentAddonIds = addons.stream().map(Addon::getUid).toList();

// get the remote addons
if (remoteEnabled()) {
List<Addon> remoteAddons = Objects.requireNonNullElse(cachedRemoteAddons.getValue(), List.of());
remoteAddons.stream().filter(a -> !installedAddons.contains(a.getUid())).peek(this::setInstalled)
.forEach(addons::add);
remoteAddons.stream().filter(a -> !currentAddonIds.contains(a.getUid())).forEach(addon -> {
setInstalled(addon);
addons.add(addon);
});
}

// remove incompatible add-ons if not enabled
Expand All @@ -175,7 +179,7 @@ public void refreshSource() {
}

cachedAddons = addons;
this.installedAddons = installedAddons;
this.installedAddonIds = currentAddonIds;

if (!missingAddons.isEmpty()) {
logger.info("Re-installing missing add-ons from remote repository: {}", missingAddons);
Expand Down Expand Up @@ -222,9 +226,6 @@ public List<Addon> getAddons(@Nullable Locale locale) {
return cachedAddons;
}

@Override
public abstract @Nullable Addon getAddon(String id, @Nullable Locale locale);

@Override
public List<AddonType> getTypes(@Nullable Locale locale) {
return AddonType.DEFAULT_TYPES;
Expand All @@ -244,6 +245,7 @@ public void install(String id) {
handler.install(addon);
addon.setInstalled(true);
installedAddonStorage.put(id, gson.toJson(addon));
cachedRemoteAddons.invalidateValue();
refreshSource();
postInstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
Expand Down Expand Up @@ -271,6 +273,7 @@ public void uninstall(String id) {
try {
handler.uninstall(addon);
installedAddonStorage.remove(id);
cachedRemoteAddons.invalidateValue();
refreshSource();
postUninstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
Expand All @@ -286,9 +289,6 @@ public void uninstall(String id) {
postFailureEvent(id, "Add-on can't be uninstalled because there is no handler for it.");
}

@Override
public abstract @Nullable String getAddonId(URI addonURI);

/**
* check if remote services are enabled
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected List<Addon> getRemoteAddons() {
// check if it is an installed add-on (cachedAddons also contains possibly incomplete results from the remote
// side, we need to retrieve them from Discourse)

if (installedAddons.contains(queryId)) {
if (installedAddonIds.contains(queryId)) {
return cachedAddons.stream().filter(e -> queryId.equals(e.getUid())).findAny().orElse(null);
}

Expand Down

0 comments on commit 6d0a3b3

Please sign in to comment.