diff options
67 files changed, 1718 insertions, 3408 deletions
diff --git a/juick-server/src/main/java/com/juick/server/AdsQueries.java b/juick-server/src/main/java/com/juick/server/AdsQueries.java deleted file mode 100644 index 06590817..00000000 --- a/juick-server/src/main/java/com/juick/server/AdsQueries.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, ugnich - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * - * @author ugnich - */ -public class AdsQueries { - - public static int getAdMID(JdbcTemplate sql, int uid) { - if (uid > 0) { - try { - return sql.queryForObject("SELECT message_id FROM ads_messages " + - "WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? " + - "AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", - Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } else { - try { - return sql.queryForObject("SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", Integer.class); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - } - - public static void logAdMID(JdbcTemplate sql, int uid, int mid) { - sql.update("INSERT INTO ads_messages_log(user_id,message_id,ts) VALUES (?,?,UNIX_TIMESTAMP())", uid, mid); - } -} diff --git a/juick-server/src/main/java/com/juick/server/CrosspostQueries.java b/juick-server/src/main/java/com/juick/server/CrosspostQueries.java deleted file mode 100644 index 9d0b5f4c..00000000 --- a/juick-server/src/main/java/com/juick/server/CrosspostQueries.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2013, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import com.juick.server.helpers.ApplicationStatus; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Pair; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; - -import java.util.Optional; - -/** - * - * @author ugnich - */ -public class CrosspostQueries { - - public static Optional<Pair<String, String>> getTwitterTokens(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1", - (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), uid); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } - - public static Optional<String> getFacebookToken(JdbcTemplate sql, int uid) { - try { - return Optional.of(sql.queryForObject("SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL " + - "AND crosspost=1", String.class, uid)); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } - public static ApplicationStatus isFBCrossPostEnabled(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT 1, crosspost FROM facebook WHERE user_id=? LIMIT 1", (rs, num) -> { - ApplicationStatus status = new ApplicationStatus(); - status.setConnected(rs.getInt(1) > 0); - status.setCrosspostEnabled(rs.getBoolean(2)); - return status; - }, uid); - } catch (EmptyResultDataAccessException e) { - return new ApplicationStatus(); - } - } - - public static String getTwitterName(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT uname FROM twitter WHERE user_id=?", String.class, uid); - } catch (EmptyResultDataAccessException e) { - return StringUtils.EMPTY; - } - } - - public static String getTelegramName(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT tg_name FROM telegram WHERE user_id=?", String.class, uid); - } catch (EmptyResultDataAccessException e) { - return StringUtils.EMPTY; - } - } - - public static Optional<Pair<String, String>> getVKTokens(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1", - (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), uid); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } -} diff --git a/juick-server/src/main/java/com/juick/server/MessagesQueries.java b/juick-server/src/main/java/com/juick/server/MessagesQueries.java deleted file mode 100644 index f3de1f9b..00000000 --- a/juick-server/src/main/java/com/juick/server/MessagesQueries.java +++ /dev/null @@ -1,660 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.server.helpers.PrivacyOpts; -import com.juick.server.helpers.TagStats; -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.util.StringUtils; - -import java.sql.*; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * - * @author Ugnich Anton - */ -public class MessagesQueries { - - public static 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.setReplyto(rs.getInt(3)); - User user = new User(); - user.setUid(rs.getInt(4)); - user.setName(rs.getString(5)); - user.setBanned(rs.getBoolean(6)); - msg.setUser(user); - msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getTimestamp(8)); - msg.ReadOnly = rs.getBoolean(9); - msg.setPrivacy(rs.getInt(10)); - msg.FriendsOnly = msg.getPrivacy() < 0; - msg.setReplies(rs.getInt(11)); - msg.setAttachmentType(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.setLikes(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.getTags().add(new Tag(t))); - } - msg.setRepliesBy(rs.getString(19)); - msg.setText(rs.getString(20)); - msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(21))); - return msg; - } - } - - public static int createMessage(JdbcTemplate sql, int uid, String txt, String attachment, List<com.juick.Tag> tags) { - KeyHolder holder = new GeneratedKeyHolder(); - sql.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 = org.apache.commons.lang3.StringUtils.EMPTY; - String tagsIDs = org.apache.commons.lang3.StringUtils.EMPTY; - - 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) { - sql.execute("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); - } - final String finalTagsNames = tagsNames; - sql.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; - } - - public static int createReply(JdbcTemplate sql, int mid, int rid, int uid, String txt, String attachment) { - int ridnew = getReplyIDIncrement(sql, mid); - - sql.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) { - sql.update("UPDATE messages SET replies=replies+1 WHERE message_id=?", mid); - } - - return ridnew; - } - - public static int getReplyIDIncrement(JdbcTemplate sql, int mid) { - return sql.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; - }); - - } - - public static boolean recommendMessage(JdbcTemplate sql, int mid, int vuid) { - boolean res = sql.update("INSERT IGNORE INTO favorites(user_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; - if (res) { - sql.update("UPDATE messages SET likes=likes+1 WHERE message_id=?", mid); - } - return res; - } - - public static boolean canViewThread(JdbcTemplate sql, int mid, int uid) { - PrivacyOpts privacyOpts; - try { - privacyOpts = sql.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 - && UserQueries.isInWL(sql, privacyOpts.getUid(), uid)); - } - - public static boolean isReadOnly(JdbcTemplate sql, int mid) { - try { - return sql.queryForObject("SELECT readonly FROM messages WHERE message_id=?", new Object[]{mid}, Integer.class) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static boolean isSubscribed(JdbcTemplate sql, int uid, int mid) { - try { - return sql.queryForObject("SELECT 1 FROM subscr_messages WHERE suser_id=? AND message_id=?", new Object[]{uid, mid}, Integer.class) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static int getMessagePrivacy(JdbcTemplate sql, int mid) { - try { - return sql.queryForObject("SELECT privacy FROM messages WHERE message_id=?", new Object[]{mid}, Integer.class); - } catch (EmptyResultDataAccessException e) { - return -4; - } - } - - - public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { - try { - return sql.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; - } - } - - public static com.juick.Message getReply(JdbcTemplate sql, int mid, int rid) { - try { - return sql.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().setName(rs.getString(2)); - msg.setReplyto(rs.getInt(3)); - msg.setDate(rs.getTimestamp(4)); - msg.setAttachmentType(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; - } - } - - public static User getMessageAuthor(JdbcTemplate sql, int mid) { - try { - return sql.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.setName(rs.getString(2)); - return res; - }); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - public static List<TagStats> getMessageTags(JdbcTemplate sql, int mid) { - return sql.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)", - (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); - TagStats s = new TagStats(); - s.setTag(t); - s.setUsageCount(rs.getInt(4)); - return s; - }, mid); - } - - public static List<Integer> getMessageTagsIDs(JdbcTemplate sql, int mid) { - return sql.queryForList("SELECT tag_id FROM messages_tags WHERE message_id=?", new Object[] {mid}, Integer.class); - } - - public static List<String> getMessageRecommendations(JdbcTemplate sql, int mid) { - return sql.queryForList("SELECT users.nick FROM favorites INNER JOIN users " + - "ON (favorites.message_id=? AND favorites.user_id=users.id)", - new Object[] {mid}, String.class); - } - - public static List<Integer> getAll(JdbcTemplate sql, int visitor_uid, int before) { - if (visitor_uid > 1) { - if (before > 0) { - return sql.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 sql.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 sql.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 sql.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); - } - } - } - - public static List<Integer> getTag(JdbcTemplate sql, int tid, int visitor_uid, int before, int cnt) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getTags(JdbcTemplate sql, String tids, int visitor_uid, int before, int cnt) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getPlace(JdbcTemplate sql, int place_id, int visitor_uid, int before) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getMyFeed(JdbcTemplate sql, int uid, int before) { - List<Integer> mids; - if (before > 0) { - mids = sql.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 = sql.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(sql.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(sql.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; - } - - public static List<Integer> getPrivate(JdbcTemplate sql, int uid, int before) { - - if (before > 0) { - return sql.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 sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy<0 " + - "ORDER BY message_id DESC LIMIT 20", new Object[]{uid}, Integer.class); - } - } - - public static List<Integer> getDiscussions(JdbcTemplate sql, int uid, int before) { - if (before > 0) { - return sql.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 sql.queryForList("SELECT message_id FROM subscr_messages WHERE suser_id=? " + - "ORDER BY message_id DESC LIMIT 20", Integer.class, uid); - } - } - - public static List<Integer> getRecommended(JdbcTemplate sql, int uid, int before) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getPopular(JdbcTemplate sql, int visitor_uid, int before) { - - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getPhotos(JdbcTemplate sql, int visitor_uid, int before) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getSearch(JdbcTemplate sql, JdbcTemplate sqlSearch, String search, 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 sql.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; - } - - public static List<Integer> getUserBlog(JdbcTemplate sql, int UID, int privacy, int before) { - if (before > 0) { - return sql.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 sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + - " ORDER BY message_id DESC LIMIT 20", Integer.class, UID); - } - } - - public static List<Integer> getUserTag(JdbcTemplate sql, int UID, int TID, int privacy, int before) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getUserRecommendations(JdbcTemplate sql, int UID, int before) { - if (before > 0) { - return sql.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 sql.queryForList("SELECT message_id FROM favorites " - + "WHERE user_id=? ORDER BY message_id DESC LIMIT 20", - Integer.class, UID); - } - } - - public static List<Integer> getUserPhotos(JdbcTemplate sql, int UID, int privacy, int before) { - if (before > 0) { - return sql.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 sql.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); - } - } - - public static List<Integer> getUserSearch(JdbcTemplate sql, JdbcTemplate sqlSearch, int UID, String search, int privacy, 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 sql.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; - } - - public static List<com.juick.Message> getMessages(JdbcTemplate sql, List<Integer> mids) { - if (!mids.isEmpty()) { - return sql.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(); - } - - public static List<com.juick.Message> getReplies(JdbcTemplate sql, int mid) { - return sql.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); - } - - public static boolean setMessagePopular(JdbcTemplate sql, int mid, int popular) { - boolean ret; - switch (popular) { - case -2: - ret = sql.update("UPDATE messages SET hidden=1 WHERE message_id=?", mid) > 0; - break; - case -1: - ret = sql.update("UPDATE messages SET popular=0 WHERE message_id=?", mid) > 0; - break; - default: - ret = sql.update("UPDATE messages SET popular=? WHERE message_id=?", popular, mid) > 0; - break; - } - if (popular == -1) { - ret = sql.update("INSERT INTO top_ignore_messages VALUES (?)", mid) > 0; - } - return ret; - } - - public static boolean setMessagePrivacy(JdbcTemplate sql, int mid) { - return sql.update("UPDATE messages SET privacy=1 WHERE message_id=?", - mid) > 0; - } - public static boolean deleteMessage(JdbcTemplate sql, int uid, int mid) { - if (sql.update("DELETE FROM messages WHERE message_id=? AND user_id=?", mid, uid) > 0) { - sql.update("DELETE FROM messages_txt WHERE message_id=?", mid); - sql.update("DELETE FROM replies WHERE message_id=?", mid); - sql.update("DELETE FROM subscr_messages WHERE message_id=?", mid); - sql.update("DELETE FROM messages_tags WHERE message_id=?", mid); - return true; - } - return false; - } -} diff --git a/juick-server/src/main/java/com/juick/server/PMQueries.java b/juick-server/src/main/java/com/juick/server/PMQueries.java deleted file mode 100644 index c79c04b6..00000000 --- a/juick-server/src/main/java/com/juick/server/PMQueries.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import com.juick.User; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; - -import java.util.List; - -/** - * - * @author Ugnich Anton - */ -public class PMQueries { - - public static boolean createPM(JdbcTemplate sql, int uid_from, int uid_to, String body) { - boolean ret = sql.update("INSERT INTO pm(user_id,user_id_to,txt) VALUES (?,?,?)", - uid_from, uid_to, body) > 0; - - if (ret) { - sql.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; - } - - public static boolean addPMinRoster(JdbcTemplate sql, int uid, String jid) { - return sql.update("INSERT INTO pm_inroster(user_id,jid) VALUES (?,?)", - uid, jid) > 0; - } - - public static boolean removePMinRoster(JdbcTemplate sql, int uid, String jid) { - return sql.update("DELETE FROM pm_inroster WHERE user_id=? AND jid=?", uid, jid) > 0; - } - - public static boolean havePMinRoster(JdbcTemplate sql, int uid, String jid) { - List<Integer> res = sql.queryForList("SELECT 1 FROM pm_inroster " - + "WHERE user_id=? AND jid=?", Integer.class, - uid, jid); - return res.size() > 0; - } - - public static String getLastView(JdbcTemplate sql, int uid_from, int uid_to) { - try { - return sql.queryForObject("SELECT lastview FROM pm_streams WHERE user_id=? AND user_id_to=?", String.class, - uid_from, uid_to); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - public static List<User> getPMLastConversationsUsers(JdbcTemplate sql, int uid, int cnt) { - return sql.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.setName(rs.getString(2)); - u.setUnreadCount(rs.getInt(3)); - return u; - }, uid); - } - - public static List<com.juick.Message> getPMMessages(JdbcTemplate sql, int uid, int uid_to) { - List<com.juick.Message> msgs = sql.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); - - sql.update("UPDATE pm_streams SET lastview=NOW(),unread=0 " - + "WHERE user_id_to=? AND user_id=?", uid, uid_to); - - return msgs; - } - - public static List<com.juick.Message> getLastPMInbox(JdbcTemplate sql, int uid) { - return sql.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().setName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - return msg; - }, uid); - } - - public static List<com.juick.Message> getLastPMSent(JdbcTemplate sql, int uid) { - return sql.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().setName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - return msg; - }, uid); - } -} diff --git a/juick-server/src/main/java/com/juick/server/PrivacyQueries.java b/juick-server/src/main/java/com/juick/server/PrivacyQueries.java deleted file mode 100644 index 40fc82c4..00000000 --- a/juick-server/src/main/java/com/juick/server/PrivacyQueries.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.juick.server; - -import com.juick.Tag; -import com.juick.User; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * Created by vitalyster on 19.10.2016. - */ -public class PrivacyQueries { - public enum PrivacyResult { - Removed, Added - } - public static PrivacyResult blacklistUser(JdbcTemplate jdbc, User user, User target) { - int result = jdbc.update("DELETE FROM bl_users WHERE user_id=? AND bl_user_id=?", user.getUid(), target.getUid()); - if (result > 0) { - return PrivacyResult.Removed; - } else { - jdbc.update("INSERT INTO bl_users(user_id,bl_user_id) VALUES (?,?)", user.getUid(), target.getUid()); - return PrivacyResult.Added; - } - } - public static PrivacyResult blacklistTag(JdbcTemplate jdbc, User user, Tag tag) { - int result = jdbc.update("DELETE FROM bl_tags WHERE user_id=? AND tag_id=?", user.getUid(), tag.TID); - if (result > 0) { - return PrivacyResult.Removed; - } else { - jdbc.update("INSERT INTO bl_tags(user_id,tag_id) VALUES (?,?)", user.getUid(), tag.TID); - return PrivacyResult.Added; - } - } -} diff --git a/juick-server/src/main/java/com/juick/server/ShowQueries.java b/juick-server/src/main/java/com/juick/server/ShowQueries.java deleted file mode 100644 index fde388c3..00000000 --- a/juick-server/src/main/java/com/juick/server/ShowQueries.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.juick.server; - -import com.juick.User; - -import java.util.List; -import org.springframework.jdbc.core.JdbcTemplate; - -/** - * Created by vt on 10/01/16. - */ -public class ShowQueries { - - public static List<String> getRecommendedUsers(JdbcTemplate sql, User forUser) { - return sql.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()); - } - - public static List<String> getTopUsers(JdbcTemplate sql) { - return sql.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/juick-server/src/main/java/com/juick/server/SubscriptionsQueries.java b/juick-server/src/main/java/com/juick/server/SubscriptionsQueries.java deleted file mode 100644 index b6cfbbd0..00000000 --- a/juick-server/src/main/java/com/juick/server/SubscriptionsQueries.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package com.juick.server; - -import com.juick.Tag; -import com.juick.User; -import com.juick.server.helpers.NotifyOpts; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.util.StringUtils; - -import java.util.*; -import java.util.stream.Collectors; - -/** - * - * @author ugnich - */ -public class SubscriptionsQueries { - - public static List<String> getJIDSubscribedToUser(JdbcTemplate sql, int uid, boolean friendsonly) { - if (friendsonly == false) { - return sql.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 sql.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); - } - } - - public static List<User> getSubscribedUsers(JdbcTemplate sql, int uid, int mid) { - User author = MessagesQueries.getMessageAuthor(sql, mid); - List<User> userids = UserQueries.getUserReaders(sql, uid); - Set<Integer> set = new HashSet<>(); - set.addAll(userids.stream().map(User::getUid).collect(Collectors.toList())); - List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, mid); - if (tags.size() > 0) { - String tagsStr = StringUtils.arrayToCommaDelimitedString(tags.toArray()); - List<Integer> tagUsers = sql.queryForList("SELECT suser_id FROM subscr_tags " + - "WHERE tag_id IN (" + tagsStr + ") AND suser_id!=? " + - " AND suser_id NOT IN (SELECT user_id FROM bl_users WHERE bl_user_id=?)" + - " AND suser_id NOT IN (SELECT user_id FROM bl_tags WHERE tag_id IN (" + tagsStr + "))", Integer.class, uid, author.getUid()); - set.addAll(tagUsers); - } - return UserQueries.getUsersByID(sql, new ArrayList<>(set)); - } - - public static List<User> getUsersSubscribedToComments(JdbcTemplate sql, int mid, int ignore_uid) { - List<Integer> userids = sql.queryForList("SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?", - Integer.class, mid, ignore_uid); - if (userids.size() > 0) { - return UserQueries.getUsersByID(sql, userids); - } else { - return Collections.emptyList(); - } - } - - public static List<User> getUsersSubscribedToUserRecommendations(JdbcTemplate sql, int uid, int mid, int muid) { - List<Integer> tags = MessagesQueries.getMessageTagsIDs(sql, 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 = sql.queryForList(query, Integer.class); - return UserQueries.getUsersByID(sql, userids); - } - - public static boolean subscribeMessage(JdbcTemplate sql, int mid, int vuid) { - return sql.update("INSERT IGNORE INTO subscr_messages(suser_id,message_id) VALUES (" + vuid + "," + mid + ")") == 1; - } - public static boolean unSubscribeMessage(JdbcTemplate sql, int mid, int vuid) { - return sql.update("DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", - mid, vuid) > 0; - } - public static boolean subscribeUser(JdbcTemplate sql, User user, User toUser) { - return sql.update("INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", - toUser.getUid(), user.getUid()) == 1; - } - public static boolean unSubscribeUser(JdbcTemplate sql, User user, User fromUser) { - return sql.update("DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", - user.getUid(), fromUser.getUid()) > 0; - } - public static boolean subscribeTag(JdbcTemplate sql, User user, Tag toTag) { - return sql.update("INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", - toTag.TID, user.getUid()) == 1; - } - public static boolean unSubscribeTag(JdbcTemplate sql, User user, Tag toTag) { - return sql.update("DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", - toTag.TID, user.getUid()) > 0; - } - - public static NotifyOpts getNotifyOptions(JdbcTemplate sql, User user) { - try { - return sql.queryForObject("SELECT jnotify,subscr_notify,recommendations FROM useroptions WHERE user_id=?", - (rs, num) -> { - NotifyOpts options = new NotifyOpts(); - options.setRepliesEnabled(rs.getInt(1) > 0); - options.setSubscriptionsEnabled(rs.getInt(2) > 0); - options.setRecommendationsEnabled(rs.getInt(3) > 0); - return options; - }, user.getUid()); - } catch (EmptyResultDataAccessException e) { - return new NotifyOpts(); - } - } - - public static boolean setNotifyOptions(JdbcTemplate sql, User user, NotifyOpts options) { - return sql.update("UPDATE useroptions SET jnotify=? WHERE user_id=?", options.isRepliesEnabled() ? 1 : 0, - user.getUid()) > 0 && - sql.update("UPDATE useroptions SET subscr_notify=? WHERE user_id=?", options.isSubscriptionsEnabled() ? 1 : 0, - user.getUid()) > 0 && - sql.update("UPDATE useroptions SET recommendations=? WHERE user_id=?", options.isRecommendationsEnabled() ? 1 : 0, - user.getUid()) > 0; - } -} diff --git a/juick-server/src/main/java/com/juick/server/TagQueries.java b/juick-server/src/main/java/com/juick/server/TagQueries.java deleted file mode 100644 index 6d416a37..00000000 --- a/juick-server/src/main/java/com/juick/server/TagQueries.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import com.juick.Tag; -import com.juick.server.helpers.TagStats; -import org.apache.commons.lang3.StringEscapeUtils; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; - -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; - -/** - * - * @author Ugnich Anton - */ -public class TagQueries { - - public static com.juick.Tag getTag(JdbcTemplate sql, int tid) { - try { - return sql.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; - } - } - - public static com.juick.Tag getTag(JdbcTemplate sql, String tag, boolean autoCreate) { - Tag ret = null; - try { - ret = sql.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(sql, tag); - } - - return ret; - } - - public static List<com.juick.Tag> getTags(JdbcTemplate sql, String[] tags, boolean autoCreate) { - List<Tag> ret = new ArrayList<>(); - - for (String tag : tags) { - if (!tag.isEmpty()) { - Tag t = getTag(sql, tag, autoCreate); - if (t != null) { - ret.add(t); - } - } - } - - return ret; - } - - public static boolean getTagNoIndex(JdbcTemplate sql, int tag_id) { - try { - return sql.queryForObject("SELECT noindex FROM tags WHERE tag_id=?", Integer.class, tag_id) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static int createTag(JdbcTemplate sql, String name) { - KeyHolder holder = new GeneratedKeyHolder(); - sql.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(); - } - - public static List<TagStats> getTagsStats(JdbcTemplate sql, int uid) { - return sql.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) -> { - TagStats s = new TagStats(); - s.setTag(new Tag(StringEscapeUtils.unescapeHtml4(rs.getString(1)))); - s.setUsageCount(rs.getInt(2)); - return s; - }, uid); - } - - public static List<String> getUserBLTags(JdbcTemplate sql, int uid) { - return sql.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); - } - - public static List<String> getPopularTags(JdbcTemplate sql) { - return sql.queryForList("SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class).stream() - .map(StringEscapeUtils::unescapeHtml4).collect(Collectors.toList()); - } - public static List<Tag> updateTags(JdbcTemplate sql, int mid, List<Tag> newTags) { - List<Tag> currentTags = MessagesQueries.getMessageTags(sql, mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); - newTags.stream().filter(currentTags::contains) - .forEach(t -> sql.update("DELETE FROM messages_tags WHERE message_id=? AND tag_id=?", mid, t.TID)); - newTags.stream().filter(t -> !currentTags.contains(t)) - .forEach(t -> sql.update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID)); - return MessagesQueries.getMessageTags(sql, mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); - } - - public static List<Tag> fromString(JdbcTemplate sql, String txt, boolean tagsOnly) { - String patternString = tagsOnly ? "^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$" : "^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"; - Pattern tagsPattern = Pattern.compile(patternString); - if (tagsPattern.matcher(txt).matches()) { - Pattern tagPattern = Pattern.compile("\\*([^ \\r\\n\\t]+)"); - Matcher tagMatcher = tagPattern.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(sql, tagMatcher.group(i), true)); - } - } - return tags; - } - return Collections.emptyList(); - } -} diff --git a/juick-server/src/main/java/com/juick/server/UserQueries.java b/juick-server/src/main/java/com/juick/server/UserQueries.java deleted file mode 100644 index 7a669230..00000000 --- a/juick-server/src/main/java/com/juick/server/UserQueries.java +++ /dev/null @@ -1,487 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.server; - -import com.juick.User; -import com.juick.server.helpers.Auth; -import com.juick.server.helpers.EmailOpts; -import com.juick.server.helpers.UserInfo; -import com.juick.util.UserUtils; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.dao.EmptyResultDataAccessException; -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.util.StringUtils; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.*; - -/** - * @author Ugnich Anton - */ -public class UserQueries { - - static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - public static 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.setName(rs.getString(2)); - user.setBanned(rs.getBoolean(3)); - return user; - } - } - - public static String getSignUpHashByJID(JdbcTemplate sql, String jid) { - String hash; - try { - hash = sql.queryForObject("SELECT loginhash FROM jids WHERE jid=? AND user_id IS NULL", - String.class, jid); - } catch (EmptyResultDataAccessException e) { - hash = UUID.randomUUID().toString(); - sql.update("INSERT INTO jids(jid,loginhash) VALUES (?,?)", jid, hash); - } - return hash; - } - - public static String getSignUpHashByTelegramID(JdbcTemplate sql, Long telegramId, String username) { - try { - return sql.queryForObject("SELECT loginhash FROM telegram WHERE tg_id=? AND user_id IS NULL", - String.class, telegramId); - } catch (EmptyResultDataAccessException e) { - String hash = UUID.randomUUID().toString(); - sql.update("INSERT INTO telegram(tg_id, loginhash, tg_name) VALUES (?, ?, ?)", telegramId, hash, username); - return hash; - } - } - - public static int createUser(JdbcTemplate sql, String username, String password) { - KeyHolder holder = new GeneratedKeyHolder(); - try { - sql.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(); - - sql.update("INSERT INTO useroptions(user_id) VALUES (?)", uid); - sql.update("INSERT INTO subscr_users(user_id,suser_id) VALUES (2,?)", uid); - - return uid; - } - - public static Optional<User> getUserByUID(JdbcTemplate sql, int uid) { - try { - return Optional.of(sql.queryForObject("SELECT id, nick,banned FROM users WHERE id=?", - new UserMapper(), uid)); - } catch (EmptyResultDataAccessException e) { - return Optional.empty(); - } - } - - public static User getUserByName(JdbcTemplate sql, String username) { - try { - return sql.queryForObject("SELECT id,nick,banned FROM users WHERE nick=?", - new UserMapper(), - username); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - public static User getUserByJID(JdbcTemplate sql, String jid) { - try { - return sql.queryForObject("SELECT id,nick,banned FROM users WHERE id=(SELECT user_id FROM jids WHERE jid=?)", - new UserMapper(), jid); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - public static List<User> getUsersByName(JdbcTemplate sql, List<String> unames) { - if (!unames.isEmpty()) { - return sql.query("SELECT id,nick,banned FROM users WHERE nick IN (\"" + StringUtils.arrayToDelimitedString(unames.toArray(), "\",\"") + "\")", - new UserMapper()); - } - return Collections.emptyList(); - } - - public static List<User> getUsersByID(JdbcTemplate sql, List<Integer> uids) { - if (!uids.isEmpty()) { - return sql.query("SELECT id,nick,banned FROM users WHERE id IN (" + StringUtils.arrayToCommaDelimitedString(uids.toArray()) + ")", - new UserMapper()); - } - return Collections.emptyList(); - } - - public static List<com.juick.User> getUsersByJID(JdbcTemplate sql, List<String> jids) { - if (!jids.isEmpty()) { - return sql.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.setName(rs.getString(2)); - user.setJid(rs.getString(3)); - return user; - }); - } - return Collections.emptyList(); - } - - public static List<String> getJIDsbyUID(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT jid FROM jids WHERE user_id=? AND active=1", String.class, uid); - } - - public static int getUIDbyJID(JdbcTemplate sql, String jid) { - try { - return sql.queryForObject("SELECT user_id FROM jids WHERE jid=?", Integer.class, jid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static int getUIDbyName(JdbcTemplate sql, String uname) { - try { - return sql.queryForObject("SELECT id FROM users WHERE nick=?", Integer.class, uname); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static int getUIDbyHash(JdbcTemplate sql, String hash) { - try { - return sql.queryForObject("SELECT user_id FROM logins WHERE hash=?", Integer.class, hash); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static com.juick.User getUserByHash(JdbcTemplate sql, String hash) { - try { - User user = sql.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(); - } - } - - public static String getHashByUID(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); - } catch (EmptyResultDataAccessException e) { - String hash = UserUtils.generateHash(16); - sql.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; - } - } - - public static int checkPassword(JdbcTemplate sql, String username, String password) { - try { - String realPassword = sql.queryForObject("SELECT passw FROM users WHERE nick=?", String.class, username); - if (realPassword.equals(password)) { - User user = UserQueries.getUserByName(sql, username); - if (user != null) { - return user.getUid(); - } else { - return -1; - } - } else { - return -1; - } - } catch (EmptyResultDataAccessException e) { - return -1; - } - } - - public static boolean updatePassword(JdbcTemplate sql, User user, String newPassword) { - return user.getUid() > 0 && sql.update("UPDATE users SET passw=? WHERE id=?", newPassword, user.getUid()) > 0; - } - - public static String updateSecretEmail(JdbcTemplate sql, User user) { - String newHash = UserUtils.generateHash(16); - if (sql.update("INSERT INTO mail(user_id,hash) VALUES (?,?) ON DUPLICATE KEY UPDATE hash=?", user.getUid(), newHash, newHash) > 0) { - return newHash; - } - return org.apache.commons.lang3.StringUtils.EMPTY; - } - - public static int getUserOptionInt(JdbcTemplate sql, int uid, String option, int defaultValue) { - try { - return sql.queryForObject("SELECT " + option + " FROM useroptions WHERE user_id=?", Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return defaultValue; - } - } - - public static void setUserOptionInt(JdbcTemplate sql, int uid, String option, int value) { - sql.update("UPDATE useroptions SET " + option + "=? WHERE user_id=?", value, uid); - } - - public static UserInfo getUserInfo(JdbcTemplate sql, User user) { - try { - return sql.queryForObject("SELECT fullname,country,url,descr FROM usersinfo WHERE user_id=?", ((rs, rowNum) -> { - UserInfo info = new UserInfo(); - info.setFullName(rs.getString(1)); - info.setCountry(rs.getString(2)); - info.setUrl(rs.getString(3)); - info.setDescription(rs.getString(4)); - return info; - }), user.getUid()); - } catch (EmptyResultDataAccessException e) { - return new UserInfo(); - } - } - - public static boolean updateUserInfo(JdbcTemplate sql, User user, UserInfo info) { - return sql.update("INSERT INTO usersinfo(user_id,fullname,country,url,descr) VALUES (?,?,?,?,?) " + - "ON DUPLICATE KEY UPDATE fullname=?,country=?,url=?,descr=?", user.getUid(), info.getFullName(), - info.getCountry(), info.getUrl(), info.getDescription(), info.getFullName(), - info.getCountry(), info.getUrl(), info.getDescription()) > 0; - } - - public static boolean getCanMedia(JdbcTemplate sql, int uid) { - try { - int res = sql.queryForObject("SELECT users.lastphoto-UNIX_TIMESTAMP() FROM users WHERE id=?", - Integer.class, uid); - return res < 3600; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static boolean isInWL(JdbcTemplate sql, int uid, int check) { - try { - return sql.queryForObject("SELECT 1 FROM wl_users WHERE user_id=? AND wl_user_id=?", - Integer.class, uid, check) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static boolean isInBL(JdbcTemplate sql, int uid, int check) { - try { - return sql.queryForObject("SELECT 1 FROM bl_users WHERE user_id=? AND bl_user_id=?", - Integer.class, uid, check) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static boolean isInBLAny(JdbcTemplate sql, int uid, int uid2) { - try { - return sql.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; - } - } - - public static List<Integer> checkBL(JdbcTemplate sql, int visitor, List<Integer> uids) { - if (!uids.isEmpty()) { - return sql.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<>(); - } - } - - public static boolean isSubscribed(JdbcTemplate sql, int uid, int check) { - try { - return sql.queryForObject("SELECT 1 FROM subscr_users WHERE suser_id=? AND user_id=?", - Integer.class, uid, check) == 1; - } catch (EmptyResultDataAccessException e) { - return false; - } - } - - public static List<Integer> getUserRead(JdbcTemplate sql, int uid) { - return sql.queryForList("SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid); - } - - public static List<com.juick.User> getUserReadLeastPopular(JdbcTemplate sql, int uid, int cnt) { - return sql.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.setName(rs.getString(2)); - return u; - }, uid, cnt); - } - - public static List<User> getUserReaders(JdbcTemplate sql, int uid) { - return sql.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.setName(rs.getString(2)); - return u; - }, uid); - } - - public static List<User> getUserFriends(JdbcTemplate sql, int uid) { - return sql.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.setName(rs.getString(2)); - return u; - }, uid, uid); - } - - public static List<com.juick.User> getUserBLUsers(JdbcTemplate sql, int uid) { - return sql.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.setName(rs.getString(2)); - return u; - }, uid); - } - - public static boolean linkTwitterAccount(JdbcTemplate sql, User user, String accessToken, - String accessTokenSecret, String screenName) { - if (sql.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 sql.update("INSERT INTO subscr_users(user_id,suser_id,jid) " + - "VALUES (?,1741,'juick\\@twitter.juick.com')", user.getUid()) > 0; - } - return false; - - } - - public static int getStatsIRead(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT COUNT(*) FROM subscr_users WHERE suser_id=?", Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static int getStatsMyReaders(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT COUNT(*) FROM subscr_users WHERE user_id=?", Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static int getStatsMessages(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT COUNT(*) FROM messages WHERE user_id=?", Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public static int getStatsReplies(JdbcTemplate sql, int uid) { - try { - return sql.queryForObject("SELECT COUNT(*) FROM replies WHERE user_id=?", Integer.class, uid); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - public enum ActiveStatus { - Inactive, - Active - } - - public static boolean setActiveStatusForJID(JdbcTemplate sql, String JID, ActiveStatus jidStatus) { - User user = getUserByJID(sql, JID); - if (user != null) { - return sql.update(con -> { - PreparedStatement preparedStatement = con.prepareStatement( - "UPDATE jids SET active=? WHERE user_id=? AND jid=?"); - int newStatus = jidStatus == ActiveStatus.Active ? 1 : 0; - preparedStatement.setInt(1, newStatus); - preparedStatement.setInt(2, user.getUid()); - preparedStatement.setString(3, JID); - return preparedStatement; - - }) >= 0; - } - return false; - } - - public static List<String> getAllJIDs(JdbcTemplate sql, User user) { - return sql.queryForList("SELECT jid FROM jids WHERE user_id=?", String.class, user.getUid()); - } - - public static List<Auth> getAuthCodes(JdbcTemplate sql, User user) { - return sql.query("SELECT account,authcode FROM auth WHERE user_id=? AND protocol='xmpp'", - (rs, num) -> new Auth(rs.getString(1), rs.getString(2)), user.getUid()); - } - - public static List<String> getEmails(JdbcTemplate sql, User user) { - return sql.queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUid()); - } - - public static EmailOpts getEmailOpts(JdbcTemplate sql, User user) { - try { - return sql.queryForObject("SELECT email,subscr_hour FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", - (rs, num) -> new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUid()); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - public static String getEmailHash(JdbcTemplate sql, User user) { - try { - return sql.queryForObject("SELECT hash FROM mail WHERE user_id=?", String.class, user.getUid()) - + "@mail.juick.com"; - } catch (EmptyResultDataAccessException e) { - return org.apache.commons.lang3.StringUtils.EMPTY; - } - } -} diff --git a/juick-server/src/main/java/com/juick/service/CrosspostService.java b/juick-server/src/main/java/com/juick/service/CrosspostService.java index 725ef297..812d798f 100644 --- a/juick-server/src/main/java/com/juick/service/CrosspostService.java +++ b/juick-server/src/main/java/com/juick/service/CrosspostService.java @@ -29,4 +29,32 @@ public interface CrosspostService { Optional<Pair<String, String>> getVkTokens(int uid); void deleteVKUser(Integer uid); + + int getUIDbyFBID(long fbID); + + boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink); + + boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink); + + int getUIDbyVKID(long vkID); + + boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink); + + boolean setUserRef(int uid, int ref); + + String getFacebookNameByHash(String hash); + + String getTelegramNameByHash(String hash); + + boolean setFacebookUser(String hash, int uid); + + String getVKNameByHash(String hash); + + boolean setVKUser(String hash, int uid); + + boolean setTelegramUser(String hash, int uid); + + String getJIDByHash(String hash); + + boolean setJIDUser(String hash, int uid); } diff --git a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java b/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java index df748526..4b3bd618 100644 --- a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -3,6 +3,7 @@ package com.juick.service; import com.juick.server.helpers.ApplicationStatus; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -129,4 +130,123 @@ public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostSe public void deleteVKUser(Integer uid) { getJdbcTemplate().update("DELETE FROM vk WHERE user_id=?", uid); } + + @Transactional(readOnly = true) + @Override + public int getUIDbyFBID(long fbID) { + try { + return getJdbcTemplate().queryForObject("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL", + Integer.class, fbID); + } catch (EmptyResultDataAccessException e) { + return 0; + } + } + + @Transactional + @Override + public boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink) { + return getJdbcTemplate().update("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)", + fbID, loginhash, token, fbName, fbLink) > 0; + } + + @Transactional + @Override + public boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink) { + return getJdbcTemplate().update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?", + token, fbName, fbLink, fbID) > 0; + } + + @Transactional(readOnly = true) + @Override + public int getUIDbyVKID(long vkID) { + try { + return getJdbcTemplate().queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID); + } catch (EmptyResultDataAccessException e) { + return 0; + } + } + + @Transactional + @Override + public boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink) { + return getJdbcTemplate().update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)", + vkID, loginhash, token, vkName, vkLink) > 0; + } + + @Transactional + @Override + public boolean setUserRef(int uid, int ref) { + return getJdbcTemplate().update("INSERT INTO users_refs(user_id,ref) VALUES (?,?)", uid, ref) > 0; + } + + @Transactional(readOnly = true) + @Override + public String getFacebookNameByHash(String hash) { + try { + List<Pair<String, String>> fb = getJdbcTemplate().query("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?", + (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash); + if (fb.size() > 0) { + return "<a href=\"" + fb.get(0).getRight() + "\" rel=\"nofollow\">" + fb.get(0).getLeft() + "</a>"; + } + return null; + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + @Transactional + @Override + public String getTelegramNameByHash(String hash) { + try { + String name = getJdbcTemplate().queryForObject("SELECT tg_name FROM telegram WHERE loginhash=?", String.class, hash); + return "<a href=\"https://telegram.me/" + name + "\" rel=\"nofollow\">" + name + "</a>"; + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + @Transactional + @Override + public boolean setFacebookUser(String hash, int uid) { + return getJdbcTemplate().update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; + } + + @Transactional + @Override + public String getVKNameByHash(String hash) { + List<Pair<String, String>> logins = getJdbcTemplate().query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?", + (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash); + if (logins.size() > 0) { + return "<a href=\"http://vk.com/" + logins.get(0).getRight() + "\" rel=\"nofollow\">" + logins.get(0).getLeft() + "</a>"; + } + return null; + } + + @Transactional + @Override + public boolean setVKUser(String hash, int uid) { + return getJdbcTemplate().update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; + } + + @Transactional + @Override + public boolean setTelegramUser(String hash, int uid) { + return getJdbcTemplate().update("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; + } + + @Transactional(readOnly = true) + @Override + public String getJIDByHash(String hash) { + try { + return getJdbcTemplate().queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + @Transactional + @Override + public boolean setJIDUser(String hash, int uid) { + return getJdbcTemplate().update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; + } } diff --git a/juick-server/src/main/java/com/juick/service/MessagesService.java b/juick-server/src/main/java/com/juick/service/MessagesService.java index 8cb0782f..284b2018 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesService.java +++ b/juick-server/src/main/java/com/juick/service/MessagesService.java @@ -66,7 +66,7 @@ public interface MessagesService { List<Integer> getUserPhotos(int uid, int privacy, int before); - List<Integer> getUserSearch(JdbcTemplate sqlSearch, int UID, String search, int privacy, int before); + List<Integer> getUserSearch(int UID, String search, int privacy, int before); List<com.juick.Message> getMessages(Collection<Integer> mids); 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 1ad29d00..736919a4 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -627,7 +627,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ @Transactional(readOnly = true) @Override - public List<Integer> getUserSearch(final JdbcTemplate sqlSearch, final int UID, final String search, final int privacy, final int before) { + public List<Integer> getUserSearch(final int UID, final String search, final int privacy, final int before) { List<Integer> mids = searchService.searchByStringAndUser(search, UID, before); if (!mids.isEmpty()) { diff --git a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java index 77100f1e..66566872 100644 --- a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java @@ -103,7 +103,7 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } } - return ret; + return ret.stream().distinct().collect(Collectors.toList()); } @Transactional(readOnly = true) 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 3fb2ae12..166cfeeb 100644 --- a/juick-server/src/main/java/com/juick/service/UserService.java +++ b/juick-server/src/main/java/com/juick/service/UserService.java @@ -115,4 +115,10 @@ public interface UserService { int deleteLoginForUser(String name); int setLoginForUser(int uid, String loginHash); + + void logout(int uid); + + boolean deleteJID(int uid, String jid); + + boolean unauthJID(int uid, String jid); } 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 f226b402..05ef4c95 100644 --- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java @@ -674,4 +674,23 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { .addValue("hash", loginHash) .addValue("uid", uid)); } + + @Transactional + @Override + public void logout(int uid) { + getJdbcTemplate().update("DELETE FROM logins WHERE user_id=?", uid); + } + + @Transactional + @Override + public boolean deleteJID(int uid, String jid) { + return getJdbcTemplate().update("DELETE FROM jids WHERE user_id=? AND jid=?", uid, jid) > 0; + } + + @Transactional + @Override + public boolean unauthJID(int uid, String jid) { + return getJdbcTemplate() + .update("DELETE FROM auth WHERE user_id=? AND protocol='xmpp' AND account=?", uid, jid) > 0; + } } diff --git a/juick-www/build.gradle b/juick-www/build.gradle index d3a6beba..8d081515 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -26,6 +26,7 @@ dependencies { compile 'com.mitchellbosecke:pebble:2.3.0' compile 'com.sun.mail:javax.mail:1.5.6' compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" + compile "org.springframework:spring-context-support:${rootProject.springFrameworkVersion}" compile 'com.mitchellbosecke:pebble-spring4:2.3.0' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' providedRuntime 'mysql:mysql-connector-java:5.1.40' diff --git a/juick-www/src/main/java/com/juick/www/Errors.java b/juick-www/src/main/java/com/juick/www/Errors.java deleted file mode 100644 index 75fdddde..00000000 --- a/juick-www/src/main/java/com/juick/www/Errors.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.juick.www; - -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import org.springframework.jdbc.core.JdbcTemplate; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.Map; - -/** - * - * @author ugnich - */ -public class Errors { - - public static void doGet404(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - response.setStatus(HttpServletResponse.SC_NOT_FOUND); - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/404.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", "404 Страница не найдена"); - context.put("visitor", visitor); - template.evaluate(out, context); - } catch (PebbleException e) { - // log("pebble exception", e); - } - } -} diff --git a/juick-www/src/main/java/com/juick/www/Help.java b/juick-www/src/main/java/com/juick/www/Help.java deleted file mode 100644 index 301d2edc..00000000 --- a/juick-www/src/main/java/com/juick/www/Help.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.www; - -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import org.springframework.jdbc.core.JdbcTemplate; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; - -/** - * - * @author Ugnich Anton - */ -public class Help { - - protected void doRedirectToHelpIndex(HttpServletResponse response) throws ServletException, IOException { - Utils.sendTemporaryRedirect(response, "/help/ru/"); - } - - protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException, URISyntaxException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - String path[] = request.getRequestURI().split("/"); - String page; - if (path.length < 3 || path.length > 4 || path[2].length() != 2 || !path[2].matches("^[a-z]+$")) { - Errors.doGet404(sql, request, response); - return; - } - - if (path.length == 4) { - page = path[3]; - if (!page.matches("^[a-zA-Z0-9\\-]*$") || page.equals("navigation") || page.equals("index")) { - Errors.doGet404(sql, request, response); - return; - } - } else { - page = "index"; - } - - URL f = getClass().getResource("/help/" + path[2] + "/" + page); - if (f == null) { - Errors.doGet404(sql, request, response); - return; - } - - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/help.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", "Помощь"); - context.put("visitor", visitor); - context.put("content", new String(Files.readAllBytes(Paths.get(f.toURI())))); - context.put("navigation", new String(Files.readAllBytes( - new File(getClass().getResource("/help/" + path[2] + "/navigation").toURI()).toPath()))); - template.evaluate(out, context); - } - } -} diff --git a/juick-www/src/main/java/com/juick/www/HelpService.java b/juick-www/src/main/java/com/juick/www/HelpService.java new file mode 100644 index 00000000..8ece4cfb --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/HelpService.java @@ -0,0 +1,48 @@ +package com.juick.www; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.cache.annotation.Cacheable; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; + +/** + * Created by aalexeev on 12/11/16. + */ +public class HelpService { + private static final Pattern LANG_PATTERN = Pattern.compile("[a-z]{2}"); + + private static final Pattern PAGE_PATTERN = Pattern.compile("[a-zA-Z0-9\\-_]+"); + + private final String helpPath; + + + public HelpService(String helpPath) { + this.helpPath = helpPath; + } + + @Cacheable("help") + public String getHelp(final String page, final String lang) { + if (canBePage(page) && canBeLang(lang)) { + String path = StringUtils.joinWith("/", helpPath, lang, page); + + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { + if (is != null) + return IOUtils.toString(is, StandardCharsets.UTF_8); + } catch (IOException e) { + } + } + return null; + } + + public boolean canBePage(final String anything) { + return anything != null && PAGE_PATTERN.matcher(anything).matches(); + } + + public boolean canBeLang(final String anything) { + return anything != null && LANG_PATTERN.matcher(anything).matches(); + } +} diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java deleted file mode 100644 index 4fb13290..00000000 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ /dev/null @@ -1,396 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.www; - -import com.juick.Message; -import com.juick.server.UserQueries; -import com.mitchellbosecke.pebble.error.PebbleException; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.CharEncoding; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.math.NumberUtils; -import org.springframework.jdbc.core.JdbcTemplate; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.XmppException; -import rocks.xmpp.core.session.Extension; -import rocks.xmpp.core.session.XmppSessionConfiguration; -import rocks.xmpp.core.session.debug.LogbackDebugger; -import rocks.xmpp.extensions.component.accept.ExternalComponent; -import ru.sape.Sape; - -import javax.servlet.ServletException; -import javax.servlet.annotation.MultipartConfig; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URLEncoder; -import java.util.Objects; -import java.util.Properties; - -/** - * - * @author Ugnich Anton - */ -@WebServlet(name = "Main", urlPatterns = {"/"}) -@MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) -public class Main extends HttpServlet { - - JdbcTemplate sql; - JdbcTemplate sqlSearch; - String sqlSearchConnStr = "jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000"; - ExternalComponent xmpp; - Home home = new Home(); - Discover discover = new Discover(); - PM pm = new PM(); - Login login = new Login(); - Help help = new Help(); - User pagesUser = new User(); - UserThread pagesUserThread = new UserThread(); - NewMessage pagesNewMessage; - XMPPPost xmppPost = new XMPPPost(); - FacebookLogin loginFacebook; - VKontakteLogin loginVK = new VKontakteLogin(); - TwitterAuth twitterAuth; - SignUp signup = new SignUp(); - Settings settings; - RSS rss = new RSS(); - - @Override - public void init() throws ServletException { - - super.init(); - try { - Properties conf = new Properties(); - conf.load(getServletContext().getResourceAsStream("/WEB-INF/juick.conf")); - - BasicDataSource dataSource = new BasicDataSource(); - dataSource.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(conf.getProperty("datasource_url")); - BasicDataSource dataSourceSearch = new BasicDataSource(); - dataSourceSearch.setDriverClassName(conf.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSourceSearch.setUrl(sqlSearchConnStr); - sql = new JdbcTemplate(dataSource); - sqlSearch = new JdbcTemplate(dataSourceSearch); - boolean isXmppDisabled = BooleanUtils.toBoolean(conf.getProperty("xmpp_disabled")); - if (!isXmppDisabled) { - setupXmppComponent(Jid.of(conf.getProperty("www_xmpp_jid", "www.juick.local")), - conf.getProperty("xmpp_password"), NumberUtils.toInt(conf.getProperty("xmpp_port", StringUtils.EMPTY), 5347)); - } - twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), - conf.getProperty("twitter_consumer_secret")); - loginFacebook = new FacebookLogin(conf.getProperty("facebook_appid"), conf.getProperty("facebook_secret")); - String tmpDir = conf.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/"); - Utils.setTmpDir(tmpDir); - String imgPath = conf.getProperty("img_path", "/var/www/juick.com/i/"); - pagesNewMessage = new NewMessage(tmpDir, imgPath); - settings = new Settings(imgPath); - String sapeUser = conf.getProperty("sape_user", StringUtils.EMPTY); - if (!Objects.equals(sapeUser, StringUtils.EMPTY)) { - PageTemplates.sape = new Sape(sapeUser, "juick.com", 2000, 3600); - } else { - log("Sape is not initialized"); - } - } catch (Exception e) { - log(null, e); - } - } - - @Override - public void destroy() { - try { - if (xmpp != null) - xmpp.close(); - log("ExternalComponent on WWW destroyed"); - } catch (Exception e) { - log("exception on destroy", e); - } - } - - public void setupXmppComponent(final Jid componentJid, final String password, final int port) { - XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() - .extensions(Extension.of(Message.class)) - .debugger(LogbackDebugger.class) - .build(); - xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, "localhost", port); - xmpp.addConnectionListener(e -> log(e.toString(), e.getCause())); - try { - xmpp.connect(); - } catch (XmppException e) { - log("xmpp extension", e); - } - } - - /** - * Handles the HTTP <code>GET</code> method. - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if (request.getCharacterEncoding() == null) { - request.setCharacterEncoding(CharEncoding.UTF_8); - } - String uri = request.getRequestURI(); - - if (uri.equals("/")) { - String tag = request.getParameter("tag"); - if (tag != null) { - Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, CharEncoding.UTF_8)); - } else { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - home.doGet(sql, sqlSearch, request, response, visitor); - } - } else if (uri.equals("/post")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0) { - pagesNewMessage.doGetNewMessage(sql, request, response, visitor); - } else { - Utils.sendTemporaryRedirect(response, "/login"); - } - } else if (uri.equals("/login")) { - if (request.getQueryString() == null) { - login.doGetLoginForm(sql, request, response); - } else { - login.doGetLogin(sql, request, response); - } - } else if (uri.startsWith("/pm/")) { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() == 0) { - Utils.sendTemporaryRedirect(response, "/login"); - } else { - switch (uri) { - case "/pm/inbox": - try { - pm.doGetInbox(sql, request, response, visitor); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - break; - case "/pm/sent": - try { - pm.doGetSent(sql, request, response, visitor); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - break; - default: - Errors.doGet404(sql, request, response); - break; - } - } - } else if (uri.startsWith("/rss/")) { - String uname = uri.substring(5); - int uid = UserQueries.getUIDbyName(sql, uname); - if (uid > 0) { - try { - rss.doGet(sql, response, uid); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } else { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - } - } else if (uri.equals("/logout")) { - login.doGetLogout(sql, request, response); - } else if (uri.equals("/settings")) { - try { - settings.doGet(sql, request, response); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } else if (uri.equals("/_fblogin")) { - loginFacebook.doGet(sql, request, response); - } else if (uri.equals("/_vklogin")) { - loginVK.doGet(sql, request, response); - } else if (uri.startsWith("/_twitter")) { - twitterAuth.doGet(sql, request, response); - } else if (uri.equals("/signup")) { - try { - signup.doGet(sql, request, response); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } else if (uri.equals("/help") || uri.equals("/help/")) { - help.doRedirectToHelpIndex(response); - } else if (uri.startsWith("/help/")) { - try { - help.doGetHelp(sql, request, response); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } catch (URISyntaxException e) { - log("help exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - } else if (uri.startsWith("/tag/")) { - discover.doGet(sql, sqlSearch, request, response); - } else if (uri.matches("^/\\d+$")) { - String strID = request.getRequestURI().substring(1); - int mid = 0; - try { - mid = Integer.parseInt(strID); - } catch (NumberFormatException e) { - } - if (mid > 0) { - com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); - if (author != null) { - Utils.sendPermanentRedirect(response, "/" + author.getName() + "/" + mid); - return; - } - } - Errors.doGet404(sql, request, response); - } else if (uri.matches("^/[^/]+$")) { - com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, request.getRequestURI().substring(1)); - if (user != null) { - Utils.sendPermanentRedirect(response, "/" + user.getName() + "/"); - } else { - Errors.doGet404(sql, request, response); - } - } else if (uri.matches("^/.+/.*")) { - String uriparts[] = uri.split("/"); - com.juick.User user = com.juick.server.UserQueries.getUserByName(sql, uriparts[1]); - if (user != null && user.getName().equals(uriparts[1]) && !user.isBanned()) { - if (uriparts.length == 2) { // http://juick.com/username/ - pagesUser.doGetBlog(sql, sqlSearch, request, response, user); - } else if (uriparts[2].equals("tags")) { - pagesUser.doGetTags(sql, request, response, user); - } else if (uriparts[2].equals("friends")) { - pagesUser.doGetFriends(sql, request, response, user); - } else if (uriparts[2].equals("readers")) { - pagesUser.doGetReaders(sql, request, response, user); - } else { - int mid = 0; - try { - mid = Integer.parseInt(uriparts[2]); - } catch (NumberFormatException e) { - } - if (mid > 0) { - com.juick.User author = com.juick.server.MessagesQueries.getMessageAuthor(sql, mid); - if (author != null) { - if (!author.getName().equals(user.getName())) { - Utils.sendPermanentRedirect(response, "/" + author.getName() + "/" + mid); - } else { - pagesUserThread.doGetThread(sql, request, response, mid); - } - } else { - Errors.doGet404(sql, request, response); - } - } else { - Errors.doGet404(sql, request, response); - } - } - } else if (user != null && !user.isBanned()) { - Utils.sendPermanentRedirect(response, "/" + user.getName() + "/" + (uriparts.length > 2 ? uriparts[2] : StringUtils.EMPTY)); - } else { - Errors.doGet404(sql, request, response); - } - } else { - Errors.doGet404(sql, request, response); - } - } - - /** - * Handles the HTTP <code>POST</code> method. - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if (request.getCharacterEncoding() == null) { - request.setCharacterEncoding(CharEncoding.UTF_8); - } - - String uri = request.getRequestURI(); - switch (uri) { - case "/post": { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0 && !visitor.isBanned()) { - pagesNewMessage.doPostMessage(sql, request, response, xmpp, visitor); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - break; - } - case "/comment": { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0 && !visitor.isBanned()) { - pagesNewMessage.doPostComment(sql, request, response, xmpp, visitor); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - break; - } - case "/like": { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0 && !visitor.isBanned()) { - pagesNewMessage.doPostRecomm(sql, request, response, xmpp, visitor); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - break; - } - case "/pm/send": { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0 && !visitor.isBanned()) { - pm.doPostPM(sql, request, response, xmpp, visitor); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - break; - } - case "/login": - login.doPostLogin(sql, request, response); - break; - case "/signup": - signup.doPost(sql, request, response); - break; - case "/settings": - try { - settings.doPost(sql, request, response); - } catch (PebbleException e) { - log("pebble exception", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - break; - case "/post2": - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - if (visitor.getUid() > 0 && !visitor.isBanned()) { - xmppPost.doPostMessage(sql, request, response, xmpp, visitor); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - break; - default: - response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED); - break; - } - } -} diff --git a/juick-www/src/main/java/com/juick/www/RSS.java b/juick-www/src/main/java/com/juick/www/RSS.java deleted file mode 100644 index 91ba9380..00000000 --- a/juick-www/src/main/java/com/juick/www/RSS.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2013, ugnich - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.www; - -import com.juick.Message; -import com.juick.server.MessagesQueries; -import com.juick.util.DateFormattersHolder; -import com.juick.util.MessageUtils; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import org.springframework.jdbc.core.JdbcTemplate; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - * @author ugnich - */ -public class RSS { - - protected void doGet(JdbcTemplate sql, HttpServletResponse response, int uid) throws ServletException, IOException, PebbleException { - List<Integer> mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); - if (mids.isEmpty()) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; - } - - List<Message> msgs = MessagesQueries.getMessages(sql, mids); - - msgs.forEach(m -> MessageUtils.formatMessage(m.getText())); - - response.setContentType("application/rss+xml; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("layouts/rss.xml"); - Map<String, Object> context = new HashMap<>(); - context.put("user", msgs.stream().findFirst().get().getUser()); - context.put("msgs", msgs); - context.put("sdfRSS", DateFormattersHolder.getRssFormatterInstance()); - template.evaluate(out, context); - } - } -} diff --git a/juick-www/src/main/java/com/juick/www/SignUp.java b/juick-www/src/main/java/com/juick/www/SignUp.java deleted file mode 100644 index ea40c810..00000000 --- a/juick-www/src/main/java/com/juick/www/SignUp.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2013, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ -package com.juick.www; - -import com.juick.server.UserQueries; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; -import org.apache.commons.lang3.tuple.Pair; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; - -import javax.servlet.ServletException; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * - * @author Ugnich Anton - */ -public class SignUp { - - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - String type = request.getParameter("type"); - String hash = request.getParameter("hash"); - if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - - String account = null; - switch (type) { - case "fb": - account = getFacebookNameByHash(sql, hash); - break; - case "vk": - account = getVKNameByHash(sql, hash); - break; - case "xmpp": - account = getJIDByHash(sql, hash); - break; - case "durov": - account = getTelegramNameByHash(sql, hash); - break; - } - if (account == null) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/signup.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", "Новый пользователь"); - context.put("visitor", visitor); - context.put("account", account); - context.put("type", type); - context.put("hash", hash); - template.evaluate(out, context); - } - } - - protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - int uid = 0; - - String type = request.getParameter("type"); - String hash = request.getParameter("hash"); - if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - - String action = request.getParameter("action"); - if (action.charAt(0) == 'l') { - - if (visitor.getUid() == 0) { - String username = request.getParameter("username"); - String password = request.getParameter("password"); - if (username == null || password == null || username.length() > 32 || password.isEmpty()) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - uid = com.juick.server.UserQueries.checkPassword(sql, username, password); - } else { - uid = visitor.getUid(); - } - - if (uid <= 0) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; - } - - if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid)) - && !(type.charAt(0) == 'v' && setVKUser(sql, hash, uid)) - && !(type.charAt(0) == 'd' && setTelegramUser(sql, hash, uid)) - && !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; - } - - } else { // Create new account - String username = request.getParameter("username"); - String password = request.getParameter("password"); - if (username == null || password == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - - // CHECK USERNAME - - uid = UserQueries.createUser(sql, username, password); - if (uid <= 0) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; - } - - if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid)) - && !(type.charAt(0) == 'v' && setVKUser(sql, hash, uid)) - && !(type.charAt(0) == 'd' && setTelegramUser(sql, hash, uid)) - && !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; - } - - int ref = 0; - String sRef = Utils.getCookie(request, "ref"); - if (sRef != null) { - try { - ref = Integer.parseInt(sRef); - } catch (Exception e) { - } - } - - if (ref > 0) { - setUserRef(sql, uid, ref); - } - - visitor = null; - } - - if (visitor == null) { - hash = com.juick.server.UserQueries.getHashByUID(sql, uid); - Cookie c = new Cookie("hash", hash); - c.setMaxAge(365 * 24 * 60 * 60); - response.addCookie(c); - } - - response.sendRedirect("/"); - } - - private boolean setUserRef(JdbcTemplate sql, int uid, int ref) { - return sql.update("INSERT INTO users_refs(user_id,ref) VALUES (?,?)", uid, ref) > 0; - } - - private String getFacebookNameByHash(JdbcTemplate sql, String hash) { - try { - List<Pair<String, String>> fb = sql.query("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?", - (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash); - if (fb.size() > 0) { - return "<a href=\"" + fb.get(0).getRight() + "\" rel=\"nofollow\">" + fb.get(0).getLeft() + "</a>"; - } - return null; - } catch (EmptyResultDataAccessException e) { - return null; - } - } - private String getTelegramNameByHash(JdbcTemplate sql, String hash) { - try { - String name = sql.queryForObject("SELECT tg_name FROM telegram WHERE loginhash=?", String.class, hash); - return "<a href=\"https://telegram.me/" + name + "\" rel=\"nofollow\">" + name + "</a>"; - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - private boolean setFacebookUser(JdbcTemplate sql, String hash, int uid) { - return sql.update("UPDATE facebook SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; - } - - private String getVKNameByHash(JdbcTemplate sql, String hash) { - List<Pair<String, String>> logins = sql.query("SELECT vk_name,vk_link FROM vk WHERE loginhash=?", - (rs, num) -> Pair.of(rs.getString(1), rs.getString(2)), hash); - if (logins.size() > 0) { - return "<a href=\"http://vk.com/" + logins.get(0).getRight() + "\" rel=\"nofollow\">" + logins.get(0).getLeft() + "</a>"; - } - return null; - } - - private boolean setVKUser(JdbcTemplate sql, String hash, int uid) { - return sql.update("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; - } - private boolean setTelegramUser(JdbcTemplate sql, String hash, int uid) { - return sql.update("UPDATE telegram SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; - } - - private String getJIDByHash(JdbcTemplate sql, String hash) { - try { - return sql.queryForObject("SELECT jid FROM jids WHERE loginhash=?", String.class, hash); - } catch (EmptyResultDataAccessException e) { - return null; - } - } - - private boolean setJIDUser(JdbcTemplate sql, String hash, int uid) { - return sql.update("UPDATE jids SET user_id=?,loginhash=NULL WHERE loginhash=?", uid, hash) > 0; - } -} diff --git a/juick-www/src/main/java/com/juick/www/Utils.java b/juick-www/src/main/java/com/juick/www/Utils.java index ca3535fd..37016ed2 100644 --- a/juick-www/src/main/java/com/juick/www/Utils.java +++ b/juick-www/src/main/java/com/juick/www/Utils.java @@ -17,31 +17,21 @@ */ package com.juick.www; -import com.juick.Tag; -import com.juick.User; -import com.juick.server.TagQueries; -import com.mitchellbosecke.pebble.PebbleEngine; -import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; -import java.nio.file.Paths; -import java.util.*; +import java.util.Arrays; /** * @@ -51,9 +41,6 @@ public class Utils { private static final Logger logger = LoggerFactory.getLogger(Utils.class); - private static final PebbleEngine engine = new PebbleEngine.Builder().build(); - private static String tmpDir = "/var/www/juick.com/i/tmp/"; - public static String getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); if (cookies != null) { @@ -63,40 +50,7 @@ public class Utils { return null; } - public static String receiveMultiPartFile(Part filePart) throws IOException, ServletException { - String attachmentFName = null; - - if (filePart != null) { - String partname = Utils.getPartFilename(filePart); - if (partname != null && partname.length() > 0) { - String attachmentType = partname.substring(partname.length() - 3).toLowerCase(); - if (attachmentType.equals("jpg") || attachmentType.equals("peg") || attachmentType.equals("png")) { - if (attachmentType.equals("peg")) { - attachmentType = "jpg"; - } - attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; - filePart.write(Paths.get(getTmpDir(), attachmentFName).toString()); - } else { - throw new IOException("Wrong file type"); - } - } - } - - return attachmentFName; - } - public static com.juick.User getVisitorUser(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) { - String hash = getCookie(request, "hash"); - if (hash != null) { - com.juick.User visitor = com.juick.server.UserQueries.getUserByHash(sql, hash); - if (response != null && visitor.getUid() > 0) { - response.setHeader("X-Username", visitor.getName()); - } - return visitor; - } else { - return new User(); - } - } public static void sendTemporaryRedirect(HttpServletResponse response, String location) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); @@ -154,96 +108,4 @@ public class Utils { return null; } } - - public static String downloadImage(URL url) throws Exception { - String attachmentFName = null; - Exception ex = null; - - InputStream is = null; - FileOutputStream fos = null; - try { - URLConnection urlConn = url.openConnection(); - is = urlConn.getInputStream(); - String mime = urlConn.getContentType(); - - String attachmentType; - if (mime != null && mime.equals("image/jpeg")) { - attachmentType = "jpg"; - } else if (mime != null && mime.equals("image/png")) { - attachmentType = "png"; - } else { - throw new Exception("Wrong file type"); - } - - attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; - fos = new FileOutputStream("/var/www/juick.com/i/tmp/" + attachmentFName); - byte[] buffer = new byte[10240]; - int len; - while ((len = is.read(buffer)) > 0) { - fos.write(buffer, 0, len); - } - } catch (Exception e) { - ex = e; - attachmentFName = null; - } finally { - try { - if (is != null) { - is.close(); - } - } finally { - if (fos != null) { - fos.close(); - } - } - } - - if (ex != null) { - throw ex; - } else { - return attachmentFName; - } - } - - public static List<Tag> parseTags(JdbcTemplate sql, String tagsStr) { - List<com.juick.Tag> tags = new ArrayList<>(); - String tagsArr[]; - if (tagsStr != null && !tagsStr.isEmpty()) { - tagsArr = tagsStr.split("[ \\,]"); - for (int i = 0; i < tagsArr.length; i++) { - if (tagsArr[i].startsWith("*")) { - tagsArr[i] = tagsArr[i].substring(1); - } - if (tagsArr[i].length() > 64) { - tagsArr[i] = tagsArr[i].substring(0, 64); - } - } - tags = TagQueries.getTags(sql, tagsArr, true); - while (tags.size() > 5) { - tags.remove(5); - } - } - return tags; - } - - public static String receiveAttachment(Part part, String paramImg) throws Exception { - String attachmentFName = receiveMultiPartFile(part); - - if (attachmentFName == null && paramImg != null && paramImg.length() > 10) { - URL imgUrl = new URL(paramImg); - attachmentFName = downloadImage(imgUrl); - } - return attachmentFName; - } - - public static PebbleEngine getEngine() { - return engine; - } - - public static String getTmpDir() { - return tmpDir; - } - - public static void setTmpDir(String tmpDir) { - Utils.tmpDir = tmpDir; - } } diff --git a/juick-www/src/main/java/com/juick/www/WebApp.java b/juick-www/src/main/java/com/juick/www/WebApp.java new file mode 100644 index 00000000..85ae64d4 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/WebApp.java @@ -0,0 +1,154 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.www; + +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.www.controllers.PageTemplates; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.core.session.debug.LogbackDebugger; +import rocks.xmpp.extensions.component.accept.ExternalComponent; +import ru.sape.Sape; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * + * @author Ugnich Anton + */ +public class WebApp implements AutoCloseable { + private static Logger logger = LoggerFactory.getLogger(WebApp.class); + + private ExternalComponent xmpp; + + Environment env; + PageTemplates templates; + UserService userService; + TagService tagService; + + private String imgDir; + private String tmpDir; + + public WebApp(Environment env, PageTemplates templates, UserService userService, TagService tagService) { + this.env = env; + this.templates = templates; + this.userService = userService; + this.tagService = tagService; + tmpDir = env.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/"); + imgDir = env.getProperty("img_path", "/var/www/juick.com/i/"); + boolean isXmppDisabled = BooleanUtils.toBoolean(env.getProperty("xmpp_disabled")); + if (!isXmppDisabled) { + setupXmppComponent(Jid.of(env.getProperty("www_xmpp_jid", "www.juick.local")), + env.getProperty("xmpp_password"), NumberUtils.toInt(env.getProperty("xmpp_port", StringUtils.EMPTY), 5347)); + } + String sapeUser = env.getProperty("sape_user", StringUtils.EMPTY); + if (!Objects.equals(sapeUser, StringUtils.EMPTY)) { + templates.sape = new Sape(sapeUser, "juick.com", 2000, 3600); + } else { + logger.error("Sape is not initialized"); + } + } + + @Override + public void close() { + try { + if (xmpp != null) + xmpp.close(); + logger.error("ExternalComponent on WWW destroyed"); + } catch (Exception e) { + logger.error("exception on destroy", e); + } + } + + public void setupXmppComponent(final Jid componentJid, final String password, final int port) { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(Message.class)) + .debugger(LogbackDebugger.class) + .build(); + xmpp = ExternalComponent.create(componentJid.toString(), password, configuration, "localhost", port); + xmpp.addConnectionListener(e -> logger.error(e.toString(), e.getCause())); + try { + xmpp.connect(); + } catch (XmppException e) { + logger.error("xmpp extension", e); + } + } + + public ExternalComponent getXmpp() { + return xmpp; + } + + + public com.juick.User getVisitorUser(HttpServletRequest request, HttpServletResponse response) { + String hash = Utils.getCookie(request, "hash"); + if (hash != null) { + com.juick.User visitor = userService.getUserByHash(hash); + if (response != null && visitor.getUid() > 0) { + response.setHeader("X-Username", visitor.getName()); + } + return visitor; + } else { + return new User(); + } + } + + public String getImgDir() { + return imgDir; + } + + public String getTmpDir() { + return tmpDir; + } + + public List<Tag> parseTags(String tagsStr) { + List<Tag> tags = new ArrayList<>(); + String tagsArr[]; + if (tagsStr != null && !tagsStr.isEmpty()) { + tagsArr = tagsStr.split("[ \\,]"); + for (int i = 0; i < tagsArr.length; i++) { + if (tagsArr[i].startsWith("*")) { + tagsArr[i] = tagsArr[i].substring(1); + } + if (tagsArr[i].length() > 64) { + tagsArr[i] = tagsArr[i].substring(0, 64); + } + } + tags = tagService.getTags(tagsArr, true); + while (tags.size() > 5) { + tags.remove(5); + } + } + return tags; + } +} diff --git a/juick-www/src/main/java/com/juick/www/XMPPPost.java b/juick-www/src/main/java/com/juick/www/XMPPPost.java deleted file mode 100644 index c2ec42d7..00000000 --- a/juick-www/src/main/java/com/juick/www/XMPPPost.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.juick.www; - -import com.juick.Tag; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.math.NumberUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.session.XmppSession; -import rocks.xmpp.core.stanza.model.Message; -import rocks.xmpp.extensions.oob.model.x.OobX; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Created by vitalyster on 08.12.2016. - */ -public class XMPPPost { - private final static Logger logger = LoggerFactory.getLogger(XMPPPost.class); - - public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, - HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) - throws ServletException, IOException { - if (visitor.getUid() == 0) { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - return; - } - String body = request.getParameter("body").replace("\r", StringUtils.EMPTY); - int mid = NumberUtils.toInt(request.getParameter("mid"), 0); - int rid = NumberUtils.toInt(request.getParameter("rid"), 0); - if (mid > 0 && rid > 0) { - body = String.format("#%d/%d %s", mid, rid, body); - } else if (mid > 0) { - body = String.format("#%d %s", mid, body); - } else { - // is a post - List<Tag> tags = Utils.parseTags(sql, request.getParameter("tags")); - body = String.format("%s %s", tags.stream() - .map(t -> "*" + t.getName()).collect(Collectors.joining(" ")), body); - } - String attachmentFName; - try { - attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img")); - } catch (Exception e) { - logger.error("MULTIPART ERROR", e); - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - Message msg = new Message(); - msg.setType(Message.Type.CHAT); - msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); - msg.setTo(Jid.of("juick@juick.com/Juick")); - msg.setBody(body); - try { - if (attachmentFName != null) { - String attachmentUrl = String.format("juick://%s", attachmentFName); - msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); - } - xmpp.sendMessage(msg); - } catch (URISyntaxException e1) { - logger.warn("attachment error", e1); - } - String referer = request.getHeader("referer"); - if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") - || referer.substring(0, 22).equals("https://juick.com/post")) { - response.sendRedirect("/?show=my"); - return; - } - response.sendRedirect(referer); - } -} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java new file mode 100644 index 00000000..cd681190 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java @@ -0,0 +1,50 @@ +package com.juick.www.configuration; + +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.www.HelpService; +import com.juick.www.controllers.PageTemplates; +import com.juick.www.WebApp; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.guava.GuavaCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/22/16. + */ +@Configuration +@PropertySource("classpath:juick.conf") +@EnableCaching +public class WebAppConfiguration { + @Resource + private Environment env; + @Inject + private UserService userService; + @Inject + private TagService tagService; + + @Bean + public WebApp webApp() { + return new WebApp(env, templates(), userService, tagService); + } + @Bean + public PageTemplates templates() { + return new PageTemplates(); + } + @Bean + public GuavaCacheManager cacheManager() { + return new GuavaCacheManager("help"); + } + + @Bean + public HelpService helpService() { + return new HelpService("help"); + } + +} diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java new file mode 100644 index 00000000..cd5429c2 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwInitializer.java @@ -0,0 +1,52 @@ +package com.juick.www.configuration; + +import com.juick.configuration.DataConfiguration; +import com.juick.configuration.SearchConfiguration; +import org.apache.commons.lang3.CharEncoding; +import org.springframework.web.filter.CharacterEncodingFilter; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +import javax.servlet.FilterRegistration; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; + +/** + * Created by aalexeev on 11/20/16. + */ +public class WwwInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + @Override + protected Class<?>[] getRootConfigClasses() { + return new Class<?>[]{ + WebAppConfiguration.class, + DataConfiguration.class, + SearchConfiguration.class + }; + } + + @Override + protected Class<?>[] getServletConfigClasses() { + return new Class<?>[]{WwwServletConfiguration.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } + + @Override + protected String getServletName() { + return "WWW-spring dispatcher servlet"; + } + + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + super.onStartup(servletContext); + + FilterRegistration.Dynamic registration = servletContext.addFilter( + "encodingFilter", new CharacterEncodingFilter(CharEncoding.UTF_8, true)); + + registration.addMappingForUrlPatterns(null, true, "/*"); + } +} + diff --git a/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java new file mode 100644 index 00000000..c4e6c07c --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/configuration/WwwServletConfiguration.java @@ -0,0 +1,99 @@ +package com.juick.www.configuration; + +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.loader.Loader; +import com.mitchellbosecke.pebble.loader.ServletLoader; +import com.mitchellbosecke.pebble.spring4.PebbleViewResolver; +import com.mitchellbosecke.pebble.spring4.extension.SpringExtension; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.http.CacheControl; +import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.servlet.resource.PathResourceResolver; + +import javax.inject.Inject; +import javax.servlet.ServletContext; +import java.util.concurrent.TimeUnit; + +/** + * Created by vitalyster on 28.06.2016. + */ +@Configuration +@ComponentScan(basePackages = {"com.juick.www.controllers"}) +@PropertySource("classpath:juick.conf") +public class WwwServletConfiguration extends WebMvcConfigurationSupport { + @Inject + private Environment env; + + @Override + protected void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.setOrder(0); + registry.addResourceHandler( + "/scripts.js*", + "/style.css*", + "/*.png", + "/favicon.ico") + .addResourceLocations("/") + .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) + .resourceChain(true) + .addResolver(new PathResourceResolver()); + + registry.addResourceHandler("/static/**") + .addResourceLocations("/static/") + .setCacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)); + } + + @Override + public RequestMappingHandlerMapping requestMappingHandlerMapping() { + RequestMappingHandlerMapping result = super.requestMappingHandlerMapping(); + + result.setOrder(1); + + return result; + } + + @Bean + public MultipartResolver multipartResolver() { + CommonsMultipartResolver resolver = new CommonsMultipartResolver(); + resolver.setMaxUploadSize(10000000); + return resolver; + } + + @Inject + private ServletContext servletContext; + + @Bean + public Loader templateLoader(){ + return new ServletLoader(servletContext); + } + + @Bean + public SpringExtension springExtension() { + return new SpringExtension(); + } + + @Bean + public PebbleEngine pebbleEngine() { + return new PebbleEngine.Builder() + .loader(this.templateLoader()) + .extension(springExtension()) + .build(); + } + + @Bean + public ViewResolver viewResolver() { + PebbleViewResolver viewResolver = new PebbleViewResolver(); + viewResolver.setPrefix("/WEB-INF/"); + viewResolver.setSuffix(".html"); + viewResolver.setPebbleEngine(pebbleEngine()); + return viewResolver; + } +} diff --git a/juick-www/src/main/java/com/juick/www/Discover.java b/juick-www/src/main/java/com/juick/www/controllers/Discover.java index af2b6659..e5d17501 100644 --- a/juick-www/src/main/java/com/juick/www/Discover.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Discover.java @@ -15,17 +15,21 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; -import com.juick.server.AdsQueries; -import com.juick.server.MessagesQueries; -import com.juick.server.TagQueries; +import com.juick.service.AdsService; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import com.juick.www.Utils; +import com.juick.www.WebApp; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -38,18 +42,30 @@ import java.util.List; * * @author Ugnich Anton */ +@Controller public class Discover { - - protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + @Inject + WebApp webApp; + @Inject + MessagesService messagesService; + @Inject + TagService tagService; + @Inject + AdsService adsService; + @Inject + PageTemplates templates; + + @RequestMapping(value = "/tag/{tagName}", method = RequestMethod.GET) + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); String paramTagStr = URLDecoder.decode(request.getRequestURI().substring(5), CharEncoding.UTF_8); - com.juick.Tag paramTag = TagQueries.getTag(sql, paramTagStr, false); + com.juick.Tag paramTag = tagService.getTag(paramTagStr, false); if (paramTag == null) { - Errors.doGet404(sql, request, response); + response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) { - com.juick.Tag synTag = TagQueries.getTag(sql, paramTag.SynonymID); + com.juick.Tag synTag = tagService.getTag(paramTag.SynonymID); String url = "/tag/" + URLEncoder.encode(synTag.getName(), CharEncoding.UTF_8); if (request.getQueryString() != null) { url += "?" + request.getQueryString(); @@ -77,32 +93,32 @@ public class Discover { int visitor_uid = visitor.getUid(); String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName()); - List<Integer> mids = MessagesQueries.getTag(sql, paramTag.TID, visitor_uid, paramBefore, (visitor_uid == 0) ? 40 : 20); + List<Integer> mids = messagesService.getTag(paramTag.TID, visitor_uid, paramBefore, (visitor_uid == 0) ? 40 : 20); response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = StringUtils.EMPTY; - if (TagQueries.getTagNoIndex(sql, paramTag.TID)) { + if (tagService.getTagNoIndex(paramTag.TID)) { head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; } else if (paramBefore > 0 || mids.size() < 5) { head = "<meta name=\"robots\" content=\"noindex\"/>"; } - PageTemplates.pageHead(out, visitor, title, head); - PageTemplates.pageNavigation(out, visitor, null); + templates.pageHead(out, visitor, title, head); + templates.pageNavigation(out, visitor, null); out.println("<section id=\"content\">"); if (mids.size() > 0) { int vuid = visitor.getUid(); - int ad_mid = AdsQueries.getAdMID(sql, vuid); + int ad_mid = adsService.getAdMid(vuid); if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) { mids.add(0, ad_mid); - AdsQueries.logAdMID(sql, vuid, ad_mid); + adsService.logAdMid(vuid, ad_mid); } else { ad_mid = 0; } - PageTemplates.printMessages(out, sql, null, mids, visitor, visitor_uid == 0 ? 2 : 3, ad_mid); + templates.printMessages(out, null, mids, visitor, visitor_uid == 0 ? 2 : 3, ad_mid); } if (mids.size() >= 20) { @@ -112,11 +128,11 @@ public class Discover { out.println("</section>"); - PageTemplates.pageHomeColumn(out, sql, visitor); + templates.pageHomeColumn(out, visitor); - PageTemplates.pageFooter(request, out, visitor, true); + templates.pageFooter(request, out, visitor, true); - PageTemplates.pageEnd(out); + templates.pageEnd(out); } } } diff --git a/juick-www/src/main/java/com/juick/www/FacebookLogin.java b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java index ba7ab29f..cc11f99a 100644 --- a/juick-www/src/main/java/com/juick/www/FacebookLogin.java +++ b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java @@ -15,25 +15,29 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.server.UserQueries; +import com.juick.service.CrosspostService; +import com.juick.service.UserService; +import com.juick.www.Utils; import com.juick.www.facebook.Graph; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.UUID; @@ -41,6 +45,7 @@ import java.util.UUID; * * @author Ugnich Anton */ +@Controller public class FacebookLogin { private static final Logger logger = LoggerFactory.getLogger(FacebookLogin.class); @@ -50,16 +55,24 @@ public class FacebookLogin { private final String FACEBOOK_REDIRECT = "http://juick.com/_fblogin"; private final ObjectMapper mapper; - public FacebookLogin(String ApplicationID, String secret) { - this.FACEBOOK_APPID = ApplicationID; - this.FACEBOOK_SECRET = secret; + @Inject + CrosspostService crosspostService; + @Inject + UserService userService; + + @Inject + public FacebookLogin(Environment env) { + FACEBOOK_APPID = env.getProperty("facebook_appid"); + FACEBOOK_SECRET = env.getProperty("facebook_secret"); + mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + @RequestMapping(value = "/_fblogin", method = RequestMethod.GET) + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { String fbstate; String code = request.getParameter("code"); @@ -111,19 +124,19 @@ public class FacebookLogin { throw new Exception(); } - int uid = getUIDbyFBID(sql, fbID); + int uid = crosspostService.getUIDbyFBID(fbID); if (uid > 0) { - if (!updateDB(sql, fbID, token, fb.getName(), fb.getLink())) { + if (!crosspostService.updateFacebookUser(fbID, token, fb.getName(), fb.getLink())) { throw new Exception(); } - Cookie c = new Cookie("hash", UserQueries.getHashByUID(sql, uid)); + Cookie c = new Cookie("hash", userService.getHashByUID(uid)); c.setMaxAge(50 * 24 * 60 * 60); response.addCookie(c); response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", "/"); } else if (fb.getVerified()) { String loginhash = UUID.randomUUID().toString(); - if (!insertDB(sql, fbID, loginhash, token, fb.getName(), fb.getLink())) { + if (!crosspostService.createFacebookUser(fbID, loginhash, token, fb.getName(), fb.getLink())) { throw new Exception(); } response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); @@ -137,23 +150,4 @@ public class FacebookLogin { return; } } - - private int getUIDbyFBID(JdbcTemplate sql, long fbID) { - try { - return sql.queryForObject("SELECT user_id FROM facebook WHERE fb_id=? AND user_id IS NOT NULL", - Integer.class, fbID); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - private boolean insertDB(JdbcTemplate sql, long fbID, String loginhash, String token, String fbName, String fbLink) { - return sql.update("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)", - fbID, loginhash, token, fbName, fbLink) > 0; - } - - private boolean updateDB(JdbcTemplate sql, long fbID, String token, String fbName, String fbLink) { - return sql.update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?", - token, fbName, fbLink, fbID) > 0; - } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/Help.java b/juick-www/src/main/java/com/juick/www/controllers/Help.java new file mode 100644 index 00000000..58949827 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/controllers/Help.java @@ -0,0 +1,74 @@ +package com.juick.www.controllers; + +import com.juick.server.util.HttpNotFoundException; +import com.juick.www.HelpService; +import com.juick.www.WebApp; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Locale; +import java.util.Objects; +import java.util.Optional; + +/** + * Created by aalexeev on 11/21/16. + */ +@Controller +public class Help { + @Inject + private HelpService helpService; + @Inject + private WebApp webApp; + + @RequestMapping({"/help/", "/help", "/help/{langOrPage}", "/help/{lang}/{page}"}) + public String showHelp( + HttpServletRequest request, + HttpServletResponse response, + Locale locale, + @PathVariable("lang") Optional<String> langParam, + @PathVariable("page") Optional<String> pageParam, + @PathVariable("langOrPage") Optional<String> langOrPageParam, + Model model) throws IOException, URISyntaxException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + String page = pageParam.orElse("index"); + String lang = langParam.orElse(locale.getLanguage()); + + String navigation = null; + + if (langOrPageParam.isPresent()) { + String langOrPage = langOrPageParam.get(); + + if (helpService.canBeLang(langOrPage)) { + navigation = helpService.getHelp("navigation", langOrPage); + if (navigation != null) + lang = langOrPage; + } + + if (navigation == null && helpService.canBePage(langOrPage)) + page = langOrPage; + } + + String content = helpService.getHelp(page, lang); + if (content == null && !Objects.equals("index", page)) + content = helpService.getHelp("index", lang); + + if (navigation == null) + navigation = helpService.getHelp("navigation", lang); + + if (content == null || navigation == null) + throw new HttpNotFoundException(); + + model.addAttribute("navigation", navigation); + model.addAttribute("content", content); + model.addAttribute("visitor", visitor); + + return "views/help"; + } +} diff --git a/juick-www/src/main/java/com/juick/www/Home.java b/juick-www/src/main/java/com/juick/www/controllers/Home.java index 1d4fd521..2f9dc903 100644 --- a/juick-www/src/main/java/com/juick/www/Home.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Home.java @@ -15,17 +15,25 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; - -import com.juick.server.AdsQueries; -import com.juick.server.MessagesQueries; +package com.juick.www.controllers; + +import com.juick.service.AdsService; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import com.juick.util.WebUtils; +import com.juick.www.Utils; +import com.juick.www.WebApp; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -37,9 +45,62 @@ import java.util.List; * * @author Ugnich Anton */ +@Controller public class Home { + @Inject + UserService userService; + @Inject + MessagesService messagesService; + @Inject + AdsService adsService; + @Inject + PageTemplates templates; + @Inject + WebApp webApp; + + @RequestMapping(value = "/{anything}/**", method = RequestMethod.GET) + protected void parseAnyThing(HttpServletResponse response, @PathVariable String anything, + @RequestParam(required = false, defaultValue = "0") int before) throws IOException { + if (before == 0) { + boolean isPostNumber = WebUtils.isPostNumber(anything); + int messageId = isPostNumber ? + NumberUtils.toInt(anything) : 0; + + if (isPostNumber && anything.equals(Integer.toString(messageId))) { + if (messageId > 0) { + com.juick.User author = messagesService.getMessageAuthor(messageId); + + if (author != null) { + Utils.sendPermanentRedirect(response, "/" + author.getName() + "/" + anything); + return; + } + } + } + com.juick.User user = userService.getUserByName(anything); + if (user.getUid() > 0) { + Utils.sendPermanentRedirect(response, "/" + user.getName() + "/"); + return; + } + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + com.juick.User user = userService.getUserByName(anything); + if (user.getUid() > 0) { + Utils.sendPermanentRedirect(response, "/" + user.getName() + "/?before=" + before); + return; + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + } - protected void doGet(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/", method = RequestMethod.GET) + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + String tag = request.getParameter("tag"); + if (tag != null) { + Utils.sendPermanentRedirect(response, "/tag/" + URLEncoder.encode(tag, CharEncoding.UTF_8)); + } + com.juick.User visitor = webApp.getVisitorUser(request, response); int paramBefore = NumberUtils.toInt(request.getParameter("before"), 0); String paramSearch = request.getParameter("search"); @@ -53,14 +114,14 @@ public class Home { String paramShow = request.getParameter("show"); if (paramSearch != null) { title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch); - mids = MessagesQueries.getSearch(sql, sqlSearch, Utils.encodeSphinx(paramSearch), paramBefore); + mids = messagesService.getSearch(Utils.encodeSphinx(paramSearch), paramBefore); } else if (paramShow == null) { if (visitor.getUid() > 0) { title = "Популярные"; - mids = MessagesQueries.getPopular(sql, visitor.getUid(), paramBefore); + mids = messagesService.getPopular(visitor.getUid(), paramBefore); } else { title = "Микроблоги Juick: популярные записи"; - mids = MessagesQueries.getPopular(sql, 0, paramBefore); + mids = messagesService.getPopular(0, paramBefore); } } else if (paramShow.equals("top")) { @@ -68,32 +129,32 @@ public class Home { return; } else if (paramShow.equals("my") && visitor != null) { title = "Моя лента"; - mids = MessagesQueries.getMyFeed(sql, visitor.getUid(), paramBefore); + mids = messagesService.getMyFeed(visitor.getUid(), paramBefore); } else if (paramShow.equals("private") && visitor != null) { title = "Приватные"; - mids = MessagesQueries.getPrivate(sql, visitor.getUid(), paramBefore); + mids = messagesService.getPrivate(visitor.getUid(), paramBefore); } else if (paramShow.equals("discuss") && visitor != null) { title = "Обсуждения"; - mids = MessagesQueries.getDiscussions(sql, visitor.getUid(), paramBefore); + mids = messagesService.getDiscussions(visitor.getUid(), paramBefore); } else if (paramShow.equals("recommended") && visitor != null) { title = "Рекомендации"; - mids = MessagesQueries.getRecommended(sql, visitor.getUid(), paramBefore); + mids = messagesService.getRecommended(visitor.getUid(), paramBefore); } else if (paramShow.equals("photos")) { title = "Фотографии"; if (visitor != null) { - mids = MessagesQueries.getPhotos(sql, visitor.getUid(), paramBefore); + mids = messagesService.getPhotos(visitor.getUid(), paramBefore); } else { - mids = MessagesQueries.getPhotos(sql, 0, paramBefore); + mids = messagesService.getPhotos(0, paramBefore); } } else if (paramShow.equals("all")) { title = "Все сообщения"; if (visitor != null) { - mids = MessagesQueries.getAll(sql, visitor.getUid(), paramBefore); + mids = messagesService.getAll(visitor.getUid(), paramBefore); } else { - mids = MessagesQueries.getAll(sql, 0, paramBefore); + mids = messagesService.getAll(0, paramBefore); } } else { - Errors.doGet404(sql, request, response); + response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } @@ -103,8 +164,8 @@ public class Home { if (paramBefore > 0 || paramShow != null) { head = "<meta name=\"robots\" content=\"noindex\"/>"; } - PageTemplates.pageHead(out, visitor, title, head); - PageTemplates.pageNavigation(out, visitor, paramSearch); + templates.pageHead(out, visitor, title, head); + templates.pageNavigation(out, visitor, paramSearch); out.println("<section id=\"content\">"); @@ -132,16 +193,16 @@ public class Home { int ad_mid = 0; if (paramShow == null || paramShow.equals("top") || paramShow.equals("all")) { int vuid = visitor.getUid(); - ad_mid = AdsQueries.getAdMID(sql, vuid); + ad_mid = adsService.getAdMid(vuid); if (ad_mid > 0 && mids.indexOf(ad_mid) == -1) { mids.add(0, ad_mid); - AdsQueries.logAdMID(sql, vuid, ad_mid); + adsService.logAdMid(vuid, ad_mid); } else { ad_mid = 0; } } - PageTemplates.printMessages(out, sql, null, mids, visitor, visitor.getUid() == 0 ? 2 : 3, ad_mid); + templates.printMessages(out, null, mids, visitor, visitor.getUid() == 0 ? 2 : 3, ad_mid); } if (mids.size() >= 20) { @@ -162,10 +223,10 @@ public class Home { out.println("</section>"); - PageTemplates.pageHomeColumn(out, sql, visitor, paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); + templates.pageHomeColumn(out, visitor, paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); - PageTemplates.pageFooter(request, out, visitor, true); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, true); + templates.pageEnd(out); } } } diff --git a/juick-www/src/main/java/com/juick/www/Login.java b/juick-www/src/main/java/com/juick/www/controllers/Login.java index 27f34c1c..bce3e000 100644 --- a/juick-www/src/main/java/com/juick/www/Login.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Login.java @@ -15,25 +15,52 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; -import org.springframework.jdbc.core.JdbcTemplate; +import com.juick.service.UserService; +import com.juick.www.Utils; +import com.juick.www.WebApp; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import java.io.IOException; -import java.io.PrintWriter; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; /** * * @author Ugnich Anton */ +@Controller public class Login { + @Inject + UserService userService; + @Inject + WebApp webApp; + + @RequestMapping(value = "/login", method = RequestMethod.GET) + protected void doGetLoginForm(HttpServletRequest request, HttpServletResponse response) throws IOException { + String hash = request.getQueryString(); + if (hash != null) { + if (hash.length() > 32) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } - protected void doGetLoginForm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + if (userService.getUIDbyHash(hash) > 0) { + Cookie c = new Cookie("hash", hash); + c.setMaxAge(365 * 24 * 60 * 60); + response.addCookie(c); + response.sendRedirect("/"); + } else { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + } + } + com.juick.User visitor = webApp.getVisitorUser(request, response); if (visitor.getUid() > 0) { Utils.sendTemporaryRedirect(response, "/"); return; @@ -183,24 +210,8 @@ public class Login { } } - protected void doGetLogin(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String hash = request.getQueryString(); - if (hash.length() > 32) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - - if (com.juick.server.UserQueries.getUIDbyHash(sql, hash) > 0) { - Cookie c = new Cookie("hash", hash); - c.setMaxAge(365 * 24 * 60 * 60); - response.addCookie(c); - response.sendRedirect("/"); - } else { - response.sendError(HttpServletResponse.SC_FORBIDDEN); - } - } - - protected void doPostLogin(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + @RequestMapping(value = "/login", method = RequestMethod.POST) + protected void doPostLogin(HttpServletRequest request, HttpServletResponse response) throws IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if (username == null || password == null || username.length() > 32 || password.isEmpty()) { @@ -208,9 +219,9 @@ public class Login { return; } - int uid = com.juick.server.UserQueries.checkPassword(sql, username, password); + int uid = userService.checkPassword(username, password); if (uid > 0) { - String hash = com.juick.server.UserQueries.getHashByUID(sql, uid); + String hash = userService.getHashByUID(uid); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); @@ -226,10 +237,11 @@ public class Login { } } - protected void doGetLogout(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + @RequestMapping(value = "/logout", method = RequestMethod.GET) + protected void doGetLogout(HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); if (visitor.getUid() > 0) { - sql.update("DELETE FROM logins WHERE user_id=?", visitor.getUid()); + userService.logout(visitor.getUid()); } Cookie c = new Cookie("hash", "-"); diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 43ea039c..dacd54a3 100644 --- a/juick-www/src/main/java/com/juick/www/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -15,11 +15,15 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; import com.juick.Tag; -import com.juick.server.*; import com.juick.server.helpers.TagStats; +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpUtils; +import com.juick.service.*; +import com.juick.www.Utils; +import com.juick.www.WebApp; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; @@ -27,14 +31,18 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.session.XmppSession; import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -52,26 +60,42 @@ import java.util.List; /** * @author Ugnich Anton */ +@Controller public class NewMessage { - private static final Logger logger = LoggerFactory.getLogger(NewMessage.class); - - private String tmpDir, imgDir; + @Inject + Environment env; + @Inject + TagService tagService; + @Inject + MessagesService messagesService; + @Inject + UserService userService; + @Inject + SubscriptionService subscriptionService; + @Inject + CrosspostService crosspostService; + @Inject + WebApp webApp; + @Inject + PageTemplates templates; - public NewMessage(String tmpDir, String imgDir) { - this.tmpDir = tmpDir; - this.imgDir = imgDir; - } + private static final Logger logger = LoggerFactory.getLogger(NewMessage.class); - protected void doGetNewMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, - com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/post", method = RequestMethod.GET) + protected void doGetNewMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + Utils.sendTemporaryRedirect(response, "/login"); + return; + } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - PageTemplates.pageHead(out, visitor, "Написать", "<script src=\"//maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAVVtPtxkw4soCEHg44FsNChRB4OFYjAXt73He16Zkp6a_0tPs2RTU6i6UlcMs4QvPBYvIY8rWvcxqOg\" type=\"text/javascript\"></script>" + templates.pageHead(out, visitor, "Написать", "<script src=\"//maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAVVtPtxkw4soCEHg44FsNChRB4OFYjAXt73He16Zkp6a_0tPs2RTU6i6UlcMs4QvPBYvIY8rWvcxqOg\" type=\"text/javascript\"></script>" + "<script src=\"//static.juick.com/mc.js\" type=\"text/javascript\" defer=\"defer\"></script>" + "<script src=\"//static.juick.com/maps.js?2010111500\" type=\"text/javascript\" defer=\"defer\"></script>" + "<script src=\"//static.juick.com/post3.js\" type=\"text/javascript\" defer=\"defer\"></script>"); - PageTemplates.pageNavigation(out, visitor, null); + templates.pageNavigation(out, visitor, null); out.println("<section id=\"content\" class=\"pagetext\">"); out.println("<form action=\"/post2\" method=\"post\" id=\"postmsg\" enctype=\"multipart/form-data\">"); @@ -93,16 +117,16 @@ public class NewMessage { out.println("</form>"); out.println("<div id=\"geomap\"></div>"); out.println("<p style=\"text-align: left\"><b>Теги:</b></p>"); - printUserTags(sql, out, visitor); + printUserTags(out, visitor); out.println("</section>"); - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, false); + templates.pageEnd(out); } } - void printUserTags(JdbcTemplate sql, PrintWriter out, com.juick.User visitor) { - List<TagStats> tags = TagQueries.getTagsStats(sql, visitor.getUid()); + void printUserTags(PrintWriter out, com.juick.User visitor) { + List<TagStats> tags = tagService.getUserTagStats(visitor.getUid()); if (tags.isEmpty()) { return; @@ -149,7 +173,15 @@ public class NewMessage { out.println("</p>"); } - public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/post", method = RequestMethod.POST) + public void doPostMessage(HttpServletRequest request, HttpServletResponse response, + @RequestParam(required = false) String img, + @RequestParam(required = false) MultipartFile attach) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } String body = request.getParameter("body"); if (body == null || body.length() < 1 || body.length() > 4096) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); @@ -157,41 +189,44 @@ public class NewMessage { } body = body.replace("\r", StringUtils.EMPTY); - List<Tag> tags = Utils.parseTags(sql, request.getParameter("tags")); + List<Tag> tags = webApp.parseTags(request.getParameter("tags")); - String attachmentFName; - try { - attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img")); - } catch (Exception e) { - logger.error("MULTIPART ERROR", e); - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + String attachmentFName = HttpUtils.receiveMultiPartFile(attach, webApp.getTmpDir()); + + if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + try { + URL imgUrl = new URL(img); + attachmentFName = HttpUtils.downloadImage(imgUrl); + } catch (Exception e) { + logger.error("DOWNLOAD ERROR", e); + throw new HttpBadRequestException(); + } } - String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; - int mid = MessagesQueries.createMessage(sql, visitor.getUid(), body, attachmentType, tags); - SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUid()); + String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; + int mid = messagesService.createMessage(visitor.getUid(), body, attachmentType, tags); + subscriptionService.subscribeMessage(mid, visitor.getUid()); Message xmsg = new Message(); xmsg.setFrom(Jid.of("juick@juick.com")); xmsg.setType(Message.Type.CHAT); xmsg.setThread("juick-" + mid); - com.juick.Message jmsg = MessagesQueries.getMessage(sql, mid); + com.juick.Message jmsg = messagesService.getMessage(mid); xmsg.addExtension(jmsg); xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - if (attachmentFName != null) { + if (StringUtils.isNotEmpty(attachmentFName)) { String fname = mid + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - Path origName = Paths.get(imgDir, "p", fname); - Files.move(Paths.get(tmpDir, attachmentFName), origName); + Path origName = Paths.get(webApp.getImgDir(), "p", fname); + Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName); Thumbnails.of(origName.toFile()).size(1024, 1024).outputQuality(0.9) - .toFile(Paths.get(imgDir, "photos-1024", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile()); Thumbnails.of(origName.toFile()).size(512, 512).outputQuality(0.9) - .toFile(Paths.get(imgDir, "photos-512", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "photos-512", fname).toFile()); Thumbnails.of(origName.toFile()).size(160, 120).outputQuality(0.9) - .toFile(Paths.get(imgDir, "ps", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "ps", fname).toFile()); body = attachmentURL + "\n" + body; try { @@ -200,26 +235,26 @@ public class NewMessage { logger.warn("invalid uri: {} exception {}", attachmentURL, e); } } - if (xmpp != null) { + if (webApp.getXmpp() != null) { xmsg.setBody("@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid); xmsg.setTo(Jid.of("juick@s2s.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("juick@ws.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("juick@push.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("twitter@crosspost.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("fb@crosspost.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("jubo@nologin.ru")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); } else { logger.warn("XMPP unavailable"); } @@ -228,8 +263,8 @@ public class NewMessage { response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - PageTemplates.pageHead(out, visitor, "Сообщение опубликовано", null); - PageTemplates.pageNavigation(out, visitor, null); + templates.pageHead(out, visitor, "Сообщение опубликовано", null); + templates.pageNavigation(out, visitor, null); String hashtags = StringUtils.EMPTY; String tagscomma = StringUtils.EMPTY; @@ -253,32 +288,40 @@ public class NewMessage { out.println("<section id=\"content\">"); out.println("<h1>Сообщение опубликовано</h1>"); out.println("<p>Поделитесь своим новым постом в социальных сетях:</p>"); - if (CrosspostQueries.getTwitterTokens(sql, visitor.getUid()).isPresent()) { + if (crosspostService.getTwitterTokens(visitor.getUid()).isPresent()) { out.println("<p class=\"social\"><a href=\"https://twitter.com/intent/tweet?text=" + URLEncoder.encode(sharetwi, CharEncoding.UTF_8) + "\" class=\"ico32-twi sharenew\">Отправить в Twitter</a></p>"); } out.println("<p class=\"social\"><a href=\"http://www.livejournal.com/update.bml?subject=" + URLEncoder.encode(hashtags, CharEncoding.UTF_8) + "&event=" + sharelj + "&prop_taglist=" + URLEncoder.encode(tagscomma, CharEncoding.UTF_8) + "\" target=\"_blank\" class=\"ico32-lj sharenew\">Отправить в LiveJournal</a></p>"); out.println("<p class=\"social\"><a href=\"https://vk.com/share.php?url=" + url + "\" class=\"ico32-vk sharenew\">Отправить в ВКонтакте</a></p>"); - if (CrosspostQueries.getFacebookToken(sql, visitor.getUid()).isPresent()) { + if (crosspostService.getFacebookToken(visitor.getUid()).isPresent()) { out.println("<p class=\"social\"><a href=\"https://www.facebook.com/sharer/sharer.php?u=" + url + "\" class=\"ico32-fb sharenew\">Отправить в Facebook</a></p>"); } out.println("<p class=\"social\"><a href=\"https://plus.google.com/share?url=" + url + "\" class=\"ico32-gp sharenew\">Отправить в Google+</a></p>"); out.println("<p>Ссылка на сообщение: <a href=\"http://juick.com/" + mid + "\">http://juick.com/" + mid + "</a></p>"); out.println("</section>"); - PageTemplates.pageHomeColumn(out, sql, visitor); + templates.pageHomeColumn(out, visitor); - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, false); + templates.pageEnd(out); } } - public void doPostComment(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/comment", method = RequestMethod.POST) + public void doPostComment(HttpServletRequest request, HttpServletResponse response, + @RequestParam(required = false) String img, + @RequestParam(required = false) MultipartFile attach) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } - com.juick.Message msg = MessagesQueries.getMessage(sql, mid); + com.juick.Message msg = messagesService.getMessage(mid); if (msg == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; @@ -287,7 +330,7 @@ public class NewMessage { int rid = NumberUtils.toInt(request.getParameter("rid"), 0); com.juick.Message reply = null; if (rid > 0) { - reply = MessagesQueries.getReply(sql, mid, rid); + reply = messagesService.getReply(mid, rid); if (reply == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; @@ -301,41 +344,35 @@ public class NewMessage { } body = body.replace("\r", StringUtils.EMPTY); - if ((msg.ReadOnly && msg.getUser().getUid() != visitor.getUid()) || UserQueries.isInBLAny(sql, msg.getUser().getUid(), visitor.getUid()) || (reply != null && UserQueries.isInBLAny(sql, reply.getUser().getUid(), visitor.getUid()))) { + if ((msg.ReadOnly && msg.getUser().getUid() != visitor.getUid()) + || userService.isInBLAny(msg.getUser().getUid(), visitor.getUid()) + || (reply != null && userService.isInBLAny(reply.getUser().getUid(), visitor.getUid()))) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } - String attachmentFName = null; - try { - attachmentFName = Utils.receiveMultiPartFile(request.getPart("attach")); - } catch (Exception e) { - logger.error("MULTIPART ERROR", e); - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } + String attachmentFName = HttpUtils.receiveMultiPartFile(attach, webApp.getTmpDir()); - String paramImg = request.getParameter("img"); - if (attachmentFName == null && paramImg != null && paramImg.length() > 10) { + if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { try { - attachmentFName = Utils.downloadImage(new URL(paramImg)); + URL imgUrl = new URL(img); + attachmentFName = HttpUtils.downloadImage(imgUrl); } catch (Exception e) { logger.error("DOWNLOAD ERROR", e); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; + throw new HttpBadRequestException(); } } - String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null; - int ridnew = MessagesQueries.createReply(sql, mid, rid, visitor.getUid(), body, attachmentType); - SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUid()); + String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; + int ridnew = messagesService.createReply(mid, rid, visitor.getUid(), body, attachmentType); + subscriptionService.subscribeMessage(mid, visitor.getUid()); Message xmsg = new Message(); xmsg.setFrom(Jid.of("juick@juick.com")); xmsg.setType(Message.Type.CHAT); xmsg.setThread("juick-" + mid); - com.juick.Message jmsg = MessagesQueries.getReply(sql, mid, ridnew); + com.juick.Message jmsg = messagesService.getReply(mid, ridnew); xmsg.addExtension(jmsg); String quote = reply != null ? reply.getText() : msg.getText(); @@ -344,18 +381,18 @@ public class NewMessage { } xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - if (attachmentFName != null) { + if (StringUtils.isNotEmpty(attachmentFName)) { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - Path origName = Paths.get(imgDir, "p", fname); - Files.move(Paths.get(tmpDir, attachmentFName), origName); + Path origName = Paths.get(webApp.getImgDir(), "p", fname); + Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName); Thumbnails.of(origName.toFile()).size(1024, 1024).outputQuality(0.9) - .toFile(Paths.get(imgDir, "photos-1024", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile()); Thumbnails.of(origName.toFile()).size(512, 512).outputQuality(0.9) - .toFile(Paths.get(imgDir, "photos-512", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "photos-512", fname).toFile()); Thumbnails.of(origName.toFile()).size(160, 120).outputQuality(0.9) - .toFile(Paths.get(imgDir, "ps", fname).toFile()); + .toFile(Paths.get(webApp.getImgDir(), "ps", fname).toFile()); body = attachmentURL + "\n" + body; try { @@ -365,19 +402,19 @@ public class NewMessage { } } - if (xmpp != null) { + if (webApp.getXmpp() != null) { xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew); xmsg.setTo(Jid.of("juick@s2s.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("juick@ws.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); xmsg.setTo(Jid.of("juick@push.juick.com")); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); } else { logger.warn("XMPP unavailable"); } @@ -385,13 +422,19 @@ public class NewMessage { Utils.sendTemporaryRedirect(response, "/" + msg.getUser().getName() + "/" + mid + "#" + ridnew); } - public void doPostRecomm(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/like", method = RequestMethod.POST) + public void doPostRecomm(HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } int mid = NumberUtils.toInt(request.getParameter("mid"), 0); if (mid == 0) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } - com.juick.Message msg = MessagesQueries.getMessage(sql, mid); + com.juick.Message msg = messagesService.getMessage(mid); if (msg == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; @@ -401,10 +444,10 @@ public class NewMessage { return; } - boolean res = MessagesQueries.recommendMessage(sql, mid, visitor.getUid()); + boolean res = messagesService.recommendMessage(mid, visitor.getUid()); if (res) { - if (xmpp != null) { + if (webApp.getXmpp() != null) { Message xmsg = new Message(); xmsg.setFrom(Jid.of("juick@juick.com")); xmsg.setTo(Jid.of("recomm@s2s.juick.com")); @@ -412,7 +455,7 @@ public class NewMessage { jmsg.setMid(mid); jmsg.setUser(visitor); xmsg.addExtension(jmsg); - xmpp.send(xmsg); + webApp.getXmpp().send(xmsg); } else { logger.warn("XMPP unavailable"); } diff --git a/juick-www/src/main/java/com/juick/www/PM.java b/juick-www/src/main/java/com/juick/www/controllers/PM.java index d0aa26e9..56b688cf 100644 --- a/juick-www/src/main/java/com/juick/www/PM.java +++ b/juick-www/src/main/java/com/juick/www/controllers/PM.java @@ -15,87 +15,100 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; -import com.juick.server.PMQueries; -import com.juick.server.TagQueries; -import com.juick.server.UserQueries; +import com.juick.service.PMQueriesService; +import com.juick.service.TagService; +import com.juick.service.UserService; import com.juick.util.MessageUtils; import com.juick.util.WebUtils; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; +import com.juick.www.Utils; +import com.juick.www.WebApp; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import rocks.xmpp.addr.Jid; -import rocks.xmpp.core.session.XmppSession; import rocks.xmpp.core.stanza.model.Message; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.PrintWriter; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * * @author Ugnich Anton */ +@Controller public class PM { private static final Logger logger = LoggerFactory.getLogger(PM.class); - protected void doGetInbox(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException, PebbleException { - + @Inject + PMQueriesService pmQueriesService; + @Inject + TagService tagService; + @Inject + UserService userService; + @Inject + WebApp webApp; + + @RequestMapping(value = "/pm/inbox", method = RequestMethod.GET) + protected String doGetInbox(HttpServletRequest request, HttpServletResponse response, ModelMap model) { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + Utils.sendTemporaryRedirect(response, "/login"); + } String title = "PM: Inbox"; - List<com.juick.Message> msgs = PMQueries.getLastPMInbox(sql, visitor.getUid()); + List<com.juick.Message> msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText()))); - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/pm_inbox.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", title); - context.put("visitor", visitor); - context.put("msgs", msgs); - context.put("tags", TagQueries.getPopularTags(sql)); - template.evaluate(out, context); - } + model.addAttribute("title", title); + model.addAttribute("visitor", visitor); + model.addAttribute("msgs", msgs); + model.addAttribute("tags", tagService.getPopularTags()); + return "views/pm_inbox"; } - protected void doGetSent(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User visitor) throws ServletException, IOException, PebbleException { - + @RequestMapping(value = "/pm/sent", method = RequestMethod.GET) + protected String doGetSent(HttpServletRequest request, HttpServletResponse response, ModelMap model) { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0) { + Utils.sendTemporaryRedirect(response, "/login"); + } String title = "PM: Sent"; - List<com.juick.Message> msgs = PMQueries.getLastPMSent(sql, visitor.getUid()); + List<com.juick.Message> msgs = pmQueriesService.getLastPMSent(visitor.getUid()); String uname = request.getParameter("uname"); if (WebUtils.isNotUserName(uname)) { uname = StringUtils.EMPTY; } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/pm_sent.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", title); - context.put("visitor", visitor); - context.put("msgs", msgs); - context.put("tags", TagQueries.getPopularTags(sql)); - context.put("uname", uname); - template.evaluate(out, context); - } + model.addAttribute("title", title); + model.addAttribute("visitor", visitor); + model.addAttribute("msgs", msgs); + model.addAttribute("tags", tagService.getPopularTags()); + model.addAttribute("uname", uname); + return "views/pm_sent"; } - public void doPostPM(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { + @RequestMapping(value = "/pm/send", method = RequestMethod.POST) + public void doPostPM(HttpServletRequest request, HttpServletResponse response) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0 || visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } String uname = request.getParameter("uname"); if (uname.startsWith("@")) { uname = uname.substring(1); } int uid = 0; if (WebUtils.isUserName(uname)) { - uid = UserQueries.getUIDbyName(sql, uname); + uid = userService.getUIDbyName(uname); } String body = request.getParameter("body"); @@ -104,13 +117,13 @@ public class PM { return; } - if (UserQueries.isInBLAny(sql, uid, visitor.getUid())) { + if (userService.isInBLAny(uid, visitor.getUid())) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } - if (PMQueries.createPM(sql, visitor.getUid(), uid, body)) { - if (xmpp != null) { + if (pmQueriesService.createPM(visitor.getUid(), uid, body)) { + if (webApp.getXmpp() != null) { Message msg = new Message(); msg.setFrom(Jid.of("juick@juick.com")); msg.setTo(Jid.of(String.format("%d@push.juick.com", uid))); @@ -118,24 +131,24 @@ public class PM { jmsg.setUser(visitor); jmsg.setText(body); msg.addExtension(jmsg); - xmpp.send(msg); + webApp.getXmpp().send(msg); msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid))); - xmpp.send(msg); + webApp.getXmpp().send(msg); - List<String> jids = UserQueries.getJIDsbyUID(sql, uid); + List<String> jids = userService.getJIDsbyUID(uid); for (String jid : jids) { Message mm = new Message(); mm.setTo(Jid.of(jid)); mm.setType(Message.Type.CHAT); - if (PMQueries.havePMinRoster(sql, visitor.getUid(), jid)) { + if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) { mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); mm.setBody(body); } else { mm.setFrom(Jid.of("juick", "juick.com", "Juick")); mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); } - xmpp.send(mm); + webApp.getXmpp().send(mm); } } else { logger.warn("XMPP unavailable"); diff --git a/juick-www/src/main/java/com/juick/www/PageTemplates.java b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java index 4a625acc..3152d5fc 100644 --- a/juick-www/src/main/java/com/juick/www/PageTemplates.java +++ b/juick-www/src/main/java/com/juick/www/controllers/PageTemplates.java @@ -15,22 +15,22 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; import com.juick.Message; -import com.juick.server.MessagesQueries; -import com.juick.server.TagQueries; -import com.juick.server.UserQueries; import com.juick.server.helpers.TagStats; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import com.juick.service.UserService; import com.juick.util.MessageUtils; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.util.StringUtils; import ru.sape.Sape; +import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -49,13 +49,20 @@ public class PageTemplates { private static final Logger logger = LoggerFactory.getLogger(PageTemplates.class); - public static Sape sape = null; + public Sape sape = null; protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM"); private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy"); private static String tagsHTML = null; - public static void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) { + @Inject + TagService tagService; + @Inject + MessagesService messagesService; + @Inject + UserService userService; + + public void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) { out.println("<!DOCTYPE html>"); out.print("<html>"); out.print("<head>"); @@ -80,7 +87,7 @@ public class PageTemplates { } } - public static void pageNavigation(PrintWriter out, com.juick.User visitor, String search) { + public void pageNavigation(PrintWriter out, com.juick.User visitor, String search) { out.println("<header>"); out.println(" <div id=\"logo\"><a href=\"/\">Juick</a></div>"); out.print(" <nav id=\"global\"><ul>"); @@ -113,13 +120,13 @@ public class PageTemplates { out.println("</header>"); } - public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor) { - pageHomeColumn(out, sql, visitor, false); + public void pageHomeColumn(PrintWriter out, com.juick.User visitor) { + pageHomeColumn(out, visitor, false); } - public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor, boolean showAdv) { + public void pageHomeColumn(PrintWriter out, com.juick.User visitor, boolean showAdv) { if (tagsHTML == null) { - tagsHTML = PageTemplates.formatPopularTags(sql, 80); + tagsHTML = formatPopularTags(80); } out.println("<aside id=\"column\">"); @@ -134,13 +141,13 @@ public class PageTemplates { out.println("</aside>"); } - public static String formatPopularTags(JdbcTemplate sql, int cnt) { - List<String> popularTags = TagQueries.getPopularTags(sql).stream() + public String formatPopularTags(int cnt) { + List<String> popularTags = tagService.getPopularTags().stream() .map(t -> "<a href=\"/tag/" + URLEncoder.encode(t) + "\">" + StringEscapeUtils.escapeHtml4(t) + "</a>").collect(Collectors.toList()); return StringUtils.collectionToDelimitedString(popularTags, " "); } - public static void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { + public void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) { out.println("<div id=\"footer\">"); out.println(" <div id=\"footer-right\"><a href=\"/settings\" rel=\"nofollow\">Настройки</a> · <a href=\"/help/ru/contacts\" rel=\"nofollow\">Контакты</a> · <a href=\"/help/\" rel=\"nofollow\">Справка</a> · <a href=\"/help/ru/adv\" rel=\"nofollow\">Реклама</a></div>"); out.print(" <div id=\"footer-social\">"); @@ -182,11 +189,11 @@ public class PageTemplates { out.println("</script>"); } - public static void pageEnd(PrintWriter out) { + public void pageEnd(PrintWriter out) { out.println("</body></html>"); } - public static String formatTags(List<TagStats> tags) { + public String formatTags(List<TagStats> tags) { String ret = org.apache.commons.lang3.StringUtils.EMPTY; for (TagStats tag : tags) { String tagName = StringEscapeUtils.escapeHtml4(tag.getTag().getName()); @@ -203,7 +210,7 @@ public class PageTemplates { return ret; } - public static String formatDate(int minutes, Date fulldate) { + public String formatDate(int minutes, Date fulldate) { if (minutes < 1) { return "сейчас"; } else if (minutes < 60) { @@ -261,14 +268,14 @@ public class PageTemplates { } } - public static String formatJSLocalTime(Date ts) { + public String formatJSLocalTime(Date ts) { return "<script type=\"text/javascript\">" + "var d=new Date(" + ts.getTime() + ");" + "document.write((d.getDate()<10?'0':'')+d.getDate()+'.'+(d.getMonth()<9?'0':'')+(d.getMonth()+1)+'.'+d.getFullYear()+' '+(d.getHours()<10?'0':'')+d.getHours()+':'+(d.getMinutes()<10?'0':'')+d.getMinutes());" + "</script>"; } - public static String formatReplies(int replies) { + public String formatReplies(int replies) { int ld = replies % 10; int lh = replies % 100; if ((lh < 10 || lh > 20) && ld == 1) { @@ -280,8 +287,8 @@ public class PageTemplates { } } - public static void printMessages(PrintWriter out, JdbcTemplate sql, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) { - List<com.juick.Message> msgs = MessagesQueries.getMessages(sql, mids); + public void printMessages(PrintWriter out, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) { + List<com.juick.Message> msgs = messagesService.getMessages(mids); for (int i = 0; i < msgs.size(); i++) { com.juick.Message msg = msgs.get(i); @@ -297,12 +304,12 @@ public class PageTemplates { for (Message msg : msgs) { blUIDs.add(msg.getUser().getUid()); } - blUIDs = UserQueries.checkBL(sql, visitor.getUid(), blUIDs); + blUIDs = userService.checkBL(visitor.getUid(), blUIDs); } for (Message msg : msgs) { - List<TagStats> tags = MessagesQueries.getMessageTags(sql, msg.getMid()); + List<TagStats> tags = tagService.getMessageTags(msg.getMid()); String tagsStr = formatTags(tags); if (msg.ReadOnly) { tagsStr += "<a>readonly</a>"; diff --git a/juick-www/src/main/java/com/juick/www/controllers/RSS.java b/juick-www/src/main/java/com/juick/www/controllers/RSS.java new file mode 100644 index 00000000..79fd8e67 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/controllers/RSS.java @@ -0,0 +1,66 @@ +/* + * Juick + * Copyright (C) 2008-2013, ugnich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.www.controllers; + +import com.juick.Message; +import com.juick.server.util.HttpNotFoundException; +import com.juick.service.MessagesService; +import com.juick.service.UserService; +import com.juick.util.DateFormattersHolder; +import com.juick.util.MessageUtils; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.inject.Inject; +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +/** + * + * @author ugnich + */ +@Controller +public class RSS { + @Inject + UserService userService; + @Inject + MessagesService messagesService; + + @RequestMapping(value = "/rss/{uname}", method = RequestMethod.GET) + protected String doGet(JdbcTemplate sql, HttpServletResponse response, + @PathVariable String uname, ModelMap model) { + int uid = userService.getUIDbyName(uname); + List<Integer> mids = messagesService.getUserBlog(uid, 0, 0); + if (mids.isEmpty()) { + throw new HttpNotFoundException(); + } + + List<Message> msgs = messagesService.getMessages(mids); + + msgs.forEach(m -> MessageUtils.formatMessage(m.getText())); + + model.addAttribute("user", msgs.stream().findFirst().get().getUser()); + model.addAttribute("msgs", msgs); + model.addAttribute("sdfRSS", DateFormattersHolder.getRssFormatterInstance()); + return "webapp/WEB-INF/layouts/rss"; + } +} diff --git a/juick-www/src/main/java/com/juick/www/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index 1b2a6c8d..63cf99e6 100644 --- a/juick-www/src/main/java/com/juick/www/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -15,25 +15,29 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; -import com.juick.server.CrosspostQueries; -import com.juick.server.SubscriptionsQueries; -import com.juick.server.TagQueries; -import com.juick.server.UserQueries; import com.juick.server.helpers.NotifyOpts; import com.juick.server.helpers.UserInfo; +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpUtils; +import com.juick.service.*; import com.juick.util.UserUtils; -import com.mitchellbosecke.pebble.error.PebbleException; -import com.mitchellbosecke.pebble.template.PebbleTemplate; +import com.juick.www.WebApp; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; +import javax.inject.Inject; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; @@ -45,15 +49,12 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.io.PrintWriter; import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -61,16 +62,28 @@ import java.util.stream.IntStream; * * @author Ugnich Anton */ +@Controller public class Settings { private static final Logger logger = LoggerFactory.getLogger(Settings.class); - private final String imgPath; - public Settings(String avatarsPath) { - this.imgPath = avatarsPath; - } + @Inject + WebApp webApp; + @Inject + TagService tagService; + @Inject + UserService userService; + @Inject + CrosspostService crosspostService; + @Inject + SubscriptionService subscriptionService; + @Inject + EmailService emailService; + @Inject + TelegramService telegramService; - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + @RequestMapping(value = "/settings", method = RequestMethod.GET) + protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); if (visitor.getUid() == 0) { response.sendRedirect("/login"); } @@ -80,61 +93,55 @@ public class Settings { page = "main"; } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate(String.format("views/settings_%s.html", page)); - Map<String, Object> context = new HashMap<>(); - context.put("title", "Настройки"); - context.put("visitor", visitor); - context.put("tags", TagQueries.getPopularTags(sql)); - context.put("auths", UserQueries.getAuthCodes(sql, visitor)); - context.put("eopts", UserQueries.getEmailOpts(sql, visitor)); - context.put("ehash", UserQueries.getEmailHash(sql, visitor)); - context.put("emails", UserQueries.getEmails(sql, visitor)); - context.put("jids", UserQueries.getAllJIDs(sql, visitor)); - List<String> hours = IntStream.rangeClosed(0, 23).boxed() - .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList()); - context.put("hours", hours); - context.put("fbstatus", CrosspostQueries.isFBCrossPostEnabled(sql, visitor.getUid())); - context.put("twitter_name", CrosspostQueries.getTwitterName(sql, visitor.getUid())); - context.put("telegram_name", CrosspostQueries.getTelegramName(sql, visitor.getUid())); - context.put("notify_options", SubscriptionsQueries.getNotifyOptions(sql, visitor)); - context.put("userinfo", UserQueries.getUserInfo(sql, visitor)); - if (page.equals("auth-email")) { - try { - String account = sql.queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?", - String.class, visitor.getUid(), request.getParameter("code")); - sql.update("INSERT INTO emails(user_id,email) VALUES (?,?)", visitor.getUid(), account); - sql.update("DELETE FROM auth WHERE user_id=? AND authcode=?", visitor.getUid(), request.getParameter("code")); - context.put("result", "OK!"); - } catch (EmptyResultDataAccessException e) { - context.put("result", "Sorry, code unknown."); - } + model.addAttribute("title", "Настройки"); + model.addAttribute("visitor", visitor); + model.addAttribute("tags", tagService.getPopularTags()); + model.addAttribute("auths", userService.getAuthCodes(visitor)); + model.addAttribute("eopts", userService.getEmailOpts(visitor)); + model.addAttribute("ehash", userService.getEmailHash(visitor)); + model.addAttribute("emails", userService.getEmails(visitor)); + model.addAttribute("jids", userService.getAllJIDs(visitor)); + List<String> hours = IntStream.rangeClosed(0, 23).boxed() + .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList()); + model.addAttribute("hours", hours); + model.addAttribute("fbstatus", crosspostService.getFbCrossPostStatus(visitor.getUid()).isCrosspostEnabled()); + model.addAttribute("twitter_name", crosspostService.getTwitterName(visitor.getUid())); + model.addAttribute("telegram_name", crosspostService.getTelegramName(visitor.getUid())); + model.addAttribute("notify_options", subscriptionService.getNotifyOptions(visitor)); + model.addAttribute("userinfo", userService.getUserInfo(visitor)); + if (page.equals("auth-email")) { + if (emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("code"))) { + ; + model.addAttribute("result", "OK!"); + } else { + model.addAttribute("result", "Sorry, code unknown."); } - template.evaluate(out, context); } + return String.format("views/settings_%s", page); } - protected void doPost(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + @RequestMapping(value = "/settings", method = RequestMethod.POST) + protected String doPost(HttpServletRequest request, HttpServletResponse response, + @RequestParam(required = false) MultipartFile avatar, + ModelMap model) + throws IOException, ServletException { + com.juick.User visitor = webApp.getVisitorUser(request, response); if (visitor.getUid() == 0) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + throw new HttpBadRequestException(); } List<String> pages = Arrays.asList("main", "password", "about", "email", "email-add", "email-del", "email-subscr", "auth-email", "privacy", "jid-del", "twitter-del", "telegram-del", "facebook-disable", "facebook-enable", "vk-del"); String page = request.getParameter("page"); if (StringUtils.isEmpty(page) || !pages.contains(page)) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; + throw new HttpBadRequestException(); } String result = StringUtils.EMPTY; switch (page) { case "password": - if (UserQueries.updatePassword(sql, visitor, request.getParameter("password"))) { + if (userService.updatePassword(visitor, request.getParameter("password"))) { result = "<p>Password has been changed.</p>"; - String hash = com.juick.server.UserQueries.getHashByUID(sql, visitor.getUid()); + String hash = userService.getHashByUID(visitor.getUid()); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); @@ -145,7 +152,7 @@ public class Settings { opts.setRepliesEnabled(StringUtils.isNotEmpty(request.getParameter("jnotify"))); opts.setSubscriptionsEnabled(StringUtils.isNotEmpty(request.getParameter("subscr_notify"))); opts.setRecommendationsEnabled(StringUtils.isNotEmpty(request.getParameter("recomm"))); - if (SubscriptionsQueries.setNotifyOptions(sql, visitor, opts)) { + if (subscriptionService.setNotifyOptions(visitor, opts)) { result = "<p>Notification options has been updated</p>"; } break; @@ -155,54 +162,52 @@ public class Settings { info.setCountry(request.getParameter("country")); info.setUrl(request.getParameter("url")); info.setDescription(request.getParameter("descr")); - String avatarTmpPath = Utils.receiveMultiPartFile(request.getPart("avatar")); + String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, webApp.getTmpDir()); if (StringUtils.isNotEmpty(avatarTmpPath)) { String originalExtension = FilenameUtils.getExtension(avatarTmpPath); String originalName = String.format("%s.%s", visitor.getUid(), originalExtension); String targetName = String.format("%s.png", visitor.getUid()); - Path ao = Paths.get(imgPath, "ao", originalName); - Path a = Paths.get(imgPath, "a", targetName); - Path as = Paths.get(imgPath, "as", targetName); - Files.move(Paths.get(Utils.getTmpDir(), avatarTmpPath), ao, StandardCopyOption.REPLACE_EXISTING); + Path ao = Paths.get(webApp.getImgDir(), "ao", originalName); + Path a = Paths.get(webApp.getImgDir(), "a", targetName); + Path as = Paths.get(webApp.getImgDir(), "as", targetName); + Files.move(Paths.get(webApp.getTmpDir(), avatarTmpPath), ao, StandardCopyOption.REPLACE_EXISTING); Thumbnails.of(ao.toFile()).size(96, 96).toFile(a.toFile()); Thumbnails.of(ao.toFile()).size(32, 32).toFile(as.toFile()); } - if (UserQueries.updateUserInfo(sql, visitor, info)) { + if (userService.updateUserInfo(visitor, info)) { result = String.format("<p>Your info is updated.</p><p><a href='/%s/'>Back to blog</a>.</p>", visitor.getName()); } break; case "jid-del": + // FIXME: stop using ugnich-csv in parameters String[] params = request.getParameter("delete").split(";", 2); - int res = -1; + boolean res = false; if (params[0].equals("xmpp")) { - res = sql.update("DELETE FROM jids WHERE user_id=? AND jid=?", visitor.getUid(), params[1]); + res = userService.deleteJID(visitor.getUid(), params[1]); } else if (params[0].equals("xmpp-unauth")) { - res = sql.update("DELETE FROM auth WHERE user_id=? AND protocol='xmpp' AND account=?", visitor.getUid(), params[1]); + res = userService.unauthJID(visitor.getUid(), params[1]); } - if (res == 1) { + if (res) { result = "<p>Deleted. <a href=\"/settings\">Back</a>.</p>"; } else { result = "<p>Error</p>"; } break; case "email": - String newHash = UserQueries.updateSecretEmail(sql, visitor); + String newHash = userService.updateSecretEmail(visitor); if (StringUtils.isNotEmpty(newHash)) { result = String.format("<p>New secret email: <strong>%s@mail.juick.com</strong></p>" + "<p><a href=\"/settings\">Back</a>.</p>", newHash); } else { - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; + throw new HttpBadRequestException(); } break; case "email-add": try { - sql.queryForObject("SELECT authcode FROM auth WHERE user_id=? AND protocol='email' " + - "AND account=?", String.class, visitor.getUid(), request.getParameter("account")); + emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("account")); } catch (EmptyResultDataAccessException e) { String authCode = UserUtils.generateHash(8); - if (sql.update("INSERT INTO auth(user_id,protocol,account,authcode) VALUES (?,'email',?,?)", - visitor.getUid(), request.getParameter("account"), authCode) > 0) { + if (emailService.addVerificationCode(visitor.getUid(), request.getParameter("account"), authCode)) { Session session = Session.getDefaultInstance(System.getProperties()); try { MimeMessage message = new MimeMessage(session); @@ -218,34 +223,30 @@ public class Settings { } catch (MessagingException ex) { logger.error("mail exception", ex); - response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - return; + throw new HttpBadRequestException(); } } } break; case "email-del": - if (sql.update("DELETE FROM emails WHERE user_id=? AND email=?", visitor.getUid(), request.getParameter("account")) > 0) { + if (emailService.deleteEmail(visitor.getUid(), request.getParameter("account"))) { result = "<p>Deleted. <a href=\"/settings\">Back</a>.</p>"; } else { result = "<p>An error occured while deleting.</p>"; } break; case "email-subscr": - sql.update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", visitor.getUid()); - String email = request.getParameter("account"); - if (StringUtils.isNotEmpty(email)) { - sql.update("UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?", - request.getParameter("time"), visitor.getUid(), email); + if (emailService.setSubscriptionHour(visitor.getUid(), request.getParameter("account"), + request.getParameter("time"))) { result = String.format("<p>Saved! Will send to <strong>%s</strong> at <strong>%s:00 GMT</strong>." + - "</p><p><a href=\"/settings\">Back</a></p>", email, request.getParameter("time")); + "</p><p><a href=\"/settings\">Back</a></p>", request.getParameter("account"), + request.getParameter("time")); } else { result = "<p>Disabled.</p><p><a href=\"/settings\">Back</a></p>"; } break; case "twitter-del": - sql.update("DELETE FROM twitter WHERE user_id=?", visitor.getUid()); - sql.update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=1741", visitor.getUid()); + crosspostService.deleteTwitterToken(visitor.getUid()); for (Cookie cookie : request.getCookies()) { if (cookie.getName().equals("request_token")) { cookie.setMaxAge(0); @@ -259,35 +260,28 @@ public class Settings { result = "<p><a href=\"/settings\">Back</a></p>"; break; case "telegram-del": - sql.update("DELETE FROM telegram WHERE user_id=?", visitor.getUid()); + telegramService.deleteTelegramUser(visitor.getUid()); result = "<p><a href=\"/settings\">Back</a></p>"; break; case "facebook-disable": - sql.update("UPDATE facebook SET crosspost=0 WHERE user_id=?", visitor.getUid()); - sql.update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=5863", visitor.getUid()); + crosspostService.disableFBCrosspost(visitor.getUid()); result = "<p><a href=\"/settings\">Back</a></p>"; break; case "facebook-enable": - sql.update("UPDATE facebook SET crosspost=1 WHERE user_id=?", visitor.getUid()); - sql.update("INSERT INTO subscr_users(user_id,suser_id,jid,active) VALUES (?,5863,'juick@facebook.juick.com',1)", visitor.getUid()); + crosspostService.enableFBCrosspost(visitor.getUid()); result = "<p><a href=\"/settings\">Back</a></p>"; break; case "vk-del": - sql.update("DELETE FROM vk WHERE user_id=?", visitor.getUid()); + crosspostService.deleteVKUser(visitor.getUid()); result = "<p><a href=\"/settings\">Back</a></p>"; break; default: - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - response.setContentType("text/html; charset=UTF-8"); - try (PrintWriter out = response.getWriter()) { - PebbleTemplate template = Utils.getEngine().getTemplate("views/settings_result.html"); - Map<String, Object> context = new HashMap<>(); - context.put("title", "Настройки"); - context.put("visitor", visitor); - context.put("result", result); - template.evaluate(out, context); + throw new HttpBadRequestException(); } + + model.addAttribute("title", "Настройки"); + model.addAttribute("visitor", visitor); + model.addAttribute("result", result); + return "views/settings_result"; } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java new file mode 100644 index 00000000..937a3242 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java @@ -0,0 +1,170 @@ +/* + * Juick + * Copyright (C) 2008-2013, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package com.juick.www.controllers; + +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.service.CrosspostService; +import com.juick.service.UserService; +import com.juick.www.Utils; +import com.juick.www.WebApp; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.inject.Inject; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Ugnich Anton + */ +@Controller +public class SignUp { + + @Inject + WebApp webApp; + @Inject + UserService userService; + @Inject + CrosspostService crosspostService; + + + @RequestMapping(value = "/signup", method = RequestMethod.GET) + protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) { + com.juick.User visitor = webApp.getVisitorUser(request, response); + + String type = request.getParameter("type"); + String hash = request.getParameter("hash"); + if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 + || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { + throw new HttpBadRequestException(); + } + + String account = null; + switch (type) { + case "fb": + account = crosspostService.getFacebookNameByHash(hash); + break; + case "vk": + account = crosspostService.getVKNameByHash(hash); + break; + case "xmpp": + account = crosspostService.getJIDByHash(hash); + break; + case "durov": + account = crosspostService.getTelegramNameByHash(hash); + break; + } + if (account == null) { + throw new HttpBadRequestException(); + } + + model.addAttribute("title", "Новый пользователь"); + model.addAttribute("visitor", visitor); + model.addAttribute("account", account); + model.addAttribute("type", type); + model.addAttribute("hash", hash); + return "views/signup"; + } + + @RequestMapping(value = "/signup", method = RequestMethod.POST) + protected String doPost(HttpServletRequest request, HttpServletResponse response) { + com.juick.User visitor = webApp.getVisitorUser(request, response); + int uid = 0; + + String type = request.getParameter("type"); + String hash = request.getParameter("hash"); + if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { + throw new HttpBadRequestException(); + } + + String action = request.getParameter("action"); + if (action.charAt(0) == 'l') { + + if (visitor.getUid() == 0) { + String username = request.getParameter("username"); + String password = request.getParameter("password"); + if (username == null || password == null || username.length() > 32 || password.isEmpty()) { + throw new HttpBadRequestException(); + } + uid = userService.checkPassword(username, password); + } else { + uid = visitor.getUid(); + } + + if (uid <= 0) { + throw new HttpForbiddenException(); + } + + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid))) { + throw new HttpBadRequestException(); + } + + } else { // Create new account + String username = request.getParameter("username"); + String password = request.getParameter("password"); + if (username == null || password == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) { + throw new HttpBadRequestException(); + } + + // CHECK USERNAME + + uid = userService.createUser(username, password); + if (uid <= 0) { + throw new HttpBadRequestException(); + } + + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid))) { + throw new HttpBadRequestException(); + } + + int ref = 0; + String sRef = Utils.getCookie(request, "ref"); + if (sRef != null) { + try { + ref = Integer.parseInt(sRef); + } catch (Exception e) { + } + } + + if (ref > 0) { + crosspostService.setUserRef(uid, ref); + } + + visitor = null; + } + + if (visitor == null) { + hash = userService.getHashByUID(uid); + Cookie c = new Cookie("hash", hash); + c.setMaxAge(365 * 24 * 60 * 60); + response.addCookie(c); + } + return "redirect:/"; + } +} diff --git a/juick-www/src/main/java/com/juick/www/TwitterAuth.java b/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java index 35e00efc..901a8362 100644 --- a/juick-www/src/main/java/com/juick/www/TwitterAuth.java +++ b/juick-www/src/main/java/com/juick/www/controllers/TwitterAuth.java @@ -1,4 +1,4 @@ -package com.juick.www; +package com.juick.www.controllers; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; @@ -9,11 +9,14 @@ import com.github.scribejava.core.model.OAuth1RequestToken; import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.oauth.OAuth10aService; -import com.juick.server.UserQueries; +import com.juick.service.UserService; import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -22,6 +25,7 @@ import java.io.IOException; /** * Created by vt on 01.12.2015. */ +@Controller public class TwitterAuth { private final static String VERIFY_URL = "https://api.twitter.com/1.1/account/verify_credentials.json"; @@ -30,17 +34,22 @@ public class TwitterAuth { private final ObjectMapper mapper; - public TwitterAuth(String consumerKey, String consumerSecret) { - this.consumerKey = consumerKey; - this.consumerSecret = consumerSecret; + @Inject + UserService userService; + + @Inject + public TwitterAuth(Environment env) { + this.consumerKey = env.getProperty("twitter_consumer_key"); + this.consumerSecret = env.getProperty("twitter_consumer_secret"); mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); } - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { + @RequestMapping(value = "/_twitter", method = RequestMethod.GET) + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException { String hash = StringUtils.EMPTY, request_token = StringUtils.EMPTY, request_token_secret = StringUtils.EMPTY; String verifier = request.getParameter("oauth_verifier"); Cookie[] cookies = request.getCookies(); @@ -55,7 +64,7 @@ public class TwitterAuth { request_token_secret = cookie.getValue(); } } - com.juick.User user = UserQueries.getUserByHash(sql, hash); + com.juick.User user = userService.getUserByHash(hash); if ( user == null || user.getUid() == 0) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; @@ -81,7 +90,7 @@ public class TwitterAuth { OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, VERIFY_URL, oAuthService.getConfig()); oAuthService.signRequest(accessToken, oAuthRequest); com.juick.www.twitter.User twitterUser = mapper.readValue(oAuthRequest.send().getBody(), com.juick.www.twitter.User.class); - if (UserQueries.linkTwitterAccount(sql, user, accessToken.getToken(), accessToken.getTokenSecret(), + if (userService.linkTwitterAccount(user, accessToken.getToken(), accessToken.getTokenSecret(), twitterUser.getScreenName())) { response.setStatus(HttpServletResponse.SC_FOUND); response.setHeader("Location", "http://juick.com/settings"); diff --git a/juick-www/src/main/java/com/juick/www/User.java b/juick-www/src/main/java/com/juick/www/controllers/User.java index 926ce3e3..d3406f4e 100644 --- a/juick-www/src/main/java/com/juick/www/User.java +++ b/juick-www/src/main/java/com/juick/www/controllers/User.java @@ -15,19 +15,24 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; -import com.juick.server.MessagesQueries; -import com.juick.server.TagQueries; -import com.juick.server.UserQueries; import com.juick.server.helpers.TagStats; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.www.Utils; +import com.juick.www.WebApp; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import javax.inject.Inject; import javax.servlet.ServletException; -import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @@ -42,10 +47,28 @@ import java.util.stream.Collectors; * * @author Ugnich Anton */ +@Controller public class User { - - protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); + @Inject + WebApp webApp; + @Inject + UserService userService; + @Inject + TagService tagService; + @Inject + MessagesService messagesService; + @Inject + PageTemplates templates; + + @RequestMapping("/{uname}/") + protected void doGetBlog(HttpServletRequest request, HttpServletResponse response, + @PathVariable String uname) throws IOException { + com.juick.User user = userService.getUserByName(uname); + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } List<Integer> mids; @@ -55,10 +78,10 @@ public class User { String paramTagStr = request.getParameter("tag"); if (paramTagStr != null) { if (paramTagStr.length() < 64) { - paramTag = TagQueries.getTag(sql, paramTagStr, false); + paramTag = tagService.getTag(paramTagStr, false); } if (paramTag == null) { - Errors.doGet404(sql, request, response); + response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } else if (!paramTag.getName().equals(paramTagStr)) { String url = "/" + user.getName() + "/?tag=" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8); @@ -85,7 +108,7 @@ public class User { if (visitor.getUid() > 0) { if (user.getUid() == visitor.getUid() || visitor.getUid() == 1) { privacy = -3; - } else if (UserQueries.isInWL(sql, user.getUid(), visitor.getUid())) { + } else if (userService.isInWL(user.getUid(), visitor.getUid())) { privacy = -2; } } @@ -94,41 +117,37 @@ public class User { if (paramShow == null) { if (paramTag != null) { title = "Блог " + user.getName() + ": *" + StringEscapeUtils.escapeHtml4(paramTag.getName()); - mids = MessagesQueries.getUserTag(sql, user.getUid(), paramTag.TID, privacy, paramBefore); + mids = messagesService.getUserTag(user.getUid(), paramTag.TID, privacy, paramBefore); } else if (paramSearch != null) { title = "Блог " + user.getName() + ": " + StringEscapeUtils.escapeHtml4(paramSearch); - mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.getUid(), Utils.encodeSphinx(paramSearch), privacy, paramBefore); + mids = messagesService.getUserSearch(user.getUid(), Utils.encodeSphinx(paramSearch), privacy, paramBefore); } else { title = "Блог " + user.getName(); - mids = MessagesQueries.getUserBlog(sql, user.getUid(), privacy, paramBefore); + mids = messagesService.getUserBlog(user.getUid(), privacy, paramBefore); } } else if (paramShow.equals("recomm")) { title = "Рекомендации " + user.getName(); - mids = MessagesQueries.getUserRecommendations(sql, user.getUid(), paramBefore); + mids = messagesService.getUserRecommendations(user.getUid(), paramBefore); } else if (paramShow.equals("photos")) { title = "Фотографии " + user.getName(); - mids = MessagesQueries.getUserPhotos(sql, user.getUid(), privacy, paramBefore); + mids = messagesService.getUserPhotos(user.getUid(), privacy, paramBefore); } else { - Errors.doGet404(sql, request, response); + response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } - if (visitor.getUid() == 0) { - pageUserRefCookie(request, response, user.getUid()); - } - response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.getName() + "\" href=\"//rss.juick.com/" + user.getName() + "/blog\"/>"; - if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) { + if (paramTag != null && tagService.getTagNoIndex(paramTag.TID)) { head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; } else if (paramBefore > 0 || paramShow != null) { head += "<meta name=\"robots\" content=\"noindex\"/>"; } - PageTemplates.pageHead(out, visitor, title, head); - PageTemplates.pageNavigation(out, visitor, null); - pageUserColumn(out, sql, user, visitor); + templates.pageHead(out, visitor, title, head); + templates.pageNavigation(out, visitor, null); + pageUserColumn(out, user, visitor); if (mids.size() > 0) { out.println("<section id=\"content\">"); @@ -139,7 +158,7 @@ public class User { StringEscapeUtils.escapeHtml4(paramTag.getName()) + "</b></a></p>"); } - PageTemplates.printMessages(out, sql, user, mids, visitor, visitor.getUid() == 0 ? 4 : 5, 0); + templates.printMessages(out, user, mids, visitor, visitor.getUid() == 0 ? 4 : 5, 0); if (mids.size() >= 20) { String nextpage = "?before=" + mids.get(mids.size() - 1); @@ -158,52 +177,58 @@ public class User { out.println("</section>"); } - PageTemplates.pageFooter(request, out, visitor, true); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, true); + templates.pageEnd(out); } } - protected void doGetTags(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - if (visitor.getUid() == 0) { - pageUserRefCookie(request, response, user.getUid()); + @RequestMapping(value = "/{uname}/tags", method = RequestMethod.GET) + protected void doGetTags(HttpServletRequest request, HttpServletResponse response, + @PathVariable String uname) throws IOException { + com.juick.User user = userService.getUserByName(uname); + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>"; - PageTemplates.pageHead(out, visitor, "Теги " + user.getName(), head); - PageTemplates.pageNavigation(out, visitor, null); - pageUserColumn(out, sql, user, visitor); + templates.pageHead(out, visitor, "Теги " + user.getName(), head); + templates.pageNavigation(out, visitor, null); + pageUserColumn(out, user, visitor); out.println("<section id=\"content\">"); - out.println("<p>" + pageUserTags(sql, user, visitor, 0) + "</p>"); + out.println("<p>" + pageUserTags(user, visitor, 0) + "</p>"); out.println("</section>"); - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, false); + templates.pageEnd(out); } } - protected void doGetFriends(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - if (visitor.getUid() == 0) { - pageUserRefCookie(request, response, user.getUid()); + @RequestMapping(value = "/{uname}/friends", method = RequestMethod.GET) + protected void doGetFriends(HttpServletRequest request, HttpServletResponse response, + @PathVariable String uname) throws ServletException, IOException { + com.juick.User user = userService.getUserByName(uname); + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = "<meta name=\"robots\" content=\"noindex\"/>"; - PageTemplates.pageHead(out, visitor, "Подписки " + user.getName(), head); - PageTemplates.pageNavigation(out, visitor, null); - pageUserColumn(out, sql, user, visitor); + templates.pageHead(out, visitor, "Подписки " + user.getName(), head); + templates.pageNavigation(out, visitor, null); + pageUserColumn(out, user, visitor); out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); - List<com.juick.User> friends = UserQueries.getUserFriends(sql, user.getUid()); + List<com.juick.User> friends = userService.getUserFriends(user.getUid()); for (int i = 0; i < friends.size(); i++) { if (i % 3 == 0 && i > 0) { out.print("</tr><tr>"); @@ -216,29 +241,32 @@ public class User { out.println("</tr></table>"); out.println("</section>"); - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + templates.pageFooter(request, out, visitor, false); + templates.pageEnd(out); } } - protected void doGetReaders(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - if (visitor.getUid() == 0) { - pageUserRefCookie(request, response, user.getUid()); + @RequestMapping(value = "/{uname}/readers", method = RequestMethod.GET) + protected void doGetReaders(HttpServletRequest request, HttpServletResponse response, + @PathVariable String uname) throws ServletException, IOException { + com.juick.User user = userService.getUserByName(uname); + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String head = "<meta name=\"robots\" content=\"noindex\"/>"; - PageTemplates.pageHead(out, visitor, "Читатели " + user.getName(), head); - PageTemplates.pageNavigation(out, visitor, null); - pageUserColumn(out, sql, user, visitor); + templates.pageHead(out, visitor, "Читатели " + user.getName(), head); + templates.pageNavigation(out, visitor, null); + pageUserColumn(out, user, visitor); out.println("<section id=\"content\">"); out.println("<table class=\"users\"><tr>"); - List<com.juick.User> readers = UserQueries.getUserReaders(sql, user.getUid()); + List<com.juick.User> readers = userService.getUserReaders(user.getUid()); for (int i = 0; i < readers.size(); i++) { if (i % 3 == 0 && i > 0) { out.print("</tr><tr>"); @@ -251,39 +279,27 @@ public class User { out.println("</tr></table>"); out.println("</section>"); - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); - } - } - - public static void pageUserRefCookie(HttpServletRequest request, HttpServletResponse response, int uid) { - String hReferer = request.getHeader("Referer"); - String ref = Utils.getCookie(request, "ref"); - - if (ref == null && (hReferer == null || !(hReferer.startsWith("http://juick.com/") || hReferer.startsWith("https://juick.com/")))) { - Cookie c = new Cookie("ref", Integer.toString(uid)); - c.setMaxAge(7 * 24 * 60 * 60); - c.setPath("/"); - response.addCookie(c); + templates.pageFooter(request, out, visitor, false); + templates.pageEnd(out); } } - public static void pageUserColumn(PrintWriter out, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) { + public void pageUserColumn(PrintWriter out, com.juick.User user, com.juick.User visitor) { out.println("<aside id=\"column\">"); out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUid() + ".png\" alt=\"\"/>" + user.getName() + "</a></div>"); if (visitor.getUid() > 0 && visitor.getUid() != user.getUid()) { out.println(" <ul id=\"ctoolbar\">"); - if (UserQueries.isSubscribed(sql, visitor.getUid(), user.getUid())) { + if (userService.isSubscribed(visitor.getUid(), user.getUid())) { out.println(" <li><a href=\"/post?body=U+%40" + user.getName() + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>"); } else { out.println(" <li><a href=\"/post?body=S+%40" + user.getName() + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>"); } - if (UserQueries.isInBL(sql, visitor.getUid(), user.getUid())) { + if (userService.isInBL(visitor.getUid(), user.getUid())) { out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>"); } else { out.println(" <li><a href=\"/post?body=BL+%40" + user.getName() + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>"); } - if (!UserQueries.isInBLAny(sql, user.getUid(), visitor.getUid())) { + if (!userService.isInBLAny(user.getUid(), visitor.getUid())) { out.println(" <li><a href=\"/pm/sent?uname=" + user.getName() + "\" title=\"Написать приватное сообщение\"><div style=\"background-position: -112px 0\"></div></a></li>"); } out.println(" </ul>"); @@ -299,16 +315,16 @@ public class User { out.println(" <form action=\"./\">"); out.println(" <p><input type=\"text\" name=\"search\" class=\"inp\" placeholder=\"Поиск\"/></p>"); out.println(" </form>"); - out.println(" <p class=\"tags\">" + pageUserTags(sql, user, visitor, 20) + "<a href=\"./tags\" rel=\"nofollow\">...</a></p>"); + out.println(" <p class=\"tags\">" + pageUserTags(user, visitor, 20) + "<a href=\"./tags\" rel=\"nofollow\">...</a></p>"); out.println(" <hr/>"); out.println(" <div id=\"ustats\"><ul>"); - out.println(" <li><a href=\"./friends\">Я читаю: " + UserQueries.getStatsIRead(sql, user.getUid()) + "</a></li>"); - out.println(" <li><a href=\"./readers\">Мои подписчики: " + UserQueries.getStatsMyReaders(sql, user.getUid()) + "</a></li>"); - out.println(" <li>Сообщений: " + UserQueries.getStatsMessages(sql, user.getUid()) + "</li>"); - out.println(" <li>Комментариев: " + UserQueries.getStatsReplies(sql, user.getUid()) + "</li>"); + out.println(" <li><a href=\"./friends\">Я читаю: " + userService.getStatsIRead(user.getUid()) + "</a></li>"); + out.println(" <li><a href=\"./readers\">Мои подписчики: " + userService.getStatsMyReaders(user.getUid()) + "</a></li>"); + out.println(" <li>Сообщений: " + userService.getStatsMessages(user.getUid()) + "</li>"); + out.println(" <li>Комментариев: " + userService.getStatsReplies(user.getUid()) + "</li>"); out.println(" </ul>"); - List<com.juick.User> iread = UserQueries.getUserReadLeastPopular(sql, user.getUid(), 8); + List<com.juick.User> iread = userService.getUserReadLeastPopular(user.getUid(), 8); if (!iread.isEmpty()) { out.println("<table class=\"iread\"><tr>"); for (int i = 0; i < iread.size(); i++) { @@ -325,8 +341,8 @@ public class User { out.println("</aside>"); } - public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) { - List<TagStats> tags = TagQueries.getTagsStats(sql, user.getUid()).stream() + public String pageUserTags(com.juick.User user, com.juick.User visitor, int cnt) { + List<TagStats> tags = tagService.getUserTagStats(user.getUid()).stream() .sorted((e1, e2) -> Integer.compare(e2.getUsageCount(), e1.getUsageCount())).collect(Collectors.toList()); int maxUsageCnt = tags.stream().map(TagStats::getUsageCount).max(Comparator.naturalOrder()).orElse(0); String ret = StringUtils.EMPTY; diff --git a/juick-www/src/main/java/com/juick/www/UserThread.java b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java index 79ec42fc..4020e149 100644 --- a/juick-www/src/main/java/com/juick/www/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java @@ -15,15 +15,21 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; import com.juick.Message; -import com.juick.server.MessagesQueries; -import com.juick.server.UserQueries; import com.juick.server.helpers.TagStats; +import com.juick.service.MessagesService; +import com.juick.service.TagService; +import com.juick.service.UserService; import com.juick.util.MessageUtils; -import org.springframework.jdbc.core.JdbcTemplate; +import com.juick.www.WebApp; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import javax.inject.Inject; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,17 +42,31 @@ import java.util.List; * * @author Ugnich Anton */ +@Controller public class UserThread { - protected void doGetThread(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, int MID) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - if (!MessagesQueries.canViewThread(sql, MID, visitor.getUid())) { + @Inject + WebApp webApp; + @Inject + MessagesService messagesService; + @Inject + UserService userService; + @Inject + TagService tagService; + @Inject + PageTemplates templates; + + @RequestMapping(value = "/{uname}/{mid}", method = RequestMethod.GET) + protected void doGetThread(HttpServletRequest request, HttpServletResponse response, + @PathVariable int mid) throws ServletException, IOException { + com.juick.User visitor = webApp.getVisitorUser(request, response); + + if (!messagesService.canViewThread(mid, visitor.getUid())) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } - com.juick.Message msg = MessagesQueries.getMessage(sql, MID); + com.juick.Message msg = messagesService.getMessage(mid); boolean listview = false; String paramView = request.getParameter("view"); @@ -54,21 +74,17 @@ public class UserThread { if (paramView.equals("list")) { listview = true; if (visitor.getUid() > 0) { - UserQueries.setUserOptionInt(sql, visitor.getUid(), "repliesview", 1); + userService.setUserOptionInt(visitor.getUid(), "repliesview", 1); } } else if (paramView.equals("tree") && visitor.getUid() > 0) { - UserQueries.setUserOptionInt(sql, visitor.getUid(), "repliesview", 0); + userService.setUserOptionInt(visitor.getUid(), "repliesview", 0); } - } else if (visitor.getUid() > 0 && UserQueries.getUserOptionInt(sql, visitor.getUid(), "repliesview", 0) == 1) { + } else if (visitor.getUid() > 0 && userService.getUserOptionInt(visitor.getUid(), "repliesview", 0) == 1) { listview = true; } String title = msg.getUser().getName() + ": " + msg.getTagsString(); - if (visitor.getUid() > 0) { - User.pageUserRefCookie(request, response, msg.getUser().getUid()); - } - response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String headers = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + msg.getUser().getName() + "\" href=\"//rss.juick.com/" + msg.getUser().getName() + "/blog\"/>"; @@ -78,25 +94,25 @@ public class UserThread { if (msg.Hidden) { headers += "<meta name=\"robots\" content=\"noindex\"/>"; } - PageTemplates.pageHead(out, visitor, title, headers); - PageTemplates.pageNavigation(out, visitor, null); + templates.pageHead(out, visitor, title, headers); + templates.pageNavigation(out, visitor, null); out.println("<section id=\"content\" data-mid=\"" + msg.getMid() + "\" style=\"margin-left: 0; width: 100%\">"); - printMessage(out, sql, msg, visitor); - printReplies(out, sql, msg, visitor, listview); + printMessage(out, msg, visitor); + printReplies(out, msg, visitor, listview); out.println("</section>"); - PageTemplates.pageFooter(request, out, visitor, false); + templates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); + templates.pageEnd(out); } } - public static com.juick.Message printMessage(PrintWriter out, JdbcTemplate sql, com.juick.Message msg, com.juick.User visitor) { + public com.juick.Message printMessage(PrintWriter out, com.juick.Message msg, com.juick.User visitor) { msg.VisitorCanComment = visitor.getUid() > 0; - List<TagStats> tags = MessagesQueries.getMessageTags(sql, msg.getMid()); - String tagsStr = PageTemplates.formatTags(tags); + List<TagStats> tags = tagService.getMessageTags(msg.getMid()); + String tagsStr = templates.formatTags(tags); if (msg.ReadOnly) { tagsStr += "<a>readonly</a>"; msg.VisitorCanComment = false; @@ -120,7 +136,7 @@ public class UserThread { out.println(" <li id=\"msg-" + msg.getMid() + "\" data-mid=\"" + msg.getMid() + "\" class=\"msg msgthread\">"); out.println(" <div class=\"msg-cont\">"); out.println(" <div class=\"msg-menu\"><a href=\"#\"></a></div>"); - out.println(" <div class=\"msg-ts\">" + PageTemplates.formatJSLocalTime(msg.getDate()) + "</div>"); + out.println(" <div class=\"msg-ts\">" + templates.formatJSLocalTime(msg.getDate()) + "</div>"); out.println(" <div class=\"msg-avatar\"><a href=\"/" + msg.getUser().getName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUid() + ".png\" alt=\"" + msg.getUser().getName() + "\"/></a></div>"); out.println(" <div class=\"msg-header\">@<a href=\"/" + msg.getUser().getName() + "/\">" + msg.getUser().getName() + "</a>:" + tagsStr + "</div>"); out.println(" <div class=\"msg-txt\">" + txt + "</div>"); @@ -134,7 +150,7 @@ public class UserThread { if (visitor.getUid() == msg.getUser().getUid()) { msg.VisitorCanComment = true; } else { - visitorInBL = UserQueries.isInBL(sql, msg.getUser().getUid(), visitor.getUid()); + visitorInBL = userService.isInBL(msg.getUser().getUid(), visitor.getUid()); if (visitorInBL) { msg.VisitorCanComment = false; } @@ -147,7 +163,7 @@ public class UserThread { out.println(" </form>"); } - List<String> recomm = MessagesQueries.getMessageRecommendations(sql, msg.getMid()); + List<String> recomm = messagesService.getMessageRecommendations(msg.getMid()); if (!recomm.isEmpty()) { out.print(" <div class=\"msg-recomms\">Рекомендовали (" + recomm.size() + "): "); for (int i = 0; i < recomm.size(); i++) { @@ -165,7 +181,7 @@ public class UserThread { out.println(" <li><a href=\"/" + msg.getMid() + "\"><div style=\"background-position: -64px 0\"></div>" + msg.getMid() + "</a></li>"); if (visitor.getUid() > 0) { if (visitor.getUid() != msg.getUser().getUid()) { - if (MessagesQueries.isSubscribed(sql, visitor.getUid(), msg.getMid())) { + if (messagesService.isSubscribed(visitor.getUid(), msg.getMid())) { out.println(" <li><a href=\"/post?body=U+%23" + msg.getMid() + "\"><div style=\"background-position: -48px 0\"></div>Подписан</a></li>"); } else { out.println(" <li><a href=\"/post?body=S+%23" + msg.getMid() + "\"><div style=\"background-position: -16px 0\"></div>Подписаться</a></li>"); @@ -183,8 +199,8 @@ public class UserThread { return msg; } - public static void printReplies(PrintWriter out, JdbcTemplate sql, com.juick.Message msg, com.juick.User visitor, boolean listview) { - List<com.juick.Message> replies = MessagesQueries.getReplies(sql, msg.getMid()); + public void printReplies(PrintWriter out, com.juick.Message msg, com.juick.User visitor, boolean listview) { + List<com.juick.Message> replies = messagesService.getReplies(msg.getMid()); List<Integer> blUIDs = new ArrayList<Integer>(); for (int i = 0; i < replies.size(); i++) { @@ -213,7 +229,7 @@ public class UserThread { reply.VisitorCanComment = true; } } else if (visitor.getUid() > 0 && msg.VisitorCanComment) { - blUIDs = UserQueries.checkBL(sql, visitor.getUid(), blUIDs); + blUIDs = userService.checkBL(visitor.getUid(), blUIDs); for (Message reply : replies) { reply.VisitorCanComment = reply.getUser().getUid() == visitor.getUid() || !blUIDs.contains(reply.getUser().getUid()); } @@ -262,7 +278,7 @@ public class UserThread { } } - public static void printTree(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor, int ReplyTo, int margin, boolean hidden) { + public void printTree(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor, int ReplyTo, int margin, boolean hidden) { if (margin > 240) { margin = 240; } @@ -289,7 +305,7 @@ public class UserThread { out.println(" <div class=\"msg-avatar\"><img src=\"//i.juick.com/av-96.png\"/></div>"); } out.println(" <div class=\"msg-menu\"><a href=\"#\" class=\"a-thread-links\"></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>"); + out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + templates.sdfSQL.format(msg.getDate()) + " GMT\">" + templates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>"); out.println(" </div>"); out.println(" <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>"); if (msg.getAttachmentType() != null) { @@ -308,7 +324,7 @@ public class UserThread { int childs = msg.getChildsCount(); if (ReplyTo == 0 && childs > 1 && replies.size() > 10) { - out.println(" <div class=\"msg-comments\"><a href=\"#\">" + PageTemplates.formatReplies(childs) + "</a></div>"); + out.println(" <div class=\"msg-comments\"><a href=\"#\">" + templates.formatReplies(childs) + "</a></div>"); } out.println(" </div>"); out.println(" </li>"); @@ -322,7 +338,7 @@ public class UserThread { } } - public static void printList(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor) { + public void printList(PrintWriter out, List<com.juick.Message> replies, com.juick.User visitor) { for (Message msg : replies) { out.print(" <li id=\"" + msg.getRid() + "\" class=\"msg\">"); out.println(" <div class=\"msg-cont\">"); @@ -335,7 +351,7 @@ public class UserThread { out.println(" <div class=\"msg-avatar\"><img src=\"//i.juick.com/av-96.png\"/></div>"); } out.println(" <div class=\"msg-menu\"><a href=\"#\" class=\"a-thread-links\"></a></div>"); - out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + PageTemplates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>"); + out.println(" <div class=\"msg-ts\"><a href=\"/" + msg.getMid() + "#" + msg.getRid() + "\" title=\"" + PageTemplates.sdfSQL.format(msg.getDate()) + " GMT\">" + templates.formatDate(msg.TimeAgo, msg.getDate()) + "</a></div>"); out.println(" </div>"); out.println(" <div class=\"msg-txt\">" + MessageUtils.formatMessage(msg.getText()) + "</div>"); if (msg.getAttachmentType() != null) { diff --git a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java index 2184fc9a..d860a7bc 100644 --- a/juick-www/src/main/java/com/juick/www/VKontakteLogin.java +++ b/juick-www/src/main/java/com/juick/www/controllers/VKontakteLogin.java @@ -15,21 +15,24 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.www; +package com.juick.www.controllers; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.server.UserQueries; +import com.juick.service.CrosspostService; +import com.juick.service.UserService; +import com.juick.www.Utils; import com.juick.www.vk.Token; import com.juick.www.vk.UsersResponse; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.ServletException; +import javax.inject.Inject; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -40,12 +43,18 @@ import java.util.UUID; /** * @author Ugnich Anton */ +@Controller public class VKontakteLogin { private static final Logger logger = LoggerFactory.getLogger(VKontakteLogin.class); private static final String VK_APPID = "3544101"; private static final String VK_SECRET = "z2afNI8jA5lIpZ2jsTm1"; private static final String VK_REDIRECT = "http://juick.com/_vklogin"; + @Inject + CrosspostService crosspostService; + @Inject + UserService userService; + public VKontakteLogin() { mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); @@ -55,7 +64,8 @@ public class VKontakteLogin { private final ObjectMapper mapper; - protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + @RequestMapping(value = "/_vklogin", method = RequestMethod.GET) + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String code = request.getParameter("code"); if (StringUtils.isBlank(code)) { response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); @@ -97,16 +107,16 @@ public class VKontakteLogin { throw new Exception(); } - int uid = getUIDbyVKID(sql, vkID); + int uid = crosspostService.getUIDbyVKID(vkID); if (uid > 0) { - Cookie c = new Cookie("hash", UserQueries.getHashByUID(sql, uid)); + Cookie c = new Cookie("hash", userService.getHashByUID(uid)); c.setMaxAge(50 * 24 * 60 * 60); response.addCookie(c); response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", "/"); } else { String loginhash = UUID.randomUUID().toString(); - if (!insertDB(sql, vkID, loginhash, token, vkName, vkLink)) { + if (!crosspostService.createVKUser(vkID, loginhash, token, vkName, vkLink)) { throw new Exception(); } response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); @@ -117,17 +127,4 @@ public class VKontakteLogin { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } - - private int getUIDbyVKID(JdbcTemplate sql, long vkID) { - try { - return sql.queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - private boolean insertDB(JdbcTemplate sql, long vkID, String loginhash, String token, String vkName, String vkLink) { - return sql.update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)", - vkID, loginhash, token, vkName, vkLink) > 0; - } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java b/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java new file mode 100644 index 00000000..f64907b2 --- /dev/null +++ b/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java @@ -0,0 +1,84 @@ +package com.juick.www.controllers; + +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpUtils; +import com.juick.service.TagService; +import com.juick.www.WebApp; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; +import rocks.xmpp.addr.Jid; +import rocks.xmpp.core.stanza.model.Message; +import rocks.xmpp.extensions.oob.model.x.OobX; + +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +/** + * Created by vitalyster on 08.12.2016. + */ +@Controller +public class XMPPPost { + private final static Logger logger = LoggerFactory.getLogger(XMPPPost.class); + + @Inject + WebApp webApp; + @Inject + TagService tagService; + + @RequestMapping(value = "/post2", method = RequestMethod.POST) + public void doPostMessage(HttpServletRequest request, HttpServletResponse response, + @RequestParam(required = false) String img, + @RequestParam(required = false) MultipartFile attach) throws IOException { + + com.juick.User visitor = webApp.getVisitorUser(request, response); + if (visitor.getUid() == 0 || visitor.isBanned()) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } + String body = request.getParameter("body").replace("\r", StringUtils.EMPTY); + + String attachmentFName = HttpUtils.receiveMultiPartFile(attach, webApp.getTmpDir()); + + if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + try { + URL imgUrl = new URL(img); + attachmentFName = HttpUtils.downloadImage(imgUrl); + } catch (Exception e) { + logger.error("DOWNLOAD ERROR", e); + throw new HttpBadRequestException(); + } + } + Message msg = new Message(); + msg.setType(Message.Type.CHAT); + msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); + msg.setTo(Jid.of("juick@juick.com/Juick")); + msg.setBody(body); + try { + if (StringUtils.isNotEmpty(attachmentFName)) { + String attachmentUrl = String.format("juick://%s", attachmentFName); + msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); + } + webApp.getXmpp().sendMessage(msg); + } catch (URISyntaxException e1) { + logger.warn("attachment error", e1); + } + String referer = request.getHeader("referer"); + if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") + || referer.substring(0, 22).equals("https://juick.com/post")) { + response.sendRedirect("/?show=my"); + return; + } + response.sendRedirect(referer); + } +} diff --git a/juick-www/src/main/webapp/WEB-INF/juick.conf.example b/juick-www/src/main/resources/juick.conf.example index bbe2a9e5..bbe2a9e5 100644 --- a/juick-www/src/main/webapp/WEB-INF/juick.conf.example +++ b/juick-www/src/main/resources/juick.conf.example diff --git a/juick-www/src/main/resources/views/settings_privacy.html b/juick-www/src/main/resources/views/settings_privacy.html deleted file mode 100644 index 48756d8e..00000000 --- a/juick-www/src/main/resources/views/settings_privacy.html +++ /dev/null @@ -1,9 +0,0 @@ -{% extends "layouts/content.html" %} -{% block content %} -<article> - <p>Privacy</p> -</article> -{% endblock %} -{% block "column" %} -{% include "views/partial/settings_tabs.html" %} -{% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/layouts/content.html b/juick-www/src/main/webapp/WEB-INF/layouts/content.html index ef12382c..6a564034 100644 --- a/juick-www/src/main/resources/layouts/content.html +++ b/juick-www/src/main/webapp/WEB-INF/layouts/content.html @@ -13,7 +13,7 @@ <link rel="icon" href="//i.juick.com/favicon.png" /> </head> <body> - {% include "views/partial/navigation.html" %} + {% include "views/partial/navigation" %} <section id="content"> {% block content %} {% endblock %} @@ -22,6 +22,6 @@ {% block column %} {% endblock %} </aside> - {% include "views/partial/footer.html" %} + {% include "views/partial/footer" %} </body> </html>
\ No newline at end of file diff --git a/juick-www/src/main/resources/layouts/rss.xml b/juick-www/src/main/webapp/WEB-INF/layouts/rss.xml index 23866c8a..23866c8a 100644 --- a/juick-www/src/main/resources/layouts/rss.xml +++ b/juick-www/src/main/webapp/WEB-INF/layouts/rss.xml diff --git a/juick-www/src/main/resources/views/404.html b/juick-www/src/main/webapp/WEB-INF/views/404.html index c993df4b..21f42d75 100644 --- a/juick-www/src/main/resources/views/404.html +++ b/juick-www/src/main/webapp/WEB-INF/views/404.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <h1>Страница не найдена</h1> @@ -7,5 +7,5 @@ {% endblock %} {% block "column" %} -{% include "views/partial/homecolumn.html" %} +{% include "views/partial/homecolumn" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/help.html b/juick-www/src/main/webapp/WEB-INF/views/help.html index 3d9960d1..a4b76676 100644 --- a/juick-www/src/main/resources/views/help.html +++ b/juick-www/src/main/webapp/WEB-INF/views/help.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> {{ content | raw }} diff --git a/juick-www/src/main/resources/views/partial/footer.html b/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html index 6978a2c0..6978a2c0 100644 --- a/juick-www/src/main/resources/views/partial/footer.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/footer.html diff --git a/juick-www/src/main/resources/views/partial/homecolumn.html b/juick-www/src/main/webapp/WEB-INF/views/partial/homecolumn.html index 5293918d..28a73662 100644 --- a/juick-www/src/main/resources/views/partial/homecolumn.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/homecolumn.html @@ -1,5 +1,5 @@ <p class="tags"> - {% include "views/partial/tags.html" %} + {% include "views/partial/tags" %} {% if showAdv %} <a href="http://ru.wix.com/">конструктор сайтов</a> {% endif %} diff --git a/juick-www/src/main/resources/views/partial/navigation.html b/juick-www/src/main/webapp/WEB-INF/views/partial/navigation.html index 2863d0c3..2863d0c3 100644 --- a/juick-www/src/main/resources/views/partial/navigation.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/navigation.html diff --git a/juick-www/src/main/resources/views/partial/settings_tabs.html b/juick-www/src/main/webapp/WEB-INF/views/partial/settings_tabs.html index d7901d5e..d7901d5e 100644 --- a/juick-www/src/main/resources/views/partial/settings_tabs.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/settings_tabs.html diff --git a/juick-www/src/main/resources/views/partial/tags.html b/juick-www/src/main/webapp/WEB-INF/views/partial/tags.html index 4d05b7fb..4d05b7fb 100644 --- a/juick-www/src/main/resources/views/partial/tags.html +++ b/juick-www/src/main/webapp/WEB-INF/views/partial/tags.html diff --git a/juick-www/src/main/resources/views/pm_inbox.html b/juick-www/src/main/webapp/WEB-INF/views/pm_inbox.html index 36b3e511..cb55582a 100644 --- a/juick-www/src/main/resources/views/pm_inbox.html +++ b/juick-www/src/main/webapp/WEB-INF/views/pm_inbox.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} {% if not msgs.isEmpty() %} <ul id="private-messages"> @@ -33,5 +33,5 @@ {% endif %} {% endblock %} {% block "column" %} -{% include "views/partial/homecolumn.html" %} +{% include "views/partial/homecolumn" %} {% endblock %} diff --git a/juick-www/src/main/resources/views/pm_sent.html b/juick-www/src/main/webapp/WEB-INF/views/pm_sent.html index 7c6fb686..41f3814b 100644 --- a/juick-www/src/main/resources/views/pm_sent.html +++ b/juick-www/src/main/webapp/WEB-INF/views/pm_sent.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <form action="/pm/send" method="POST" enctype="multipart/form-data"> <div class="newpm"> @@ -29,5 +29,5 @@ {% endif %} {% endblock %} {% block "column" %} -{% include "views/partial/homecolumn.html" %} +{% include "views/partial/homecolumn" %} {% endblock %} diff --git a/juick-www/src/main/resources/views/settings_about.html b/juick-www/src/main/webapp/WEB-INF/views/settings_about.html index 0252af35..a11a1609 100644 --- a/juick-www/src/main/resources/views/settings_about.html +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_about.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <form action="/settings" method="POST" enctype="multipart/form-data"> @@ -16,5 +16,5 @@ </article> {% endblock %} {% block "column" %} -{% include "views/partial/settings_tabs.html" %} +{% include "views/partial/settings_tabs" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/settings_auth-email.html b/juick-www/src/main/webapp/WEB-INF/views/settings_auth-email.html index 79691083..e7ed985a 100644 --- a/juick-www/src/main/resources/views/settings_auth-email.html +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_auth-email.html @@ -1,9 +1,9 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <p>{{ result }}</p><p><a href="/settings">Settings</a>.</p> </article> {% endblock %} {% block "column" %} -{% include "views/partial/settings_tabs.html" %} +{% include "views/partial/settings_tabs" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/settings_main.html b/juick-www/src/main/webapp/WEB-INF/views/settings_main.html index f4630be2..39c6d875 100644 --- a/juick-www/src/main/resources/views/settings_main.html +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_main.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <h1>Настройки</h1> @@ -152,5 +152,5 @@ </article> {% endblock %} {% block "column" %} -{% include "views/partial/settings_tabs.html" %} +{% include "views/partial/settings_tabs" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/settings_password.html b/juick-www/src/main/webapp/WEB-INF/views/settings_password.html index e9c2dce0..d7883546 100644 --- a/juick-www/src/main/resources/views/settings_password.html +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_password.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <fieldset> @@ -13,5 +13,5 @@ </article> {% endblock %} {% block "column" %} -{% include "views/partial/settings_tabs.html" %} +{% include "views/partial/settings_tabs" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/webapp/WEB-INF/views/settings_privacy.html b/juick-www/src/main/webapp/WEB-INF/views/settings_privacy.html new file mode 100644 index 00000000..29ed5e95 --- /dev/null +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_privacy.html @@ -0,0 +1,9 @@ +{% extends "layouts/content" %} +{% block content %} +<article> + <p>Privacy</p> +</article> +{% endblock %} +{% block "column" %} +{% include "views/partial/settings_tabs" %} +{% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/settings_result.html b/juick-www/src/main/webapp/WEB-INF/views/settings_result.html index 3f5482d4..88a52938 100644 --- a/juick-www/src/main/resources/views/settings_result.html +++ b/juick-www/src/main/webapp/WEB-INF/views/settings_result.html @@ -1,9 +1,9 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <article> <p>{{ result | raw }}</p> </article> {% endblock %} {% block "column" %} -{% include "views/partial/settings_tabs.html" %} +{% include "views/partial/settings_tabs" %} {% endblock %}
\ No newline at end of file diff --git a/juick-www/src/main/resources/views/signup.html b/juick-www/src/main/webapp/WEB-INF/views/signup.html index 724ad961..e454f7dd 100644 --- a/juick-www/src/main/resources/views/signup.html +++ b/juick-www/src/main/webapp/WEB-INF/views/signup.html @@ -1,4 +1,4 @@ -{% extends "layouts/content.html" %} +{% extends "layouts/content" %} {% block content %} <h1 class="signup-h1"> {% if type | slice(0, 1) == 'f' %} diff --git a/juick-www/src/main/webapp/WEB-INF/web.xml b/juick-www/src/main/webapp/WEB-INF/web.xml index ed9e8e62..a31c71d0 100644 --- a/juick-www/src/main/webapp/WEB-INF/web.xml +++ b/juick-www/src/main/webapp/WEB-INF/web.xml @@ -2,42 +2,4 @@ <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> - <servlet> - <servlet-name>Main</servlet-name> - <servlet-class>com.juick.www.Main</servlet-class> - <load-on-startup>1</load-on-startup> - </servlet> - <servlet-mapping> - <servlet-name>Main</servlet-name> - <url-pattern>/</url-pattern> - </servlet-mapping> - <servlet-mapping> - <servlet-name>default</servlet-name> - <url-pattern>/scripts.js</url-pattern> - </servlet-mapping> - <servlet-mapping> - <servlet-name>default</servlet-name> - <url-pattern>/style.css</url-pattern> - </servlet-mapping> - <servlet-mapping> - <servlet-name>default</servlet-name> - <url-pattern>/scripts.js.map</url-pattern> - </servlet-mapping> - <servlet-mapping> - <servlet-name>default</servlet-name> - <url-pattern>/style.css.map</url-pattern> - </servlet-mapping> - <mime-mapping> - <extension>js</extension> - <mime-type>application/javascript;charset=UTF-8</mime-type> - </mime-mapping> - <mime-mapping> - <extension>css</extension> - <mime-type>text/css;charset=UTF-8</mime-type> - </mime-mapping> - <session-config> - <session-timeout> - 30 - </session-timeout> - </session-config> </web-app> diff --git a/src/test/java/com/juick/tests/ApiTests.java b/src/test/java/com/juick/tests/ApiTests.java index 6a945d4a..53a8fd7c 100644 --- a/src/test/java/com/juick/tests/ApiTests.java +++ b/src/test/java/com/juick/tests/ApiTests.java @@ -15,7 +15,7 @@ import com.juick.service.SubscriptionService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.service.search.SearchService; -import com.juick.www.PageTemplates; +import com.juick.www.controllers.PageTemplates; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.StringEscapeUtils; import org.junit.Before; |