From d233943fc29508dc37714852b11b96b0b46b55d9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 24 Nov 2016 21:38:52 +0300 Subject: juick-api: now on spring-webmvc --- .../com/juick/service/MessagesServiceImpl.java | 48 +++++++++++------- .../java/com/juick/service/TelegramService.java | 15 ++++++ .../com/juick/service/TelegramServiceImpl.java | 58 ++++++++++++++++++++++ .../main/java/com/juick/service/UserService.java | 2 + .../java/com/juick/service/UserServiceImpl.java | 13 +++++ 5 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/service/TelegramService.java create mode 100644 juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java (limited to 'juick-server/src') 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 getMyFeed(final int uid, final int before) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("before", before); + List 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=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 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 getChats(); + int getUser(long tgId); + boolean createTelegramUser(long tgID, String tgName); + List getSubscribers(int uid); + List 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 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 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 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) { -- cgit v1.2.3