aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'juick-server/src/main')
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java48
-rw-r--r--juick-server/src/main/java/com/juick/service/TelegramService.java15
-rw-r--r--juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java58
-rw-r--r--juick-server/src/main/java/com/juick/service/UserService.java2
-rw-r--r--juick-server/src/main/java/com/juick/service/UserServiceImpl.java13
5 files changed, 118 insertions, 18 deletions
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
index 25ce61fb..5019ef0c 100644
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -459,25 +459,37 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
public List<Integer> getMyFeed(final int uid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("before", 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);
+ }
- List<Integer> mids = getNamedParameterJdbcTemplate().queryForList(
- "(SELECT message_id FROM messages " +
- " INNER JOIN subscr_users ON (subscr_users.suser_id = :uid AND subscr_users.user_id = messages.user_id) " +
- " WHERE " +
- (before > 0 ?
- " message_id < :before AND " : "") +
- " (privacy >= 0 OR (privacy >= -2 AND privacy <= -1" +
- " AND EXISTS (SELECT 1 FROM wl_users w WHERE w.wl_user_id = :uid and w.user_id = messages.user_id)))) " +
- " UNION " +
- " (SELECT message_id FROM messages WHERE user_id=:uid " +
- (before > 0 ?
- " AND message_id < :before " : "") +
- " ORDER BY message_id ASC LIMIT 20",
- sqlParameterSource,
- Integer.class);
+
+ 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));
+ }
+
+ mids.sort(Collections.reverseOrder());
+ int remove = mids.size() - 20;
+ for (int i = 0; i < remove; i++) {
+ mids.remove(20);
+ }
return mids;
}
diff --git a/juick-server/src/main/java/com/juick/service/TelegramService.java b/juick-server/src/main/java/com/juick/service/TelegramService.java
new file mode 100644
index 00000000..d5aa4fa8
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/service/TelegramService.java
@@ -0,0 +1,15 @@
+package com.juick.service;
+
+import java.util.List;
+
+/**
+ * Created by vt on 24/11/2016.
+ */
+public interface TelegramService {
+ void addChat(Long id);
+ List<Long> getChats();
+ int getUser(long tgId);
+ boolean createTelegramUser(long tgID, String tgName);
+ List<Long> getSubscribers(int uid);
+ List<Long> getSubscribersToComments(int mid, int ignore_uid);
+}
diff --git a/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java
new file mode 100644
index 00000000..d6fabc96
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java
@@ -0,0 +1,58 @@
+package com.juick.service;
+
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+import javax.inject.Inject;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Created by vt on 24/11/2016.
+ */
+@Repository
+public class TelegramServiceImpl extends BaseJdbcService implements TelegramService {
+ @Inject
+ protected TelegramServiceImpl(JdbcTemplate jdbc) {
+ super(jdbc, null);
+ }
+
+ @Override
+ public void addChat(Long id) {
+ getJdbcTemplate().update("INSERT IGNORE INTO telegram_chats(chat_id) VALUES(?)", id);
+ }
+
+ @Override
+ public List<Long> getChats() {
+ return getJdbcTemplate().queryForList("SELECT chat_id FROM telegram_chats", Long.class);
+ }
+
+ @Override
+ public int getUser(long tgId) {
+ try {
+ return getJdbcTemplate().queryForObject("SELECT id FROM users INNER JOIN telegram " +
+ "ON telegram.user_id = users.id WHERE telegram.tg_id=?", Integer.class, tgId);
+ } catch (EmptyResultDataAccessException e) {
+ return 0;
+ }
+ }
+
+ @Override
+ public boolean createTelegramUser(long tgID, String tgName) {
+ return getJdbcTemplate().update("INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)",
+ tgID, tgName, UUID.randomUUID().toString()) > 0;
+ }
+
+ @Override
+ public List<Long> getSubscribers(int uid) {
+ return getJdbcTemplate().queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_users " +
+ "ON (subscr_users.user_id=? AND telegram.user_id=subscr_users.suser_id)", Long.class, uid);
+ }
+
+ @Override
+ public List<Long> getSubscribersToComments(int mid, int ignore_uid) {
+ return getJdbcTemplate().queryForList("SELECT tg_id FROM telegram INNER JOIN subscr_messages " +
+ "ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid);
+ }
+}
diff --git a/juick-server/src/main/java/com/juick/service/UserService.java b/juick-server/src/main/java/com/juick/service/UserService.java
index bc7742cb..cfd5092a 100644
--- a/juick-server/src/main/java/com/juick/service/UserService.java
+++ b/juick-server/src/main/java/com/juick/service/UserService.java
@@ -48,6 +48,8 @@ public interface UserService {
String getHashByUID(int uid);
+ int getUIDByHttpAuth(String header);
+
int checkPassword(String username, String password);
boolean updatePassword(User user, String newPassword);
diff --git a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java
index 8bd3adef..a84f2fc3 100644
--- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java
+++ b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java
@@ -1,6 +1,7 @@
package com.juick.service;
import com.juick.User;
+import com.juick.server.UserQueries;
import com.juick.server.helpers.Auth;
import com.juick.server.helpers.EmailOpts;
import com.juick.server.helpers.UserInfo;
@@ -259,6 +260,18 @@ public class UserServiceImpl extends BaseJdbcService implements UserService {
return list.get(0);
}
+ @Override
+ public int getUIDByHttpAuth(String auth) {
+ if (auth != null && auth.length() > 8 && auth.startsWith("Basic ")) {
+ Base64.Decoder dec = Base64.getDecoder();
+ String loginpassw[] = new String(dec.decode(auth.substring(6))).split(":", 2);
+ if (loginpassw.length == 2 && loginpassw[0].length() > 1 && loginpassw[0].length() < 16 && loginpassw[0].matches("[a-zA-Z0-9\\-]+") && !loginpassw[1].isEmpty()) {
+ return checkPassword(loginpassw[0], loginpassw[1]);
+ }
+ }
+ return 0;
+ }
+
@Transactional(readOnly = true)
@Override
public int checkPassword(final String username, final String password) {