| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public void init(@NotNull PluginContext context) throws Exception { |
| 40 | WebhookAPI api = new WebhookAPIImpl(); |
| 41 | |
| 42 | // This ensures that other plugins can retrieve WebhookAPI by calling |
| 43 | // WebhookAPIProvider.instance() |
| 44 | APIRegistrationUtil.register(api); |
| 45 | |
| 46 | // This ensures that other plugins can retrieve WebhookAPI by calling |
| 47 | // FetcharrAPIProvider.instance().registry().getFirst(WebhookAPI.class) |
| 48 | FetcharrAPIProvider.instance().registry().register(this, WebhookAPI.class, api); |
| 49 | |
| 50 | CommentedConfigurationNode config = loadConfig(new File(context.configDir(), "config.yaml")); |
| 51 | if (config == null) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | for (Map.Entry<Object, CommentedConfigurationNode> child : config.node("webhooks").childrenMap().entrySet()) { |
| 56 | String id = (String) child.getKey(); |
| 57 | String type = child.getValue().node("type").getString("").trim().toLowerCase(Locale.ROOT); |
| 58 | if (type.isEmpty()) { |
| 59 | logger.warn("Webhook {} type is not set", id); |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | WebhookTransform transform = null; |
| 64 | if (type.equalsIgnoreCase("discord")) { |
| 65 | transform = new DiscordWebhookTransform(child.getValue()); |
| 66 | } |
| 67 | if (type.equalsIgnoreCase("notifiarr")) { |
| 68 | transform = new NotifiarrWebhookTransform(child.getValue()); |
| 69 | } |
| 70 | if (transform == null) { |
| 71 | logger.warn("Could not find transform for type {}", type); |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | WebhookDestination destination = null; |
| 76 | if (type.equalsIgnoreCase("discord")) { |
| 77 | destination = new DiscordWebhookDestination(api, id, child.getValue(), transform); |
| 78 | } |
| 79 | if (type.equalsIgnoreCase("notifiarr")) { |
| 80 | destination = new NotifiarrWebhookDestination(api, id, child.getValue(), transform); |
| 81 | } |
| 82 | if (destination == null) { |
| 83 | logger.warn("Could not find destination for type {}", type); |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (api.register(destination)) { |
| 88 | logger.debug("Registered {} for {}", destination.getClass().getSimpleName(), id); |
| 89 | } else { |
| 90 | logger.warn("Destination registration was unsuccessful for {}", id); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | FetcharrAPIProvider.instance().events().subscribe(FetcharrEvent.class, EventConfig.of(Integer.MAX_VALUE, false, false), this::fetcharrEvent); |
| 95 | |