Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #53

Merged
merged 9 commits into from
Jun 10, 2023
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation("de.chojo", "nexus-api-wrapper", "1.0.5")

// Mailing
implementation("org.eclipse.angus", "angus-mail", "2.0.1")
implementation("org.eclipse.angus", "angus-mail", "2.0.2")
implementation("org.jsoup", "jsoup", "1.16.1")


Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
postgres:
networks:
- lyna
image: postgres:15.2
image: postgres:15.3
expose:
- 5432
volumes:
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/chojo/lyna/data/dao/licenses/Licenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public Optional<License> create(Product product, String identifier) {
.query("INSERT INTO license(product_id, user_identifier, key) VALUES(?,?,?) ON CONFLICT DO NOTHING RETURNING id")
.parameter(stmt -> stmt.setInt(product.id()).setString(identifier).setString(key))
.readRow(row -> new License(product, identifier, row.getInt("id"), key))
.firstSync();
.firstSync()
.or(() -> byKey(key));
}

public Optional<License> byKey(String key) {
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/de/chojo/lyna/mail/MailingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -91,10 +93,16 @@ protected PasswordAuthentication getPasswordAuthentication() {
}
}

private void startMailMonitor() throws MessagingException {
private void startMailMonitor() {
log.info(LogNotify.STATUS, "Starting monitoring");
IMAPFolder inbox = getFolder("Inbox");

inbox.open(Folder.READ_WRITE);
try {
inbox.open(Folder.READ_WRITE);
} catch (MessagingException e) {
// c:
throw new RuntimeException(e);
}
inbox.addMessageCountListener(new MessageCountAdapter() {
@Override
public void messagesAdded(MessageCountEvent e) {
Expand All @@ -120,27 +128,31 @@ public void messagesAdded(MessageCountEvent e) {
}

private void waitForMail(IMAPFolder folder) {
threading.botWorker().execute(() -> {
CompletableFuture.runAsync(() -> {
var inbox = folder;
while (true) {
try {
try {
inbox.idle();
log.info("Waiting for new mail.");
inbox.idle(true);
} catch (FolderClosedException e) {
log.error(LogNotify.NOTIFY_ADMIN, "Folder closed. Attempting to restart monitoring.");
startMailMonitor();
break;
} catch (MessagingException e) {
log.error(LogNotify.NOTIFY_ADMIN, "Could not start connection idling", e);
startMailMonitor();
break;
}
} catch (Exception e) {
log.error(LogNotify.NOTIFY_ADMIN, "Connection to folder failed.", e);
break;
continue;
}
log.info("New email received");
}
});
}, threading.botWorker())
.completeOnTimeout(null, 1, TimeUnit.HOURS)
.thenRun(this::startMailMonitor);
}

public void registerMessageListener(ThrowingConsumer<Message, Exception> listener) {
Expand Down
40 changes: 20 additions & 20 deletions src/main/resources/database/postgresql/1/patch_6.sql
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
alter table lyna.mail_products
drop constraint mail_products_platform_id_fkk;
drop constraint IF EXISTS mail_products_platform_id_fkk;

alter table lyna.mail_products
drop column platform_id;
drop column IF EXISTS platform_id;

alter table lyna.license
drop constraint keys_platform_platform_id_id_fk;
drop constraint IF EXISTS keys_platform_platform_id_id_fk;

DROP VIEW lyna.user_product_access;
DROP VIEW lyna.user_license_all;
drop view lyna.user_platforms;
drop view lyna.user_products;
drop view lyna.user_products_all;
drop view lyna.guild_license;
drop view lyna.user_guild_license;
drop view lyna.user_guild_sub_license;
DROP VIEW IF EXISTS lyna.user_product_access;
DROP VIEW IF EXISTS lyna.user_license_all;
drop view IF EXISTS lyna.user_platforms;
drop view IF EXISTS lyna.user_products;
drop view IF EXISTS lyna.user_products_all;
drop view IF EXISTS lyna.guild_license;
drop view IF EXISTS lyna.user_guild_license;
drop view IF EXISTS lyna.user_guild_sub_license;


CREATE VIEW lyna.user_products AS
CREATE OR REPLACE VIEW lyna.user_products AS
SELECT guild_id, user_id, p.id, p.name, url, role
FROM lyna.user_license u
LEFT JOIN lyna.license l ON u.license_id = l.id
LEFT JOIN lyna.product p ON l.product_id = p.id;

CREATE VIEW lyna.user_products_all AS
CREATE OR REPLACE VIEW lyna.user_products_all AS
SELECT DISTINCT (p.name), guild_id, user_id, p.id, url, role
FROM (SELECT user_id, license_id
FROM lyna.user_license
Expand All @@ -33,25 +33,25 @@ FROM (SELECT user_id, license_id
LEFT JOIN lyna.license l ON u.license_id = l.id
LEFT JOIN lyna.product p ON l.product_id = p.id;

CREATE VIEW lyna.guild_license AS
CREATE OR REPLACE VIEW lyna.guild_license AS
SELECT product_id, user_identifier, l.id, key, guild_id
FROM lyna.license l
LEFT JOIN lyna.product p ON l.product_id = p.id;

CREATE VIEW lyna.user_guild_license AS
CREATE OR REPLACE VIEW lyna.user_guild_license AS
SELECT product_id, user_identifier, l.id, key, user_id, guild_id
FROM lyna.license l
LEFT JOIN lyna.user_license u ON l.id = u.license_id
LEFT JOIN lyna.product p ON l.product_id = p.id;

CREATE VIEW lyna.user_guild_sub_license AS
CREATE OR REPLACE VIEW lyna.user_guild_sub_license AS
SELECT product_id, user_identifier, l.id, key, user_id, guild_id
FROM lyna.license l
LEFT JOIN lyna.user_sub_license u ON l.id = u.license_id
LEFT JOIN lyna.product p ON l.product_id = p.id;


CREATE VIEW lyna.user_license_all AS
CREATE OR REPLACE VIEW lyna.user_license_all AS
SELECT
guild_id,
user_id,
Expand All @@ -78,7 +78,7 @@ CREATE VIEW lyna.user_license_all AS
LEFT JOIN lyna.product p
ON l.product_id = p.id;

CREATE VIEW lyna.user_product_access AS
CREATE OR REPLACE VIEW lyna.user_product_access AS
SELECT
user_id,
release_type,
Expand All @@ -90,7 +90,7 @@ CREATE VIEW lyna.user_product_access AS


alter table lyna.license
drop column platform_id;
drop column IF EXISTS platform_id;

DROP TABLE lyna.platform;
DROP TABLE IF EXISTS lyna.platform;