Merge branch 'main' into lang_de
This commit is contained in:
@@ -11,6 +11,7 @@ import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import static de.srsoftware.widerhall.Util.t;
|
||||
import static de.srsoftware.widerhall.data.MailingList.HOLD_TIME;
|
||||
|
||||
/**
|
||||
* @author Stephan Richter, 2022
|
||||
@@ -280,9 +281,30 @@ public class Database {
|
||||
if (!tableExists(MailingList.TABLE_NAME)) MailingList.createTable();
|
||||
if (!tableExists(ListMember.TABLE_NAME)) ListMember.createTable();
|
||||
if (!tableExists(Post.TABLE_NAME)) Post.createTable();
|
||||
if (!columnExists(MailingList.TABLE_NAME,HOLD_TIME)) MailingList.createHoldTimeColumn();
|
||||
return this;
|
||||
}
|
||||
|
||||
private boolean columnExists(String tableName, String columnName) throws SQLException {
|
||||
var rs = Database.open().select("pragma_table_info('"+tableName+"')","COUNT(*) AS num").where("name",columnName).compile().exec();
|
||||
try {
|
||||
if (rs.next()) return rs.getInt("num") > 0;
|
||||
} finally {
|
||||
rs.close();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void createColumn(String tableName, String colName, String...typeArgs) throws SQLException {
|
||||
var sql = new StringBuilder("ALTER TABLE ")
|
||||
.append(tableName)
|
||||
.append(" ADD COLUMN ")
|
||||
.append(colName)
|
||||
.append(" ")
|
||||
.append(String.join(" ",typeArgs));
|
||||
new Request(sql).compile().run();
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare a deletion statement
|
||||
* @param tableName
|
||||
|
||||
@@ -12,8 +12,7 @@ import org.stringtemplate.v4.ST;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.ws.rs.HEAD;
|
||||
import java.io.*;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
@@ -28,14 +27,16 @@ import static de.srsoftware.widerhall.data.User.PERMISSION_ADMIN;
|
||||
* this class encapsulates a MailingList db object
|
||||
*/
|
||||
public class MailingList implements MessageHandler, ProblemListener {
|
||||
public static final String KEY_FORWARD_FROM = "forward_from";
|
||||
public static final String KEY_ARCHIVE = "archive";
|
||||
public static final String KEY_DELETE_MESSAGES = "delete_messages";
|
||||
public static final String KEY_FORWARD_ATTACHED = "forward_attached";
|
||||
public static final String KEY_FORWARD_FROM = "forward_from";
|
||||
public static final String KEY_HIDE_RECEIVERS = "hide_receivers";
|
||||
public static final String KEY_REPLY_TO_LIST = "reply_to_list";
|
||||
public static final String KEY_MODS_CAN_EDIT_MODS = "edit_mods";
|
||||
public static final String KEY_OPEN_FOR_GUESTS = "open_for_guests";
|
||||
public static final String KEY_OPEN_FOR_SUBSCRIBERS = "open_for_subscribers";
|
||||
public static final String KEY_ARCHIVE = "archive";
|
||||
public static final String KEY_MODS_CAN_EDIT_MODS = "edit_mods";
|
||||
public static final String KEY_REPLY_TO_LIST = "reply_to_list";
|
||||
public static final String HOLD_TIME = "hold_time";
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MailingList.class);
|
||||
private static final String IMAP_HOST = "imap_host";
|
||||
private static final String IMAP_PORT = "imap_port";
|
||||
@@ -69,6 +70,7 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
private ImapClient imap;
|
||||
|
||||
private static final HashMap<String,MailingList> cache = new HashMap<>();
|
||||
private Integer holdTime = null;
|
||||
|
||||
/**
|
||||
* create a new ML object
|
||||
@@ -124,6 +126,10 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, inbox, smtpHost, smtpPort, smtpUser, smtpPass, DEFAULT_STATE).save();
|
||||
}
|
||||
|
||||
public static void createHoldTimeColumn() throws SQLException {
|
||||
Database.open().createColumn(TABLE_NAME,HOLD_TIME,INT);
|
||||
}
|
||||
|
||||
/**
|
||||
* create underlying db table
|
||||
* @throws SQLException
|
||||
@@ -149,6 +155,20 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
Database.open().query(sql).compile().run();
|
||||
}
|
||||
|
||||
public void deleteMessages(boolean enable, String daysString) {
|
||||
try {
|
||||
holdTime = enable ? Integer.parseInt(daysString) : null;
|
||||
Database.open().update(TABLE_NAME).set(HOLD_TIME, holdTime).run();
|
||||
} catch (SQLException throwables) {
|
||||
LOG.warn("Failed to update {} setting!",HOLD_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
private void dropOldMails() throws MessagingException {
|
||||
if (holdTime == null) return;
|
||||
imap.dropMailsOlderThan(holdTime);
|
||||
}
|
||||
|
||||
public String email() {
|
||||
return email;
|
||||
}
|
||||
@@ -227,6 +247,10 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
return setFlag(STATE_HIDE_RECEIVERS,hide);
|
||||
}
|
||||
|
||||
public Integer holdTime() {
|
||||
return holdTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* test, whether the current ML is subscribable by a given user
|
||||
* @param user
|
||||
@@ -437,7 +461,7 @@ public class MailingList implements MessageHandler, ProblemListener {
|
||||
} catch (SQLException e){
|
||||
LOG.warn("Failed to process message '{}'",subject,e);
|
||||
}
|
||||
|
||||
dropOldMails();
|
||||
}
|
||||
|
||||
public MailingList openForGuests(boolean open) throws SQLException {
|
||||
|
||||
Reference in New Issue
Block a user