From 26ed351bbc59767be8ce2500cbb5250de63df5c9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 3 Sep 2016 11:06:41 +0300 Subject: Juick settings --- .../java/com/juick/server/CrosspostQueries.java | 37 ++++++++++-- .../com/juick/server/SubscriptionsQueries.java | 25 ++++++++ .../main/java/com/juick/server/UserQueries.java | 66 +++++++++++++++++++++- .../juick/server/helpers/ApplicationStatus.java | 25 ++++++++ .../main/java/com/juick/server/helpers/Auth.java | 22 ++++++++ .../java/com/juick/server/helpers/EmailOpts.java | 24 ++++++++ .../java/com/juick/server/helpers/NotifyOpts.java | 34 +++++++++++ .../java/com/juick/server/helpers/UserInfo.java | 43 ++++++++++++++ 8 files changed, 269 insertions(+), 7 deletions(-) create mode 100644 juick-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java create mode 100644 juick-core/src/main/java/com/juick/server/helpers/Auth.java create mode 100644 juick-core/src/main/java/com/juick/server/helpers/EmailOpts.java create mode 100644 juick-core/src/main/java/com/juick/server/helpers/NotifyOpts.java create mode 100644 juick-core/src/main/java/com/juick/server/helpers/UserInfo.java (limited to 'juick-core/src/main') diff --git a/juick-core/src/main/java/com/juick/server/CrosspostQueries.java b/juick-core/src/main/java/com/juick/server/CrosspostQueries.java index 04898d0a..827dad09 100644 --- a/juick-core/src/main/java/com/juick/server/CrosspostQueries.java +++ b/juick-core/src/main/java/com/juick/server/CrosspostQueries.java @@ -17,6 +17,7 @@ */ package com.juick.server; +import com.juick.server.helpers.ApplicationStatus; import org.apache.commons.lang3.tuple.Pair; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -32,9 +33,7 @@ public class CrosspostQueries { public static Optional> getTwitterTokens(JdbcTemplate sql, int uid) { try { return sql.queryForObject("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1", - (rs, num) -> { - return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); - }, uid); + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), uid); } catch (EmptyResultDataAccessException e) { return Optional.empty(); } @@ -48,13 +47,39 @@ public class CrosspostQueries { return Optional.empty(); } } + public static ApplicationStatus isFBCrossPostEnabled(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT 1, crosspost FROM facebook WHERE user_id=? LIMIT 1", (rs, num) -> { + ApplicationStatus status = new ApplicationStatus(); + status.setConnected(rs.getInt(1) > 0); + status.setCrosspostEnabled(rs.getBoolean(2)); + return status; + }, uid); + } catch (EmptyResultDataAccessException e) { + return new ApplicationStatus(); + } + } + + public static String getTwitterName(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT uname FROM twitter WHERE user_id=?", String.class, uid); + } catch (EmptyResultDataAccessException e) { + return ""; + } + } + + public static String getTelegramName(JdbcTemplate sql, int uid) { + try { + return sql.queryForObject("SELECT tg_name FROM telegram WHERE user_id=?", String.class, uid); + } catch (EmptyResultDataAccessException e) { + return ""; + } + } public static Optional> getVKTokens(JdbcTemplate sql, int uid) { try { return sql.queryForObject("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1", - (rs, num) -> { - return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); - }, uid); + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), uid); } catch (EmptyResultDataAccessException e) { return Optional.empty(); } diff --git a/juick-core/src/main/java/com/juick/server/SubscriptionsQueries.java b/juick-core/src/main/java/com/juick/server/SubscriptionsQueries.java index cedd2fd4..9a09a5cd 100644 --- a/juick-core/src/main/java/com/juick/server/SubscriptionsQueries.java +++ b/juick-core/src/main/java/com/juick/server/SubscriptionsQueries.java @@ -6,6 +6,8 @@ package com.juick.server; import com.juick.Tag; import com.juick.User; +import com.juick.server.helpers.NotifyOpts; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; @@ -97,4 +99,27 @@ public class SubscriptionsQueries { toTag.TID, user.getUID()) > 0; } + public static NotifyOpts getNotifyOptions(JdbcTemplate sql, User user) { + try { + return sql.queryForObject("SELECT jnotify,subscr_notify,recommendations FROM useroptions WHERE user_id=?", + (rs, num) -> { + NotifyOpts options = new NotifyOpts(); + options.setRepliesEnabled(rs.getInt(1) > 0); + options.setSubscriptionsEnabled(rs.getInt(2) > 0); + options.setRecommendationsEnabled(rs.getInt(3) > 0); + return options; + }, user.getUID()); + } catch (EmptyResultDataAccessException e) { + return new NotifyOpts(); + } + } + + public static boolean setNotifyOptions(JdbcTemplate sql, User user, NotifyOpts options) { + return sql.update("UPDATE useroptions SET jnotify=? WHERE user_id=?", options.isRepliesEnabled() ? 1 : 0, + user.getUID()) > 0 && + sql.update("UPDATE useroptions SET subscr_notify=? WHERE user_id=?", options.isSubscriptionsEnabled() ? 1 : 0, + user.getUID()) > 0 && + sql.update("UPDATE useroptions SET recommendations=? WHERE user_id=?", options.isRecommendationsEnabled() ? 1 : 0, + user.getUID()) > 0; + } } diff --git a/juick-core/src/main/java/com/juick/server/UserQueries.java b/juick-core/src/main/java/com/juick/server/UserQueries.java index b79f179c..50e3562f 100644 --- a/juick-core/src/main/java/com/juick/server/UserQueries.java +++ b/juick-core/src/main/java/com/juick/server/UserQueries.java @@ -18,6 +18,9 @@ package com.juick.server; import com.juick.User; +import com.juick.server.helpers.Auth; +import com.juick.server.helpers.EmailOpts; +import com.juick.server.helpers.UserInfo; import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -31,7 +34,6 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; -import java.util.stream.Collectors; /** * @@ -242,6 +244,18 @@ public class UserQueries { } } + public static boolean updatePassword(JdbcTemplate sql, User user, String newPassword) { + return user.getUID() > 0 && sql.update("UPDATE users SET passw=? WHERE id=?", newPassword, user.getUID()) > 0; + } + + public static String updateSecretEmail(JdbcTemplate sql, User user) { + String newHash = generateHash(16); + if (sql.update("INSERT INTO mail(user_id,hash) VALUES (?,?) ON DUPLICATE KEY UPDATE hash=?", user.getUID(), newHash, newHash) > 0) { + return newHash; + } + return ""; + } + public static int getUserOptionInt(JdbcTemplate sql, int uid, String option, int defaultValue) { try { return sql.queryForObject("SELECT " + option + " FROM useroptions WHERE user_id=?", Integer.class, uid); @@ -254,6 +268,28 @@ public class UserQueries { sql.update("UPDATE useroptions SET " + option + "=? WHERE user_id=?", value, uid); } + public static UserInfo getUserInfo(JdbcTemplate sql, User user) { + try { + return sql.queryForObject("SELECT fullname,country,url,descr FROM usersinfo WHERE user_id=?", ((rs, rowNum) -> { + UserInfo info = new UserInfo(); + info.setFullName(rs.getString(1)); + info.setCountry(rs.getString(2)); + info.setUrl(rs.getString(3)); + info.setDescription(rs.getString(4)); + return info; + }), user.getUID()); + } catch (EmptyResultDataAccessException e) { + return new UserInfo(); + } + } + + public static boolean updateUserInfo(JdbcTemplate sql, User user, UserInfo info) { + return sql.update("INSERT INTO usersinfo(user_id,fullname,country,url,descr) VALUES (?,?,?,?,?) " + + "ON DUPLICATE KEY UPDATE fullname=?,country=?,url=?,descr=?", user.getUID(), info.getFullName(), + info.getCountry(), info.getUrl(), info.getDescription(), info.getFullName(), + info.getCountry(), info.getUrl(), info.getDescription()) > 0; + } + public static boolean getCanMedia(JdbcTemplate sql, int uid) { try { int res = sql.queryForObject("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?", @@ -429,4 +465,32 @@ public class UserQueries { } return false; } + public static List getAllJIDs(JdbcTemplate sql, User user) { + return sql.queryForList("SELECT jid FROM jids WHERE user_id=?", String.class, user.getUID()); + } + public static List getAuthCodes(JdbcTemplate sql, User user) { + return sql.query("SELECT account,authcode FROM auth WHERE user_id=? AND protocol='xmpp'", + (rs, num) -> new Auth(rs.getString(1), rs.getString(2)), user.getUID()); + } + + public static List getEmails(JdbcTemplate sql, User user) { + return sql.queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUID()); + } + + public static EmailOpts getEmailOpts(JdbcTemplate sql, User user) { + try { + return sql.queryForObject("SELECT email,subscr_hour FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", + (rs, num) ->new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUID()); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + public static String getEmailHash(JdbcTemplate sql, User user) { + try { + return sql.queryForObject("SELECT hash FROM mail WHERE user_id=?", String.class, user.getUID()) + + "@mail.juick.com"; + } catch (EmptyResultDataAccessException e) { + return ""; + } + } } diff --git a/juick-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java b/juick-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java new file mode 100644 index 00000000..986c8275 --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java @@ -0,0 +1,25 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 03/09/16. + */ +public class ApplicationStatus { + private boolean connected; + private boolean crosspostEnabled; + + public boolean isConnected() { + return connected; + } + + public void setConnected(boolean connected) { + this.connected = connected; + } + + public boolean isCrosspostEnabled() { + return crosspostEnabled; + } + + public void setCrosspostEnabled(boolean crosspostEnabled) { + this.crosspostEnabled = crosspostEnabled; + } +} diff --git a/juick-core/src/main/java/com/juick/server/helpers/Auth.java b/juick-core/src/main/java/com/juick/server/helpers/Auth.java new file mode 100644 index 00000000..3e1f0bd9 --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/helpers/Auth.java @@ -0,0 +1,22 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 09/02/16. + */ +public class Auth { + private String account; + private String authCode; + + public Auth(String account, String authCode) { + this.account = account; + this.authCode = authCode; + } + + public String getAccount() { + return account; + } + + public String getAuthCode() { + return authCode; + } +} \ No newline at end of file diff --git a/juick-core/src/main/java/com/juick/server/helpers/EmailOpts.java b/juick-core/src/main/java/com/juick/server/helpers/EmailOpts.java new file mode 100644 index 00000000..679d1a8d --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/helpers/EmailOpts.java @@ -0,0 +1,24 @@ +package com.juick.server.helpers; + +import org.apache.commons.lang3.StringUtils; + +/** + * Created by vitalyster on 09.02.2016. + */ +public class EmailOpts { + private String email; + private String subscriptionHour; + + public EmailOpts(String email, int subscriptionHour) { + this.email = email; + this.subscriptionHour = StringUtils.leftPad(String.format("%d", subscriptionHour), 2, "0"); + } + + public String getSubscriptionHour() { + return subscriptionHour; + } + + public String getEmail() { + return email; + } +} \ No newline at end of file diff --git a/juick-core/src/main/java/com/juick/server/helpers/NotifyOpts.java b/juick-core/src/main/java/com/juick/server/helpers/NotifyOpts.java new file mode 100644 index 00000000..377b0a50 --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/helpers/NotifyOpts.java @@ -0,0 +1,34 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 03/09/16. + */ +public class NotifyOpts { + private boolean repliesEnabled; + private boolean subscriptionsEnabled; + private boolean recommendationsEnabled; + + public boolean isRepliesEnabled() { + return repliesEnabled; + } + + public void setRepliesEnabled(boolean repliesEnabled) { + this.repliesEnabled = repliesEnabled; + } + + public boolean isSubscriptionsEnabled() { + return subscriptionsEnabled; + } + + public void setSubscriptionsEnabled(boolean subscriptionsEnabled) { + this.subscriptionsEnabled = subscriptionsEnabled; + } + + public boolean isRecommendationsEnabled() { + return recommendationsEnabled; + } + + public void setRecommendationsEnabled(boolean recommendationsEnabled) { + this.recommendationsEnabled = recommendationsEnabled; + } +} diff --git a/juick-core/src/main/java/com/juick/server/helpers/UserInfo.java b/juick-core/src/main/java/com/juick/server/helpers/UserInfo.java new file mode 100644 index 00000000..5a4b6894 --- /dev/null +++ b/juick-core/src/main/java/com/juick/server/helpers/UserInfo.java @@ -0,0 +1,43 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 03/09/16. + */ +public class UserInfo { + private String fullName; + private String country; + private String url; + private String description; + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} -- cgit v1.2.3