-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug where telegram callback data for add buttons no longer works (#…
…105) Specifically persist callback data in the local database so it can be retrieved by an integer instead
- Loading branch information
Showing
17 changed files
with
241 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/com/botdarr/clients/telegram/TelegramCallbackManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.botdarr.clients.telegram; | ||
|
||
import com.botdarr.database.DatabaseHelper; | ||
import com.botdarr.utilities.DateWrapper; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.util.Strings; | ||
|
||
import java.sql.*; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TelegramCallbackManager { | ||
private final DateWrapper dateWrapper; | ||
|
||
public TelegramCallbackManager(DateWrapper dateWrapper) { | ||
this.dateWrapper = dateWrapper; | ||
} | ||
|
||
public TelegramCallbackManager() { | ||
this.dateWrapper = new DateWrapper(); | ||
} | ||
|
||
private void deleteCallback(Connection conn, int id) throws SQLException { | ||
PreparedStatement statement = conn.prepareStatement("delete from telegram_callbacks where id = ?"); | ||
statement.setInt(1, id); | ||
statement.executeUpdate(); | ||
} | ||
|
||
public int saveCallback(String callback) { | ||
String url = databaseHelper.getJdbcUrl(); | ||
try (Connection conn = DriverManager.getConnection(url)) { | ||
PreparedStatement statement = conn.prepareStatement("insert into telegram_callbacks (callback, createdDt) values (?, ?)"); | ||
statement.setString(1, callback); | ||
statement.setDate(2, Date.valueOf(this.dateWrapper.getNow())); | ||
statement.executeUpdate(); | ||
try (ResultSet rs = statement.getGeneratedKeys()) { | ||
if (rs.next()) { | ||
return rs.getInt(1); | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Error trying to save telegram callback", e); | ||
throw new RuntimeException(e); | ||
} | ||
throw new RuntimeException("Could not save telegram callback"); | ||
} | ||
|
||
public String getCallback(int id) { | ||
String url = databaseHelper.getJdbcUrl(); | ||
try (Connection conn = DriverManager.getConnection(url)) { | ||
PreparedStatement statement = conn.prepareStatement("select callback from telegram_callbacks where id = ?"); | ||
statement.setInt(1, id); | ||
String callback = null; | ||
try (ResultSet rs = statement.executeQuery()) { | ||
if (rs.next()) { | ||
callback = rs.getString("callback"); | ||
} | ||
} | ||
if (!Strings.isEmpty(callback)) { | ||
// delete the callback entry | ||
deleteCallback(conn, id); | ||
return callback; | ||
} | ||
throw new TelegramCallbackMissing(); | ||
} catch (TelegramCallbackMissing e) { | ||
throw e; | ||
} catch (Exception e) { | ||
LOGGER.error("Error trying to get telegram callback", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void deleteOldCallbacks() { | ||
LOGGER.info("Checking for telegram callbacks to delete"); | ||
String url = databaseHelper.getJdbcUrl(); | ||
try (Connection conn = DriverManager.getConnection(url)) { | ||
// if callbacks haven't been used in 10 days, delete them | ||
PreparedStatement statement = conn.prepareStatement("SELECT id FROM telegram_callbacks WHERE createdDt < ?"); | ||
LocalDate tenDaysAgo = this.dateWrapper.getNow().minusDays(10); | ||
statement.setDate(1, Date.valueOf(tenDaysAgo)); | ||
List<Integer> rowsToDelete = new ArrayList<>(); | ||
try (ResultSet rs = statement.executeQuery()) { | ||
if (rs.next()) { | ||
rowsToDelete.add(rs.getInt("id")); | ||
} | ||
} | ||
LOGGER.info("Found telegram callbacks to delete, count=" + rowsToDelete.size()); | ||
for (int rowIdToDelete : rowsToDelete) { | ||
LOGGER.info("Deleting telegram callback, id=" + rowIdToDelete); | ||
deleteCallback(conn, rowIdToDelete); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Error trying to delete old telegram callback", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static class TelegramCallbackMissing extends RuntimeException { | ||
|
||
} | ||
|
||
private final DatabaseHelper databaseHelper = new DatabaseHelper(); | ||
Logger LOGGER = LogManager.getLogger(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.botdarr.utilities; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class DateWrapper { | ||
public LocalDate getNow() { | ||
return LocalDate.now(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
create table telegram_callbacks (id integer primary key not null, callback text not null, createdDt date not null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.6.6 | ||
5.6.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/botdarr/clients/telegram/TelegramCallbackManagerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.botdarr.clients.telegram; | ||
|
||
import com.botdarr.database.DatabaseBootstrap; | ||
import com.botdarr.database.MockedDatabase; | ||
import com.botdarr.utilities.DateWrapper; | ||
import org.junit.*; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
|
||
public class TelegramCallbackManagerTests { | ||
@Before | ||
public void beforeTests() throws IOException { | ||
//create temporary database | ||
new MockedDatabase(temporaryFolder.newFile()); | ||
DatabaseBootstrap.init(); | ||
} | ||
|
||
@Test | ||
public void saveCallback_savesAndGetsCallbackDataSuccessfully() { | ||
TelegramCallbackManager telegramCallbackManager = new TelegramCallbackManager(); | ||
String callback = "!movie find new test"; | ||
int result = telegramCallbackManager.saveCallback(callback); | ||
Assert.assertTrue(result > 0); | ||
String actualCallback = telegramCallbackManager.getCallback(result); | ||
Assert.assertEquals(callback, actualCallback); | ||
} | ||
|
||
@Test(expected = TelegramCallbackManager.TelegramCallbackMissing.class) | ||
public void deleteCallbacks_removes10DayOldCallbacks() { | ||
LocalDate fakeNow = LocalDate.now(); | ||
fakeNow = fakeNow.minusDays(11); | ||
|
||
// insert callbacks that are 11 days old | ||
TelegramCallbackManager telegramCallbackManager = new TelegramCallbackManager(new FakeDateWrapper(fakeNow)); | ||
String callback = "!movie find new test"; | ||
int result = telegramCallbackManager.saveCallback(callback); | ||
Assert.assertTrue(result > 0); | ||
|
||
// trigger the callback delete logic | ||
telegramCallbackManager = new TelegramCallbackManager(); | ||
telegramCallbackManager.deleteOldCallbacks(); | ||
|
||
// this should throw an exception now | ||
telegramCallbackManager.getCallback(result); | ||
} | ||
|
||
private static class FakeDateWrapper extends DateWrapper { | ||
private LocalDate now; | ||
|
||
public FakeDateWrapper(LocalDate now) { | ||
this.now = now; | ||
} | ||
|
||
@Override | ||
public LocalDate getNow() { | ||
return this.now; | ||
} | ||
} | ||
|
||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.botdarr.database; | ||
|
||
import mockit.Mock; | ||
import mockit.MockUp; | ||
|
||
import java.io.File; | ||
|
||
public class MockedDatabase extends MockUp<DatabaseHelper> { | ||
public MockedDatabase(File temporaryDatabase) { | ||
this.temporaryDatabase = temporaryDatabase; | ||
} | ||
|
||
@Mock | ||
public File getDatabaseFile() { | ||
return temporaryDatabase; | ||
} | ||
|
||
private final File temporaryDatabase; | ||
} |