aboutsummaryrefslogtreecommitdiff
path: root/server-core/src/main/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'server-core/src/main/java/com')
-rw-r--r--server-core/src/main/java/com/juick/service/AdsServiceImpl.java4
-rw-r--r--server-core/src/main/java/com/juick/service/BaseJdbcService.java (renamed from server-core/src/main/java/com/juick/service/BaseJdbcDao.java)15
-rw-r--r--server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java4
-rw-r--r--server-core/src/main/java/com/juick/service/MessagesService.java74
-rw-r--r--server-core/src/main/java/com/juick/service/MessagesServiceImpl.java841
-rw-r--r--server-core/src/main/java/com/juick/service/PMQueriesService.java21
-rw-r--r--server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java150
-rw-r--r--server-core/src/main/java/com/juick/service/PrivacyQueriesService.java10
-rw-r--r--server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java40
-rw-r--r--server-core/src/main/java/com/juick/service/PushQueriesService.java13
-rw-r--r--server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java55
-rw-r--r--server-core/src/main/java/com/juick/service/ShowQueriesService.java7
-rw-r--r--server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java36
-rw-r--r--server-core/src/main/java/com/juick/service/SubscriptionService.java29
-rw-r--r--server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java177
-rw-r--r--server-core/src/main/java/com/juick/service/TagService.java23
-rw-r--r--server-core/src/main/java/com/juick/service/TagServiceImpl.java174
-rw-r--r--server-core/src/main/java/com/juick/service/UserService.java100
-rw-r--r--server-core/src/main/java/com/juick/service/UserServiceImpl.java599
19 files changed, 2359 insertions, 13 deletions
diff --git a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java
index 32effd21..aeb2dd85 100644
--- a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java
@@ -9,7 +9,8 @@ import java.util.List;
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class AdsServiceImpl extends BaseJdbcDao implements AdsService {
+@Transactional
+public class AdsServiceImpl extends BaseJdbcService implements AdsService {
@Transactional(readOnly = true)
@Override
@@ -33,7 +34,6 @@ public class AdsServiceImpl extends BaseJdbcDao implements AdsService {
0 : list.get(0);
}
- @Transactional
@Override
public int logAdMid(final int uid, final int mid) {
return getJdbcTemplate().update(
diff --git a/server-core/src/main/java/com/juick/service/BaseJdbcDao.java b/server-core/src/main/java/com/juick/service/BaseJdbcService.java
index 948e6e6b..2f5669f5 100644
--- a/server-core/src/main/java/com/juick/service/BaseJdbcDao.java
+++ b/server-core/src/main/java/com/juick/service/BaseJdbcService.java
@@ -5,6 +5,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;
import javax.inject.Inject;
@@ -12,10 +14,11 @@ import javax.inject.Inject;
/**
* Created by aalexeev on 11/13/16.
*/
-public class BaseJdbcDao implements InitializingBean {
+public abstract class BaseJdbcService implements InitializingBean {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private JdbcTemplate jdbcTemplate;
+ private TransactionTemplate transactionTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
protected JdbcTemplate getJdbcTemplate() {
@@ -26,12 +29,17 @@ public class BaseJdbcDao implements InitializingBean {
return namedParameterJdbcTemplate;
}
+ protected TransactionTemplate getTransactionTemplate() {
+ return transactionTemplate;
+ }
+
@Override
public void afterPropertiesSet() throws Exception {
logger.debug("Checking required beans");
Assert.notNull(jdbcTemplate);
Assert.notNull(namedParameterJdbcTemplate);
+ Assert.notNull(transactionTemplate);
}
@Inject
@@ -39,4 +47,9 @@ public class BaseJdbcDao implements InitializingBean {
this.jdbcTemplate = jdbcTemplate;
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
}
+
+ @Inject
+ public void setTransactionManager(PlatformTransactionManager transactionManager) {
+ transactionTemplate = new TransactionTemplate(transactionManager);
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java
index 6c6f7442..87e307d3 100644
--- a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java
@@ -12,9 +12,9 @@ import java.util.Optional;
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class CrosspostServiceImpl extends BaseJdbcDao implements CrosspostService {
+@Transactional(readOnly = true)
+public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService {
- @Transactional(readOnly = true)
@Override
public Optional<Pair<String, String>> getTwitterTokens(final int uid) {
List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
diff --git a/server-core/src/main/java/com/juick/service/MessagesService.java b/server-core/src/main/java/com/juick/service/MessagesService.java
index 23ccf8e9..00f97b75 100644
--- a/server-core/src/main/java/com/juick/service/MessagesService.java
+++ b/server-core/src/main/java/com/juick/service/MessagesService.java
@@ -1,7 +1,81 @@
package com.juick.service;
+import com.juick.User;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface MessagesService {
+ int createMessage(int uid, String txt, String attachment, List<com.juick.Tag> tags);
+
+ int createReply(int mid, int rid, int uid, String txt, String attachment);
+
+ int getReplyIDIncrement(int mid);
+
+ boolean recommendMessage(int mid, int vuid);
+
+ boolean canViewThread(int mid, int uid);
+
+ boolean isReadOnly(int mid);
+
+ boolean isSubscribed(int uid, int mid);
+
+ int getMessagePrivacy(int mid);
+
+ com.juick.Message getMessage(int mid);
+
+ com.juick.Message getReply(int mid, int rid);
+
+ User getMessageAuthor(int mid);
+
+ List<com.juick.Tag> getMessageTags(int mid);
+
+ List<Integer> getMessageTagsIDs(int mid);
+
+ List<String> getMessageRecommendations(int mid);
+
+ List<Integer> getAll(int visitor_uid, int before);
+
+ List<Integer> getTag(int tid, int visitor_uid, int before, int cnt);
+
+ List<Integer> getTags(String tids, int visitor_uid, int before, int cnt);
+
+ List<Integer> getPlace(int place_id, int visitor_uid, int before);
+
+ List<Integer> getMyFeed(int uid, int before);
+
+ List<Integer> getPrivate(int uid, int before);
+
+ List<Integer> getDiscussions(int uid, int before);
+
+ List<Integer> getRecommended(int uid, int before);
+
+ List<Integer> getPopular(int visitor_uid, int before);
+
+ List<Integer> getPhotos(int visitor_uid, int before);
+
+ List<Integer> getSearch(JdbcTemplate sqlSearch, String search, int before);
+
+ List<Integer> getUserBlog(int UID, int privacy, int before);
+
+ List<Integer> getUserTag(int UID, int TID, int privacy, int before);
+
+ List<Integer> getUserRecommendations(int UID, int before);
+
+ List<Integer> getUserPhotos(int UID, int privacy, int before);
+
+ List<Integer> getUserSearch(JdbcTemplate sqlSearch, int UID, String search, int privacy, int before);
+
+ List<com.juick.Message> getMessages(List<Integer> mids);
+
+ List<com.juick.Message> getReplies(int mid);
+
+ boolean setMessagePopular(int mid, int popular);
+
+ boolean setMessagePrivacy(int mid);
+
+ boolean deleteMessage(int uid, int mid);
}
diff --git a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java
index 1955d9f9..924f9e03 100644
--- a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -1,10 +1,849 @@
package com.juick.service;
+import com.juick.Message;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.server.helpers.PrivacyOpts;
+import com.juick.util.MessageUtils;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.dao.IncorrectResultSizeDataAccessException;
+import org.springframework.jdbc.core.ConnectionCallback;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
+import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+
+import javax.inject.Inject;
+import java.sql.*;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class MessagesServiceImpl extends BaseJdbcDao implements MessagesService {
+public class MessagesServiceImpl extends BaseJdbcService implements MessagesService {
+ @Inject
+ private UserService userService;
+
+ private class MessageMapper implements RowMapper<Message> {
+ @Override
+ public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
+ Message msg = new Message();
+ msg.setMID(rs.getInt(1));
+ msg.setRID(rs.getInt(2));
+ msg.ReplyTo = rs.getInt(3);
+ User user = new User();
+ user.setUID(rs.getInt(4));
+ user.setUName(rs.getString(5));
+ user.Banned = rs.getBoolean(6);
+ msg.setUser(user);
+ msg.TimeAgo = rs.getInt(7);
+ msg.setDate(rs.getTimestamp(8));
+ msg.ReadOnly = rs.getBoolean(9);
+ msg.Privacy = rs.getInt(10);
+ msg.FriendsOnly = msg.Privacy < 0;
+ msg.Replies = rs.getInt(11);
+ msg.AttachmentType = rs.getString(12);
+ if (rs.getDouble(13) != 0) {
+ msg.Place = new com.juick.Place();
+ msg.Place.lat = rs.getDouble(14);
+ msg.Place.lon = rs.getDouble(15);
+ }
+ msg.Likes = rs.getInt(16);
+ msg.Hidden = rs.getBoolean(17);
+ // parse tags string
+ String tagsStr = rs.getString(18);
+ if (tagsStr != null) {
+ Arrays.stream(tagsStr.split(" ")).forEach(t -> msg.Tags.add(new Tag(t)));
+ }
+ msg.RepliesBy = rs.getString(19);
+ msg.setText(rs.getString(20));
+ msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(21)));
+ return msg;
+ }
+ }
+
+ @Transactional
+ @Override
+ public int createMessage(final int uid, final String txt, final String attachment, final List<com.juick.Tag> tags) {
+ KeyHolder holder = new GeneratedKeyHolder();
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO messages(user_id,attach) VALUES (?,?)",
+ Statement.RETURN_GENERATED_KEYS);
+ stmt.setInt(1, uid);
+ if (attachment != null) {
+ stmt.setString(2, attachment);
+ } else {
+ stmt.setNull(2, Types.VARCHAR);
+ }
+ return stmt;
+ }, holder);
+
+ int mid = holder.getKey().intValue();
+
+ if (mid > 0) {
+ String tagsNames = "";
+ String tagsIDs = "";
+
+ for (int i = 0; i < tags.size(); i++) {
+ if (i > 0) {
+ tagsNames += " ";
+ tagsIDs += ",";
+ }
+ tagsNames += tags.get(i).getName();
+ tagsIDs += "(" + mid + "," + tags.get(i).TID + ")";
+ }
+ if (tags.size() > 0) {
+ getJdbcTemplate().execute(
+ "INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs);
+ }
+ final String finalTagsNames = tagsNames;
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO messages_txt(message_id,tags,txt) " +
+ "VALUES (?,?,?)", Statement.NO_GENERATED_KEYS);
+ stmt.setInt(1, mid);
+ if (finalTagsNames.isEmpty()) {
+ stmt.setNull(2, Types.VARCHAR);
+ } else {
+ stmt.setString(2, finalTagsNames);
+ }
+ stmt.setString(3, txt);
+ return stmt;
+ });
+ }
+
+ return mid;
+ }
+
+ @Transactional
+ @Override
+ public int createReply(final int mid, final int rid, final int uid, final String txt, final String attachment) {
+ int ridnew = getReplyIDIncrement(mid);
+
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO replies(message_id,reply_id,user_id,replyto,attach,txt) VALUES (?,?,?,?,?,?)",
+ Statement.NO_GENERATED_KEYS);
+ stmt.setInt(1, mid);
+ stmt.setInt(2, ridnew);
+ stmt.setInt(3, uid);
+ stmt.setInt(4, rid);
+ if (attachment != null) {
+ stmt.setString(5, attachment);
+ } else {
+ stmt.setNull(5, Types.VARCHAR);
+ }
+ stmt.setString(6, txt);
+ return stmt;
+ });
+
+ if (ridnew > 0)
+ getJdbcTemplate().update(
+ "UPDATE messages SET replies=replies+1 WHERE message_id=?",
+ mid);
+
+ return ridnew;
+ }
+
+ @Override
+ public int getReplyIDIncrement(final int mid) {
+ return getJdbcTemplate().execute((ConnectionCallback<Integer>) conn -> {
+ conn.setAutoCommit(false);
+ final int replyNo;
+ try (PreparedStatement ps = conn.prepareStatement("START TRANSACTION")) {
+ ps.executeUpdate();
+ }
+ try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) {
+ ps.setInt(1, mid);
+ try (ResultSet resultSet = ps.executeQuery()) {
+ if (resultSet.next()) {
+ replyNo = resultSet.getInt(1);
+ } else {
+ throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0);
+ }
+ }
+ }
+ try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) {
+ ps.setInt(1, replyNo);
+ ps.setInt(2, mid);
+ if (ps.executeUpdate() != 1) {
+ throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0);
+ }
+ }
+ conn.commit();
+ return replyNo;
+ });
+
+ }
+
+ @Transactional
+ @Override
+ public boolean recommendMessage(final int mid, final int vuid) {
+ boolean res = getJdbcTemplate().update(
+ "INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1;
+ if (res) {
+ getJdbcTemplate().update(
+ "UPDATE messages SET likes=likes+1 WHERE message_id=?",
+ mid);
+ }
+ return res;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean canViewThread(final int mid, final int uid) {
+ PrivacyOpts privacyOpts;
+ try {
+ privacyOpts = getJdbcTemplate().queryForObject(
+ "SELECT user_id,privacy FROM messages WHERE messages.message_id=?",
+ (rs, rowNum) -> {
+ PrivacyOpts res = new PrivacyOpts();
+ res.setUid(rs.getInt(1));
+ res.setPrivacy(rs.getInt(2));
+ return res;
+ }, mid);
+ } catch (EmptyResultDataAccessException e) {
+ return true;
+ }
+ return privacyOpts.getPrivacy() >= 0
+ || uid == privacyOpts.getUid()
+ || ((privacyOpts.getPrivacy() == -1 || privacyOpts.getPrivacy() == -2) && uid > 0
+ && userService.isInWL(privacyOpts.getUid(), uid));
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isReadOnly(final int mid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT readonly FROM messages WHERE message_id=?",
+ new Object[]{mid},
+ Integer.class) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isSubscribed(final int uid, final int mid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?",
+ new Object[]{uid, mid},
+ Integer.class) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getMessagePrivacy(final int mid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT privacy FROM messages WHERE message_id=?",
+ new Object[]{mid},
+ Integer.class);
+ } catch (EmptyResultDataAccessException e) {
+ return -4;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public com.juick.Message getMessage(final int mid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT messages.message_id, 0 as rid, 0 as replyto, "
+ + "messages.user_id,users.nick, 0 as banned, "
+ + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),"
+ + "messages.ts,"
+ + "messages.readonly,messages.privacy,messages.replies,"
+ + "messages.attach,messages.place_id,messages.lat,"
+ + "messages.lon,messages.likes,messages.hidden,"
+ + "txt.tags,txt.repliesby,txt.txt, '' as q FROM messages "
+ + "INNER JOIN users ON messages.user_id=users.id "
+ + "INNER JOIN messages_txt AS txt "
+ + "ON messages.message_id=txt.message_id "
+ + "WHERE messages.message_id=?",
+ new MessageMapper(),
+ mid);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public com.juick.Message getReply(final int mid, final int rid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT replies.user_id,users.nick,"
+ + "replies.replyto,replies.ts,"
+ + "replies.attach,replies.txt, IFNULL(q.txt,t.txt) as quote FROM replies INNER JOIN users "
+ + "ON replies.user_id=users.id "
+ + "LEFT JOIN replies q "
+ + "ON replies.message_id=q.message_id and replies.replyto=q.reply_id "
+ + "LEFT JOIN messages_txt t ON replies.message_id=t.message_id "
+ + "WHERE replies.message_id=? AND replies.reply_id=?",
+ (rs, num) -> {
+ Message msg = new Message();
+ msg.setMID(mid);
+ msg.setRID(rid);
+ msg.setUser(new User());
+ msg.getUser().setUID(rs.getInt(1));
+ msg.getUser().setUName(rs.getString(2));
+ msg.ReplyTo = rs.getInt(3);
+ msg.setDate(rs.getTimestamp(4));
+ msg.AttachmentType = rs.getString(5);
+ msg.setText(rs.getString(6));
+ String quote = rs.getString(7);
+ if (!StringUtils.isEmpty(quote)) {
+ msg.setReplyQuote(MessageUtils.formatQuote(quote));
+ }
+ return msg;
+ }, mid, rid);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public User getMessageAuthor(final int mid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT messages.user_id,users.nick "
+ + "FROM messages INNER JOIN users "
+ + "ON messages.user_id=users.id WHERE messages.message_id=?",
+ new Object[]{mid}, (rs, num) -> {
+ User res = new com.juick.User();
+ res.setUID(rs.getInt(1));
+ res.setUName(rs.getString(2));
+ return res;
+ });
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Tag> getMessageTags(final int mid) {
+ return getJdbcTemplate().query(
+ "SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " +
+ "INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)",
+ new Object[]{mid}, (rs, num) -> {
+ com.juick.Tag t = new com.juick.Tag(StringEscapeUtils.unescapeHtml4(rs.getString(3)));
+ t.TID = rs.getInt(1);
+ t.SynonymID = rs.getInt(2);
+ t.UsageCnt = rs.getInt(4);
+ return t;
+ });
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getMessageTagsIDs(final int mid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT tag_id FROM messages_tags WHERE message_id=?",
+ new Object[]{mid},
+ Integer.class);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getMessageRecommendations(final int mid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT users.nick FROM favorites INNER JOIN users " +
+ "ON (favorites.message_id=? AND favorites.user_id=users.id)",
+ new Object[]{mid},
+ String.class);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getAll(final int visitor_uid, final int before) {
+ if (visitor_uid > 1) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id<? AND hidden=0" +
+ " AND (privacy>0 OR user_id=?) AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" +
+ " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20",
+ new Object[]{before, visitor_uid, visitor_uid},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE hidden=0" +
+ " AND (privacy>0 OR user_id=?)" +
+ " AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?)" +
+ " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20",
+ new Object[]{visitor_uid, visitor_uid},
+ Integer.class);
+ }
+ } else {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id<?" +
+ " AND hidden=0 AND privacy>0 AND user_id NOT IN (SELECT id from users WHERE banned=1) " +
+ " ORDER BY message_id DESC LIMIT 20",
+ new Object[]{before},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE hidden=0 AND privacy>0" +
+ " AND user_id NOT IN (SELECT id from users WHERE banned=1) ORDER BY message_id DESC LIMIT 20",
+ Integer.class);
+ }
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getTag(final int tid, final int visitor_uid, final int before, final int cnt) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM (tags INNER JOIN messages_tags " +
+ "ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) " +
+ "INNER JOIN messages USING(message_id) WHERE messages.message_id<? " +
+ "AND (messages.privacy>0 OR messages.user_id=?) ORDER BY message_id DESC LIMIT ?",
+ new Object[]{tid, tid, before, visitor_uid, cnt},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM (tags INNER JOIN messages_tags " +
+ "ON ((tags.synonym_id=? OR tags.tag_id=?) AND tags.tag_id=messages_tags.tag_id)) " +
+ "INNER JOIN messages USING(message_id) WHERE messages.privacy>0 OR messages.user_id=? " +
+ "ORDER BY message_id DESC LIMIT ?",
+ new Object[]{tid, tid, visitor_uid, cnt},
+ Integer.class);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getTags(final String tids, final int visitor_uid, final int before, final int cnt) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT messages.message_id FROM messages_tags " +
+ "INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") " +
+ "AND messages.message_id<? AND (messages.privacy>0 OR messages.user_id=?) " +
+ "ORDER BY messages.message_id DESC LIMIT ?",
+ new Object[]{before, visitor_uid, cnt},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT messages.message_id FROM messages_tags " +
+ "INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") " +
+ "AND (messages.privacy>0 OR messages.user_id=?) " +
+ "ORDER BY messages.message_id DESC LIMIT ?",
+ new Object[]{visitor_uid, cnt}, Integer.class);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getPlace(final int place_id, final int visitor_uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE place_id=? AND message_id<? " +
+ "AND (privacy>0 OR user_id=?) ORDER BY message_id DESC LIMIT 20",
+ new Object[]{place_id, before, visitor_uid},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE place_id=? AND (privacy>0 OR user_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ new Object[]{place_id, visitor_uid},
+ Integer.class);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getMyFeed(final int uid, final int before) {
+ List<Integer> mids;
+ if (before > 0) {
+ mids = getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages " +
+ "INNER JOIN subscr_users ON (subscr_users.suser_id=? AND subscr_users.user_id=messages.user_id) " +
+ "WHERE message_id<? AND (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " +
+ "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid,
+ before,
+ uid);
+ } else {
+ mids = getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages " +
+ "INNER JOIN subscr_users ON (subscr_users.suser_id=? " +
+ "AND subscr_users.user_id=messages.user_id) " +
+ "WHERE (privacy>=0 OR (privacy>=-2 AND privacy<=-1 AND messages.user_id " +
+ "IN (SELECT user_id FROM wl_users WHERE wl_user_id=?))) ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid,
+ uid);
+ }
+
+ if (before > 0) {
+ mids.addAll(getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages " +
+ "WHERE user_id=? AND message_id<? ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid,
+ before));
+ } else {
+ mids.addAll(getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages " +
+ "WHERE user_id=? ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid));
+ }
+
+ Collections.sort(mids, Collections.reverseOrder());
+ int remove = mids.size() - 20;
+ for (int i = 0; i < remove; i++) {
+ mids.remove(20);
+ }
+
+ return mids;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getPrivate(final int uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList
+ ("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 AND message_id<? " +
+ "ORDER BY message_id DESC LIMIT 20",
+ new Object[]{uid, before},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE user_id=? AND privacy<0 " +
+ "ORDER BY message_id DESC LIMIT 20",
+ new Object[]{uid},
+ Integer.class);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getDiscussions(final int uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM subscr_messages WHERE suser_id=? AND message_id<? " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid,
+ before);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM subscr_messages WHERE suser_id=? " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getRecommended(final int uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM favorites WHERE user_id " +
+ "IN (SELECT user_id FROM subscr_users WHERE suser_id=?) AND message_id<? " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid,
+ before);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM favorites WHERE user_id " +
+ "IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ uid);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getPopular(final int visitor_uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id<? AND privacy>0 " +
+ "AND popular>0 AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ before,
+ visitor_uid);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE privacy>0 " +
+ "AND popular>0 AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ visitor_uid);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getPhotos(final int visitor_uid, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id<? AND (privacy>0 OR user_id=?) " +
+ "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " +
+ "AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ before,
+ visitor_uid,
+ visitor_uid);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE (privacy>0 OR user_id=?) " +
+ "AND attach IS NOT NULL AND user_id NOT IN (SELECT id from users WHERE banned=1) " +
+ "AND user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " +
+ "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ visitor_uid,
+ visitor_uid);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getSearch(final JdbcTemplate sqlSearch, final String search, final int before) {
+ List<Integer> mids;
+
+ if (before > 0) {
+ mids = sqlSearch.queryForList(
+ "SELECT id AS message_id FROM messages WHERE MATCH(?) AND id<? " +
+ "ORDER BY id DESC LIMIT 25",
+ Integer.class,
+ search,
+ before);
+ } else {
+ mids = sqlSearch.queryForList(
+ "SELECT id AS message_id FROM messages WHERE MATCH(?) " +
+ "ORDER BY id DESC LIMIT 25",
+ Integer.class,
+ search);
+ }
+ if (mids.size() > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id " +
+ "IN (" + StringUtils.arrayToCommaDelimitedString(mids.toArray()) + ") AND privacy>0 ORDER BY message_id DESC LIMIT 20",
+ Integer.class);
+ }
+ return mids;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserBlog(final int UID, final int privacy, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE user_id=? AND message_id<? AND privacy>=" +
+ privacy + " ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ UID,
+ before);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy +
+ " ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ UID);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserTag(final int UID, final int TID, final int privacy, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT messages.message_id FROM messages_tags INNER JOIN messages " +
+ "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " +
+ "AND messages.message_id<? AND messages.privacy>=" + privacy +
+ " ORDER BY messages.message_id DESC LIMIT 20",
+ Integer.class,
+ UID,
+ TID,
+ before);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT messages.message_id FROM messages_tags INNER JOIN messages " +
+ "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " +
+ "AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20",
+ Integer.class,
+ UID,
+ TID);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserRecommendations(final int UID, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM favorites "
+ + "WHERE user_id=? AND message_id<? "
+ + "ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ UID,
+ before);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM favorites "
+ + "WHERE user_id=? ORDER BY message_id DESC LIMIT 20",
+ Integer.class,
+ UID);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserPhotos(final int UID, final int privacy, final int before) {
+ if (before > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE user_id=? AND message_id<? AND privacy>=" +
+ privacy + " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20",
+ new Object[]{UID, before},
+ Integer.class);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy +
+ " AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20",
+ new Object[]{UID},
+ Integer.class);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserSearch(final JdbcTemplate sqlSearch, final int UID, final String search, final int privacy, final int before) {
+ List<Integer> mids;
+
+ if (before > 0) {
+ mids = sqlSearch.queryForList(
+ "SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) AND id<? " +
+ "ORDER BY id DESC LIMIT 20",
+ new Object[]{UID, search, before},
+ Integer.class);
+ } else {
+ mids = sqlSearch.queryForList(
+ "SELECT id AS message_id FROM messages WHERE user_id=? AND MATCH(?) " +
+ "ORDER BY id DESC LIMIT 20",
+ new Object[]{UID, search},
+ Integer.class);
+ }
+
+ if (mids.size() > 0) {
+ return getJdbcTemplate().queryForList(
+ "SELECT message_id FROM messages WHERE message_id IN (" +
+ StringUtils.arrayToCommaDelimitedString(mids.toArray()) + ") AND privacy>=" + privacy + " ORDER BY message_id DESC",
+ Integer.class);
+ }
+ return mids;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Message> getMessages(final List<Integer> mids) {
+ if (!mids.isEmpty()) {
+ return getJdbcTemplate().query(
+ "SELECT messages.message_id, 0 as rid, 0 as replyto, "
+ + "messages.user_id,users.nick, 0 as banned, "
+ + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),"
+ + "messages.ts,"
+ + "messages.readonly,messages.privacy,messages.replies,"
+ + "messages.attach,messages.place_id,messages.lat,"
+ + "messages.lon,messages.likes,messages.hidden,"
+ + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q "
+ + "FROM (messages INNER JOIN messages_txt "
+ + "ON messages.message_id=messages_txt.message_id) "
+ + "INNER JOIN users ON messages.user_id=users.id "
+ + "WHERE messages.message_id "
+ + "IN (" + StringUtils.arrayToCommaDelimitedString(mids.toArray()) + ") "
+ + "ORDER BY messages.message_id DESC",
+ new MessageMapper());
+ }
+ return Collections.emptyList();
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Message> getReplies(final int mid) {
+ return getJdbcTemplate().query(
+ "SELECT ? as mid, replies.reply_id,replies.replyto, " +
+ "replies.user_id,users.nick,users.banned, " +
+ "TIMESTAMPDIFF(MINUTE,replies.ts,NOW()),replies.ts," +
+ "0 as readonly, 0 as privacy, 0 as replies," +
+ "replies.attach, 0 as place_id, 0 as lat," +
+ "0 as lon, 0 as likes, 0 as hidden," +
+ "NULL as tags,NULL as repliesby, replies.txt, " +
+ "IFNULL(qw.txt, t.txt) as q " +
+ "FROM replies INNER JOIN users " +
+ "ON replies.user_id=users.id " +
+ "LEFT JOIN replies qw ON replies.message_id=qw.message_id and replies.replyto=qw.reply_id " +
+ "LEFT JOIN messages_txt t on replies.message_id=t.message_id " +
+ "WHERE replies.message_id=? " +
+ "ORDER BY replies.reply_id ASC",
+ new MessageMapper(),
+ mid,
+ mid);
+ }
+
+ @Transactional
+ @Override
+ public boolean setMessagePopular(final int mid, final int popular) {
+ boolean ret;
+ switch (popular) {
+ case -2:
+ ret = getJdbcTemplate().update("UPDATE messages SET hidden=1 WHERE message_id=?", mid) > 0;
+ break;
+ case -1:
+ ret = getJdbcTemplate().update("UPDATE messages SET popular=0 WHERE message_id=?", mid) > 0;
+ break;
+ default:
+ ret = getJdbcTemplate().update("UPDATE messages SET popular=? WHERE message_id=?", popular, mid) > 0;
+ break;
+ }
+ if (popular == -1) {
+ ret = getJdbcTemplate().update("INSERT INTO top_ignore_messages VALUES (?)", mid) > 0;
+ }
+ return ret;
+ }
+
+ @Transactional
+ @Override
+ public boolean setMessagePrivacy(final int mid) {
+ return getJdbcTemplate().update("UPDATE messages SET privacy=1 WHERE message_id=?",
+ mid) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean deleteMessage(final int uid, final int mid) {
+ if (getJdbcTemplate().update("DELETE FROM messages WHERE message_id=? AND user_id=?", mid, uid) > 0) {
+ getJdbcTemplate().update("DELETE FROM messages_txt WHERE message_id=?", mid);
+ getJdbcTemplate().update("DELETE FROM replies WHERE message_id=?", mid);
+ getJdbcTemplate().update("DELETE FROM subscr_messages WHERE message_id=?", mid);
+ getJdbcTemplate().update("DELETE FROM messages_tags WHERE message_id=?", mid);
+ return true;
+ }
+ return false;
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/PMQueriesService.java b/server-core/src/main/java/com/juick/service/PMQueriesService.java
index 5e2e2b75..df332870 100644
--- a/server-core/src/main/java/com/juick/service/PMQueriesService.java
+++ b/server-core/src/main/java/com/juick/service/PMQueriesService.java
@@ -1,7 +1,28 @@
package com.juick.service;
+import com.juick.User;
+
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface PMQueriesService {
+ boolean createPM(int uid_from, int uid_to, String body);
+
+ boolean addPMinRoster(int uid, String jid);
+
+ boolean removePMinRoster(int uid, String jid);
+
+ boolean havePMinRoster(int uid, String jid);
+
+ String getLastView(int uid_from, int uid_to);
+
+ List<User> getPMLastConversationsUsers(int uid, int cnt);
+
+ List<com.juick.Message> getPMMessages(int uid, int uid_to);
+
+ List<com.juick.Message> getLastPMInbox(int uid);
+
+ List<com.juick.Message> getLastPMSent(int uid);
}
diff --git a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java
index 4c8f4ae9..30231f98 100644
--- a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java
@@ -1,10 +1,158 @@
package com.juick.service;
+import com.juick.User;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class PMQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService {
+public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesService {
+
+ @Transactional
+ @Override
+ public boolean createPM(final int uid_from, final int uid_to, final String body) {
+ boolean ret = getJdbcTemplate().update(
+ "INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)",
+ uid_from, uid_to, body) > 0;
+
+ if (ret) {
+ getJdbcTemplate().update(
+ "INSERT INTO pm_streams(user_id,user_id_to,lastmessage,unread) "
+ + "VALUES (?,?,NOW(),1) "
+ + "ON DUPLICATE KEY UPDATE lastmessage=NOW(),unread=unread+1",
+ uid_from, uid_to);
+ }
+ return ret;
+ }
+
+ @Transactional
+ @Override
+ public boolean addPMinRoster(final int uid, final String jid) {
+ return getJdbcTemplate().update(
+ "INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)",
+ uid, jid) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean removePMinRoster(final int uid, final String jid) {
+ return getJdbcTemplate().update(
+ "DELETE FROM pm_inroster WHERE user_id=? AND jid=?", uid, jid) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean havePMinRoster(final int uid, final String jid) {
+ List<Integer> res = getJdbcTemplate().queryForList(
+ "SELECT 1 FROM pm_inroster WHERE user_id=? AND jid=?",
+ Integer.class,
+ uid,
+ jid);
+ return res.size() > 0;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getLastView(final int uid_from, final int uid_to) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?",
+ String.class,
+ uid_from,
+ uid_to);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getPMLastConversationsUsers(final int uid, final int cnt) {
+ return getJdbcTemplate().query(
+ "SELECT pm_streams.user_id, users.nick, pm_streams.unread FROM pm_streams "
+ + "INNER JOIN users ON users.id = pm_streams.user_id "
+ + "WHERE pm_streams.user_id_to=? "
+ + "ORDER BY pm_streams.unread DESC, pm_streams.lastmessage DESC LIMIT " + cnt,
+ (rs, rowNum) -> {
+ com.juick.User u = new com.juick.User();
+ u.setUID(rs.getInt(1));
+ u.setUName(rs.getString(2));
+ u.setUnreadCount(rs.getInt(3));
+ return u;
+ },
+ uid);
+ }
+
+ @Transactional
+ @Override
+ public List<com.juick.Message> getPMMessages(final int uid, final int uid_to) {
+ List<com.juick.Message> msgs = getJdbcTemplate().query(
+ "SELECT user_id,txt,ts FROM pm "
+ + "WHERE (user_id=? AND user_id_to=?) "
+ + "OR (user_id_to=? AND user_id=?) ORDER BY ts DESC LIMIT 20",
+ (rs, rowNum) -> {
+ com.juick.Message msg = new com.juick.Message();
+ int uuid = rs.getInt(1);
+ msg.setUser(new User());
+ msg.getUser().setUID(uuid);
+ msg.setText(rs.getString(2));
+ msg.setDate(rs.getTimestamp(3));
+ return msg;
+ },
+ uid,
+ uid_to,
+ uid,
+ uid_to);
+
+ getJdbcTemplate().update(
+ "UPDATE pm_streams SET lastview=NOW(),unread=0 WHERE user_id_to=? AND user_id=?",
+ uid,
+ uid_to);
+
+ return msgs;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Message> getLastPMInbox(final int uid) {
+ return getJdbcTemplate().query(
+ "SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts " +
+ "FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20",
+ (rs, num) -> {
+ com.juick.Message msg = new com.juick.Message();
+ msg.setUser(new User());
+ msg.getUser().setUID(rs.getInt(1));
+ msg.getUser().setUName(rs.getString(2));
+ msg.setText(rs.getString(3));
+ msg.TimeAgo = rs.getInt(4);
+ msg.setDate(rs.getTimestamp(5));
+ return msg;
+ },
+ uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Message> getLastPMSent(final int uid) {
+ return getJdbcTemplate().query(
+ "SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW())," +
+ "pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id " +
+ "WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20",
+ (rs, num) -> {
+ com.juick.Message msg = new com.juick.Message();
+ msg.setUser(new User());
+ msg.getUser().setUID(rs.getInt(1));
+ msg.getUser().setUName(rs.getString(2));
+ msg.setText(rs.getString(3));
+ msg.TimeAgo = rs.getInt(4);
+ msg.setDate(rs.getTimestamp(5));
+ return msg;
+ },
+ uid);
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java
index 29116081..61eb199b 100644
--- a/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java
+++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java
@@ -1,7 +1,17 @@
package com.juick.service;
+import com.juick.Tag;
+import com.juick.User;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface PrivacyQueriesService {
+ enum PrivacyResult {
+ Removed, Added
+ }
+
+ PrivacyResult blacklistUser(User user, User target);
+
+ PrivacyResult blacklistTag(User user, Tag tag);
}
diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java
index d4e43728..b83f61c1 100644
--- a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java
@@ -1,10 +1,48 @@
package com.juick.service;
+import com.juick.Tag;
+import com.juick.User;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class PrivacyQueriesServiceImpl extends BaseJdbcDao implements PrivacyQueriesService {
+@Transactional
+public class PrivacyQueriesServiceImpl extends BaseJdbcService implements PrivacyQueriesService {
+
+ @Override
+ public PrivacyResult blacklistUser(final User user, final User target) {
+ int result = getJdbcTemplate().update(
+ "DELETE FROM bl_users WHERE user_id=? AND bl_user_id=?",
+ user.getUID(),
+ target.getUID());
+ if (result > 0) {
+ return PrivacyResult.Removed;
+ } else {
+ getJdbcTemplate().update(
+ "INSERT INTO bl_users(user_id,bl_user_id) VALUES (?,?)",
+ user.getUID(),
+ target.getUID());
+ return PrivacyResult.Added;
+ }
+ }
+
+ @Override
+ public PrivacyResult blacklistTag(final User user, final Tag tag) {
+ int result = getJdbcTemplate().update(
+ "DELETE FROM bl_tags WHERE user_id=? AND tag_id=?",
+ user.getUID(),
+ tag.TID);
+ if (result > 0) {
+ return PrivacyResult.Removed;
+ } else {
+ getJdbcTemplate().update(
+ "INSERT INTO bl_tags(user_id,tag_id) VALUES (?,?)",
+ user.getUID(),
+ tag.TID);
+ return PrivacyResult.Added;
+ }
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/PushQueriesService.java b/server-core/src/main/java/com/juick/service/PushQueriesService.java
index dbb5ac06..8e7ce543 100644
--- a/server-core/src/main/java/com/juick/service/PushQueriesService.java
+++ b/server-core/src/main/java/com/juick/service/PushQueriesService.java
@@ -1,7 +1,20 @@
package com.juick.service;
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface PushQueriesService {
+ List<String> getAndroidRegID(int uid);
+
+ List<String> getAndroidTokens(List<Integer> uids);
+
+ List<String> getWinPhoneURL(int uid);
+
+ List<String> getWindowsTokens(List<Integer> uids);
+
+ List<String> getAPNSToken(int uid);
+
+ List<String> getAPNSTokens(List<Integer> uids);
}
diff --git a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java
index 0381f913..7c3f9d5b 100644
--- a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java
@@ -1,10 +1,63 @@
package com.juick.service;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+
+import java.util.List;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class PushQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService {
+@Transactional(readOnly = true)
+public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueriesService {
+
+ @Override
+ public List<String> getAndroidRegID(final int uid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT regid FROM android WHERE user_id=?",
+ String.class,
+ uid);
+ }
+
+ @Override
+ public List<String> getAndroidTokens(final List<Integer> uids) {
+ return getJdbcTemplate().queryForList(
+ "SELECT regid FROM android INNER JOIN users " +
+ "ON (users.id=android.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")",
+ String.class);
+ }
+
+ @Override
+ public List<String> getWinPhoneURL(final int uid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT url FROM winphone WHERE user_id=?",
+ String.class,
+ uid);
+ }
+
+ @Override
+ public List<String> getWindowsTokens(final List<Integer> uids) {
+ return getJdbcTemplate().queryForList(
+ "SELECT url FROM winphone INNER JOIN users " +
+ "ON (users.id=winphone.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")",
+ String.class);
+ }
+
+ @Override
+ public List<String> getAPNSToken(final int uid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT token from ios WHERE user_id=?",
+ String.class,
+ uid);
+ }
+
+ @Override
+ public List<String> getAPNSTokens(final List<Integer> uids) {
+ return getJdbcTemplate().queryForList(
+ "SELECT token FROM ios INNER JOIN users " +
+ "ON (users.id=ios.user_id) WHERE users.id IN (" + StringUtils.collectionToCommaDelimitedString(uids) + ")",
+ String.class);
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesService.java b/server-core/src/main/java/com/juick/service/ShowQueriesService.java
index 2d42988d..a7e1c364 100644
--- a/server-core/src/main/java/com/juick/service/ShowQueriesService.java
+++ b/server-core/src/main/java/com/juick/service/ShowQueriesService.java
@@ -1,7 +1,14 @@
package com.juick.service;
+import com.juick.User;
+
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface ShowQueriesService {
+ List<String> getRecommendedUsers(User forUser);
+
+ List<String> getTopUsers();
}
diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java
index d3470c75..4f8d8ff2 100644
--- a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java
@@ -1,10 +1,44 @@
package com.juick.service;
+import com.juick.User;
+import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class ShowQueriesServiceImpl extends BaseJdbcDao implements SubscriptionService {
+@Transactional(readOnly = true)
+public class ShowQueriesServiceImpl extends BaseJdbcService implements ShowQueriesService {
+
+ @Override
+ public List<String> getRecommendedUsers(final User forUser) {
+ return getJdbcTemplate().queryForList(
+ "SELECT users.nick FROM subscr_users INNER JOIN users " +
+ "ON subscr_users.user_id=users.id " +
+ "WHERE subscr_users.user_id NOT IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " +
+ "AND subscr_users.suser_id IN (SELECT user_id FROM subscr_users WHERE suser_id=?) " +
+ "AND subscr_users.user_id NOT IN (SELECT bl_user_id FROM bl_users WHERE user_id=?) " +
+ "AND subscr_users.user_id!=? AND users.lastmessage>UNIX_TIMESTAMP()-259200 " +
+ "GROUP BY subscr_users.user_id ORDER BY count(*) DESC LIMIT 10",
+ String.class,
+ forUser.getUID(),
+ forUser.getUID(),
+ forUser.getUID(),
+ forUser.getUID());
+ }
+
+ @Override
+ public List<String> getTopUsers() {
+ return getJdbcTemplate().queryForList(
+ "SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " +
+ "FROM (subscr_users INNER JOIN users ON subscr_users.user_id=users.id) " +
+ "INNER JOIN useroptions ON users.id=useroptions.user_id " +
+ "WHERE useroptions.privacy_view>0 AND users.lastmessage > UNIX_TIMESTAMP() - 259200 " +
+ "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10",
+ String.class);
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/SubscriptionService.java b/server-core/src/main/java/com/juick/service/SubscriptionService.java
index 0ed3f55a..00aee15d 100644
--- a/server-core/src/main/java/com/juick/service/SubscriptionService.java
+++ b/server-core/src/main/java/com/juick/service/SubscriptionService.java
@@ -1,7 +1,36 @@
package com.juick.service;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.server.helpers.NotifyOpts;
+
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface SubscriptionService {
+ List<String> getJIDSubscribedToUser(int uid, boolean friendsonly);
+
+ List<User> getSubscribedUsers(int uid, int mid);
+
+ List<User> getUsersSubscribedToComments(int mid, int ignore_uid);
+
+ List<User> getUsersSubscribedToUserRecommendations(int uid, int mid, int muid);
+
+ boolean subscribeMessage(int mid, int vuid);
+
+ boolean unSubscribeMessage(int mid, int vuid);
+
+ boolean subscribeUser(User user, User toUser);
+
+ boolean unSubscribeUser(User user, User fromUser);
+
+ boolean subscribeTag(User user, Tag toTag);
+
+ boolean unSubscribeTag(User user, Tag toTag);
+
+ NotifyOpts getNotifyOptions(User user);
+
+ boolean setNotifyOptions(User user, NotifyOpts options);
}
diff --git a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java
index 811f5af0..57d52cbf 100644
--- a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java
@@ -1,10 +1,185 @@
package com.juick.service;
+import com.juick.Tag;
+import com.juick.User;
+import com.juick.server.helpers.NotifyOpts;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+
+import javax.inject.Inject;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class SubscriptionServiceImpl extends BaseJdbcDao implements SubscriptionService {
+public class SubscriptionServiceImpl extends BaseJdbcService implements SubscriptionService {
+ @Inject
+ UserService userService;
+ @Inject
+ MessagesService messagesService;
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getJIDSubscribedToUser(final int uid, final boolean friendsonly) {
+ if (friendsonly == false) {
+ return getJdbcTemplate().queryForList(
+ "SELECT jids.jid FROM subscr_users INNER JOIN jids " +
+ "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1",
+ String.class,
+ uid);
+ } else {
+ return getJdbcTemplate().queryForList(
+ "SELECT jids.jid FROM subscr_users INNER JOIN jids " +
+ "ON (subscr_users.user_id=? AND subscr_users.suser_id=jids.user_id) WHERE jids.active=1 " +
+ "AND jids.user_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=?)",
+ String.class,
+ uid,
+ uid);
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getSubscribedUsers(final int uid, final int mid) {
+ User author = messagesService.getMessageAuthor(mid);
+
+ List<User> userids = userService.getUserReaders(uid);
+
+ Set<Integer> set = new HashSet<>();
+ set.addAll(userids.stream().map(User::getUID).collect(Collectors.toList()));
+ List<Integer> tags = messagesService.getMessageTagsIDs(mid);
+ if (tags.size() > 0) {
+ List<Integer> tagUsers = getJdbcTemplate().queryForList(
+ "SELECT suser_id FROM subscr_tags " +
+ "WHERE tag_id IN (" + StringUtils.arrayToCommaDelimitedString(tags.toArray()) + ") AND suser_id!=? " +
+ " AND suser_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=?)",
+ Integer.class,
+ uid,
+ author.getUID());
+ set.addAll(tagUsers);
+ }
+ return userService.getUsersByID(new ArrayList<>(set));
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUsersSubscribedToComments(final int mid, final int ignore_uid) {
+ List<Integer> userids = getJdbcTemplate().queryForList(
+ "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?",
+ Integer.class,
+ mid,
+ ignore_uid);
+ if (userids.size() > 0) {
+ return userService.getUsersByID(userids);
+ } else {
+ return Collections.emptyList();
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUsersSubscribedToUserRecommendations(final int uid, final int mid, final int muid) {
+ List<Integer> tags = messagesService.getMessageTagsIDs(mid);
+
+ String query = "SELECT suser_id FROM subscr_users WHERE user_id=" + uid;
+ query += " AND user_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=" + muid + ")";
+ query += " AND user_id NOT IN (SELECT suser_id FROM subscr_users WHERE user_id=" + muid + ")";
+ query += " AND user_id NOT IN (SELECT suser_id FROM subscr_messages WHERE message_id=" + mid + ")";
+ query += " AND user_id NOT IN (SELECT user_id FROM favorites WHERE message_id=" + mid + ")";
+ query += " AND user_id NOT IN (SELECT subscr_users.suser_id FROM subscr_users INNER JOIN favorites ON (favorites.message_id=" + mid + " AND subscr_users.user_id=favorites.user_id AND favorites.user_id!=" + uid + "))";
+
+ if (!tags.isEmpty()) {
+ String tagsStr = StringUtils.arrayToCommaDelimitedString(tags.toArray());
+ query += " AND user_id NOT IN (SELECT suser_id FROM subscr_tags WHERE tag_id IN (" + tagsStr + "))";
+ query += " AND user_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + tagsStr + "))";
+ }
+
+ List<Integer> userids = getJdbcTemplate().queryForList(query, Integer.class);
+
+ return userService.getUsersByID(userids);
+ }
+
+ @Transactional
+ @Override
+ public boolean subscribeMessage(final int mid, final int vuid) {
+ return getJdbcTemplate().update(
+ "INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1;
+ }
+
+ @Transactional
+ @Override
+ public boolean unSubscribeMessage(final int mid, final int vuid) {
+ return getJdbcTemplate().update(
+ "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?",
+ mid, vuid) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean subscribeUser(final User user, final User toUser) {
+ return getJdbcTemplate().update(
+ "INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)",
+ toUser.getUID(), user.getUID()) == 1;
+ }
+
+ @Transactional
+ @Override
+ public boolean unSubscribeUser(final User user, final User fromUser) {
+ return getJdbcTemplate().update(
+ "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?",
+ user.getUID(), fromUser.getUID()) > 0;
+ }
+
+ @Transactional
+ @Override
+ public boolean subscribeTag(final User user, final Tag toTag) {
+ return getJdbcTemplate().update(
+ "INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)",
+ toTag.TID, user.getUID()) == 1;
+ }
+
+ @Transactional
+ @Override
+ public boolean unSubscribeTag(final User user, final Tag toTag) {
+ return getJdbcTemplate().update(
+ "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?",
+ toTag.TID, user.getUID()) > 0;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public NotifyOpts getNotifyOptions(final User user) {
+ try {
+ return getJdbcTemplate().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();
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean setNotifyOptions(final User user, final NotifyOpts options) {
+ return getJdbcTemplate().update(
+ "UPDATE useroptions SET jnotify=? WHERE user_id=?", options.isRepliesEnabled() ? 1 : 0,
+ user.getUID()) > 0 &&
+ getJdbcTemplate().update(
+ "UPDATE useroptions SET subscr_notify=? WHERE user_id=?", options.isSubscriptionsEnabled() ? 1 : 0,
+ user.getUID()) > 0 &&
+ getJdbcTemplate().update(
+ "UPDATE useroptions SET recommendations=? WHERE user_id=?", options.isRecommendationsEnabled() ? 1 : 0,
+ user.getUID()) > 0;
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/TagService.java b/server-core/src/main/java/com/juick/service/TagService.java
index 1083d663..d5f63e3a 100644
--- a/server-core/src/main/java/com/juick/service/TagService.java
+++ b/server-core/src/main/java/com/juick/service/TagService.java
@@ -1,7 +1,30 @@
package com.juick.service;
+import com.juick.Tag;
+
+import java.util.List;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface TagService {
+ com.juick.Tag getTag(int tid);
+
+ com.juick.Tag getTag(String tag, boolean autoCreate);
+
+ List<Tag> getTags(String[] tags, boolean autoCreate);
+
+ boolean getTagNoIndex(int tag_id);
+
+ int createTag(String name);
+
+ List<com.juick.Tag> getUserTagsAll(int uid);
+
+ List<String> getUserBLTags(int uid);
+
+ List<String> getPopularTags();
+
+ List<Tag> updateTags(int mid, List<Tag> newTags);
+
+ List<Tag> fromString(String txt, boolean tagsOnly);
}
diff --git a/server-core/src/main/java/com/juick/service/TagServiceImpl.java b/server-core/src/main/java/com/juick/service/TagServiceImpl.java
index 9649ce74..3aed4b9b 100644
--- a/server-core/src/main/java/com/juick/service/TagServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/TagServiceImpl.java
@@ -1,10 +1,182 @@
package com.juick.service;
+import com.juick.Tag;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
+import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.inject.Inject;
+import java.sql.PreparedStatement;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class TagServiceImpl extends BaseJdbcDao implements TagService {
+public class TagServiceImpl extends BaseJdbcService implements TagService {
+ private static final Pattern TAGS_PATTERN1 = Pattern.compile("^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$");
+ private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)");
+ private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)");
+
+ @Inject
+ MessagesService messagesService;
+
+ @Transactional(readOnly = true)
+ @Override
+ public com.juick.Tag getTag(final int tid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT synonym_id,name FROM tags WHERE tag_id=?",
+ (rs, num) -> {
+ Tag ret = new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(2)));
+ ret.TID = tid;
+ ret.SynonymID = rs.getInt(1);
+ return ret;
+ },
+ tid);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public com.juick.Tag getTag(final String tag, final boolean autoCreate) {
+ Tag ret = null;
+ try {
+ ret = getJdbcTemplate().queryForObject(
+ "SELECT tag_id,synonym_id,name FROM tags WHERE name=?",
+ (rs, rowNum) -> {
+ Tag ret1 = new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(3)));
+ ret1.TID = rs.getInt(1);
+ ret1.SynonymID = rs.getInt(2);
+ return ret1;
+ },
+ StringEscapeUtils.escapeHtml4(tag));
+ } catch (EmptyResultDataAccessException e) {
+ // tag not found
+ }
+ if (ret == null && autoCreate) {
+ ret = new com.juick.Tag(tag);
+ ret.TID = createTag(tag);
+ }
+
+ return ret;
+ }
+
+ @Override
+ public List<Tag> getTags(final String[] tags, final boolean autoCreate) {
+ List<Tag> ret = new ArrayList<>();
+
+ for (String tag : tags) {
+ if (!tag.isEmpty()) {
+ Tag t = getTag(tag, autoCreate);
+ if (t != null) {
+ ret.add(t);
+ }
+ }
+ }
+
+ return ret;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean getTagNoIndex(final int tag_id) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT noindex FROM tags WHERE tag_id=?", Integer.class, tag_id) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional
+ @Override
+ public int createTag(final String name) {
+ KeyHolder holder = new GeneratedKeyHolder();
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO tags(name) VALUES (?)",
+ Statement.RETURN_GENERATED_KEYS);
+ stmt.setString(1, StringEscapeUtils.escapeHtml4(name));
+ return stmt;
+ }, holder);
+
+ return holder.getKey().intValue();
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.Tag> getUserTagsAll(final int uid) {
+ return getJdbcTemplate().query(
+ "SELECT tags.name,COUNT(messages.message_id) " +
+ "FROM (messages INNER JOIN messages_tags ON (messages.user_id=? " +
+ "AND messages.message_id=messages_tags.message_id)) " +
+ "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC",
+ (rs, rowNum) -> {
+ Tag t = new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(1)));
+ t.UsageCnt = rs.getInt(2);
+ return t;
+ }, uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getUserBLTags(final int uid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT tags.name FROM tags INNER JOIN bl_tags " +
+ "ON (bl_tags.user_id=? AND bl_tags.tag_id=tags.tag_id) ORDER BY tags.name",
+ String.class, uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getPopularTags() {
+ return getJdbcTemplate().queryForList(
+ "SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class).stream()
+ .map(StringEscapeUtils::unescapeHtml4).collect(Collectors.toList());
+ }
+
+ @Transactional
+ @Override
+ public List<Tag> updateTags(final int mid, final List<Tag> newTags) {
+ List<Tag> currentTags = messagesService.getMessageTags(mid);
+ newTags.stream().filter(currentTags::contains)
+ .forEach(t -> getJdbcTemplate().update(
+ "DELETE FROM messages_tags WHERE message_id=? AND tag_id=?",
+ mid,
+ t.TID));
+ newTags.stream().filter(t -> !currentTags.contains(t))
+ .forEach(t -> getJdbcTemplate().update(
+ "INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID));
+ return messagesService.getMessageTags(mid);
+ }
+
+ @Override
+ public List<Tag> fromString(final String txt, final boolean tagsOnly) {
+ Pattern tagsPattern = tagsOnly ? TAGS_PATTERN1 : TAGS_PATTERN2;
+
+ if (tagsPattern.matcher(txt).matches()) {
+ Matcher tagMatcher = TAG_PATTERN.matcher(txt);
+ List<Tag> tags = new ArrayList<>();
+ // TODO: process readonly, private, friends, public
+ while (tagMatcher.find()) {
+ for (int i = 1; i <= tagMatcher.groupCount(); i++) {
+ tags.add(getTag(tagMatcher.group(i), true));
+ }
+ }
+ return tags;
+ }
+ return Collections.emptyList();
+ }
}
diff --git a/server-core/src/main/java/com/juick/service/UserService.java b/server-core/src/main/java/com/juick/service/UserService.java
index ecf8ad07..e970afb7 100644
--- a/server-core/src/main/java/com/juick/service/UserService.java
+++ b/server-core/src/main/java/com/juick/service/UserService.java
@@ -1,7 +1,107 @@
package com.juick.service;
+import com.juick.User;
+import com.juick.server.helpers.Auth;
+import com.juick.server.helpers.EmailOpts;
+import com.juick.server.helpers.UserInfo;
+
+import java.util.List;
+import java.util.Optional;
+
/**
* Created by aalexeev on 11/13/16.
*/
public interface UserService {
+ enum ActiveStatus {
+ Inactive,
+ Active
+ }
+
+ String getSignUpHashByJID(String jid);
+
+ String getSignUpHashByTelegramID(Long telegramId, String username);
+
+ int createUser(String username, String password);
+
+ Optional<User> getUserByUID(int uid);
+
+ User getUserByName(String username);
+
+ User getUserByJID(String jid);
+
+ List<User> getUsersByName(List<String> unames);
+
+ List<User> getUsersByID(List<Integer> uids);
+
+ List<com.juick.User> getUsersByJID(List<String> jids);
+
+ List<String> getJIDsbyUID(int uid);
+
+ int getUIDbyJID(String jid);
+
+ int getUIDbyName(String uname);
+
+ int getUIDbyHash(String hash);
+
+ com.juick.User getUserByHash(String hash);
+
+ String getHashByUID(int uid);
+
+ int checkPassword(String username, String password);
+
+ boolean updatePassword(User user, String newPassword);
+
+ String updateSecretEmail(User user);
+
+ int getUserOptionInt(int uid, String option, int defaultValue);
+
+ void setUserOptionInt(int uid, String option, int value);
+
+ UserInfo getUserInfo(User user);
+
+ boolean updateUserInfo(User user, UserInfo info);
+
+ boolean getCanMedia(int uid);
+
+ boolean isInWL(int uid, int check);
+
+ boolean isInBL(int uid, int check);
+
+ boolean isInBLAny(int uid, int uid2);
+
+ List<Integer> checkBL(int visitor, List<Integer> uids);
+
+ boolean isSubscribed(int uid, int check);
+
+ List<Integer> getUserRead(int uid);
+
+ List<com.juick.User> getUserReadLeastPopular(int uid, int cnt);
+
+ List<User> getUserReaders(int uid);
+
+ List<User> getUserFriends(int uid);
+
+ List<com.juick.User> getUserBLUsers(int uid);
+
+ boolean linkTwitterAccount(User user, String accessToken, String accessTokenSecret, String screenName);
+
+ int getStatsIRead(int uid);
+
+ int getStatsMyReaders(int uid);
+
+ int getStatsMessages(int uid);
+
+ int getStatsReplies(int uid);
+
+ boolean setActiveStatusForJID(String JID, ActiveStatus jidStatus);
+
+ List<String> getAllJIDs(User user);
+
+ List<Auth> getAuthCodes(User user);
+
+ List<String> getEmails(User user);
+
+ EmailOpts getEmailOpts(User user);
+
+ String getEmailHash(User user);
}
diff --git a/server-core/src/main/java/com/juick/service/UserServiceImpl.java b/server-core/src/main/java/com/juick/service/UserServiceImpl.java
index 93dc90dc..2740e52c 100644
--- a/server-core/src/main/java/com/juick/service/UserServiceImpl.java
+++ b/server-core/src/main/java/com/juick/service/UserServiceImpl.java
@@ -1,10 +1,607 @@
package com.juick.service;
+import com.juick.User;
+import com.juick.server.helpers.Auth;
+import com.juick.server.helpers.EmailOpts;
+import com.juick.server.helpers.UserInfo;
+import com.juick.util.UserUtils;
+import org.springframework.dao.DuplicateKeyException;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
+import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.*;
/**
* Created by aalexeev on 11/13/16.
*/
@Repository
-public class UserServiceImpl extends BaseJdbcDao implements UserService {
+public class UserServiceImpl extends BaseJdbcService implements UserService {
+
+ private class UserMapper implements RowMapper<User> {
+ @Override
+ public User mapRow(ResultSet rs, int rowNum) throws SQLException {
+ User user = new User();
+ user.setUID(rs.getInt(1));
+ user.setUName(rs.getString(2));
+ user.Banned = rs.getBoolean(3);
+ return user;
+ }
+ }
+
+ @Transactional
+ @Override
+ public String getSignUpHashByJID(final String jid) {
+ String hash;
+ try {
+ hash = getJdbcTemplate().queryForObject(
+ "SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL",
+ String.class,
+ jid);
+ } catch (EmptyResultDataAccessException e) {
+ hash = UUID.randomUUID().toString();
+ getJdbcTemplate().update("INSERT INTO jids(jid,loginhash) VALUES (?,?)", jid, hash);
+ }
+ return hash;
+ }
+
+ @Transactional
+ @Override
+ public String getSignUpHashByTelegramID(final Long telegramId, final String username) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT loginhash FROM telegram WHERE tg_id=? AND user_id IS NULL",
+ String.class,
+ telegramId);
+ } catch (EmptyResultDataAccessException e) {
+ String hash = UUID.randomUUID().toString();
+ getJdbcTemplate().update(
+ "INSERT INTO telegram(tg_id, loginhash, tg_name) VALUES (?, ?, ?)", telegramId, hash, username);
+ return hash;
+ }
+ }
+
+ @Transactional
+ @Override
+ public int createUser(final String username, final String password) {
+ KeyHolder holder = new GeneratedKeyHolder();
+ try {
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement(
+ "INSERT INTO users(nick,passw) VALUES (?,?)",
+ Statement.RETURN_GENERATED_KEYS);
+ stmt.setString(1, username);
+ stmt.setString(2, password);
+ return stmt;
+ }, holder);
+ } catch (DuplicateKeyException e) {
+ return -1;
+ }
+
+ int uid = holder.getKey().intValue();
+
+ getJdbcTemplate().update("INSERT INTO useroptions(user_id) VALUES (?)", uid);
+ getJdbcTemplate().update("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid);
+
+ return uid;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public Optional<User> getUserByUID(final int uid) {
+ try {
+ return Optional.of(
+ getJdbcTemplate().queryForObject("SELECT id, nick,banned FROM users WHERE id=?",
+ new UserMapper(),
+ uid));
+ } catch (EmptyResultDataAccessException e) {
+ return Optional.empty();
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public User getUserByName(final String username) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT id,nick,banned FROM users WHERE nick=?",
+ new UserMapper(),
+ username);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public User getUserByJID(final String jid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)",
+ new UserMapper(),
+ jid);
+ } catch (EmptyResultDataAccessException e) {
+ return null;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUsersByName(final List<String> unames) {
+ if (!unames.isEmpty()) {
+ return getJdbcTemplate().query(
+ "SELECT id,nick,banned FROM users WHERE nick IN (\"" + StringUtils.arrayToDelimitedString(unames.toArray(), "\",\"") + "\")",
+ new UserMapper());
+ }
+ return Collections.emptyList();
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUsersByID(final List<Integer> uids) {
+ if (!uids.isEmpty()) {
+ return getJdbcTemplate().query(
+ "SELECT id,nick,banned FROM users WHERE id IN (" + StringUtils.arrayToCommaDelimitedString(uids.toArray()) + ")",
+ new UserMapper());
+ }
+ return Collections.emptyList();
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.User> getUsersByJID(final List<String> jids) {
+ if (!jids.isEmpty()) {
+ return getJdbcTemplate().query(
+ "SELECT users.id,users.nick,jids.jid FROM users "
+ + "INNER JOIN jids ON jids.user_id=users.id "
+ + "WHERE jids.jid IN (\"" + StringUtils.arrayToDelimitedString(jids.toArray(), "\",\"") + "\")",
+ (rs, rowNum) -> {
+ com.juick.User user = new com.juick.User();
+ user.setUID(rs.getInt(1));
+ user.setUName(rs.getString(2));
+ user.setJID(rs.getString(3));
+ return user;
+ });
+ }
+ return Collections.emptyList();
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getJIDsbyUID(final int uid) {
+ return getJdbcTemplate().queryForList("SELECT jid FROM jids WHERE user_id=? AND active=1", String.class, uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUIDbyJID(final String jid) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT user_id FROM jids WHERE jid=?", Integer.class, jid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUIDbyName(final String uname) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT id FROM users WHERE nick=?", Integer.class, uname);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUIDbyHash(final String hash) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT user_id FROM logins WHERE hash=?", Integer.class, hash);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public com.juick.User getUserByHash(final String hash) {
+ try {
+ User user = getJdbcTemplate().queryForObject(
+ "SELECT logins.user_id,users.nick, users.banned FROM logins " +
+ "INNER JOIN users ON logins.user_id=users.id WHERE logins.hash=?",
+ new UserMapper(),
+ hash);
+ user.setAuthHash(hash);
+ return user;
+ } catch (EmptyResultDataAccessException e) {
+ return new User();
+ }
+ }
+
+ @Transactional
+ @Override
+ public String getHashByUID(final int uid) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT hash FROM logins WHERE user_id=?", String.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ String hash = UserUtils.generateHash(16);
+ getJdbcTemplate().update(con -> {
+ PreparedStatement stmt = con.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)");
+ stmt.setInt(1, uid);
+ stmt.setString(2, hash);
+ return stmt;
+ });
+ return hash;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int checkPassword(final String username, final String password) {
+ try {
+ String realPassword = getJdbcTemplate().queryForObject(
+ "SELECT passw FROM users WHERE nick=?", String.class, username);
+ if (realPassword.equals(password)) {
+ User user = getUserByName(username);
+ if (user != null) {
+ return user.getUID();
+ } else {
+ return -1;
+ }
+ } else {
+ return -1;
+ }
+ } catch (EmptyResultDataAccessException e) {
+ return -1;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean updatePassword(final User user, final String newPassword) {
+ return user.getUID() > 0 && getJdbcTemplate().update("UPDATE users SET passw=? WHERE id=?", newPassword, user.getUID()) > 0;
+ }
+
+ @Transactional
+ @Override
+ public String updateSecretEmail(final User user) {
+ String newHash = UserUtils.generateHash(16);
+ if (getJdbcTemplate().update(
+ "INSERT INTO mail(user_id,hash) VALUES (?,?) ON DUPLICATE KEY UPDATE hash=?",
+ user.getUID(),
+ newHash,
+ newHash) > 0) {
+ return newHash;
+ }
+ return "";
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getUserOptionInt(final int uid, final String option, final int defaultValue) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT " + option + " FROM useroptions WHERE user_id=?", Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return defaultValue;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public void setUserOptionInt(final int uid, final String option, final int value) {
+ getJdbcTemplate().update("UPDATE useroptions SET " + option + "=? WHERE user_id=?", value, uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public UserInfo getUserInfo(final User user) {
+ try {
+ return getJdbcTemplate().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();
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean updateUserInfo(final User user, final UserInfo info) {
+ return getJdbcTemplate().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;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean getCanMedia(final int uid) {
+ try {
+ int res = getJdbcTemplate().queryForObject(
+ "SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?",
+ Integer.class,
+ uid);
+ return res < 3600;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isInWL(final int uid, final int check) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?",
+ Integer.class, uid, check) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isInBL(final int uid, final int check) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?",
+ Integer.class, uid, check) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isInBLAny(final int uid, final int uid2) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT 1 FROM bl_users "
+ + "WHERE (user_id=? AND bl_user_id=?) "
+ + "OR (user_id=? AND bl_user_id=?)", new Object[]{uid, uid2, uid2, uid}, Integer.class) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> checkBL(final int visitor, final List<Integer> uids) {
+ if (!uids.isEmpty()) {
+ return getJdbcTemplate().queryForList(
+ "SELECT user_id FROM bl_users WHERE bl_user_id=? and user_id IN (" +
+ StringUtils.collectionToCommaDelimitedString(uids) + ")",
+ Integer.class,
+ visitor);
+ } else {
+ return new ArrayList<>();
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public boolean isSubscribed(final int uid, final int check) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?",
+ Integer.class, uid, check) == 1;
+ } catch (EmptyResultDataAccessException e) {
+ return false;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Integer> getUserRead(final int uid) {
+ return getJdbcTemplate().queryForList(
+ "SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.User> getUserReadLeastPopular(final int uid, final int cnt) {
+ return getJdbcTemplate().query(
+ "SELECT users.id,users.nick FROM (subscr_users " +
+ "INNER JOIN users_subscr ON (subscr_users.suser_id=? " +
+ "AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users " +
+ "ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?",
+ (rs, num) -> {
+ com.juick.User u = new com.juick.User();
+ u.setUID(rs.getInt(1));
+ u.setUName(rs.getString(2));
+ return u;
+ },
+ uid,
+ cnt);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUserReaders(final int uid) {
+ return getJdbcTemplate().query(
+ "SELECT users.id, users.nick FROM subscr_users " +
+ "INNER JOIN users ON subscr_users.suser_id=users.id " +
+ "WHERE subscr_users.user_id=? ORDER BY users.nick",
+ (rs, num) -> {
+ com.juick.User u = new com.juick.User();
+ u.setUID(rs.getInt(1));
+ u.setUName(rs.getString(2));
+ return u;
+ },
+ uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<User> getUserFriends(final int uid) {
+ return getJdbcTemplate().query(
+ "SELECT users.id,users.nick FROM subscr_users " +
+ "INNER JOIN users ON subscr_users.user_id=users.id " +
+ "WHERE subscr_users.suser_id=? AND users.id!=? " +
+ "ORDER BY users.nick",
+ (rs, num) -> {
+ com.juick.User u = new com.juick.User();
+ u.setUID(rs.getInt(1));
+ u.setUName(rs.getString(2));
+ return u;
+ },
+ uid,
+ uid);
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<com.juick.User> getUserBLUsers(final int uid) {
+ return getJdbcTemplate().query("SELECT users.id,users.nick FROM users INNER JOIN bl_users " +
+ "ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick",
+ (rs, num) -> {
+ com.juick.User u = new com.juick.User();
+ u.setUID(rs.getInt(1));
+ u.setUName(rs.getString(2));
+ return u;
+ }, uid);
+ }
+
+ @Transactional
+ @Override
+ public boolean linkTwitterAccount(
+ final User user, final String accessToken, final String accessTokenSecret, final String screenName) {
+ if (getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " +
+ "VALUES (?,?,?,?)" +
+ " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?",
+ user.getUID(), accessToken, accessTokenSecret, screenName, accessToken, accessTokenSecret, screenName) > 0) {
+ return getJdbcTemplate().update("INSERT INTO subscr_users(user_id,suser_id,jid) " +
+ "VALUES (?,1741,'juick\\@twitter.juick.com')", user.getUID()) > 0;
+ }
+ return false;
+
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getStatsIRead(final int uid) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getStatsMyReaders(final int uid) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT COUNT(*) FROM subscr_users WHERE user_id=?", Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getStatsMessages(final int uid) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT COUNT(*) FROM messages WHERE user_id=?", Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public int getStatsReplies(final int uid) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT COUNT(*) FROM replies WHERE user_id=?", Integer.class, uid);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Transactional
+ @Override
+ public boolean setActiveStatusForJID(final String JID, final UserService.ActiveStatus jidStatus) {
+ User user = getUserByJID(JID);
+ if (user != null) {
+ return getJdbcTemplate().update(con -> {
+ PreparedStatement preparedStatement = con.prepareStatement(
+ "UPDATE jids SET active=? WHERE user_id=? AND jid=?");
+ int newStatus = jidStatus == UserService.ActiveStatus.Active ? 1 : 0;
+ preparedStatement.setInt(1, newStatus);
+ preparedStatement.setInt(2, user.getUID());
+ preparedStatement.setString(3, JID);
+ return preparedStatement;
+
+ }) >= 0;
+ }
+ return false;
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getAllJIDs(final User user) {
+ return getJdbcTemplate().queryForList(
+ "SELECT jid FROM jids WHERE user_id=?", String.class, user.getUID());
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<Auth> getAuthCodes(final User user) {
+ return getJdbcTemplate().query(
+ "SELECT account,authcode FROM auth WHERE user_id=? AND protocol='xmpp'",
+ (rs, num) -> new Auth(rs.getString(1), rs.getString(2)),
+ user.getUID());
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public List<String> getEmails(final User user) {
+ return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUID());
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public EmailOpts getEmailOpts(final User user) {
+ try {
+ return getJdbcTemplate().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;
+ }
+ }
+
+ @Transactional(readOnly = true)
+ @Override
+ public String getEmailHash(final User user) {
+ try {
+ return getJdbcTemplate().queryForObject(
+ "SELECT hash FROM mail WHERE user_id=?",
+ String.class,
+ user.getUID())
+ + "@mail.juick.com";
+ } catch (EmptyResultDataAccessException e) {
+ return "";
+ }
+ }
}