aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/service
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-11-08 21:38:27 +0300
committerGravatar Vitaly Takmazov2018-11-08 21:38:27 +0300
commit7aaa3f9a29c280f01c677c918932620be45cdbd7 (patch)
tree39947b2c889afd08f9c73ba54fab91159d2af258 /juick-server/src/main/java/com/juick/service
parent3ea9770d0d43fbe45449ac4531ec4b0a374d98ea (diff)
Merge everything into single Spring Boot application
Diffstat (limited to 'juick-server/src/main/java/com/juick/service')
-rw-r--r--juick-server/src/main/java/com/juick/service/ActivityPubService.java59
-rw-r--r--juick-server/src/main/java/com/juick/service/BaseJdbcService.java41
-rw-r--r--juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java282
-rw-r--r--juick-server/src/main/java/com/juick/service/EmailServiceImpl.java108
-rw-r--r--juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java82
-rw-r--r--juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java1143
-rw-r--r--juick-server/src/main/java/com/juick/service/MessengerServiceImpl.java71
-rw-r--r--juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java149
-rw-r--r--juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java63
-rw-r--r--juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java143
-rw-r--r--juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java62
-rw-r--r--juick-server/src/main/java/com/juick/service/SphinxSearchService.java97
-rw-r--r--juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java229
-rw-r--r--juick-server/src/main/java/com/juick/service/TagServiceImpl.java277
-rw-r--r--juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java84
-rw-r--r--juick-server/src/main/java/com/juick/service/UserServiceImpl.java668
-rw-r--r--juick-server/src/main/java/com/juick/service/activities/ActivityListener.java19
-rw-r--r--juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java21
-rw-r--r--juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java20
-rw-r--r--juick-server/src/main/java/com/juick/service/activities/FollowEvent.java21
-rw-r--r--juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java26
-rw-r--r--juick-server/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java103
-rw-r--r--juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java53
-rw-r--r--juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java33
-rw-r--r--juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java130
-rw-r--r--juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java88
-rw-r--r--juick-server/src/main/java/com/juick/service/security/entities/JuickUser.java93
27 files changed, 0 insertions, 4165 deletions
diff --git a/juick-server/src/main/java/com/juick/service/ActivityPubService.java b/juick-server/src/main/java/com/juick/service/ActivityPubService.java
deleted file mode 100644
index 892022cf..00000000
--- a/juick-server/src/main/java/com/juick/service/ActivityPubService.java
+++ /dev/null
@@ -1,59 +0,0 @@
-package com.juick.service;
-
-import com.juick.User;
-import com.juick.model.AnonymousUser;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.util.UriComponents;
-import org.springframework.web.util.UriComponentsBuilder;
-
-import javax.annotation.Nonnull;
-import javax.inject.Inject;
-import java.util.List;
-
-@Repository
-public class ActivityPubService extends BaseJdbcService implements SocialService {
- @Value("${ap_base_uri:http://localhost:8080/}")
- private String baseUri;
- @Inject
- private UserService userService;
-
- @Transactional(readOnly = true)
- @Override
- public @Nonnull User getUserByAccountUri(String acct) {
- UriComponents baseUriComponents = UriComponentsBuilder.fromUriString(baseUri).build();
- UriComponents acctComponents = UriComponentsBuilder.fromUriString(acct).build();
- if (acctComponents.getHost().equals(baseUriComponents.getHost())) {
- // /u/ugnich -> ugnich
- String userName = acctComponents.getPath().substring(3);
- return userService.getUserByName(userName);
- }
- return AnonymousUser.INSTANCE;
- }
-
- @Transactional(readOnly = true)
- @Override
- public @Nonnull List<String> getFollowers(User user) {
- return getJdbcTemplate().queryForList("SELECT acct FROM followers WHERE user_id=?", String.class, user.getUid());
- }
-
- @Transactional
- @Override
- public void addFollower(User user, String acct) {
- getJdbcTemplate().update("INSERT INTO followers(user_id, acct) " +
- "VALUES(?, ?)", user.getUid(), acct);
- }
-
- @Transactional
- @Override
- public void removeFollower(User user, String acct) {
- getJdbcTemplate().update("DELETE FROM followers WHERE user_id=? AND acct=?", user.getUid(), acct);
- }
-
- @Transactional
- @Override
- public void removeAccount(String acct) {
- getJdbcTemplate().update("DELETE FROM followers WHERE acct=?", acct);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/BaseJdbcService.java b/juick-server/src/main/java/com/juick/service/BaseJdbcService.java
deleted file mode 100644
index 496a04ba..00000000
--- a/juick-server/src/main/java/com/juick/service/BaseJdbcService.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
-
-import javax.inject.Inject;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-public class BaseJdbcService {
- @Inject
- JdbcTemplate jdbcTemplate;
- @Inject
- NamedParameterJdbcTemplate namedParameterJdbcTemplate;
-
- public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
- return namedParameterJdbcTemplate;
- }
-
- public JdbcTemplate getJdbcTemplate() {
- return jdbcTemplate;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java b/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java
deleted file mode 100644
index d190faba..00000000
--- a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java
+++ /dev/null
@@ -1,282 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.ExternalToken;
-import com.juick.model.ApplicationStatus;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
-import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
-import java.util.Optional;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService {
-
- @Transactional(readOnly = true)
- @Override
- public Optional<ExternalToken> getTwitterToken(final int uid) {
- List<ExternalToken> list = getJdbcTemplate().query(
- "SELECT uname, access_token, access_token_secret FROM twitter WHERE user_id = ? AND crosspost = 1",
- (rs, num) -> new ExternalToken(rs.getString(1), "twitter",
- rs.getString(2), rs.getString(3)),
- uid);
-
- return list.isEmpty() ?
- Optional.empty() : Optional.of(list.get(0));
- }
-
- @Transactional
- @Override
- public boolean deleteTwitterToken(Integer uid) {
- return getJdbcTemplate().update("DELETE FROM twitter WHERE user_id=?", uid) > 0;
- }
-
- @Override
- public void addFacebookState(String state, String redirectUri) {
- jdbcTemplate.update("INSERT INTO facebook(loginhash, fb_link) VALUES(?, ?)", state, redirectUri);
- }
-
- @Override
- public void addVKState(String state, String redirectUri) {
- jdbcTemplate.update("INSERT INTO vk(loginhash, vk_link) VALUES(?, ?)", state, redirectUri);
- }
-
- @Override
- public String verifyFacebookState(String state) {
- try {
- return jdbcTemplate.queryForObject("SELECT fb_link FROM facebook WHERE loginhash=?",
- String.class, state);
- } catch (EmptyResultDataAccessException e) {
- return StringUtils.EMPTY;
- }
- }
-
- @Override
- public String verifyVKState(String state) {
- try {
- return jdbcTemplate.queryForObject("SELECT vk_link FROM vk WHERE loginhash=?",
- String.class, state);
- } catch (EmptyResultDataAccessException e) {
- return StringUtils.EMPTY;
- }
- }
-
- @Transactional(readOnly = true)
- @Override
- public Optional<Pair<String, String>> getFacebookTokens(final int uid) {
- List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
- "SELECT fb_id, access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1",
- (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))),
- uid);
- return list.isEmpty() ?
- Optional.empty() : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public ApplicationStatus getFbCrossPostStatus(final int uid) {
- List<ApplicationStatus> list = getJdbcTemplate().query(
- "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);
-
- return list.isEmpty() ?
- new ApplicationStatus() : list.get(0);
- }
-
- @Transactional
- @Override
- public boolean enableFBCrosspost(Integer uid) {
- return getJdbcTemplate().update("UPDATE facebook SET crosspost=1 WHERE user_id=?", uid) > 0;
- }
-
- @Transactional
- @Override
- public void disableFBCrosspost(Integer uid) {
- getJdbcTemplate().update("UPDATE facebook SET crosspost=0 WHERE user_id=?", uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public String getTwitterName(final int uid) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT uname FROM twitter WHERE user_id = ?",
- String.class,
- uid);
-
- return list.isEmpty() ?
- StringUtils.EMPTY : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public String getTelegramName(final int uid) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT tg_name FROM telegram WHERE user_id = ?",
- String.class,
- uid);
-
- return list.isEmpty() ?
- StringUtils.EMPTY : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public Optional<Pair<String, String>> getVkTokens(final int uid) {
- List<Optional<Pair<String, String>>> list = getJdbcTemplate().query(
- "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);
-
- return list.isEmpty() ?
- Optional.empty() : list.get(0);
- }
-
- @Transactional
- @Override
- 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("UPDATE facebook SET fb_id=?, access_token=?, fb_name=?, fb_link=? WHERE loginhash=?",
- fbID, token, fbName, fbLink, loginhash) > 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(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/EmailServiceImpl.java b/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java
deleted file mode 100644
index 78bdd42a..00000000
--- a/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.inject.Inject;
-import java.util.List;
-
-/**
- * Created by vitalyster on 09.12.2016.
- */
-@Repository
-@Transactional
-public class EmailServiceImpl extends BaseJdbcService implements EmailService {
-
- @Override
- public boolean verifyAddressByCode(Integer userId, String code) {
- try {
- String address = getJdbcTemplate().queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?",
- String.class, userId, code);
- addEmail(userId, address);
- getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", userId, code);
- } catch (EmptyResultDataAccessException e) {
- return false;
- }
- return true;
- }
-
- @Override
- public boolean addVerificationCode(Integer userId, String account, String code) {
- return getJdbcTemplate().update("INSERT INTO auth(user_id,protocol,account,authcode) VALUES (?,'email',?,?)",
- userId, account, code) > 0;
- }
-
- @Override
- public boolean addEmail(Integer userId, String email) {
- return getJdbcTemplate().update("INSERT INTO emails(user_id,email, subscr_hour) VALUES (?,?, 1)", userId, email) > 0;
- }
-
- @Override
- public boolean deleteEmail(Integer userId, String account) {
- return getNamedParameterJdbcTemplate().update("DELETE FROM emails " +
- "WHERE (SELECT COUNT(*) cnt FROM (select user_id, email FROM emails e) c WHERE user_id=:uid) > 1 " +
- "AND user_id=:uid AND email=:email",
- new MapSqlParameterSource()
- .addValue("uid", userId)
- .addValue("email", account)) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public String getNotificationsEmail(Integer userId) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT email FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", String.class, userId);
- return list.isEmpty() ? StringUtils.EMPTY : list.get(0);
- }
-
- @Override
- public boolean setNotificationsEmail(Integer userId, String account) {
- getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", userId);
- return StringUtils.isNotEmpty(account) && getJdbcTemplate().update(
- "UPDATE emails SET subscr_hour=1 WHERE user_id=? AND email=?", userId, account) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getEmails(Integer userId, boolean active) {
- return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=? " +
- (active ? "AND subscr_hour IS NOT NULL" : ""), String.class, userId);
- }
- @Transactional(readOnly = true)
- @Override
- public String getEmailByAuthCode(String code) {
- try {
- return getJdbcTemplate().queryForObject("SELECT account FROM auth WHERE protocol='email' AND authcode=?", String.class, code);
- } catch (EmptyResultDataAccessException e) {
- return StringUtils.EMPTY;
- }
- }
-
- @Transactional
- @Override
- public void deleteAuthCode(String code) {
- getJdbcTemplate().update("DELETE FROM auth WHERE authcode=?", code);
- }
-
-}
diff --git a/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
deleted file mode 100644
index 67c8360e..00000000
--- a/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
+++ /dev/null
@@ -1,82 +0,0 @@
-package com.juick.service;
-
-import com.juick.Attachment;
-import com.juick.Message;
-import com.juick.Photo;
-import com.juick.server.util.ImageUtils;
-import org.springframework.util.StringUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Paths;
-
-public class ImagesServiceImpl implements ImagesService {
- private ImageUtils imageUtils;
- private String imgDir;
- private String tmpDir;
- public ImagesServiceImpl(String imgDir, String tmpDir) {
- this.imgDir = imgDir;
- this.tmpDir = tmpDir;
- imageUtils = new ImageUtils(imgDir, tmpDir);
- }
- @Override
- public void setAttachmentMetadata(String baseUrl, Message msg) throws Exception {
- if (!StringUtils.isEmpty(msg.getAttachmentType())) {
- Photo photo = new Photo();
- if (msg.getRid()> 0) {
- photo.setSmall(String.format("%sphotos-512/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
- photo.setMedium(String.format("%sphotos-1024/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
- photo.setThumbnail(String.format("%sps/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
- } else {
- photo.setSmall(String.format("%sphotos-512/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
- photo.setMedium(String.format("%sphotos-1024/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
- photo.setThumbnail(String.format("%sps/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
- }
- msg.setPhoto(photo);
- String imageName = String.format("%s.%s", msg.getMid(), msg.getAttachmentType());
- if (msg.getRid() > 0) {
- imageName = String.format("%s-%s.%s", msg.getMid(), msg.getRid(), msg.getAttachmentType());
- }
- File fullImage = Paths.get(imgDir, "p", imageName).toFile();
- File mediumImage = Paths.get(imgDir, "photos-1024", imageName).toFile();
- File smallImage = Paths.get(imgDir, "photos-512", imageName).toFile();
- File thumbnailImage = Paths.get(imgDir, "ps", imageName).toFile();
- StringBuilder builder = new StringBuilder();
- builder.append(baseUrl);
- builder.append(msg.getAttachmentType().equals("mp4") ? "video" : "p");
- builder.append("/").append(msg.getMid());
- if (msg.getRid() > 0) {
- builder.append("-").append(msg.getRid());
- }
- builder.append(".").append(msg.getAttachmentType());
- String originalUrl = builder.toString();
-
- Attachment original = imageUtils.getAttachment(fullImage);
- original.setUrl(originalUrl);
-
- Attachment medium = imageUtils.getAttachment(mediumImage);
- medium.setUrl(photo.getMedium());
- original.setMedium(medium);
-
- Attachment small = imageUtils.getAttachment(smallImage);
- small.setUrl(photo.getSmall());
- original.setSmall(small);
-
- Attachment thumb = imageUtils.getAttachment(thumbnailImage);
- thumb.setUrl(photo.getMedium());
- original.setThumbnail(thumb);
-
- msg.setAttachment(original);
- }
- }
-
- @Override
- public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException {
- imageUtils.saveImageWithPreviews(tempFilename, outputFilename);
- }
-
- @Override
- public void saveAvatar(String tempFilename, int uid) throws IOException {
- imageUtils.saveAvatar(tempFilename, uid);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
deleted file mode 100644
index 0b7faf87..00000000
--- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ /dev/null
@@ -1,1143 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.*;
-import com.juick.model.AnonymousUser;
-import com.juick.model.PrivacyOpts;
-import com.juick.model.ResponseReply;
-import com.juick.server.util.HttpNotFoundException;
-import com.juick.util.MessageUtils;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.dao.IncorrectResultSizeDataAccessException;
-import org.springframework.jdbc.core.ConnectionCallback;
-import org.springframework.jdbc.core.RowMapper;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.jdbc.core.namedparam.SqlParameterSource;
-import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.inject.Inject;
-import java.net.URI;
-import java.sql.*;
-import java.time.Instant;
-import java.util.*;
-import java.util.Date;
-import java.util.stream.Collectors;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class MessagesServiceImpl extends BaseJdbcService implements MessagesService {
- private static final Logger logger = LoggerFactory.getLogger(MessagesServiceImpl.class);
- @Inject
- private UserService userService;
- @Inject
- private TagService tagService;
- @Inject
- private SearchService searchService;
- @Inject
- private ImagesService imagesService;
- @Value("${img_url:https://i.juick.com/}")
- private String baseImagesUrl;
-
- private class MessageMapper implements RowMapper<Message> {
- @Override
- public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
- Message msg = new Message();
- msg.setMid(rs.getInt(1));
- msg.setRid(rs.getInt(2));
- msg.setReplyto(rs.getInt(3));
- User user = new User();
- user.setUid(rs.getInt(4));
- user.setName(Optional.ofNullable(rs.getString(5)).orElse(AnonymousUser.INSTANCE.getName()));
- user.setBanned(rs.getBoolean(6));
- user.setUri(URI.create(Optional.ofNullable(rs.getString(22)).orElse(StringUtils.EMPTY)));
- msg.setUser(user);
- msg.setTimestamp(rs.getTimestamp(7).toInstant());
- msg.ReadOnly = rs.getBoolean(8);
- msg.setPrivacy(rs.getInt(9));
- msg.FriendsOnly = msg.getPrivacy() < 0;
- msg.setReplies(rs.getInt(10));
- msg.setAttachmentType(rs.getString(11));
- msg.setLikes(rs.getInt(12));
- msg.Hidden = rs.getBoolean(13);
- String tagsStr = rs.getString(14);
- msg.setTags(MessageUtils.parseTags(tagsStr));
- msg.setRepliesBy(rs.getString(15));
- msg.setText(rs.getString(16));
- msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(17)));
- msg.setUpdated(rs.getTimestamp(18).toInstant());
- int quoteUid = rs.getInt(19);
- User quoteUser = new User();
- quoteUser.setUid(quoteUid);
- quoteUser.setName(rs.getString(20));
- if (quoteUid == 0) {
- quoteUser.setName(AnonymousUser.INSTANCE.getName());
- quoteUser.setUri(URI.create(Optional.ofNullable(rs.getString(23)).orElse(StringUtils.EMPTY)));
- }
- msg.setTo(quoteUser);
- msg.setUpdatedAt(rs.getTimestamp(21).toInstant());
- msg.setReplyUri(URI.create(Optional.ofNullable(rs.getString(24)).orElse(StringUtils.EMPTY)));
- msg.setHtml(rs.getBoolean(25));
- if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
- try {
- imagesService.setAttachmentMetadata(baseImagesUrl, msg);
- } catch (Exception e) {
- logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
- }
- }
- return msg;
- }
- }
-
-
-
- /**
- * @see <a href="https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html">Java, JDBC and MySQL Types</a>
- */
- @Transactional
- @Override
- public int createMessage(final int uid, final String txt, final String attachment, final Collection<com.juick.Tag> tags) {
- SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(getJdbcTemplate()).withTableName("messages")
- .usingColumns("user_id", "attach", "ts")
- .usingGeneratedKeyColumns("message_id");
- Map<String, Object> insertMap = new HashMap<>();
- insertMap.put("user_id", uid);
- Instant now = Instant.now();
- insertMap.put("ts", Timestamp.from(now));
- if (StringUtils.isNotEmpty(attachment)) {
- insertMap.put("attach", attachment);
- }
- int mid = simpleJdbcInsert.executeAndReturnKey(insertMap).intValue();
- if (mid > 0) {
- String tagsNames = StringUtils.EMPTY;
-
- if (CollectionUtils.isNotEmpty(tags)) {
- StringBuilder tasNamesBuilder = new StringBuilder();
- List<Object[]> params = new ArrayList<>(tags.size());
-
- boolean next = false;
-
- for (Tag tag : tags) {
- if (next) {
- tasNamesBuilder.append(" ");
- } else
- next = true;
-
- tasNamesBuilder.append(tag.getName());
- params.add(new Object[]{mid, tag.TID});
- }
- tagsNames = tasNamesBuilder.toString();
-
- getJdbcTemplate().batchUpdate(
- "INSERT INTO messages_tags(message_id, tag_id) VALUES (?, ?)",
- params, new int[]{Types.INTEGER, Types.INTEGER});
- }
-
- getJdbcTemplate().update(
- "INSERT INTO messages_txt(message_id, tags, txt, updated_at) VALUES (?, ?, ?, ?)",
- new Object[]{mid, tagsNames, txt, Timestamp.from(now)},
- new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP});
- getJdbcTemplate().update("UPDATE users SET lastmessage=?, last_seen=? where id=?", Timestamp.from(now), Timestamp.from(now), uid);
- }
-
- return mid;
- }
-
- /**
- * @param mid
- * @param rid
- * @param user
- * @param txt
- * @param attachment
- * @return
- * @see <a href="https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html">Java, JDBC and MySQL Types</a>
- */
- @Transactional
- @Override
- public int createReply(final int mid, final int rid, final User user, final String txt, final String attachment) {
- int ridnew = getReplyIDIncrement(mid);
- Date ts = Date.from(Instant.now());
- getJdbcTemplate().update("INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt, ts, updated_at, user_uri) " +
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
- mid, ridnew, user.getUid(), rid, attachment, txt, ts, ts, user.getUri().toASCIIString());
-
- if (ridnew > 0) {
- getJdbcTemplate().update(
- "UPDATE messages SET replies = replies + 1, updated=? WHERE message_id = ?",
- ts, mid);
- setLastReadComment(user, mid, ridnew);
- getJdbcTemplate().update("UPDATE users SET lastmessage=?, last_seen=? where id=?", ts, ts, user.getUid());
- }
- return ridnew;
- }
-
- @Override
- public int getReplyIDIncrement(final int mid) {
- return getJdbcTemplate().execute((ConnectionCallback<Integer>) conn -> {
- conn.setAutoCommit(false);
- final int replyNo;
- try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) {
- ps.setInt(1, mid);
- try (ResultSet resultSet = ps.executeQuery()) {
- if (resultSet.next()) {
- replyNo = resultSet.getInt(1);
- } else {
- throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0);
- }
- }
- }
- try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) {
- ps.setInt(1, replyNo);
- ps.setInt(2, mid);
- if (ps.executeUpdate() != 1) {
- throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0);
- }
- }
- conn.commit();
- return replyNo;
- });
-
- }
-
- @Transactional
- void updateRepliesBy(int mid) {
- List<String> users = getJdbcTemplate().queryForList("SELECT users.nick FROM replies " +
- "INNER JOIN users ON replies.user_id=users.id WHERE replies.message_id=? " +
- "GROUP BY replies.user_id ORDER BY COUNT(replies.reply_id) DESC LIMIT 5", String.class, mid);
- String result = users.stream().map(u -> "@" + u).collect(Collectors.joining(","));
- getJdbcTemplate().update("UPDATE messages_txt SET repliesby=? WHERE message_id=?", result, mid);
- }
-
- @Transactional
- @Override
- public RecommendStatus recommendMessage(final int mid, final int vuid, final String userUri) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", vuid)
- .addValue("uri", userUri)
- .addValue("like_id", Reaction.LIKE)
- .addValue("mid", mid);
- int wasDeleted = getNamedParameterJdbcTemplate()
- .update("DELETE FROM favorites WHERE user_id=:uid AND message_id=:mid AND like_id=:like_id AND user_uri=:uri", sqlParameterSource);
- if (wasDeleted > 0) {
- return RecommendStatus.Deleted;
- } else {
- boolean wasAdded = getJdbcTemplate()
- .update("INSERT INTO favorites(user_id, message_id, ts, like_id, user_uri) VALUES (?, ?, NOW(), ?, ?)", vuid, mid,Reaction.LIKE, userUri) == 1;
- if (wasAdded) {
- return RecommendStatus.Added;
- }
- }
- return RecommendStatus.Error;
- }
-
- @Override
- public RecommendStatus recommendMessage(int mid, int vuid) {
- return recommendMessage(mid, vuid, StringUtils.EMPTY);
- }
-
- @Override
- public List<Reaction> listReactions() {
- return jdbcTemplate.query("SELECT like_id, description FROM reactions", (rs, rowNum) -> {
- Reaction reaction = new Reaction(rs.getInt("like_id"));
- reaction.setDescription(rs.getString("description"));
- return reaction;
- });
- }
-
- @Override
- public RecommendStatus likeMessage(int mid, int vuid, int reactionId) {
- return likeMessage(mid, vuid, reactionId, StringUtils.EMPTY);
- }
-
- @Transactional
- @Override
- public RecommendStatus likeMessage(int mid, int vuid, int reactionId, String userUri) throws IllegalArgumentException {
- boolean wasAdded = getJdbcTemplate()
- .update("INSERT INTO favorites(user_id, message_id, ts, like_id, user_uri) VALUES (?, ?, NOW(), ?, ?)", vuid, mid, reactionId, userUri) == 1;
- if (wasAdded) {
- return RecommendStatus.Added;
- }
-
- return RecommendStatus.Error;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean canViewThread(final int mid, final int uid) {
- List<PrivacyOpts> list = getJdbcTemplate().query(
- "SELECT user_id, privacy FROM messages WHERE message_id = ?",
- (rs, rowNum) -> {
- PrivacyOpts res = new PrivacyOpts();
-
- res.setUid(rs.getInt(1));
- res.setPrivacy(rs.getInt(2));
-
- return res;
- },
- mid);
-
- PrivacyOpts privacyOpts = list.isEmpty() ? null : list.get(0);
-
- return privacyOpts == null ||
- privacyOpts.getPrivacy() >= 0 ||
- uid == privacyOpts.getUid() ||
- ((privacyOpts.getPrivacy() == -1 || privacyOpts.getPrivacy() == -2) &&
- uid > 0 && userService.isInWL(privacyOpts.getUid(), uid));
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isReadOnly(final int mid) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT readonly FROM messages WHERE message_id = ?",
- new Object[]{mid},
- Integer.class);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isSubscribed(final int uid, final int mid) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM subscr_messages WHERE suser_id = ? AND message_id = ?",
- new Object[]{uid, mid},
- Integer.class);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getMessagePrivacy(final int mid) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT privacy FROM messages WHERE message_id = ?",
- new Object[]{mid},
- Integer.class);
-
- return list.isEmpty() ? -4 : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public com.juick.Message getMessage(final int mid) {
-
- List<com.juick.Message> list = getJdbcTemplate().query(
- "SELECT messages.message_id as mid, 0 as rid, 0 as replyto, "
- + "messages.user_id as uid, users.nick, users.banned as banned, "
- + ""
- + "messages.ts,"
- + "messages.readonly, messages.privacy, messages.replies,"
- + "messages.attach, COUNT(DISTINCT favorites.user_id) as likes, messages.hidden,"
- + "txt.tags, txt.repliesby, txt.txt, '' as q, messages.updated as updated, 0 as to_uid, "
- + "NULL as to_name, txt.updated_at, '' as user_uri, '' as to_uri, '' as reply_uri, 0 as html FROM messages "
- + "INNER JOIN users ON messages.user_id = users.id "
- + "INNER JOIN messages_txt AS txt "
- + "ON messages.message_id = txt.message_id "
- + "LEFT JOIN favorites "
- + "ON messages.message_id = favorites.message_id AND favorites.like_id=1 "
- + "WHERE messages.message_id = ? "
- + "GROUP BY mid, rid, replyto, uid, nick, banned, messages.ts, readonly, "
- + "privacy, replies, attach, tags, repliesby, q, updated_at, user_uri, to_uri, reply_uri, html",
- new MessageMapper(),
- mid);
- if (!list.isEmpty()) {
- final Message message = list.get(0);
- Map<Integer, Set<Reaction>> reactionStats = updateReactionsFor(Collections.singletonList(mid));
- message.setReactions(reactionStats.get(message.getMid()));
- return message;
- }
- return null;
- }
-
- @Transactional(readOnly = true)
- @Override
- public com.juick.Message getReply(final int mid, final int rid) {
- List<com.juick.Message> list = getJdbcTemplate().query(
- "SELECT replies.user_id, users.nick,"
- + "replies.replyto, replies.ts,"
- + "replies.attach, replies.txt, IFNULL(q.txt,t.txt) as quote, "
- + "COALESCE(q.user_id, m.user_id) AS to_uid, COALESCE(qu.nick, mu.nick) AS to_name, "
- + "replies.updated_at, replies.user_uri as uri, "
- + "q.user_uri AS to_uri, replies.reply_uri AS reply_uri, replies.html, q.reply_uri "
- + "FROM replies LEFT 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 "
- + "LEFT JOIN messages m ON replies.message_id = m.message_id "
- + "LEFT JOIN users qu ON q.user_id=qu.id "
- + "LEFT JOIN users mu ON m.user_id=mu.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));
- if (msg.getUser().getUid() == 0) {
- msg.getUser().setName(AnonymousUser.INSTANCE.getName());
- msg.getUser().setUri(
- URI.create(Optional.ofNullable(rs.getString(11)).orElse(StringUtils.EMPTY)));
- }
- msg.setReplyto(rs.getInt(3));
- msg.setTimestamp(rs.getTimestamp(4).toInstant());
- msg.setAttachmentType(rs.getString(5));
- msg.setText(rs.getString(6));
- String quote = rs.getString(7);
-
- if (!StringUtils.isEmpty(quote)) {
- msg.setReplyQuote(MessageUtils.formatQuote(quote));
- }
- int quoteUid = rs.getInt(8);
- User quoteUser = new User();
- quoteUser.setUid(quoteUid);
- quoteUser.setName(Optional.ofNullable(rs.getString(9)).orElse(AnonymousUser.INSTANCE.getName()));
- quoteUser.setUri(URI.create(Optional.ofNullable(rs.getString(12)).orElse(StringUtils.EMPTY)));
- msg.setTo(quoteUser);
- msg.setUpdatedAt(rs.getTimestamp(10).toInstant());
- msg.setReplyUri(URI.create(Optional.ofNullable(rs.getString(13)).orElse(StringUtils.EMPTY)));
- msg.setHtml(rs.getBoolean(14));
- msg.setReplyToUri(URI.create(Optional.ofNullable(rs.getString(15)).orElse(StringUtils.EMPTY)));
- if (StringUtils.isNotEmpty(msg.getAttachmentType())) {
- try {
- imagesService.setAttachmentMetadata(baseImagesUrl, msg);
- } catch (Exception e) {
- logger.warn("exception reading images for mid {} rid {}", msg.getMid(), msg.getRid(), e);
- }
- }
- return msg;
- },
- mid, rid);
-
- return list.isEmpty() ? null : list.get(0);
- }
-
- @Override
- public Message getReplyByUri(String replyUri) {
- List<Message> replies = getJdbcTemplate().query("SELECT message_id, reply_id from replies WHERE reply_uri=?", (rs, rowNum) -> getReply(rs.getInt(1), rs.getInt(2)), replyUri);
- return replies.isEmpty() ? null : replies.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public User getMessageAuthor(final int mid) {
- List<User> list = getJdbcTemplate().query(
- "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;
- });
-
- return list.isEmpty() ?
- null : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getMessageRecommendations(final int mid) {
- return getJdbcTemplate().queryForList(
- "SELECT DISTINCT users.nick FROM favorites " +
- "INNER JOIN users ON (favorites.message_id = ? AND favorites.user_id = users.id) " +
- "INNER JOIN messages m ON favorites.message_id=m.message_id WHERE favorites.like_id=1 " +
- "AND NOT EXISTS (SELECT 1 FROM bl_users WHERE " +
- "(user_id = favorites.user_id AND bl_user_id = m.user_id) " +
- "OR (user_id = m.user_id AND bl_user_id = favorites.user_id))",
- String.class, mid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getAll(final int visitorUid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("before", before)
- .addValue("visitorUid", visitorUid);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT m.message_id FROM messages m WHERE " +
- (before > 0 ?
- " m.message_id < :before AND " : StringUtils.EMPTY) +
- " m.hidden = 0 AND (m.privacy > 0" +
- (visitorUid > 1 ?
- " OR m.user_id = :visitorUid) AND NOT EXISTS (" +
- " SELECT 1 FROM bl_users b WHERE b.user_id = :visitorUid AND b.bl_user_id = m.user_id)" :
- ")") +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " +
- "(SELECT tag_id FROM messages_tags WHERE message_id = m.message_id) and :visitorUid = bt.user_id)" +
- " AND NOT EXISTS (SELECT 1 from users u WHERE u.banned = 1 and u.id = m.user_id and u.id <> :visitorUid) ORDER BY m.message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getTag(final int tid, final int visitorUid, final int before, final int cnt) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("tid", tid)
- .addValue("cnt", cnt)
- .addValue("before", before)
- .addValue("visitorUid", visitorUid);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT messages.message_id FROM (tags INNER JOIN messages_tags " +
- "ON ((tags.synonym_id = :tid OR tags.tag_id = :tid) AND tags.tag_id = messages_tags.tag_id)) " +
- "INNER JOIN messages ON messages.message_id = messages_tags.message_id WHERE " +
- (before > 0 ?
- " messages.message_id < :before AND " : StringUtils.EMPTY) +
- "(messages.privacy > 0 OR messages.user_id = :visitorUid) " +
- "AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :visitorUid and b.bl_user_id = messages.user_id) " +
- "ORDER BY messages.message_id DESC LIMIT :cnt",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getTags(final String tids, final int visitorUid, final int before, final int cnt) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("cnt", cnt)
- .addValue("before", before)
- .addValue("visitorUid", visitorUid);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT messages.message_id FROM messages_tags " +
- "INNER JOIN messages USING(message_id) WHERE messages_tags.tag_id IN (" + tids + ") " +
- (before > 0 ?
- " AND messages.message_id < :before " : StringUtils.EMPTY) +
- " AND (messages.privacy > 0 OR messages.user_id = :visitorUid) " +
- "ORDER BY messages.message_id DESC LIMIT :cnt",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getPlace(final int placeId, final int visitorUid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("placeId", placeId)
- .addValue("before", before)
- .addValue("visitorUid", visitorUid);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM messages WHERE place_id = :placeId " +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- " AND (privacy > 0 OR user_id = :visitorUid) ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getMyFeed(final int uid, final int before, boolean recommended) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("before", before);
-
- List<Integer> mids = getNamedParameterJdbcTemplate().queryForList(
- "(SELECT message_id FROM messages " +
- " INNER JOIN subscr_users ON (subscr_users.suser_id = :uid AND subscr_users.user_id = messages.user_id) " +
- " WHERE " +
- (before > 0 ?
- " message_id < :before AND " : StringUtils.EMPTY) +
- " (privacy >= 0 OR (privacy >= -2 AND privacy <= -1" +
- " AND EXISTS (SELECT 1 FROM wl_users w WHERE w.wl_user_id = :uid and w.user_id = messages.user_id))) " +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " +
- "(SELECT tag_id FROM messages_tags WHERE message_id = messages.message_id) and :uid = bt.user_id))" +
- " UNION " +
- " (SELECT message_id FROM messages WHERE user_id=:uid " +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- (recommended ?
- ") UNION " +
- " (SELECT f.message_id as message_id FROM favorites f INNER JOIN messages ON " +
- "f.message_id=messages.message_id WHERE " +
- "EXISTS (SELECT 1 FROM subscr_users s WHERE s.suser_id = :uid and f.user_id = s.user_id)" +
- (before > 0 ?
- " AND f.message_id < :before " : StringUtils.EMPTY) : StringUtils.EMPTY) +
- " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid and b.bl_user_id = messages.user_id)" +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " +
- "(SELECT tag_id FROM messages_tags WHERE message_id = messages.message_id) and :uid = bt.user_id)) " +
- "ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
-
- return mids;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getPrivate(final int uid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList
- ("SELECT message_id FROM messages WHERE user_id = :uid AND privacy < 0" +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- "ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getDiscussions(final int uid, final Long to) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("to", new Timestamp(to));
-
- if (uid == 0) {
- return getNamedParameterJdbcTemplate().query("SELECT message_id FROM messages WHERE " +
- (to != 0 ?
- " updated < :to AND" : StringUtils.EMPTY) +
- " NOT EXISTS (SELECT 1 from users u WHERE u.banned = 1" +
- " AND u.id = messages.user_id and u.id <> :uid) " +
- " ORDER BY updated DESC, message_id DESC LIMIT 20",
- sqlParameterSource,
- (rs, rowNum) -> rs.getInt(1));
- }
- return getNamedParameterJdbcTemplate().query(
- "SELECT messages.message_id, messages.updated FROM subscr_messages " +
- "INNER JOIN messages ON messages.message_id=subscr_messages.message_id " +
- "WHERE suser_id = :uid " +
- (to != 0 ?
- "AND updated < :to " : StringUtils.EMPTY) +
- " AND NOT EXISTS (SELECT 1 from users u WHERE u.banned = 1" +
- " AND u.id = messages.user_id and u.id <> :uid) " +
- "ORDER BY updated DESC, message_id DESC LIMIT 20",
- sqlParameterSource,
- (rs, rowNum) -> rs.getInt(1));
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getRecommended(final int uid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT f.message_id FROM favorites f WHERE " +
- "EXISTS (SELECT 1 FROM subscr_users s WHERE s.suser_id = :uid and f.user_id = s.user_id)" +
- (before > 0 ?
- " AND f.message_id < :before " : StringUtils.EMPTY) +
- "ORDER BY f.message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getPopular(final int visitorUid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("vid", visitorUid)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT m.message_id FROM messages m WHERE m.privacy > 0 " +
- (before > 0 ?
- " AND m.message_id < :before " : StringUtils.EMPTY) +
- " AND m.popular > 0 AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = m.user_id) " +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " +
- "(SELECT tag_id FROM messages_tags WHERE message_id = m.message_id) and :vid = bt.user_id)" +
- " ORDER BY m.message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getPhotos(final int visitorUid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("vid", visitorUid)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT m.message_id FROM messages m WHERE (m.privacy > 0 OR m.user_id = :vid) " +
- (before > 0 ?
- " AND m.message_id < :before " : StringUtils.EMPTY) +
- " AND m.attach IS NOT NULL " +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN " +
- "(SELECT tag_id FROM messages_tags WHERE message_id = m.message_id) and :vid = bt.user_id)" +
- " AND NOT EXISTS (SELECT 1 from users u WHERE u.banned = 1 and u.id = m.user_id and u.id <> :vid) " +
- " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :vid and b.bl_user_id = m.user_id) " +
- " ORDER BY m.message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getSearch(final User visitor, final String search, final int page) {
- return searchService.searchInAllMessages(visitor, search, page);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserBlog(final int uid, final int privacy, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("privacy", privacy)
- .addValue("before", before);
-
- ;
- if (userService.getUserByUID(uid).orElseThrow(IllegalStateException::new).isBanned()) {
- throw new HttpNotFoundException();
- }
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM messages WHERE user_id = :uid" +
- (before > 0 ?
- " AND message_id < :before" : StringUtils.EMPTY) +
- " AND privacy >= :privacy ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserTag(final int uid, final int tid, final int privacy, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("tid", tid)
- .addValue("privacy", privacy)
- .addValue("before", before);
-
- if (userService.getUserByUID(uid).orElseThrow(IllegalStateException::new).isBanned()) {
- throw new HttpNotFoundException();
- }
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT messages.message_id FROM messages_tags INNER JOIN messages " +
- " USING (message_id) WHERE messages.user_id = :uid AND messages_tags.tag_id = :tid " +
- (before > 0 ?
- " AND messages.message_id < :before " : StringUtils.EMPTY) +
- " AND messages.privacy >= :privacy ORDER BY messages.message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserBlogAtDay(final int uid, final int privacy, final int daysback) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("privacy", privacy)
- .addValue("daysback", daysback);
-
- if (userService.getUserByUID(uid).orElseThrow(IllegalStateException::new).isBanned()) {
- throw new HttpNotFoundException();
- }
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM messages WHERE user_id = :uid" +
- (daysback > 0 ?
- " AND ts >= date(NOW() - INTERVAL :daysback day)" +
- " AND ts < date(NOW() - INTERVAL :daysback day + INTERVAL 1 day)" : StringUtils.EMPTY) +
- " AND privacy >= :privacy ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserBlogWithRecommendations(final int uid, final int privacy, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("privacy", privacy)
- .addValue("before", before);
-
- if (userService.getUserByUID(uid).orElseThrow(IllegalStateException::new).isBanned()) {
- throw new HttpNotFoundException();
- }
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM " +
- "(SELECT message_id FROM favorites " +
- " WHERE user_id = :uid " +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- " ORDER BY message_id DESC LIMIT 20) as r" +
- " UNION ALL " +
- "SELECT message_id FROM " +
- "(SELECT message_id FROM messages WHERE user_id = :uid" +
- (before > 0 ?
- " AND message_id < :before" : StringUtils.EMPTY) +
- " AND privacy >= :privacy ORDER BY message_id DESC LIMIT 20) as m " +
- "ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserRecommendations(final int uid, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM favorites " +
- " WHERE user_id = :uid " +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- " ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserPhotos(final int uid, final int privacy, final int before) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("privacy", privacy)
- .addValue("before", before);
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT message_id FROM messages WHERE user_id = :uid " +
- (before > 0 ?
- " AND message_id < :before " : StringUtils.EMPTY) +
- " AND privacy >= :privacy AND attach IS NOT NULL ORDER BY message_id DESC LIMIT 20",
- sqlParameterSource,
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUserSearch(final User visitor, final int UID, final String search, final int privacy, final int page) {
- return searchService.searchByStringAndUser(visitor, search, UID, page);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<com.juick.Message> getMessages(final User visitor, final List<Integer> mids) {
- if (CollectionUtils.isNotEmpty(mids)) {
-
- List<com.juick.Message> msgs = getNamedParameterJdbcTemplate().query(
- "WITH RECURSIVE banned(message_id, reply_id) "
- + "AS (SELECT message_id, reply_id FROM replies WHERE replies.message_id IN (:ids) "
- + "AND (EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) "
- + "OR EXISTS (SELECT 1 from users u WHERE u.banned = 1 and u.id = replies.user_id and u.id <> :uid)) "
- + "UNION ALL SELECT replies.message_id, replies.reply_id FROM replies INNER JOIN banned "
- + "ON banned.reply_id = replies.replyto AND banned.message_id=replies.message_id "
- + "WHERE replies.message_id IN (:ids)) "
- + "SELECT messages.message_id, 0 as rid, 0 as replyto, "
- + "messages.user_id,users.nick, 0 as banned, "
- + "messages.ts,"
- + "messages.readonly,messages.privacy, messages.replies-COUNT(DISTINCT banned.reply_id) as replies,"
- + "messages.attach,COUNT(DISTINCT favorites.user_id) AS likes,messages.hidden,"
- + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q, "
- + "messages.updated, 0 as to_uid, NULL as to_name, messages_txt.updated_at, '' as user_uri, "
- + "'' as to_uri, '' as reply_uri, 0 as html "
- + "FROM (messages INNER JOIN messages_txt "
- + "ON messages.message_id=messages_txt.message_id) "
- + "INNER JOIN users ON messages.user_id=users.id "
- + "LEFT JOIN favorites "
- + "ON messages.message_id = favorites.message_id AND favorites.like_id=1 "
- + "LEFT JOIN banned "
- + "ON messages.message_id = banned.message_id "
- + "WHERE messages.message_id IN (:ids) GROUP BY "
- + "messages.message_id, rid, replyto, messages.user_id, users.nick, banned, messages.ts, "
- + "messages.readonly, messages.privacy, messages.attach, messages.hidden, messages_txt.tags, "
- + "messages_txt.repliesby, messages_txt.txt, q, messages.updated, to_uid, to_name, updated_at, "
- + "user_uri, reply_uri, html",
- new MapSqlParameterSource("ids", mids)
- .addValue("uid", visitor.getUid()),
- new MessageMapper());
-
-
- Map<Integer,Set<Reaction>> likes = updateReactionsFor(mids);
-
- msgs.forEach(i -> i.setReactions(likes.get(i.getMid())));
-
- msgs.sort(Comparator.comparing(item -> mids.indexOf(item.getMid())));
-
- return msgs;
- }
- return Collections.emptyList();
- }
-
-
- @Transactional(readOnly = true)
- @Override
- public Map<Integer,Set<Reaction>> updateReactionsFor(final List<Integer> mids) {
-
- return getNamedParameterJdbcTemplate().query("select f.message_id as mid, f.like_id as lid," +
- " r.description as descr, count(f.like_id) as cnt" +
- " from favorites f LEFT JOIN reactions r ON f.like_id = r.like_id " +
- " where f.message_id IN (:mids) " +
- " group by f.message_id, f.like_id", new MapSqlParameterSource("mids", mids), (ResultSet rs) -> {
- Map<Integer,Set<Reaction>> results = new HashMap<>();
-
-
- while (rs.next()) {
- int messageId = rs.getInt("mid");
- int likeId = rs.getInt("lid");
- int count = rs.getInt("cnt");
- String description = rs.getString("descr");
- Reaction reaction = new Reaction(likeId);
- reaction.setCount(count);
- reaction.setDescription(description);
- results.computeIfAbsent(messageId, HashSet::new);
- results.get(messageId).add(reaction);
- }
-
- return results;
- });
-
- }
-
-
- @Transactional
- @Override
- public List<Message> getReplies(final User user, final int mid) {
- List<Message> replies = getNamedParameterJdbcTemplate().query(
- "WITH RECURSIVE banned(reply_id, user_id) AS (" +
- "SELECT reply_id, user_id FROM replies " +
- "WHERE replies.message_id = :mid " +
- "AND EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) " +
- "UNION ALL SELECT replies.reply_id, replies.user_id FROM replies " +
- "INNER JOIN banned ON banned.reply_id = replies.replyto " +
- "WHERE replies.message_id = :mid) " +
- "SELECT replies.message_id as mid, replies.reply_id, replies.replyto, " +
- "replies.user_id, users.nick, users.banned, " +
- "replies.ts, " +
- "0 as readonly, 0 as privacy, 0 as replies, " +
- "replies.attach, 0 as likes, 0 as hidden, " +
- "NULL as tags, NULL as repliesby, replies.txt, " +
- "IFNULL(qw.txt, t.txt) as q, " +
- "NOW(), " +
- "COALESCE(qw.user_id, m.user_id) as to_uid, COALESCE(qu.nick, mu.nick) as to_name, " +
- "replies.updated_at, replies.user_uri as uri, " +
- "qw.user_uri as to_uri, replies.reply_uri, replies.html " +
- "FROM replies LEFT 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 " +
- "LEFT JOIN messages m on replies.message_id = m.message_id " +
- "LEFT JOIN users qu ON qw.user_id=qu.id " +
- "LEFT JOIN users mu ON m.user_id=mu.id " +
- "WHERE replies.message_id = :mid " +
- "AND NOT EXISTS (SELECT 1 from users u WHERE u.banned = 1 and u.id = replies.user_id and u.id <> :uid)" +
- "AND NOT EXISTS (SELECT 1 FROM banned WHERE banned.reply_id = replies.reply_id) " +
- "AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = m.user_id) " +
- "ORDER BY replies.reply_id ASC",
- new MapSqlParameterSource("mid", mid).addValue("uid", user.getUid()),
- new MessageMapper());
- if (replies.size() > 0) {
- setRead(user, mid);
- }
- return replies;
- }
-
- @Transactional
- @Override
- public boolean setMessagePopular(final int mid, final int popular) {
- int ret;
- MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("mid", mid)
- .addValue("popular", popular);
-
- switch (popular) {
- case -2:
- ret = getNamedParameterJdbcTemplate().update(
- "UPDATE messages SET hidden = 1 WHERE message_id = :mid",
- sqlParameterSource);
- break;
- case -1:
- sqlParameterSource.addValue("popular", 0);
- default:
- ret = getNamedParameterJdbcTemplate().update(
- "UPDATE messages SET popular = :popular WHERE message_id = :mid",
- sqlParameterSource);
- break;
- }
-
- if (popular == -1)
- ret = getNamedParameterJdbcTemplate().update(
- "INSERT INTO top_ignore_messages VALUES (:mid)",
- sqlParameterSource);
-
- return ret > 0;
- }
-
- @Transactional
- @Override
- public boolean setMessagePrivacy(final int mid) {
- return getJdbcTemplate().update("UPDATE messages SET privacy=1 WHERE message_id=?", mid) > 0;
- }
-
- @Transactional
- @Override
- public boolean deleteMessage(final int uid, final int mid) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("mid", mid)
- .addValue("uid", uid);
-
- if (getNamedParameterJdbcTemplate().update(
- "DELETE FROM messages WHERE message_id = :mid AND user_id = :uid", sqlParameterSource) > 0) {
-
- getNamedParameterJdbcTemplate().update("DELETE FROM messages_txt WHERE message_id = :mid", sqlParameterSource);
- getNamedParameterJdbcTemplate().update("DELETE FROM replies WHERE message_id = :mid", sqlParameterSource);
- getNamedParameterJdbcTemplate().update("DELETE FROM subscr_messages WHERE message_id = :mid", sqlParameterSource);
- getNamedParameterJdbcTemplate().update("DELETE FROM messages_tags WHERE message_id = :mid", sqlParameterSource);
-
- return true;
- }
- return false;
- }
- @Transactional
- @Override
- public boolean deleteReply(final int uid, final int mid, final int rid) {
- User author = getMessageAuthor(mid);
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("mid", mid)
- .addValue("uid", uid)
- .addValue("rid", rid);
- boolean result;
- if (author.getUid() == uid) {
- result = getNamedParameterJdbcTemplate()
- .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid", sqlParameterSource) > 0;
- } else {
- result = getNamedParameterJdbcTemplate()
- .update("DELETE FROM replies WHERE message_id=:mid AND reply_id=:rid AND user_id=:uid"
- , sqlParameterSource) > 0;
- }
- if (result) {
- getNamedParameterJdbcTemplate().update("UPDATE messages SET replies=replies-1 WHERE message_id=:mid", sqlParameterSource);
- updateRepliesBy(mid);
- return true;
- }
- return false;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getLastMessages(int hours) {
- return getJdbcTemplate().queryForList("SELECT message_id FROM messages WHERE messages.ts>TIMESTAMPADD(HOUR,?,NOW())",
- Integer.class, -hours);
-
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<ResponseReply> getLastReplies(int hours) {
- return getJdbcTemplate().query("SELECT users2.nick,replies.message_id,replies.reply_id," +
- "users.nick,replies.txt," +
- "replies.ts,replies.attach,replies.ts+0, replies.html " +
- "FROM ((replies INNER JOIN users ON replies.user_id=users.id) " +
- "INNER JOIN messages ON replies.message_id=messages.message_id) " +
- "INNER JOIN users AS users2 ON messages.user_id=users2.id " +
- "WHERE replies.ts>TIMESTAMPADD(HOUR,?,NOW()) AND messages.privacy>0", (rs, rowNum) -> {
- ResponseReply reply = new ResponseReply();
- reply.setMuname(rs.getString(1));
- reply.setMid(rs.getInt(2));
- reply.setRid(rs.getInt(3));
- reply.setUname(rs.getString(4));
- reply.setDescription(rs.getString(5));
- reply.setPubDate(rs.getTimestamp(6));
- reply.setAttachmentType(rs.getString(7));
- reply.setHtml(rs.getBoolean(8));
- return reply;
- }, -hours);
- }
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getPopularCandidates() {
- return getJdbcTemplate().queryForList("SELECT replies.message_id FROM replies " +
- "INNER JOIN messages ON replies.message_id = messages.message_id " +
- "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " +
- "WHERE COALESCE(messages_tags.tag_id, 0) != 2 " +
- "AND COALESCE(messages_tags.tag_id, 0) != 805 AND replies.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " +
- "AND messages.popular=0 GROUP BY messages.message_id having COUNT(DISTINCT(replies.user_id)) > 5 " +
- "UNION ALL SELECT favorites.message_id FROM favorites " +
- "INNER JOIN messages ON messages.message_id = favorites.message_id " +
- "LEFT JOIN messages_tags ON messages_tags.message_id = messages.message_id " +
- "WHERE COALESCE(messages_tags.tag_id, 0) != 2 AND favorites.ts > TIMESTAMPADD(HOUR, -2, CURRENT_TIMESTAMP) " +
- "AND messages.popular=0 GROUP BY messages.message_id HAVING COUNT(DISTINCT favorites.user_id) > 1;", Integer.class);
- }
- @Transactional
- @Override
- public void setLastReadComment(User user, Integer mid, Integer rid) {
- jdbcTemplate.update("UPDATE subscr_messages SET last_read_rid=GREATEST(?, last_read_rid) WHERE message_id=? AND suser_id=?",
- rid, mid, user.getUid());
- }
- @Transactional
- @Override
- public void setRead(User user, Integer mid) {
- jdbcTemplate.update("UPDATE subscr_messages SET last_read_rid=(select replies from messages " +
- "where messages.message_id=subscr_messages.message_id) WHERE message_id=? AND suser_id=?",
- mid, user.getUid());
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getUnread(User user) {
- return jdbcTemplate.queryForList(
- "select subscr_messages.message_id " +
- "from subscr_messages inner join messages on subscr_messages.message_id=messages.message_id " +
- "where subscr_messages.suser_id=? and " +
- "messages.replies>subscr_messages.last_read_rid",
- Integer.class, user.getUid());
- }
-
- @Transactional
- @Override
- public boolean updateMessage(Integer mid, Integer rid, String body) {
- Instant now = Instant.now();
- if (rid == 0) {
- return jdbcTemplate.update("UPDATE messages_txt SET txt=?, updated_at=? WHERE message_id=?", body, Timestamp.from(now), mid) > 0;
- } else {
- return jdbcTemplate.update("UPDATE replies SET txt=?, updated_at=? WHERE message_id=? and reply_id=?",
- body, Timestamp.from(now), mid, rid) > 0;
- }
- }
-
- @Override
- public boolean updateReplyUri(Message reply, URI replyUri) {
- return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=1 WHERE message_id=? AND reply_id=?",
- replyUri.toASCIIString(), reply.getMid(), reply.getRid()) > 0;
- }
-
- @Override
- public boolean replyExists(URI replyUri) {
- return jdbcTemplate.queryForList("SELECT reply_id FROM replies WHERE reply_uri=?",
- Integer.class, replyUri.toASCIIString()).size() > 0;
- }
-
- @Override
- public boolean deleteReply(URI userUri, URI replyUri) {
- return jdbcTemplate.update("DELETE FROM replies WHERE user_uri=? AND reply_uri=?",
- userUri.toASCIIString(), replyUri.toASCIIString()) > 0;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/MessengerServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessengerServiceImpl.java
deleted file mode 100644
index 57101ffe..00000000
--- a/juick-server/src/main/java/com/juick/service/MessengerServiceImpl.java
+++ /dev/null
@@ -1,71 +0,0 @@
-package com.juick.service;
-
-import com.juick.User;
-import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
-import java.util.Optional;
-import java.util.UUID;
-
-@Repository
-public class MessengerServiceImpl extends BaseJdbcService implements MessengerService {
-
- @Transactional(readOnly = true)
- @Override
- public Integer getUserId(String senderId) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT id FROM users INNER JOIN messenger " +
- "ON messenger.user_id = users.id WHERE messenger.sender_id=?", Integer.class, senderId);
-
- return list.isEmpty() ? 0 : list.get(0);
- }
- @Transactional(readOnly = true)
- @Override
- public Optional<String> getSenderId(User user) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT sender_id FROM messenger " +
- "WHERE user_id=?", String.class, user.getUid());
-
- return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
- }
-
- @Transactional
- @Override
- public boolean createMessengerUser(String senderId, String displayName) {
- return getJdbcTemplate().update(
- "INSERT INTO messenger(sender_id, display_name, loginhash) VALUES(?,?,?)",
- senderId, displayName, UUID.randomUUID().toString()) > 0;
- }
- @Transactional(readOnly = true)
- @Override
- public String getDisplayName(String hash) {
- try {
- return getJdbcTemplate().queryForObject("SELECT display_name FROM messenger WHERE loginhash=?", String.class, hash);
- } catch (EmptyResultDataAccessException e) {
- return null;
- }
- }
- @Transactional
- @Override
- public String getSignUpHash(final String senderId, final String username) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT loginhash FROM messenger WHERE sender_id = ? AND user_id IS NULL",
- String.class,
- senderId);
-
- if (list.isEmpty()) {
- String hash = UUID.randomUUID().toString();
- getJdbcTemplate().update(
- "INSERT INTO messenger(sender_id, loginhash, display_name) VALUES (?, ?, ?)", senderId, hash, username);
- return hash;
- }
- return list.get(0);
- }
- @Transactional
- @Override
- public boolean linkMessengerUser(String hash, int uid) {
- return getJdbcTemplate().update("UPDATE messenger SET user_id=?, loginhash=NULL WHERE loginhash=?", uid, hash) > 0;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java
deleted file mode 100644
index 712e4b0e..00000000
--- a/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.Chat;
-import com.juick.User;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.jdbc.core.namedparam.SqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.List;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesService {
-
- @Transactional
- @Override
- public boolean createPM(final int uidFrom, final int uid_to, final String body) {
- return getJdbcTemplate().update(
- "INSERT INTO pm(user_id, user_id_to, txt) VALUES (?, ?, ?)",
- uidFrom, uid_to, body) > 0;
- }
-
- @Transactional
- @Override
- public boolean addPMinRoster(final int uid, final String jid) {
- return getJdbcTemplate().update(
- "INSERT INTO pm_inroster(user_id, jid) VALUES (?, ?) ON DUPLICATE KEY UPDATE user_id=user_id", uid, jid) > 0;
- }
-
- @Transactional
- @Override
- public boolean removePMinRoster(final int uid, final String jid) {
- return getJdbcTemplate().update(
- "DELETE FROM pm_inroster WHERE user_id = ? AND jid = ?", uid, jid) > 0;
- }
-
- @Transactional
- @Override
- public boolean havePMinRoster(final int uid, final String jid) {
- List<Integer> res = getJdbcTemplate().queryForList(
- "SELECT 1 FROM pm_inroster WHERE user_id = ? AND jid = ?",
- Integer.class,
- uid, jid);
- return res.size() > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Chat> getLastChats(final User user) {
- return getJdbcTemplate().query(
- "SELECT l.user_id, users.nick, l.last, pm.txt FROM pm "
- + "INNER JOIN users ON users.id = pm.user_id "
- + ""
- + "INNER JOIN (SELECT user_id, MAX(ts) AS last FROM pm "
- + "WHERE user_id_to=? GROUP BY user_id) l ON l.last = pm.ts "
- + "WHERE pm.user_id_to=? "
- + "ORDER BY l.last DESC",
- (rs, rowNum) -> {
- com.juick.Chat u = new com.juick.Chat();
- u.setUid(rs.getInt(1));
- u.setName(rs.getString(2));
- u.setLastMessageTimestamp(rs.getTimestamp(3).toInstant());
- u.setLastMessageText(rs.getString(4).trim());
- return u;
- },
- user.getUid(), user.getUid());
- }
-
- @Transactional
- @Override
- public List<com.juick.Message> getPMMessages(final int uid, final int uidTo) {
- SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("uidTo", uidTo);
-
- return getNamedParameterJdbcTemplate().query(
- "SELECT pm.user_id, pm.txt, pm.ts, users.nick FROM pm INNER JOIN users ON users.id=pm.user_id WHERE (user_id = :uid AND user_id_to = :uidTo) "
- + "OR (user_id_to = :uid AND user_id = :uidTo) ORDER BY ts DESC LIMIT 20",
- sqlParameterSource,
- (rs, rowNum) -> {
- com.juick.Message msg = new com.juick.Message();
- int uuid = rs.getInt(1);
- User user = new User();
- user.setUid(uuid);
- user.setName(rs.getString(4));
- msg.setUser(user);
- msg.setText(rs.getString(2).trim());
- msg.setTimestamp(rs.getTimestamp(3).toInstant());
- return msg;
- });
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<com.juick.Message> getLastPMInbox(final int uid) {
- return getJdbcTemplate().query(
- "SELECT pm.user_id, users.nick, pm.txt, 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).trim());
- msg.setTimestamp(rs.getTimestamp(4).toInstant());
- return msg;
- },
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<com.juick.Message> getLastPMSent(final int uid) {
- return getJdbcTemplate().query(
- "SELECT pm.user_id_to, users.nick, pm.txt, " +
- "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).trim());
- msg.setTimestamp(rs.getTimestamp(4).toInstant());
- return msg;
- },
- uid);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java
deleted file mode 100644
index 9f9cda1d..00000000
--- a/juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.Tag;
-import com.juick.User;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-@Transactional
-public class PrivacyQueriesServiceImpl extends BaseJdbcService implements PrivacyQueriesService {
-
- @Override
- public PrivacyResult blacklistUser(final User user, final User target) {
- int result = getJdbcTemplate().update(
- "DELETE FROM bl_users WHERE user_id = ? AND bl_user_id = ?",
- user.getUid(), target.getUid());
-
- if (result > 0)
- return PrivacyResult.Removed;
-
- getJdbcTemplate().update(
- "INSERT INTO bl_users(user_id, bl_user_id) VALUES (?, ?)",
- user.getUid(), target.getUid());
-
- return PrivacyResult.Added;
- }
-
- @Override
- public PrivacyResult blacklistTag(final User user, final Tag tag) {
- int result = getJdbcTemplate().update(
- "DELETE FROM bl_tags WHERE user_id = ? AND tag_id = ?",
- user.getUid(), tag.TID);
-
- if (result > 0)
- return PrivacyResult.Removed;
-
- getJdbcTemplate().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/service/PushQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java
deleted file mode 100644
index 7f97956c..00000000
--- a/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java
+++ /dev/null
@@ -1,143 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import org.apache.commons.collections4.CollectionUtils;
-import org.springframework.dao.DuplicateKeyException;
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.inject.Inject;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class PushQueriesServiceImpl extends BaseJdbcService implements PushQueriesService {
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getGCMRegID(final int uid) {
- return getJdbcTemplate().queryForList(
- "SELECT regid FROM android WHERE user_id=?",
- String.class,
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getGCMTokens(final Collection<Integer> uids) {
- if (CollectionUtils.isEmpty(uids))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT regid FROM android INNER JOIN users ON (users.id = android.user_id) WHERE users.id IN (:ids)",
- new MapSqlParameterSource("ids", uids),
- String.class);
- }
-
- @Transactional
- @Override
- public boolean addGCMToken(Integer uid, String token) {
- return getJdbcTemplate().update("INSERT IGNORE INTO android(user_id,regid) VALUES (?, ?)",
- uid, token) > 0;
- }
-
- @Transactional
- @Override
- public boolean deleteGCMToken(String token) {
- return getJdbcTemplate().update("DELETE FROM android WHERE regid=?", token) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getMPNSURL(final int uid) {
- return getJdbcTemplate().queryForList(
- "SELECT url FROM winphone WHERE user_id=?",
- String.class,
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getMPNSTokens(final Collection<Integer> uids) {
- if (CollectionUtils.isEmpty(uids))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT url FROM winphone INNER JOIN users ON (users.id=winphone.user_id) WHERE users.id IN (:ids)",
- new MapSqlParameterSource("ids", uids),
- String.class);
- }
-
- @Transactional
- @Override
- public boolean addMPNSToken(Integer uid, String token) {
- return getJdbcTemplate().update("INSERT IGNORE INTO winphone(user_id,url) VALUES (?, ?)",
- uid, token) > 0;
- }
-
- @Transactional
- @Override
- public boolean deleteMPNSToken(String token) {
- return getJdbcTemplate().update("DELETE FROM winphone WHERE url=?", token) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getAPNSToken(final int uid) {
- return getJdbcTemplate().queryForList(
- "SELECT token from ios WHERE user_id=?",
- String.class,
- uid);
- }
-
- @Transactional
- @Override
- public boolean deleteAPNSToken(String token) {
- return getJdbcTemplate().update("DELETE FROM ios WHERE token=?", token) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getAPNSTokens(final Collection<Integer> uids) {
- if (CollectionUtils.isEmpty(uids))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT token FROM ios INNER JOIN users ON (users.id = ios.user_id) WHERE users.id IN (:ids)",
- new MapSqlParameterSource("ids", uids),
- String.class);
- }
-
- @Transactional
- @Override
- public boolean addAPNSToken(Integer uid, String token) {
- try {
- return getJdbcTemplate().update("INSERT INTO ios(user_id,token) VALUES (?, ?)",
- uid, token) > 0;
- } catch (DuplicateKeyException e) {
- return true;
- }
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java
deleted file mode 100644
index 0fba35f1..00000000
--- a/juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.User;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-@Transactional(readOnly = true)
-public class ShowQueriesServiceImpl extends BaseJdbcService implements ShowQueriesService {
-
- @Override
- public List<String> getRecommendedUsers(final User forUser) {
- if (forUser == null)
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT u.nick FROM subscr_users su1 INNER JOIN users u " +
- "ON su1.user_id = u.id " +
- "WHERE NOT EXISTS (SELECT 1 FROM subscr_users su2 WHERE su2.suser_id = :uid and su1.user_id = su2.user_id) " +
- "AND EXISTS (SELECT 1 FROM subscr_users su3 WHERE su3.suser_id = :uid and su3.user_id = su1.suser_id ) " +
- "AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid and su1.user_id = b.bl_user_id) " +
- "AND su1.user_id != :uid AND u.lastmessage > UNIX_TIMESTAMP() - 259200 " +
- "GROUP BY su1.user_id ORDER BY count(*) DESC LIMIT 10",
- new MapSqlParameterSource("uid", forUser.getUid()),
- String.class);
- }
-
- @Override
- public List<String> getTopUsers() {
- return getJdbcTemplate().query(
- "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",
- (rs, rowNum) -> rs.getString(1));
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/SphinxSearchService.java b/juick-server/src/main/java/com/juick/service/SphinxSearchService.java
deleted file mode 100644
index de7bd7f2..00000000
--- a/juick-server/src/main/java/com/juick/service/SphinxSearchService.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.User;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.inject.Inject;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Created by aalexeev on 11/18/16.
- */
-
-@Repository
-@Transactional(readOnly = true)
-public class SphinxSearchService extends BaseJdbcService implements SearchService {
- private static final int DEFAULT_MAX_RESULT = 20;
-
- private int maxResult = DEFAULT_MAX_RESULT;
-
- @Inject
- UserService userService;
-
- public String sortHint(String searchString) {
- boolean isOneWord = searchString.split("[^\\S\\+]+").length == 1;
- return isOneWord ? "extended:@id desc" : "extended:@weight desc, @id desc";
- }
-
- @Override
- public List<Integer> searchInAllMessages(User visitor, final String searchString, final int page) {
- if (StringUtils.isBlank(searchString))
- return Collections.emptyList();
-
- Map<String, String> sphinxQuery = new HashMap<>();
- sphinxQuery.put("limit", String.valueOf(maxResult));
- sphinxQuery.put("mode", "any");
- sphinxQuery.put("sort", sortHint(searchString));
- String usersFilter = userService.getUserBLUsers(visitor.getUid()).stream().map(u -> String.valueOf(u.getUid())).collect(Collectors.joining(","));
- sphinxQuery.put("!filter", "user_id," + usersFilter);
- if (page > 0) {
- sphinxQuery.put("offset", String.valueOf(page * maxResult));
- }
-
- return getJdbcTemplate().queryForList(
- String.format("SELECT id FROM search WHERE query = '%s;%s'", searchString,
- sphinxQuery.entrySet().stream().map(Object::toString)
- .collect(Collectors.joining(";"))), Integer.class);
- }
-
- @Override
- public List<Integer> searchByStringAndUser(User visitor, final String searchString, final int userId, int page) {
- if (StringUtils.isBlank(searchString))
- return Collections.emptyList();
-
- Map<String, String> sphinxQuery = new HashMap<>();
- sphinxQuery.put("limit", String.valueOf(maxResult));
- sphinxQuery.put("mode", "any");
- sphinxQuery.put("sort", sortHint(searchString));
- if (page > 0) {
- sphinxQuery.put("offset", String.valueOf(page * maxResult));
- }
- return getJdbcTemplate().queryForList(
- String.format("SELECT id FROM search WHERE query = '%s;%s;filter=user_id,%d'", searchString,
- sphinxQuery.entrySet().stream().map(Object::toString)
- .collect(Collectors.joining(";")), userId), Integer.class);
- }
-
- @Override
- public void setMaxResult(int maxResult) {
- if (maxResult <= 0)
- throw new IllegalArgumentException("maxResult value (" + maxResult + ") must be greater then 0");
-
- this.maxResult = maxResult;
- }
-} \ No newline at end of file
diff --git a/juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java
deleted file mode 100644
index 5ce3593b..00000000
--- a/juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.Message;
-import com.juick.Tag;
-import com.juick.User;
-import com.juick.model.NotifyOpts;
-import com.juick.util.MessageUtils;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.collections4.IteratorUtils;
-import org.apache.commons.collections4.ListUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.dao.DuplicateKeyException;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.annotation.Nonnull;
-import javax.inject.Inject;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class SubscriptionServiceImpl extends BaseJdbcService implements SubscriptionService {
- @Inject
- private UserService userService;
- @Inject
- private MessagesService messagesService;
- @Inject
- private TagService tagService;
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getSubscribedUsers(final int uid, final Message msg) {
- int mid = msg.getMid();
- User author = messagesService.getMessageAuthor(mid);
-
- List<User> subscribers = userService.getUserReaders(uid);
- List<User> mentionedUsers = userService.getUsersByName(MessageUtils.getMentions(msg).stream()
- .map(u -> u.substring(1)).collect(Collectors.toList()));
- List<User> users = ListUtils.union(subscribers, mentionedUsers);
- List<Integer> tags = tagService.getMessageTagsIDs(mid);
- List<String> tagsStr = tagService.getMessageTags(mid).stream().map(t -> t.getTag().getName()).collect(Collectors.toList());
-
- Set<Integer> set = new HashSet<>();
- set.addAll(
- users.stream()
- .map(User::getUid).filter(u -> Collections.disjoint(tagService.getUserBLTags(u), tagsStr))
- .collect(Collectors.toList()));
-
-
- if (!tags.isEmpty()) {
- List<Integer> tagUsers = getNamedParameterJdbcTemplate().queryForList(
- "SELECT st.suser_id FROM subscr_tags st " +
- "WHERE st.tag_id IN (:ids) AND st.suser_id != :uid " +
- " AND NOT EXISTS (SELECT 1 FROM bl_users bu WHERE bu.bl_user_id = :authorUid and st.suser_id = bu.user_id)" +
- " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN (:ids) and st.suser_id = bt.user_id)",
- new MapSqlParameterSource()
- .addValue("ids", tags)
- .addValue("uid", uid)
- .addValue("authorUid", author.getUid()),
- Integer.class);
- set.addAll(tagUsers);
- }
- return userService.getUsersByID(set);
- }
- @Override
- public List<User> getUsersSubscribedToComments(@Nonnull final Message msg, @Nonnull final Message reply) {
- return getUsersSubscribedToComments(msg, reply, false);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getUsersSubscribedToComments(@Nonnull final Message msg, @Nonnull final Message reply,
- boolean blacklisted) {
- List<User> subscribers = userService.getUsersByID(getJdbcTemplate().queryForList(
- "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?",
- Integer.class,
- msg.getMid(), reply.getUser().getUid()));
- List<User> mentionedUsers = userService.getUsersByName(MessageUtils.getMentions(reply).stream()
- .map(u -> u.substring(1)).collect(Collectors.toList()));
- List<User> users = IteratorUtils.toList(CollectionUtils.union(subscribers, mentionedUsers).iterator());
- if (!users.isEmpty()) {
- return users.stream()
- .filter(u -> blacklisted || !userService.isReplyToBL(u, reply))
- .collect(Collectors.toList());
- }
- return Collections.emptyList();
- }
-
- @Override
- public List<User> getUsersSubscribedToUserRecommendations(final int uid, final Message msg) {
- List<String> msgTags = tagService.getMessageTags(msg.getMid()).stream().map(t -> t.getTag().getName()).collect(Collectors.toList());
- if (msg.getLikes() == 1) {
- return userService.getUserReaders(uid).stream()
- .filter(u -> !u.equals(msg.getUser()))
- .filter(u -> !userService.isInBLAny(u.getUid(), msg.getUser().getUid()))
- .filter(u -> Collections.disjoint(tagService.getUserBLTags(u.getUid()), msgTags))
- .collect(Collectors.toList());
- }
- return Collections.emptyList();
- }
-
- @Transactional
- @Override
- public boolean subscribeMessage(final Message message, final User user) {
- try {
- boolean result = getJdbcTemplate().update(
- "INSERT INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", user.getUid(), message.getMid()) == 1;
- messagesService.setLastReadComment(user, message.getMid(), message.getReplies());
- return result;
- } catch (DuplicateKeyException e) {
- return true;
- }
- }
-
- @Transactional
- @Override
- public boolean unSubscribeMessage(final int mid, final int vuid) {
- return getJdbcTemplate().update(
- "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", mid, vuid) > 0;
- }
-
- @Transactional
- @Override
- public boolean subscribeUser(final User user, final User toUser) {
- try {
- return getJdbcTemplate().update(
- "INSERT INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1;
- } catch (DuplicateKeyException e) {
- return true;
- }
- }
-
- @Transactional
- @Override
- public boolean unSubscribeUser(final User user, final User fromUser) {
- return getJdbcTemplate().update(
- "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", user.getUid(), fromUser.getUid()) > 0;
- }
-
- @Transactional
- @Override
- public boolean subscribeTag(final User user, final Tag toTag) {
- try {
-
- return getJdbcTemplate().update(
- "INSERT INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID, user.getUid()) == 1;
- } catch (DuplicateKeyException e) {
- return true;
- }
- }
-
- @Transactional
- @Override
- public boolean unSubscribeTag(final User user, final Tag toTag) {
- return getJdbcTemplate().update(
- "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getSubscribedTags(User user) {
- return getJdbcTemplate().queryForList("SELECT tags.name FROM subscr_tags INNER JOIN tags " +
- "ON(tags.tag_id = subscr_tags.tag_id) " +
- "WHERE subscr_tags.suser_id=? ORDER BY tags.name", String.class, user.getUid());
- }
-
- @Transactional(readOnly = true)
- @Override
- public NotifyOpts getNotifyOptions(final User user) {
- List<NotifyOpts> list = getJdbcTemplate().query(
- "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());
-
- return list.isEmpty() ?
- new NotifyOpts() : list.get(0);
- }
-
- @Transactional
- @Override
- public boolean setNotifyOptions(final User user, final NotifyOpts options) {
- int jnotify = getJdbcTemplate().update(
- "UPDATE useroptions SET jnotify=? WHERE user_id=?",
- options.isRepliesEnabled() ? 1 : 0,
- user.getUid());
-
- int subscr_notify = getJdbcTemplate().update(
- "UPDATE useroptions SET subscr_notify=? WHERE user_id=?",
- options.isSubscriptionsEnabled() ? 1 : 0,
- user.getUid());
-
- int recommendations = getJdbcTemplate().update(
- "UPDATE useroptions SET recommendations=? WHERE user_id=?",
- options.isRecommendationsEnabled() ? 1 : 0,
- user.getUid());
-
- return jnotify > 0 && subscr_notify > 0 && recommendations > 0;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java
deleted file mode 100644
index 42159d3b..00000000
--- a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java
+++ /dev/null
@@ -1,277 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.Tag;
-import com.juick.User;
-import com.juick.model.TagStats;
-import com.juick.util.StreamUtils;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
-import org.springframework.jdbc.core.RowMapper;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.jdbc.support.GeneratedKeyHolder;
-import org.springframework.jdbc.support.KeyHolder;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class TagServiceImpl extends BaseJdbcService implements TagService {
-
- @Transactional(readOnly = true)
- @Override
- public com.juick.Tag getTag(final int tid) {
- List<Tag> list = getJdbcTemplate().query(
- "SELECT synonym_id,name FROM tags WHERE tag_id=?",
- (rs, num) -> {
- Tag ret = new Tag(rs.getString(2));
- ret.TID = tid;
- ret.SynonymID = rs.getInt(1);
- return ret;
- },
- tid);
-
- return list.isEmpty() ?
- null : list.get(0);
- }
-
- @Transactional
- @Override
- public com.juick.Tag getTag(final String tag, final boolean autoCreate) {
- if (StringUtils.isBlank(tag))
- return null;
-
- List<Tag> list = getJdbcTemplate().query(
- "SELECT tag_id, synonym_id, name FROM tags WHERE name = ?",
- (rs, rowNum) -> {
- Tag ret1 = new Tag(rs.getString(3));
- ret1.TID = rs.getInt(1);
- ret1.SynonymID = rs.getInt(2);
- return ret1;
- },
- tag);
-
- Tag ret = list.isEmpty() ?
- null : list.get(0);
-
- if (ret == null && autoCreate) {
- ret = new com.juick.Tag(tag);
- ret.TID = createTag(tag);
- }
-
- return ret;
- }
-
- @Override
- public List<Tag> getTags(Stream<String> tags, final boolean autoCreate) {
- return tags.filter(StringUtils::isNotBlank).map(tag -> getTag(tag, autoCreate)).filter(Objects::nonNull).distinct()
- .collect(Collectors.toList());
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean getTagNoIndex(final int tagId) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT noindex FROM tags WHERE tag_id=?", Integer.class, tagId);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional
- @Override
- public int createTag(final String name) {
- KeyHolder holder = new GeneratedKeyHolder();
- getJdbcTemplate().update(
- con -> {
- PreparedStatement stmt = con.prepareStatement(
- "INSERT INTO tags(name) VALUES (?)",
- Statement.RETURN_GENERATED_KEYS);
- stmt.setString(1, name);
- return stmt;
- },
- holder);
-
- return holder.getKey().intValue();
- }
-
- private class TagStatsMapper implements RowMapper<TagStats> {
-
- @Override
- public TagStats mapRow(ResultSet rs, int rowNum) throws SQLException {
- Tag t = new Tag(rs.getString(1));
- TagStats s = new TagStats();
- s.setTag(t);
- s.setUsageCount(rs.getInt(2));
- return s;
- }
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<TagStats> getUserTagStats(final int uid) {
- return getJdbcTemplate().query(
- "SELECT tags.name,COUNT(messages.message_id) " +
- "FROM (messages INNER JOIN messages_tags ON (messages.user_id=? " +
- "AND messages.message_id=messages_tags.message_id)) " +
- "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC",
- new TagStatsMapper(),
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getUserBLTags(final int uid) {
- return getJdbcTemplate().queryForList(
- "SELECT tags.name FROM tags INNER JOIN bl_tags " +
- "ON (bl_tags.user_id = ? AND bl_tags.tag_id = tags.tag_id) ORDER BY tags.name",
- String.class, uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getPopularTags() {
- return getJdbcTemplate().queryForList(
- "select name from tags where noindex=0 order by stat_messages desc limit 20", String.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<TagStats> getTagStats() {
- return getJdbcTemplate().query(
- "SELECT tags.name,COUNT(DISTINCT messages.user_id) AS cnt " +
- "FROM (messages INNER JOIN messages_tags ON (messages.ts>TIMESTAMPADD(DAY,-3,NOW()) " +
- "AND messages.message_id=messages_tags.message_id)) " +
- "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id " +
- "WHERE tags.tag_id NOT IN (SELECT tag_id FROM tags_ignore) " +
- "GROUP BY tags.tag_id ORDER BY cnt DESC LIMIT 20", new TagStatsMapper());
- }
-
- @Transactional
- @Override
- public List<Tag> updateTags(final int mid, final Collection<Tag> newTags) {
- List<Tag> currentTags = getMessageTags(mid).stream()
- .map(TagStats::getTag).collect(Collectors.toList());
-
- if (CollectionUtils.isEmpty(newTags))
- return currentTags;
-
- List<Integer> idsForDelete = newTags.stream()
- .filter(currentTags::contains)
- .map(tag -> tag.TID)
- .collect(Collectors.toList());
- if (newTags.size() - idsForDelete.size() >= 5) {
- return currentTags;
- }
-
- if (!idsForDelete.isEmpty())
- getNamedParameterJdbcTemplate().update(
- "DELETE FROM messages_tags WHERE message_id = :mid AND tag_id in (:ids)",
- new MapSqlParameterSource().addValue("ids", idsForDelete).addValue("mid", mid));
-
- newTags.stream().filter(t -> !currentTags.contains(t))
- .forEach(t -> getJdbcTemplate().update("INSERT INTO messages_tags(message_id,tag_id) VALUES (?,?)", mid, t.TID));
-
- List<Tag> result = getMessageTags(mid).stream()
- .map(TagStats::getTag).collect(Collectors.toList());
- jdbcTemplate.update("UPDATE messages_txt SET tags=? WHERE message_id=?", result.stream()
- .map(Tag::getName).collect(Collectors.joining(" ")), mid);
- return result;
- }
-
- @Override
- public Pair<String, List<Tag>> fromString(final String txt) {
- String firstLine = txt.split("\\n", 2)[0];
- Supplier<Stream<String>> tagsStream = () -> StreamUtils.takeWhile(Arrays.stream(firstLine.split("\\ ")),
- t -> !t.equals("*") && t.startsWith("*"));
- int tagsLength = tagsStream.get().collect(Collectors.joining(" ")).length();
- String body = txt.substring(tagsLength);
- List<Tag> tags = tagsStream.get().map(t -> getTag(t.substring(1), true))
- .distinct().collect(Collectors.toList());
- return Pair.of(body, tags);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<TagStats> getMessageTags(final int mid) {
- return getJdbcTemplate().query(
- "SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " +
- "INNER JOIN messages_tags ON (messages_tags.message_id = ? AND messages_tags.tag_id = tags.tag_id)",
- (rs, num) -> {
- com.juick.Tag t = new com.juick.Tag(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);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> getMessageTagsIDs(final int mid) {
- return getJdbcTemplate().queryForList(
- "SELECT tag_id FROM messages_tags WHERE message_id = ?",
- Integer.class, mid);
- }
-
- @Override
- public boolean blacklistTag(User user, Tag tag) {
- int rowcount = getNamedParameterJdbcTemplate().update("DELETE FROM bl_tags WHERE tag_id = :tid AND user_id = :uid",
- new MapSqlParameterSource().addValue("tid", tag.TID).addValue("uid", user.getUid()));
- return rowcount <= 0 && getNamedParameterJdbcTemplate()
- .update("INSERT INTO bl_tags(user_id, tag_id) VALUES(:uid,:tid)",
- new MapSqlParameterSource().addValue("tid", tag.TID)
- .addValue("uid", user.getUid())) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isInBL(User user, Tag tag) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM bl_tags WHERE user_id = ? AND tag_id = ?",
- Integer.class, user.getUid(), tag.TID);
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isSubscribed(User user, Tag tag) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM subscr_tags WHERE suser_id = ? AND tag_id = ?",
- Integer.class, user.getUid(), tag.TID);
- return !list.isEmpty() && list.get(0) == 1;
- }
-
-}
diff --git a/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java
deleted file mode 100644
index 99cbabf6..00000000
--- a/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.User;
-import org.springframework.dao.DuplicateKeyException;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
-import java.util.stream.Collectors;
-
-/**
- * Created by vt on 24/11/2016.
- */
-@Repository
-public class TelegramServiceImpl extends BaseJdbcService implements TelegramService {
-
- @Transactional
- @Override
- public boolean deleteAnonymous(Long id) {
- return getJdbcTemplate().update("DELETE FROM telegram WHERE tg_id=? AND user_id IS NULL", id) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Long> getAnonymous() {
- return getJdbcTemplate().queryForList("SELECT tg_id FROM telegram WHERE user_id IS NULL", Long.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getUser(final long tgId) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT id FROM users INNER JOIN telegram " +
- "ON telegram.user_id = users.id WHERE telegram.tg_id=?", Integer.class, tgId);
-
- return list.isEmpty() ? 0 : list.get(0);
- }
-
- @Transactional
- @Override
- public boolean createTelegramUser(final long tgID, final String tgName) {
- return getJdbcTemplate().update(
- "INSERT INTO telegram(tg_id, tg_name, loginhash) VALUES(?,?,?)",
- tgID, tgName, UUID.randomUUID().toString()) > 0;
- }
-
- @Transactional
- @Override
- public boolean deleteTelegramUser(Integer uid) {
- return getJdbcTemplate().update("DELETE FROM telegram WHERE user_id=?", uid) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Long> getTelegramIdentifiers(List<User> users) {
- List<Integer> uids = users.stream().map(User::getUid).collect(Collectors.toList());
- if (uids.isEmpty()) {
- return Collections.emptyList();
- }
- return getNamedParameterJdbcTemplate().queryForList("" +
- "SELECT tg_id FROM telegram WHERE user_id IN(:uids)", new MapSqlParameterSource()
- .addValue("uids", uids), Long.class);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server/src/main/java/com/juick/service/UserServiceImpl.java
deleted file mode 100644
index fdc4f28c..00000000
--- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java
+++ /dev/null
@@ -1,668 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service;
-
-import com.juick.Message;
-import com.juick.User;
-import com.juick.model.AnonymousUser;
-import com.juick.model.Auth;
-import com.juick.model.UserInfo;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.RandomStringUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.dao.DuplicateKeyException;
-import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.jdbc.core.RowMapper;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.jdbc.support.GeneratedKeyHolder;
-import org.springframework.jdbc.support.KeyHolder;
-import org.springframework.stereotype.Repository;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.annotation.Nonnull;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.sql.Timestamp;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
-import java.util.UUID;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-@Repository
-public class UserServiceImpl extends BaseJdbcService implements UserService {
-
- private class UserMapper implements RowMapper<User> {
- @Override
- public User mapRow(ResultSet rs, int rowNum) throws SQLException {
- User user = new User();
-
- user.setUid(rs.getInt(1));
- user.setName(rs.getString(2));
- user.setCredentials(rs.getString(3));
- user.setBanned(rs.getBoolean(4));
- Timestamp seen = rs.getTimestamp(5);
- if (seen != null) {
- user.setSeen(seen.toInstant());
- }
- return user;
- }
- }
-
- @Transactional
- @Override
- public String getSignUpHashByJID(final String jid) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT loginhash FROM jids WHERE jid = ? AND user_id IS NULL", String.class, jid);
-
- if (list.isEmpty()) {
- String hash = UUID.randomUUID().toString();
- getJdbcTemplate().update("INSERT INTO jids(jid, loginhash) VALUES (?, ?)", jid, hash);
- return hash;
- }
- return list.get(0);
- }
-
- @Transactional
- @Override
- public String getSignUpHashByTelegramID(final Long telegramId, final String username) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT loginhash FROM telegram WHERE tg_id = ? AND user_id IS NULL",
- String.class,
- telegramId);
-
- if (list.isEmpty()) {
- String hash = UUID.randomUUID().toString();
- getJdbcTemplate().update(
- "INSERT INTO telegram(tg_id, loginhash, tg_name) VALUES (?, ?, ?)", telegramId, hash, username);
- return hash;
- }
- return list.get(0);
- }
-
- @Transactional
- @Override
- public int createUser(final String username, final String password) {
- KeyHolder holder = new GeneratedKeyHolder();
- try {
- getJdbcTemplate().update(
- con -> {
- PreparedStatement stmt = con.prepareStatement(
- "INSERT INTO users(nick,passw) VALUES (?,?)",
- Statement.RETURN_GENERATED_KEYS);
- stmt.setString(1, username);
- stmt.setString(2, password);
- return stmt;
- },
- holder);
- } catch (DuplicateKeyException e) {
- return -1;
- }
-
- int uid = holder.getKey().intValue();
-
- getJdbcTemplate().update("INSERT INTO useroptions(user_id) VALUES (?)", uid);
- getJdbcTemplate().update("INSERT INTO subscr_users(user_id, suser_id) VALUES (2, ?)", uid);
-
- return uid;
- }
-
- @Transactional(readOnly = true)
- @Override
- public Optional<User> getUserByUID(final int uid) {
- List<User> list = getJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE id = ?", new UserMapper(), uid);
-
- return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
- }
-
- @Transactional(readOnly = true)
- @Nonnull
- @Override
- public User getUserByName(final String username) {
- if (StringUtils.isNotBlank(username)) {
- List<User> list = getJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE nick = ?", new UserMapper(), username);
-
- if (!list.isEmpty())
- return list.get(0);
- }
- return AnonymousUser.INSTANCE;
- }
-
- @Override
- @Transactional(readOnly = true)
- @Nonnull
- public User getUserByEmail(String email) {
- if (StringUtils.isNotBlank(email)) {
- List<User> list = getJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE id = (SELECT DISTINCT user_id FROM emails WHERE email = ?)",
- new UserMapper(),
- email);
-
- if (!list.isEmpty())
- return list.get(0);
- }
- return AnonymousUser.INSTANCE;
- }
-
- @Transactional(readOnly = true)
- @Override
- public User getUserByJID(final String jid) {
- User result = null;
-
- if (StringUtils.isNotBlank(jid)) {
- List<User> list = getJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE id = (SELECT user_id FROM jids WHERE jid = ?)",
- new UserMapper(),
- jid);
-
- if (!list.isEmpty())
- result = list.get(0);
- }
- return result;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getUsersByName(final Collection<String> unames) {
- if (CollectionUtils.isEmpty(unames))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE nick IN (:unames)",
- new MapSqlParameterSource("unames", unames),
- new UserMapper());
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getUsersByID(final Collection<Integer> uids) {
- if (CollectionUtils.isEmpty(uids))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE id IN (:ids)",
- new MapSqlParameterSource("ids", uids),
- new UserMapper());
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getJIDsbyUID(final int uid) {
- return getJdbcTemplate().queryForList("SELECT jid FROM jids WHERE user_id = ? AND active = 1", String.class, uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getUIDbyJID(final String jid) {
- if (StringUtils.isNotBlank(jid)) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT user_id FROM jids WHERE jid = ?", Integer.class, jid);
-
- if (!list.isEmpty())
- return list.get(0);
- }
- return 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getUIDbyName(final String uname) {
- if (StringUtils.isNotBlank(uname)) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT id FROM users WHERE nick = ?", Integer.class, uname);
-
- if (!list.isEmpty())
- return list.get(0);
- }
- return 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getUIDbyHash(final String hash) {
- if (StringUtils.isNotBlank(hash)) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT user_id FROM logins WHERE hash = ?", Integer.class, hash);
-
- if (!list.isEmpty())
- return list.get(0);
- }
- return 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public com.juick.User getUserByHash(final String hash) {
- if (StringUtils.isNotBlank(hash)) {
- List<User> list = getJdbcTemplate().query(
- "SELECT logins.user_id, users.nick, users.passw, users.banned, last_seen FROM logins " +
- "INNER JOIN users ON logins.user_id = users.id WHERE logins.hash = ?",
- new UserMapper(),
- hash);
-
- if (!list.isEmpty()) {
- User user = list.get(0);
- user.setAuthHash(hash);
- return user;
- }
- }
- return AnonymousUser.INSTANCE;
- }
-
- @Transactional
- @Override
- public String getHashByUID(final int uid) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT hash FROM logins WHERE user_id = ?", String.class, uid);
-
- if (list.isEmpty()) {
- String hash = RandomStringUtils.randomAlphanumeric(16).toUpperCase();
- getJdbcTemplate().update("INSERT INTO logins(user_id, hash) VALUES (?, ?)", uid, hash);
- return hash;
- }
- return list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public int checkPassword(final String username, final String password) {
- if (StringUtils.isNotBlank(username)) {
- List<User> list = getJdbcTemplate().query(
- "SELECT id, nick, passw, banned, last_seen FROM users WHERE nick = ?",
- new UserMapper(),
- username);
-
- if (!list.isEmpty()) {
- User user = list.get(0);
- if (Objects.equals(password, user.getCredentials()))
- return user.getUid();
- }
- }
- return -1;
- }
-
- @Transactional
- @Override
- public boolean updatePassword(final User user, final String newPassword) {
- return user != null &&
- user.getUid() > 0 &&
- getJdbcTemplate().update("UPDATE users SET passw = ? WHERE id = ?", newPassword, user.getUid()) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getUserOptionInt(final int uid, final String option, final int defaultValue) {
- if (StringUtils.isBlank(option))
- return defaultValue;
-
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT " + option + " FROM useroptions WHERE user_id = ?", Integer.class, uid);
-
- return list.isEmpty() ? defaultValue : list.get(0);
- }
-
- @Transactional
- @Override
- public int setUserOptionInt(final int uid, final String option, final int value) {
- if (StringUtils.isBlank(option))
- return 0;
-
- return getJdbcTemplate().update("UPDATE useroptions SET " + option + "= ? WHERE user_id = ?", value, uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public UserInfo getUserInfo(final User user) {
- List<UserInfo> list = getJdbcTemplate().query(
- "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());
-
- return list.isEmpty() ? new UserInfo() : list.get(0);
- }
-
- @Transactional
- @Override
- public boolean updateUserInfo(final User user, final UserInfo info) {
- return getJdbcTemplate().update(
- "INSERT INTO usersinfo(user_id, fullname, country, url, descr) VALUES (?, ?, ?, ?, ?) " +
- "ON DUPLICATE KEY UPDATE fullname = ?, country = ?, url = ?, descr = ?",
- user.getUid(),
- info.getFullName(),
- info.getCountry(),
- info.getUrl(),
- info.getDescription(),
- info.getFullName(),
- info.getCountry(),
- info.getUrl(),
- info.getDescription()) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean getCanMedia(final int uid) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT users.lastphoto - UNIX_TIMESTAMP() FROM users WHERE id = ?",
- Integer.class,
- uid);
-
- return !list.isEmpty() && list.get(0) < 3600;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isInWL(final int uid, final int check) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM wl_users WHERE user_id = ? AND wl_user_id = ?",
- Integer.class, uid, check);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isInBL(final int uid, final int check) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM bl_users WHERE user_id = ? AND bl_user_id = ?", Integer.class, uid, check);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isInBLAny(final int uid, final int uid2) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "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);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isReplyToBL(final User user, final Message reply) {
- return getNamedParameterJdbcTemplate().queryForObject("WITH RECURSIVE banned(reply_id, user_id) AS (" +
- "SELECT reply_id, user_id FROM replies " +
- "WHERE replies.message_id = :mid " +
- "AND EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = replies.user_id) " +
- "UNION ALL SELECT replies.reply_id, replies.user_id FROM replies " +
- "INNER JOIN banned ON banned.reply_id = replies.replyto " +
- "WHERE replies.message_id = :mid) " +
- "SELECT COUNT(reply_id) from replies " +
- "INNER JOIN messages m ON m.message_id = replies.message_id " +
- "WHERE replies.message_id = :mid " +
- "AND replies.reply_id = :rid " +
- "AND (EXISTS (SELECT 1 FROM banned WHERE banned.reply_id = replies.reply_id) " +
- "OR EXISTS (SELECT 1 FROM bl_users b WHERE b.user_id = :uid AND b.bl_user_id = m.user_id)" +
- "OR EXISTS (SELECT 1 FROM bl_users b WHERE b.bl_user_id = :uid AND b.user_id = m.user_id))",
- new MapSqlParameterSource("uid", user.getUid())
- .addValue("mid", reply.getMid())
- .addValue("rid", reply.getRid()),
- Integer.class) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Integer> checkBL(final int visitor, final Collection<Integer> uids) {
- if (CollectionUtils.isEmpty(uids))
- return Collections.emptyList();
-
- return getNamedParameterJdbcTemplate().queryForList(
- "SELECT user_id FROM bl_users WHERE bl_user_id = :visitor and user_id IN (:ids)",
- new MapSqlParameterSource()
- .addValue("visitor", visitor)
- .addValue("ids", uids),
- Integer.class);
- }
-
- @Transactional(readOnly = true)
- @Override
- public boolean isSubscribed(final int uid, final int check) {
- List<Integer> list = getJdbcTemplate().queryForList(
- "SELECT 1 FROM subscr_users WHERE suser_id = ? AND user_id = ?",
- Integer.class, uid, check);
-
- return !list.isEmpty() && list.get(0) == 1;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<com.juick.User> getUserReadLeastPopular(final int uid, final int cnt) {
- return getJdbcTemplate().query(
- "SELECT users.id,users.nick FROM (subscr_users " +
- "INNER JOIN users_subscr ON (subscr_users.suser_id=? " +
- "AND subscr_users.user_id=users_subscr.user_id)) INNER JOIN users " +
- "ON subscr_users.user_id=users.id ORDER BY cnt LIMIT ?",
- (rs, num) -> {
- com.juick.User u = new com.juick.User();
- u.setUid(rs.getInt(1));
- u.setName(rs.getString(2));
- return u;
- },
- uid,
- cnt);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getUserReaders(final int uid) {
- return getJdbcTemplate().query(
- "SELECT users.id, users.nick FROM subscr_users " +
- "INNER JOIN users ON subscr_users.suser_id=users.id " +
- "WHERE subscr_users.user_id=? ORDER BY users.nick",
- (rs, num) -> {
- com.juick.User u = new com.juick.User();
- u.setUid(rs.getInt(1));
- u.setName(rs.getString(2));
- return u;
- },
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<User> getUserFriends(final int uid) {
- return getJdbcTemplate().query(
- "SELECT users.id,users.nick FROM subscr_users " +
- "INNER JOIN users ON subscr_users.user_id=users.id " +
- "WHERE subscr_users.suser_id=? AND users.id!=? " +
- "ORDER BY users.nick",
- (rs, num) -> {
- com.juick.User u = new com.juick.User();
- u.setUid(rs.getInt(1));
- u.setName(rs.getString(2));
- return u;
- },
- uid,
- uid);
- }
-
- @Transactional(readOnly = true)
- @Override
- public Integer getUserRecommendations(User user) {
- try {
- return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM favorites WHERE user_id=?", Integer.class, user.getUid());
- } catch (EmptyResultDataAccessException e) {
- return 0;
- }
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<com.juick.User> getUserBLUsers(final int uid) {
- return getJdbcTemplate().query("SELECT users.id,users.nick FROM users INNER JOIN bl_users " +
- "ON(bl_users.bl_user_id=users.id) WHERE bl_users.user_id=? ORDER BY users.nick",
- (rs, num) -> {
- com.juick.User u = new com.juick.User();
- u.setUid(rs.getInt(1));
- u.setName(rs.getString(2));
- return u;
- }, uid);
- }
-
- @Transactional
- @Override
- public boolean linkTwitterAccount(
- final User user, final String accessToken, final String accessTokenSecret, final String screenName) {
- return getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " +
- "VALUES (?,?,?,?)" +
- " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?",
- user.getUid(), accessToken, accessTokenSecret, screenName, accessToken, accessTokenSecret, screenName) > 0;
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getStatsMyReaders(final int uid) {
- List<Integer> list = getJdbcTemplate().queryForList("SELECT COUNT(*) FROM subscr_users WHERE user_id = ?", Integer.class, uid);
- return list.isEmpty() ? 0 : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getStatsMessages(final int uid) {
- List<Integer> list = getJdbcTemplate().queryForList("SELECT COUNT(*) FROM messages WHERE user_id = ?", Integer.class, uid);
- return list.isEmpty() ? 0 : list.get(0);
- }
-
- @Transactional(readOnly = true)
- @Override
- public int getStatsReplies(final int uid) {
- List<Integer> list = getJdbcTemplate().queryForList("SELECT COUNT(*) FROM replies WHERE user_id = ?", Integer.class, uid);
- return list.isEmpty() ? 0 : list.get(0);
- }
-
- @Transactional
- @Override
- public boolean setActiveStatusForJID(final String JID, final UserService.ActiveStatus jidStatus) {
- User user = getUserByJID(JID);
- if (user != null) {
- int newStatus = jidStatus == UserService.ActiveStatus.Active ? 1 : 0;
- return getJdbcTemplate().update(
- "UPDATE jids SET active = ? WHERE user_id = ? AND jid = ?",
- newStatus, user.getUid(), JID) >= 0;
- }
- return false;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getAllJIDs(final User user) {
- return getJdbcTemplate().queryForList(
- "SELECT jid FROM jids WHERE user_id=?", String.class, user.getUid());
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<Auth> getAuthCodes(final User user) {
- return getJdbcTemplate().query(
- "SELECT account,authcode FROM auth WHERE user_id=? AND protocol='xmpp'",
- (rs, num) -> new Auth(rs.getString(1), rs.getString(2)),
- user.getUid());
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getEmails(final User user) {
- return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUid());
- }
-
- @Transactional(readOnly = true)
- @Override
- public String getEmailHash(final User user) {
- List<String> list = getJdbcTemplate().queryForList(
- "SELECT hash FROM mail WHERE user_id = ?",
- String.class,
- user.getUid());
- return list.isEmpty() ? StringUtils.EMPTY : list.get(0) + "@mail.juick.com";
- }
-
- @Transactional
- @Override
- public int deleteLoginForUser(final String name) {
- if (StringUtils.isBlank(name))
- return 0;
-
- return getJdbcTemplate().update(
- "delete from logins where user_id in (select id from users where nick = ?)", name);
- }
-
- @Transactional
- @Override
- public int setLoginForUser(final int uid, final String loginHash) {
- if (StringUtils.isEmpty(loginHash))
- return 0;
-
- return getNamedParameterJdbcTemplate().update(
- "INSERT INTO logins (user_id, hash) VALUES(:uid, :hash) ON DUPLICATE KEY UPDATE hash = :hash",
- new MapSqlParameterSource()
- .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 getNamedParameterJdbcTemplate().update("DELETE FROM jids " +
- "WHERE (SELECT COUNT(*) cnt FROM (select user_id, jid FROM jids j) c WHERE user_id=:uid) > 1 " +
- "AND user_id=:uid AND jid=:jid",
- new MapSqlParameterSource()
- .addValue("uid", uid)
- .addValue("jid", 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;
- }
-
- @Transactional(readOnly = true)
- @Override
- public List<String> getActiveJIDs() {
- return getJdbcTemplate().queryForList("SELECT jid FROM jids WHERE active=1 AND loginhash IS NULL", String.class);
- }
-
- @Override
- public void updateLastSeen(User user) {
- getJdbcTemplate().update("UPDATE users SET last_seen=now() WHERE id=?", user.getUid());
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java b/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java
deleted file mode 100644
index 863bda04..00000000
--- a/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.juick.service.activities;
-
-import org.springframework.context.event.EventListener;
-import org.springframework.scheduling.annotation.Async;
-
-public interface ActivityListener {
- @Async
- @EventListener
- void processFollowEvent(FollowEvent event);
- @Async
- @EventListener
- void undoFollowEvent(UndoFollowEvent event);
- @Async
- @EventListener
- void deleteUserEvent(DeleteUserEvent event);
- @Async
- @EventListener
- void deleteMessageEvent(DeleteMessageEvent event);
-}
diff --git a/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java b/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java
deleted file mode 100644
index 67e40f44..00000000
--- a/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.juick.service.activities;
-
-import com.juick.Message;
-import org.springframework.context.ApplicationEvent;
-
-public class DeleteMessageEvent extends ApplicationEvent {
- private Message message;
- /**
- * Create a new ApplicationEvent.
- *
- * @param source the object on which the event initially occurred (never {@code null})
- */
- public DeleteMessageEvent(Object source, Message message) {
- super(source);
- this.message = message;
- }
-
- public Message getMessage() {
- return message;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java b/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java
deleted file mode 100644
index 8b51da9d..00000000
--- a/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package com.juick.service.activities;
-
-import org.springframework.context.ApplicationEvent;
-
-public class DeleteUserEvent extends ApplicationEvent {
- private String userUri;
- /**
- * Create a new ApplicationEvent.
- *
- * @param source the object on which the event initially occurred (never {@code null})
- */
- public DeleteUserEvent(Object source, String userUri) {
- super(source);
- this.userUri = userUri;
- }
-
- public String getUserUri() {
- return userUri;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/activities/FollowEvent.java b/juick-server/src/main/java/com/juick/service/activities/FollowEvent.java
deleted file mode 100644
index c96613ba..00000000
--- a/juick-server/src/main/java/com/juick/service/activities/FollowEvent.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.juick.service.activities;
-
-import com.juick.server.api.activity.model.activities.Follow;
-import org.springframework.context.ApplicationEvent;
-
-public class FollowEvent extends ApplicationEvent {
- private Follow request;
- /**
- * Create a new ApplicationEvent.
- *
- * @param source the object on which the event initially occurred (never {@code null})
- */
- public FollowEvent(Object source, Follow followRequest) {
- super(source);
- this.request = followRequest;
- }
-
- public Follow getRequest() {
- return request;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java b/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java
deleted file mode 100644
index 2b48e6f6..00000000
--- a/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.juick.service.activities;
-
-import org.springframework.context.ApplicationEvent;
-
-public class UndoFollowEvent extends ApplicationEvent {
- private String actor;
- private String object;
- /**
- * Create a new ApplicationEvent.
- *
- * @param source the object on which the event initially occurred (never {@code null})
- */
- public UndoFollowEvent(Object source, String actor, String object) {
- super(source);
- this.actor = actor;
- this.object = object;
- }
-
- public String getActor() {
- return actor;
- }
-
- public String getObject() {
- return object;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java b/juick-server/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java
deleted file mode 100644
index 9215d09a..00000000
--- a/juick-server/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security;
-
-import com.juick.User;
-import com.juick.service.security.entities.JuickUser;
-import com.juick.service.UserService;
-import org.springframework.security.authentication.AnonymousAuthenticationToken;
-import org.springframework.security.authentication.RememberMeAuthenticationToken;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.context.SecurityContextHolder;
-import org.springframework.security.web.authentication.RememberMeServices;
-import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
-import org.springframework.util.Assert;
-import org.springframework.web.filter.OncePerRequestFilter;
-import org.springframework.web.util.WebUtils;
-
-import javax.servlet.FilterChain;
-import javax.servlet.ServletException;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-
-/**
- * Created by aalexeev on 4/5/17.
- */
-public class HashParamAuthenticationFilter extends OncePerRequestFilter {
- public static final String PARAM_NAME = "hash";
-
- private final UserService userService;
- private final RememberMeServices rememberMeServices;
-
-
- public HashParamAuthenticationFilter(
- final UserService userService,
- final RememberMeServices rememberMeServices) {
- Assert.notNull(userService, "userService should not be null");
- Assert.notNull(rememberMeServices, "rememberMeServices should not be null");
-
- this.userService = userService;
- this.rememberMeServices = rememberMeServices;
- }
-
- @Override
- protected void doFilterInternal(
- HttpServletRequest request,
- HttpServletResponse response,
- FilterChain filterChain) throws ServletException, IOException {
-
- String hash = getHashFromRequest(request);
-
- if (hash != null && authenticationIsRequired()) {
- User user = userService.getUserByHash(hash);
-
- if (!user.isAnonymous()) {
- User userWithPassword = userService.getUserByName(user.getName());
- userWithPassword.setAuthHash(userService.getHashByUID(userWithPassword.getUid()));
- Authentication authentication = new RememberMeAuthenticationToken(
- ((AbstractRememberMeServices)rememberMeServices).getKey(), new JuickUser(userWithPassword), JuickUser.USER_AUTHORITY);
-
- SecurityContextHolder.getContext().setAuthentication(authentication);
-
- rememberMeServices.loginSuccess(request, response, authentication);
- }
- }
-
- filterChain.doFilter(request, response);
- }
-
- private boolean authenticationIsRequired() {
- Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
-
- return existingAuth == null ||
- !existingAuth.isAuthenticated() ||
- existingAuth instanceof AnonymousAuthenticationToken;
- }
-
- private String getHashFromRequest(HttpServletRequest request) {
- String paramHash = request.getParameter(PARAM_NAME);
- Cookie cookieHash = WebUtils.getCookie(request, PARAM_NAME);
-
- if (paramHash == null && cookieHash != null) {
- return cookieHash.getValue();
- }
- return paramHash;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java b/juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java
deleted file mode 100644
index 59425fab..00000000
--- a/juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security;
-
-import com.juick.service.UserService;
-import com.juick.service.security.entities.JuickUser;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.security.core.userdetails.UserDetails;
-import org.springframework.security.core.userdetails.UserDetailsService;
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
-import org.springframework.util.Assert;
-
-/**
- * Created by aalexeev on 11/28/16.
- */
-public class JuickUserDetailsService implements UserDetailsService {
- private final UserService userService;
-
- public JuickUserDetailsService(final UserService userService) {
- Assert.notNull(userService, "UserService must be initialized");
- this.userService = userService;
- }
-
- @Override
- public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
- if (StringUtils.isBlank(username))
- throw new UsernameNotFoundException("Invalid user name " + username);
-
- com.juick.User user = userService.getUserByName(username);
-
- if (!user.isAnonymous()) {
- user.setAuthHash(userService.getHashByUID(user.getUid()));
- return new JuickUser(user);
- }
-
- throw new UsernameNotFoundException("The username " + username + " is not found");
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java b/juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java
deleted file mode 100644
index 91acefa3..00000000
--- a/juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security;
-
-import org.springframework.security.core.userdetails.UserDetails;
-import org.springframework.security.core.userdetails.UserDetailsService;
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
-
-/**
- * Created by aalexeev on 11/28/16.
- */
-public class NullUserDetailsService implements UserDetailsService {
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- throw new UsernameNotFoundException(
- "loadUserByUsername called for NullUserDetailsService, user " + username + "can not be found");
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java b/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java
deleted file mode 100644
index e385d7dd..00000000
--- a/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security.deprecated;
-
-import com.juick.User;
-import com.juick.service.security.entities.JuickUser;
-import com.juick.service.UserService;
-import com.juick.service.security.NullUserDetailsService;
-import org.apache.commons.lang3.RandomStringUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.core.env.Environment;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.userdetails.UserDetails;
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
-import org.springframework.security.web.authentication.RememberMeServices;
-import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
-import org.springframework.security.web.authentication.rememberme.InvalidCookieException;
-import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
-import org.springframework.util.Assert;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.util.Optional;
-
-/**
- * Created by aalexeev on 11/28/16.
- *
- * @deprecated not recommended use for secure reasons
- */
-@Deprecated
-public class CookieSimpleHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices {
- private static final Logger logger = LoggerFactory.getLogger(CookieSimpleHashRememberMeServices.class);
-
- private static final String COOKIE_PARAM_NAME = "hash";
-
- private final UserService userService;
-
- public CookieSimpleHashRememberMeServices(
- final String key, final UserService userService, final Environment environment) {
- super(key, new NullUserDetailsService());
-
- Assert.notNull(userService);
- Assert.notNull(environment);
-
- this.userService = userService;
-
- setCookieName(COOKIE_PARAM_NAME);
- setCookieDomain(environment.getProperty("web_domain", "localhost"));
- setAlwaysRemember(true);
- }
-
- @Override
- public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
- super.logout(request, response, authentication);
- userService.deleteLoginForUser(authentication.getName());
- }
-
- @Override
- protected void onLoginSuccess(
- HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
- String username = successfulAuthentication.getName();
-
- logger.debug("Creating new persistent login for user {}", username);
-
- try {
- int uid = userService.getUIDbyName(username);
-
- Assert.isTrue(uid > 0);
-
- String hash = RandomStringUtils.randomAlphanumeric(16).toUpperCase();
-
- userService.setLoginForUser(uid, hash);
-
- setCookie(new String[]{hash}, getTokenValiditySeconds(), request, response);
- } catch (Exception e) {
- logger.error("Failed to save cookies", e);
- }
- }
-
- @Override
- protected UserDetails processAutoLoginCookie(
- String[] cookieTokens, HttpServletRequest request, HttpServletResponse response)
- throws RememberMeAuthenticationException, UsernameNotFoundException {
- String hash = cookieTokens[0];
-
- if (StringUtils.isBlank(hash)) {
- hash = request.getParameter("hash");
- }
- if (StringUtils.isBlank(hash)) {
- throw new InvalidCookieException("Cookie is invalid and hash parameter not found");
- }
-
- int uid = userService.getUIDbyHash(hash);
- if (uid <= 0)
- throw new UsernameNotFoundException("User not found by hash, cookies" + cookieTokens);
-
- Optional<User> userOptional = userService.getUserByUID(uid);
-
- Assert.isTrue(userOptional.isPresent());
-
- return new JuickUser(userService.getUserByName(userOptional.get().getName()));
- }
-
- @Override
- protected String[] decodeCookie(String cookieValue) throws InvalidCookieException {
- return new String[]{cookieValue};
- }
-
- @Override
- protected String encodeCookie(String[] cookieTokens) {
- return cookieTokens != null && cookieTokens.length > 0 ? cookieTokens[0] : StringUtils.EMPTY;
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java b/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java
deleted file mode 100644
index 3631e5a4..00000000
--- a/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security.deprecated;
-
-import com.juick.User;
-import com.juick.service.security.entities.JuickUser;
-import com.juick.service.UserService;
-import com.juick.service.security.NullUserDetailsService;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.userdetails.UserDetails;
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
-import org.springframework.security.web.authentication.RememberMeServices;
-import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
-import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException;
-import org.springframework.util.Assert;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-/**
- * Created by aalexeev on 11/30/16.
- *
- * @deprecated for security reasons
- */
-@Deprecated
-public class RequestParamHashRememberMeServices extends AbstractRememberMeServices implements RememberMeServices {
- private static final String PARAM_NAME = "hash";
-
- private final UserService userService;
-
- public RequestParamHashRememberMeServices(String key, UserService userService) {
- super(key, new NullUserDetailsService());
-
- Assert.notNull(userService);
- this.userService = userService;
- setAlwaysRemember(false);
- }
-
- @Override
- protected void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
- // do nothing
- }
-
- @Override
- protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
- return false; // always false
- }
-
- @Override
- protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) {
- // do nothing
- }
-
- @Override
- protected String extractRememberMeCookie(HttpServletRequest request) {
- return PARAM_NAME; // return any not blank value
- }
-
- @Override
- protected UserDetails processAutoLoginCookie(
- String[] cookieTokens, HttpServletRequest request, HttpServletResponse response)
- throws RememberMeAuthenticationException, UsernameNotFoundException {
- String hash = request.getParameter(PARAM_NAME);
-
- if (StringUtils.isNotBlank(hash)) {
- User user = userService.getUserByHash(hash);
- if (!user.isAnonymous())
- return new JuickUser(userService.getUserByName(user.getName()));
- }
- throw new UsernameNotFoundException("User not found by hash " + hash);
- }
-}
diff --git a/juick-server/src/main/java/com/juick/service/security/entities/JuickUser.java b/juick-server/src/main/java/com/juick/service/security/entities/JuickUser.java
deleted file mode 100644
index c43f112f..00000000
--- a/juick-server/src/main/java/com/juick/service/security/entities/JuickUser.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * 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.service.security.entities;
-
-import com.juick.User;
-import com.juick.model.AnonymousUser;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.security.core.GrantedAuthority;
-import org.springframework.security.core.authority.SimpleGrantedAuthority;
-import org.springframework.security.core.userdetails.UserDetails;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Created by aalexeev on 11/21/16.
- */
-public class JuickUser implements UserDetails {
- static final GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER");
- static final GrantedAuthority ROLE_ANONYMOUS = new SimpleGrantedAuthority("ROLE_ANONYMOUS");
-
- public static final List<GrantedAuthority> USER_AUTHORITY = Collections.singletonList(ROLE_USER);
- public static final List<GrantedAuthority> ANONYMOUS_AUTHORITY = Collections.singletonList(ROLE_ANONYMOUS);
-
- public static final JuickUser ANONYMOUS_USER = new JuickUser(AnonymousUser.INSTANCE, ANONYMOUS_AUTHORITY);
-
- private final com.juick.User user;
- private final Collection<? extends GrantedAuthority> authorities;
-
- public JuickUser(com.juick.User user) {
- this(user, USER_AUTHORITY);
- }
-
- public JuickUser(com.juick.User user, Collection<? extends GrantedAuthority> authorities) {
- this.user = user;
- this.authorities = authorities;
- }
-
- @Override
- public Collection<? extends GrantedAuthority> getAuthorities() {
- return authorities;
- }
-
- @Override
- public String getPassword() {
- return "{noop}" + user.getCredentials();
- }
-
- @Override
- public String getUsername() {
- return user.getName();
- }
-
- @Override
- public boolean isAccountNonExpired() {
- return true;
- }
-
- @Override
- public boolean isAccountNonLocked() {
- return StringUtils.isNotBlank(user.getCredentials());
- }
-
- @Override
- public boolean isCredentialsNonExpired() {
- return isAccountNonLocked();
- }
-
- @Override
- public boolean isEnabled() {
- return !user.isBanned() && isCredentialsNonExpired();
- }
-
- public User getUser() {
- return user;
- }
-}