implemented hiding/unhiding of mailing lists on the database side
This commit is contained in:
@@ -25,10 +25,10 @@ public class MailingList {
|
||||
private final String name;
|
||||
private final String email;
|
||||
public static final String TABLE_NAME = "Lists";
|
||||
private final String imapPass,smtpPass,imapHost,smtpHost,imapUser,smtpUser;
|
||||
private final int imapPort,smtpPort,state;
|
||||
private final String imapPass, smtpPass, imapHost, smtpHost, imapUser, smtpUser;
|
||||
private final int imapPort, smtpPort, state;
|
||||
|
||||
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state){
|
||||
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state) {
|
||||
this.email = email;
|
||||
this.name = name;
|
||||
this.imapHost = imapHost;
|
||||
@@ -43,7 +43,7 @@ public class MailingList {
|
||||
}
|
||||
|
||||
public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException {
|
||||
return new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,ENABLED).save();
|
||||
return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, ENABLED).save();
|
||||
}
|
||||
|
||||
public static void createTable() throws SQLException {
|
||||
@@ -65,18 +65,22 @@ public class MailingList {
|
||||
Database.open().query(sql.toString()).run();
|
||||
}
|
||||
|
||||
public static void hide(String listEmail, boolean hide) throws SQLException {
|
||||
// https://stackoverflow.com/questions/16440831/bitwise-xor-in-sqlite-bitwise-not-not-working-as-i-expect
|
||||
String expression = hide ? "state = (~(state & "+PUBLIC+"))&(state|"+PUBLIC+")" : ("state = state | "+PUBLIC);
|
||||
Database.open().query("UPDATE " + TABLE_NAME + " SET "+expression).where(EMAIL, listEmail).run();
|
||||
}
|
||||
|
||||
public static List<MailingList> listsOf(User user) {
|
||||
|
||||
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user);
|
||||
List<String> keys = (user.is(ADMIN)) ? null : ListMember.listsOwnedBy(user);
|
||||
var list = new ArrayList<MailingList>();
|
||||
if (keys != null && keys.isEmpty()) return list;
|
||||
try {
|
||||
Database.Request q = Database.open().query("SELECT * FROM " + TABLE_NAME);
|
||||
if (keys != null){
|
||||
if (keys.isEmpty()) return list;
|
||||
q.where(EMAIL,keys);
|
||||
}
|
||||
var rs = q.exec();
|
||||
while (rs.next()){
|
||||
Database.Request query = Database.open().query("SELECT * FROM " + TABLE_NAME);
|
||||
if (keys != null) query.where(EMAIL, keys);
|
||||
var rs = query.exec();
|
||||
while (rs.next()) {
|
||||
var email = rs.getString(EMAIL);
|
||||
var name = rs.getString(NAME);
|
||||
var imapHost = rs.getString(IMAP_HOST);
|
||||
@@ -88,46 +92,60 @@ public class MailingList {
|
||||
var smtpUser = rs.getString(SMTP_USER);
|
||||
var smtpPass = rs.getString(SMTP_PASS);
|
||||
var state = rs.getInt(STATE);
|
||||
list.add(new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,state));
|
||||
list.add(new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, state));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Listing mailing lists failed: ",e);
|
||||
LOG.warn("Listing mailing lists failed: ", e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public Map<String,Object> safeMap() {
|
||||
return Map.of(EMAIL,email,NAME,name,
|
||||
IMAP_HOST,imapHost,IMAP_PORT,imapPort,IMAP_USER,imapUser,
|
||||
SMTP_HOST,smtpHost,SMTP_PORT,smtpPort,SMTP_USER,smtpUser,
|
||||
STATE,stateName(state));
|
||||
public static List<MailingList> openLists() {
|
||||
var list = new ArrayList<MailingList>();
|
||||
try {
|
||||
var rs = Database.open().query("SELECT *, (" + STATE + " & " + PUBLIC + ") as test FROM " + TABLE_NAME).where("test", PUBLIC).exec();
|
||||
while (rs.next()) {
|
||||
var email = rs.getString(EMAIL);
|
||||
var name = rs.getString(NAME);
|
||||
var state = rs.getInt(STATE);
|
||||
list.add(new MailingList(email, name, null, 0, null, null, null, 0, null, null, state));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Listing mailing lists failed: ", e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String stateName(int state) {
|
||||
switch (state){
|
||||
case ENABLED:
|
||||
return "enabled";
|
||||
default:
|
||||
return "disabled";
|
||||
}
|
||||
public Map<String, Object> safeMap() {
|
||||
return Map.of(EMAIL, email, NAME, name,
|
||||
IMAP_HOST, imapHost, IMAP_PORT, imapPort, IMAP_USER, imapUser,
|
||||
SMTP_HOST, smtpHost, SMTP_PORT, smtpPort, SMTP_USER, smtpUser,
|
||||
STATE, stateString(state));
|
||||
}
|
||||
|
||||
private static String stateString(int state) {
|
||||
var states = new ArrayList<String>();
|
||||
states.add((state & ENABLED) == ENABLED ? "enabled" : "disabled");
|
||||
states.add((state & PUBLIC) == PUBLIC ? "public" : "hidden");
|
||||
return String.join(", ", states);
|
||||
}
|
||||
|
||||
private MailingList save() throws SQLException {
|
||||
Database.open().insertInto(TABLE_NAME)
|
||||
.values(Map.ofEntries(
|
||||
Map.entry(EMAIL,email),
|
||||
Map.entry(NAME,name),
|
||||
Map.entry(IMAP_HOST,imapHost),
|
||||
Map.entry(IMAP_PORT,imapPort),
|
||||
Map.entry(IMAP_USER,imapUser),
|
||||
Map.entry(IMAP_PASS,imapPass),
|
||||
Map.entry(SMTP_HOST,smtpHost),
|
||||
Map.entry(SMTP_PORT,smtpPort),
|
||||
Map.entry(SMTP_USER,smtpUser),
|
||||
Map.entry(SMTP_PASS,smtpPass),
|
||||
Map.entry(STATE,state)))
|
||||
Map.entry(EMAIL, email),
|
||||
Map.entry(NAME, name),
|
||||
Map.entry(IMAP_HOST, imapHost),
|
||||
Map.entry(IMAP_PORT, imapPort),
|
||||
Map.entry(IMAP_USER, imapUser),
|
||||
Map.entry(IMAP_PASS, imapPass),
|
||||
Map.entry(SMTP_HOST, smtpHost),
|
||||
Map.entry(SMTP_PORT, smtpPort),
|
||||
Map.entry(SMTP_USER, smtpUser),
|
||||
Map.entry(SMTP_PASS, smtpPass),
|
||||
Map.entry(STATE, state)))
|
||||
.run();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user