From 02723131139806c761539a42a5fa80b68ecadee8 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 29 Jun 2017 14:03:04 +0300 Subject: project structure: split server into jdbc + web --- juick-api/build.gradle | 5 +- .../juick/api/configuration/ApiSecurityConfig.java | 3 +- .../java/com/juick/api/controllers/Messages.java | 2 +- .../com/juick/api/controllers/Notifications.java | 2 +- .../java/com/juick/api/controllers/Others.java | 2 +- .../main/java/com/juick/api/controllers/PM.java | 4 +- .../main/java/com/juick/api/controllers/Post.java | 5 +- .../com/juick/api/controllers/Subscriptions.java | 2 +- .../main/java/com/juick/api/controllers/Tags.java | 2 +- .../main/java/com/juick/api/controllers/Users.java | 4 +- juick-crosspost/build.gradle | 2 +- juick-notifications/build.gradle | 2 +- .../juick/components/service/BaseRestService.java | 18 - .../service/NotificationsTokenService.java | 1 + juick-rss/build.gradle | 3 +- juick-server-core/build.gradle | 61 ++ .../com/juick/server/helpers/AnonymousUser.java | 132 +++ .../juick/server/helpers/ApplicationStatus.java | 35 + .../main/java/com/juick/server/helpers/Auth.java | 22 + .../java/com/juick/server/helpers/EmailOpts.java | 24 + .../java/com/juick/server/helpers/NotifyOpts.java | 34 + .../java/com/juick/server/helpers/PrivacyOpts.java | 29 + .../com/juick/server/helpers/PrivateChats.java | 22 + .../com/juick/server/helpers/ResponseReply.java | 72 ++ .../java/com/juick/server/helpers/TagStats.java | 29 + .../java/com/juick/server/helpers/UserInfo.java | 43 + .../com/juick/server/protocol/JuickProtocol.java | 426 ++++++++++ .../juick/server/protocol/ProtocolListener.java | 13 + .../server/protocol/annotation/UserCommand.java | 33 + .../main/java/com/juick/server/util/HashUtils.java | 19 + .../main/java/com/juick/server/util/TagUtils.java | 25 + .../java/com/juick/service/CrosspostService.java | 58 ++ .../main/java/com/juick/service/EmailService.java | 11 + .../java/com/juick/service/MessagesService.java | 86 ++ .../java/com/juick/service/PMQueriesService.java | 28 + .../com/juick/service/PrivacyQueriesService.java | 17 + .../java/com/juick/service/PushQueriesService.java | 33 + .../java/com/juick/service/ShowQueriesService.java | 14 + .../com/juick/service/SubscriptionService.java | 38 + .../main/java/com/juick/service/TagService.java | 37 + .../java/com/juick/service/TelegramService.java | 22 + .../main/java/com/juick/service/UserService.java | 126 +++ .../com/juick/service/search/SearchService.java | 14 + .../src/main/resources/juick.conf.example | 86 ++ .../src/main/resources/logback.xml.example | 35 + juick-server-core/src/main/resources/schema.sql | 885 +++++++++++++++++++++ juick-server-core/src/main/resources/update.sql | 12 + juick-server-jdbc/build.gradle | 76 ++ .../com/juick/configuration/DataConfiguration.java | 79 ++ .../juick/configuration/SearchConfiguration.java | 43 + .../juick/configuration/UpdaterConfiguration.java | 30 + .../main/java/com/juick/database/MySqlUpdater.java | 135 ++++ .../java/com/juick/service/BaseJdbcService.java | 55 ++ .../com/juick/service/CrosspostServiceImpl.java | 246 ++++++ .../java/com/juick/service/EmailServiceImpl.java | 52 ++ .../com/juick/service/MessagesServiceImpl.java | 814 +++++++++++++++++++ .../com/juick/service/PMQueriesServiceImpl.java | 160 ++++ .../juick/service/PrivacyQueriesServiceImpl.java | 54 ++ .../com/juick/service/PushQueriesServiceImpl.java | 126 +++ .../com/juick/service/ShowQueriesServiceImpl.java | 52 ++ .../com/juick/service/SubscriptionServiceImpl.java | 219 +++++ .../java/com/juick/service/TagServiceImpl.java | 260 ++++++ .../com/juick/service/TelegramServiceImpl.java | 73 ++ .../java/com/juick/service/UserServiceImpl.java | 704 ++++++++++++++++ .../service/search/SphinxSearchServiceImpl.java | 85 ++ .../xmpp/core/session/debug/LogbackDebugger.java | 39 + .../juick/configuration/MockDataConfiguration.java | 47 ++ .../configuration/RepositoryConfiguration.java | 65 ++ .../java/com/juick/service/MessageServiceTest.java | 23 + .../test/java/com/juick/tests/util/MockUtils.java | 34 + juick-server-web/build.gradle | 76 ++ .../juick/server/util/HttpBadRequestException.java | 15 + .../juick/server/util/HttpForbiddenException.java | 16 + .../juick/server/util/HttpNotFoundException.java | 15 + .../main/java/com/juick/server/util/HttpUtils.java | 112 +++ .../java/com/juick/server/util/ImageUtils.java | 66 ++ .../main/java/com/juick/server/util/UserUtils.java | 36 + .../main/java/com/juick/server/util/WebUtils.java | 45 ++ .../java/com/juick/service/BaseRestService.java | 18 + .../security/HashParamAuthenticationFilter.java | 83 ++ .../service/security/JuickUserDetailsService.java | 34 + .../NotAuthorizedAuthenticationEntryPoint.java | 20 + .../service/security/NullUserDetailsService.java | 16 + .../CookieSimpleHashRememberMeServices.java | 113 +++ .../RequestParamHashRememberMeServices.java | 71 ++ .../juick/service/security/entities/JuickUser.java | 75 ++ juick-server/build.gradle | 76 -- .../com/juick/configuration/DataConfiguration.java | 80 -- .../juick/configuration/SearchConfiguration.java | 43 - .../juick/configuration/UpdaterConfiguration.java | 30 - .../main/java/com/juick/database/MySqlUpdater.java | 135 ---- .../juick/server/helpers/ApplicationStatus.java | 35 - .../main/java/com/juick/server/helpers/Auth.java | 22 - .../java/com/juick/server/helpers/EmailOpts.java | 24 - .../java/com/juick/server/helpers/NotifyOpts.java | 34 - .../java/com/juick/server/helpers/PrivacyOpts.java | 29 - .../com/juick/server/helpers/PrivateChats.java | 22 - .../com/juick/server/helpers/ResponseReply.java | 72 -- .../java/com/juick/server/helpers/TagStats.java | 29 - .../java/com/juick/server/helpers/UserInfo.java | 43 - .../com/juick/server/protocol/JuickProtocol.java | 426 ---------- .../juick/server/protocol/ProtocolListener.java | 13 - .../server/protocol/annotation/UserCommand.java | 33 - .../security/HashParamAuthenticationFilter.java | 83 -- .../NotAuthorizedAuthenticationEntryPoint.java | 20 - .../server/security/entities/AnonymousUser.java | 132 --- .../juick/server/security/entities/JuickUser.java | 74 -- .../juick/server/util/HttpBadRequestException.java | 15 - .../juick/server/util/HttpForbiddenException.java | 16 - .../juick/server/util/HttpNotFoundException.java | 15 - .../main/java/com/juick/server/util/HttpUtils.java | 112 --- .../java/com/juick/server/util/ImageUtils.java | 66 -- .../java/com/juick/service/BaseJdbcService.java | 55 -- .../java/com/juick/service/CrosspostService.java | 58 -- .../com/juick/service/CrosspostServiceImpl.java | 246 ------ .../main/java/com/juick/service/EmailService.java | 11 - .../java/com/juick/service/EmailServiceImpl.java | 52 -- .../java/com/juick/service/MessagesService.java | 88 -- .../com/juick/service/MessagesServiceImpl.java | 814 ------------------- .../java/com/juick/service/PMQueriesService.java | 28 - .../com/juick/service/PMQueriesServiceImpl.java | 160 ---- .../com/juick/service/PrivacyQueriesService.java | 17 - .../juick/service/PrivacyQueriesServiceImpl.java | 54 -- .../java/com/juick/service/PushQueriesService.java | 33 - .../com/juick/service/PushQueriesServiceImpl.java | 126 --- .../java/com/juick/service/ShowQueriesService.java | 14 - .../com/juick/service/ShowQueriesServiceImpl.java | 52 -- .../com/juick/service/SubscriptionService.java | 38 - .../com/juick/service/SubscriptionServiceImpl.java | 219 ----- .../main/java/com/juick/service/TagService.java | 37 - .../java/com/juick/service/TagServiceImpl.java | 260 ------ .../java/com/juick/service/TelegramService.java | 22 - .../com/juick/service/TelegramServiceImpl.java | 73 -- .../main/java/com/juick/service/UserService.java | 126 --- .../java/com/juick/service/UserServiceImpl.java | 704 ---------------- .../com/juick/service/search/SearchService.java | 14 - .../service/search/SphinxSearchServiceImpl.java | 85 -- .../service/security/JuickUserDetailsService.java | 34 - .../service/security/NullUserDetailsService.java | 16 - .../CookieSimpleHashRememberMeServices.java | 113 --- .../RequestParamHashRememberMeServices.java | 71 -- .../src/main/java/com/juick/util/TagUtils.java | 25 - .../src/main/java/com/juick/util/UserUtils.java | 49 -- .../src/main/java/com/juick/util/WebUtils.java | 45 -- .../xmpp/core/session/debug/LogbackDebugger.java | 39 - juick-server/src/main/resources/juick.conf.example | 86 -- .../src/main/resources/logback.xml.example | 35 - juick-server/src/main/resources/schema.sql | 885 --------------------- juick-server/src/main/resources/update.sql | 12 - .../juick/configuration/MockDataConfiguration.java | 47 -- .../configuration/RepositoryConfiguration.java | 65 -- .../java/com/juick/service/MessageServiceTest.java | 23 - .../test/java/com/juick/tests/util/MockUtils.java | 34 - juick-ws/build.gradle | 2 +- juick-www/build.gradle | 5 +- .../juick/www/configuration/WebSecurityConfig.java | 4 +- .../main/java/com/juick/www/controllers/Help.java | 2 +- .../main/java/com/juick/www/controllers/Home.java | 4 +- .../main/java/com/juick/www/controllers/Login.java | 2 +- .../java/com/juick/www/controllers/NewMessage.java | 2 +- .../main/java/com/juick/www/controllers/PM.java | 5 +- .../java/com/juick/www/controllers/Settings.java | 7 +- .../java/com/juick/www/controllers/SignUp.java | 2 +- .../main/java/com/juick/www/controllers/Tags.java | 2 +- .../main/java/com/juick/www/controllers/User.java | 2 +- .../java/com/juick/www/controllers/UserThread.java | 2 +- .../java/com/juick/www/controllers/XMPPPost.java | 2 +- juick-xmpp-bot/build.gradle | 2 +- juick-xmpp/build.gradle | 2 +- settings.gradle | 2 +- 170 files changed, 6847 insertions(+), 6706 deletions(-) delete mode 100644 juick-notifications/src/main/java/com/juick/components/service/BaseRestService.java create mode 100644 juick-server-core/build.gradle create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/Auth.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java create mode 100644 juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java create mode 100644 juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java create mode 100644 juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java create mode 100644 juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java create mode 100644 juick-server-core/src/main/java/com/juick/server/util/HashUtils.java create mode 100644 juick-server-core/src/main/java/com/juick/server/util/TagUtils.java create mode 100644 juick-server-core/src/main/java/com/juick/service/CrosspostService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/EmailService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/MessagesService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/PMQueriesService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/PrivacyQueriesService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/PushQueriesService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/ShowQueriesService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/SubscriptionService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/TagService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/TelegramService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/UserService.java create mode 100644 juick-server-core/src/main/java/com/juick/service/search/SearchService.java create mode 100644 juick-server-core/src/main/resources/juick.conf.example create mode 100644 juick-server-core/src/main/resources/logback.xml.example create mode 100644 juick-server-core/src/main/resources/schema.sql create mode 100644 juick-server-core/src/main/resources/update.sql create mode 100644 juick-server-jdbc/build.gradle create mode 100644 juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/database/MySqlUpdater.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/ShowQueriesServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java create mode 100644 juick-server-jdbc/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java create mode 100644 juick-server-jdbc/src/test/java/com/juick/configuration/MockDataConfiguration.java create mode 100644 juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java create mode 100644 juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java create mode 100644 juick-server-jdbc/src/test/java/com/juick/tests/util/MockUtils.java create mode 100644 juick-server-web/build.gradle create mode 100644 juick-server-web/src/main/java/com/juick/server/util/HttpBadRequestException.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/HttpForbiddenException.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/HttpNotFoundException.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/HttpUtils.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/UserUtils.java create mode 100644 juick-server-web/src/main/java/com/juick/server/util/WebUtils.java create mode 100644 juick-server-web/src/main/java/com/juick/service/BaseRestService.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/JuickUserDetailsService.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/NullUserDetailsService.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java create mode 100644 juick-server-web/src/main/java/com/juick/service/security/entities/JuickUser.java delete mode 100644 juick-server/build.gradle delete mode 100644 juick-server/src/main/java/com/juick/configuration/DataConfiguration.java delete mode 100644 juick-server/src/main/java/com/juick/configuration/SearchConfiguration.java delete mode 100644 juick-server/src/main/java/com/juick/configuration/UpdaterConfiguration.java delete mode 100644 juick-server/src/main/java/com/juick/database/MySqlUpdater.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/ApplicationStatus.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/Auth.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/EmailOpts.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/NotifyOpts.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/PrivacyOpts.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/PrivateChats.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/TagStats.java delete mode 100644 juick-server/src/main/java/com/juick/server/helpers/UserInfo.java delete mode 100644 juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java delete mode 100644 juick-server/src/main/java/com/juick/server/protocol/ProtocolListener.java delete mode 100644 juick-server/src/main/java/com/juick/server/protocol/annotation/UserCommand.java delete mode 100644 juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java delete mode 100644 juick-server/src/main/java/com/juick/server/security/NotAuthorizedAuthenticationEntryPoint.java delete mode 100644 juick-server/src/main/java/com/juick/server/security/entities/AnonymousUser.java delete mode 100644 juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java delete mode 100644 juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java delete mode 100644 juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java delete mode 100644 juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java delete mode 100644 juick-server/src/main/java/com/juick/server/util/HttpUtils.java delete mode 100644 juick-server/src/main/java/com/juick/server/util/ImageUtils.java delete mode 100644 juick-server/src/main/java/com/juick/service/BaseJdbcService.java delete mode 100644 juick-server/src/main/java/com/juick/service/CrosspostService.java delete mode 100644 juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/EmailService.java delete mode 100644 juick-server/src/main/java/com/juick/service/EmailServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/MessagesService.java delete mode 100644 juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/PMQueriesService.java delete mode 100644 juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/PrivacyQueriesService.java delete mode 100644 juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/PushQueriesService.java delete mode 100644 juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/ShowQueriesService.java delete mode 100644 juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/SubscriptionService.java delete mode 100644 juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/TagService.java delete mode 100644 juick-server/src/main/java/com/juick/service/TagServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/TelegramService.java delete mode 100644 juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/UserService.java delete mode 100644 juick-server/src/main/java/com/juick/service/UserServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/search/SearchService.java delete mode 100644 juick-server/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java delete mode 100644 juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java delete mode 100644 juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java delete mode 100644 juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java delete mode 100644 juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java delete mode 100644 juick-server/src/main/java/com/juick/util/TagUtils.java delete mode 100644 juick-server/src/main/java/com/juick/util/UserUtils.java delete mode 100644 juick-server/src/main/java/com/juick/util/WebUtils.java delete mode 100644 juick-server/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java delete mode 100644 juick-server/src/main/resources/juick.conf.example delete mode 100644 juick-server/src/main/resources/logback.xml.example delete mode 100644 juick-server/src/main/resources/schema.sql delete mode 100644 juick-server/src/main/resources/update.sql delete mode 100644 juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java delete mode 100644 juick-server/src/test/java/com/juick/configuration/RepositoryConfiguration.java delete mode 100644 juick-server/src/test/java/com/juick/service/MessageServiceTest.java delete mode 100644 juick-server/src/test/java/com/juick/tests/util/MockUtils.java diff --git a/juick-api/build.gradle b/juick-api/build.gradle index bcf4bd25..b85bb4fc 100644 --- a/juick-api/build.gradle +++ b/juick-api/build.gradle @@ -9,7 +9,8 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') + compile project(':juick-server-web') compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" compile "org.springframework:spring-websocket:${rootProject.springFrameworkVersion}" @@ -19,7 +20,7 @@ dependencies { compile 'org.imgscalr:imgscalr-lib:4.2' providedRuntime 'mysql:mysql-connector-java:5.1.40' - testCompile project(path: ':juick-server', configuration: 'testArtifacts') + testCompile project(path: ':juick-server-jdbc', configuration: 'testArtifacts') testRuntime 'org.apache.tomcat:tomcat-websocket:8.5.15' testRuntime 'com.jayway.jsonpath:json-path:2.2.0' diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java index f1dcf30a..969ebd03 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java @@ -1,6 +1,6 @@ package com.juick.api.configuration; -import com.juick.server.security.NotAuthorizedAuthenticationEntryPoint; +import com.juick.service.security.NotAuthorizedAuthenticationEntryPoint; import com.juick.service.UserService; import com.juick.service.security.JuickUserDetailsService; import com.juick.service.security.deprecated.RequestParamHashRememberMeServices; @@ -8,7 +8,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.dao.DaoAuthenticationProvider; import org.springframework.security.config.annotation.web.builders.HttpSecurity; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Messages.java b/juick-api/src/main/java/com/juick/api/controllers/Messages.java index 76bbb5be..0d77bb74 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Messages.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Messages.java @@ -9,7 +9,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java index 2f0dc335..c121efb6 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Notifications.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Notifications.java @@ -9,7 +9,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.PushQueriesService; import com.juick.service.SubscriptionService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Others.java b/juick-api/src/main/java/com/juick/api/controllers/Others.java index 722653a5..4f646af8 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Others.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Others.java @@ -6,7 +6,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.HttpNotFoundException; import com.juick.service.PMQueriesService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/juick-api/src/main/java/com/juick/api/controllers/PM.java b/juick-api/src/main/java/com/juick/api/controllers/PM.java index 9faf05ea..f4bf2763 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/PM.java +++ b/juick-api/src/main/java/com/juick/api/controllers/PM.java @@ -6,8 +6,8 @@ import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.service.PMQueriesService; import com.juick.service.UserService; -import com.juick.util.UserUtils; -import com.juick.util.WebUtils; +import com.juick.server.util.UserUtils; +import com.juick.server.util.WebUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java index cbd5ac0c..ffabe9ad 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Post.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java @@ -10,7 +10,7 @@ import com.juick.server.util.ImageUtils; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -28,13 +28,10 @@ import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; import javax.inject.Inject; -import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServletRequest; -import java.io.BufferedInputStream; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Subscriptions.java b/juick-api/src/main/java/com/juick/api/controllers/Subscriptions.java index fcae3373..c4a214d9 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Subscriptions.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Subscriptions.java @@ -7,7 +7,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Tags.java b/juick-api/src/main/java/com/juick/api/controllers/Tags.java index 7b1ce313..ab2ee7ce 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Tags.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Tags.java @@ -4,7 +4,7 @@ import com.juick.User; import com.juick.server.helpers.TagStats; import com.juick.service.TagService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/juick-api/src/main/java/com/juick/api/controllers/Users.java b/juick-api/src/main/java/com/juick/api/controllers/Users.java index 3805b2ef..b84f375f 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Users.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Users.java @@ -4,8 +4,8 @@ import com.juick.User; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.HttpNotFoundException; import com.juick.service.UserService; -import com.juick.util.UserUtils; -import com.juick.util.WebUtils; +import com.juick.server.util.UserUtils; +import com.juick.server.util.WebUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/juick-crosspost/build.gradle b/juick-crosspost/build.gradle index a0987f5b..c704cbdb 100644 --- a/juick-crosspost/build.gradle +++ b/juick-crosspost/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" compile "org.springframework:spring-websocket:${rootProject.springFrameworkVersion}" providedRuntime 'mysql:mysql-connector-java:5.1.40' diff --git a/juick-notifications/build.gradle b/juick-notifications/build.gradle index 7b97f05b..9c99557d 100644 --- a/juick-notifications/build.gradle +++ b/juick-notifications/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-core') + compile project(':juick-server-web') compile 'com.ganyo:gcm-server:1.1.0' compile 'com.notnoop.apns:apns:1.0.0.Beta6' compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" diff --git a/juick-notifications/src/main/java/com/juick/components/service/BaseRestService.java b/juick-notifications/src/main/java/com/juick/components/service/BaseRestService.java deleted file mode 100644 index d4bc369c..00000000 --- a/juick-notifications/src/main/java/com/juick/components/service/BaseRestService.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.juick.components.service; - -import org.springframework.web.client.RestTemplate; - -/** - * Created by vitalyster on 15.12.2016. - */ -public abstract class BaseRestService { - private RestTemplate rest; - - public BaseRestService(RestTemplate rest) { - this.rest = rest; - } - - public RestTemplate getRest() { - return rest; - } -} diff --git a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java index 9ea3fd3e..4dd949c3 100644 --- a/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java +++ b/juick-notifications/src/main/java/com/juick/components/service/NotificationsTokenService.java @@ -1,6 +1,7 @@ package com.juick.components.service; import com.juick.TokensList; +import com.juick.service.BaseRestService; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; diff --git a/juick-rss/build.gradle b/juick-rss/build.gradle index 61fb4ba6..3a1b82ab 100644 --- a/juick-rss/build.gradle +++ b/juick-rss/build.gradle @@ -3,7 +3,8 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') + compile project(':juick-server-web') compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" compile 'com.rometools:rome:1.7.3' compile 'com.rometools:rome-modules:1.7.3' diff --git a/juick-server-core/build.gradle b/juick-server-core/build.gradle new file mode 100644 index 00000000..b93fb533 --- /dev/null +++ b/juick-server-core/build.gradle @@ -0,0 +1,61 @@ +apply plugin: 'java' +apply plugin: 'war' + +sourceCompatibility = 1.8 + +dependencies { + compile project(':juick-core') + + compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${rootProject.jacksonVersion}" + + providedCompile "ch.qos.logback:logback-classic:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-core:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-access:${rootProject.logbackVersion}" + + providedCompile "org.slf4j:slf4j-api:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jcl-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:log4j-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jul-to-slf4j:${rootProject.slf4jVersion}" + + compile "org.apache.httpcomponents:httpclient:4.5.3" + providedCompile "org.apache.commons:commons-lang3:3.6" + providedCompile "org.apache.commons:commons-collections4:4.1" + providedCompile "org.apache.commons:commons-text:1.1" + providedCompile "commons-io:commons-io:2.5" + providedCompile "commons-codec:commons-codec:1.10" + + compile 'org.imgscalr:imgscalr-lib:4.2' + + compile "javax.inject:javax.inject:1" + + providedCompile "javax.servlet:javax.servlet-api:3.1.0" + + providedRuntime "commons-fileupload:commons-fileupload:1.3.3" + + testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" + testCompile "junit:junit:${rootProject.junitVersion}" + testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" + testCompile "org.mockito:mockito-core:${rootProject.mockitoVersion}" + + testRuntime "mysql:mysql-connector-java:5.1.40" +} + +compileJava.options.encoding = 'UTF-8' + +configurations { + all*.exclude module: 'commons-logging' +} + +configurations { + testArtifacts.extendsFrom testRuntime +} +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output +} +artifacts { + testArtifacts testJar +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java b/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java new file mode 100644 index 00000000..1ce1c2c2 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/AnonymousUser.java @@ -0,0 +1,132 @@ +package com.juick.server.helpers; + +import com.juick.User; + +/** + * Created by aalexeev on 12/11/16. + */ +public final class AnonymousUser extends User { + public static final AnonymousUser INSTANCE = new AnonymousUser(); + + private AnonymousUser() { + super.setUid(getUid()); + super.setName(getName()); + super.setAvatar(getAvatar()); + super.setFullName(getFullName()); + super.setJid(getJid()); + super.setMessagesCount(getMessagesCount()); + super.setAuthHash(getAuthHash()); + super.setBanned(isBanned()); + super.setCredentials(getCredentials()); + super.setLang(getLang()); + } + + @Override + public boolean equals(Object obj) { + return obj == this || obj instanceof AnonymousUser; + } + + @Override + public int getUid() { + return 0; + } + + @Override + public String getName() { + return "Anonymous"; + } + + @Override + public String getFullName() { + return getName(); + } + + @Override + public String getJid() { + return "anonym@localhost"; + } + + @Override + public String getAuthHash() { + return null; + } + + @Override + public Integer getUnreadCount() { + return 0; + } + + @Override + public boolean isBanned() { + return false; + } + + @Override + public Object getAvatar() { + return null; + } + + @Override + public String getCredentials() { + return null; + } + + @Override + public String getLang() { + return "__"; + } + + @Override + public int getMessagesCount() { + return 0; + } + + @Override + public boolean isAnonymous() { + return true; + } + + @Override + public void setUid(int uid) { + } + + @Override + public void setName(String name) { + } + + @Override + public void setFullName(String fullName) { + } + + @Override + public void setJid(String jid) { + } + + @Override + public void setAuthHash(String authHash) { + } + + @Override + public void setUnreadCount(Integer count) { + } + + @Override + public void setBanned(boolean banned) { + } + + @Override + public void setAvatar(Object avatar) { + } + + @Override + public void setCredentials(String credentials) { + } + + @Override + public void setLang(String lang) { + } + + @Override + public void setMessagesCount(int messagesCount) { + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java b/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java new file mode 100644 index 00000000..61109c47 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/ApplicationStatus.java @@ -0,0 +1,35 @@ +package com.juick.server.helpers; + +import org.apache.commons.lang3.builder.ToStringBuilder; + +/** + * Created by vt on 03/09/16. + */ +public class ApplicationStatus { + private boolean connected; + private boolean crosspostEnabled; + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("connected", connected) + .append("crosspostEnabled", crosspostEnabled) + .toString(); + } + + public boolean isConnected() { + return connected; + } + + public void setConnected(boolean connected) { + this.connected = connected; + } + + public boolean isCrosspostEnabled() { + return crosspostEnabled; + } + + public void setCrosspostEnabled(boolean crosspostEnabled) { + this.crosspostEnabled = crosspostEnabled; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java b/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java new file mode 100644 index 00000000..3e1f0bd9 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/Auth.java @@ -0,0 +1,22 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 09/02/16. + */ +public class Auth { + private String account; + private String authCode; + + public Auth(String account, String authCode) { + this.account = account; + this.authCode = authCode; + } + + public String getAccount() { + return account; + } + + public String getAuthCode() { + return authCode; + } +} \ No newline at end of file diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java new file mode 100644 index 00000000..679d1a8d --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/EmailOpts.java @@ -0,0 +1,24 @@ +package com.juick.server.helpers; + +import org.apache.commons.lang3.StringUtils; + +/** + * Created by vitalyster on 09.02.2016. + */ +public class EmailOpts { + private String email; + private String subscriptionHour; + + public EmailOpts(String email, int subscriptionHour) { + this.email = email; + this.subscriptionHour = StringUtils.leftPad(String.format("%d", subscriptionHour), 2, "0"); + } + + public String getSubscriptionHour() { + return subscriptionHour; + } + + public String getEmail() { + return email; + } +} \ No newline at end of file diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java new file mode 100644 index 00000000..377b0a50 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/NotifyOpts.java @@ -0,0 +1,34 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 03/09/16. + */ +public class NotifyOpts { + private boolean repliesEnabled; + private boolean subscriptionsEnabled; + private boolean recommendationsEnabled; + + public boolean isRepliesEnabled() { + return repliesEnabled; + } + + public void setRepliesEnabled(boolean repliesEnabled) { + this.repliesEnabled = repliesEnabled; + } + + public boolean isSubscriptionsEnabled() { + return subscriptionsEnabled; + } + + public void setSubscriptionsEnabled(boolean subscriptionsEnabled) { + this.subscriptionsEnabled = subscriptionsEnabled; + } + + public boolean isRecommendationsEnabled() { + return recommendationsEnabled; + } + + public void setRecommendationsEnabled(boolean recommendationsEnabled) { + this.recommendationsEnabled = recommendationsEnabled; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java b/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java new file mode 100644 index 00000000..66cf9410 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/PrivacyOpts.java @@ -0,0 +1,29 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 16/01/16. + */ +public class PrivacyOpts { + private int uid; + private int privacy; + + public PrivacyOpts() { + + } + + public int getUid() { + return uid; + } + + public void setUid(int uid) { + this.uid = uid; + } + + public int getPrivacy() { + return privacy; + } + + public void setPrivacy(int privacy) { + this.privacy = privacy; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java b/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java new file mode 100644 index 00000000..b1bfccf8 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/PrivateChats.java @@ -0,0 +1,22 @@ +package com.juick.server.helpers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.juick.User; + +import java.util.List; + +/** + * Created by vt on 24/11/2016. + */ +public class PrivateChats { + private List users; + + @JsonProperty("pms") + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java b/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java new file mode 100644 index 00000000..f941c743 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/ResponseReply.java @@ -0,0 +1,72 @@ +package com.juick.server.helpers; + +import java.util.Date; + +/** + * Created by vitalyster on 13.12.2016. + */ +public class ResponseReply { + private String muname; + private int mid; + private int rid; + private String uname; + private String description; + private Date pubDate; + private String attachmentType; + + public String getMuname() { + return muname; + } + + public void setMuname(String muname) { + this.muname = muname; + } + + public int getMid() { + return mid; + } + + public void setMid(int mid) { + this.mid = mid; + } + + public int getRid() { + return rid; + } + + public void setRid(int rid) { + this.rid = rid; + } + + public String getUname() { + return uname; + } + + public void setUname(String uname) { + this.uname = uname; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getPubDate() { + return pubDate; + } + + public void setPubDate(Date pubDate) { + this.pubDate = pubDate; + } + + public String getAttachmentType() { + return attachmentType; + } + + public void setAttachmentType(String attachmentType) { + this.attachmentType = attachmentType; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java b/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java new file mode 100644 index 00000000..e8720991 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/TagStats.java @@ -0,0 +1,29 @@ +package com.juick.server.helpers; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.juick.Tag; + +/** + * Created by vitalyster on 01.12.2016. + */ +public class TagStats { + private Tag tag; + private int usageCount; + + public Tag getTag() { + return tag; + } + + public void setTag(Tag tag) { + this.tag = tag; + } + + @JsonProperty("messages") + public int getUsageCount() { + return usageCount; + } + + public void setUsageCount(int usageCount) { + this.usageCount = usageCount; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java b/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java new file mode 100644 index 00000000..5a4b6894 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/helpers/UserInfo.java @@ -0,0 +1,43 @@ +package com.juick.server.helpers; + +/** + * Created by vt on 03/09/16. + */ +public class UserInfo { + private String fullName; + private String country; + private String url; + private String description; + + public String getFullName() { + return fullName; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java new file mode 100644 index 00000000..6ac05624 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -0,0 +1,426 @@ +package com.juick.server.protocol; + +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.formatters.PlainTextFormatter; +import com.juick.server.helpers.TagStats; +import com.juick.server.protocol.annotation.UserCommand; +import com.juick.server.util.TagUtils; +import com.juick.service.*; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.commons.lang3.reflect.MethodUtils; + +import javax.inject.Inject; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Created by oxpa on 22.03.16. + */ + +public class JuickProtocol { + + private String baseUri; + private ProtocolListener listener; + + @Inject + UserService userService; + @Inject + TagService tagService; + @Inject + MessagesService messagesService; + @Inject + SubscriptionService subscriptionService; + @Inject + PMQueriesService pmQueriesService; + @Inject + PrivacyQueriesService privacyQueriesService; + @Inject + ShowQueriesService showQueriesService; + + public JuickProtocol(String baseUri) { + this.baseUri = baseUri; + } + + /** + * find command by pattern and invoke + * @param user who send command + * @param userInput given by user + * @return command result + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws NoSuchMethodException + */ + public String getReply(User user, String userInput) throws InvocationTargetException, + IllegalAccessException, NoSuchMethodException { + Optional cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() + .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), + m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches()) + .findFirst(); + if (!cmd.isPresent()) { + // default command - post as new message + return postMessage(user, userInput.trim()); + } else { + Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(), + cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput.trim()); + List groups = new ArrayList<>(); + while (matcher.find()) { + for (int i = 1; i <= matcher.groupCount(); i++) { + groups.add(matcher.group(i)); + } + } + return (String) getClass().getMethod(cmd.get().getName(), User.class, String[].class) + .invoke(this, user, groups.toArray(new String[groups.size()])); + } + } + + public String postMessage(User user, String input) { + List tags = tagService.fromString(input, false); + String body = input.substring(TagUtils.toString(tags).length()); + int mid = messagesService.createMessage(user.getUid(), body, null, tags); + subscriptionService.subscribeMessage(mid, user.getUid()); + listener.messagePosted(messagesService.getMessage(mid)); + return "New message posted.\n#" + mid + " " + baseUri + mid; + } + + @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages") + public String commandLast(User user, String... arguments) { + List mids = messagesService.getAll(user.getUid(), 0); + List messages = messagesService.getMessages(mids); + return "Last messages: \n" + + messages.stream().sorted(Collections.reverseOrder()).map(PlainTextFormatter::formatPostSummary) + .collect(Collectors.joining("\n\n")); + } + + @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL - Show your blacklist") + public String commandBL(User user_from, String... arguments) { + List blusers; + List bltags; + + blusers = userService.getUserBLUsers(user_from.getUid()); + bltags = tagService.getUserBLTags(user_from.getUid()); + + + String txt = StringUtils.EMPTY; + if (bltags.size() > 0) { + for (String bltag : bltags) { + txt += "*" + bltag + "\n"; + } + + if (blusers.size() > 0) { + txt += "\n"; + } + } + if (blusers.size() > 0) { + for (User bluser : blusers) { + txt += "@" + bluser.getName() + "\n"; + } + } + if (txt.isEmpty()) { + txt = "You don't have any users or tags in your blacklist."; + } + return txt; + } + + @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL @username - add @username to your blacklist") + public String blacklistUser(User from, String... arguments) { + User blUser = userService.getUserByName(arguments[0]); + if (blUser != null) { + PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(from, blUser); + if (result == PrivacyQueriesService.PrivacyResult.Added) { + return "User added to your blacklist"; + } else { + return "User removed from your blacklist"; + } + } + return "User not found"; + } + + @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "BL *tag - add *tag to your blacklist") + public String blacklistTag(User from, String... arguments) { + User blUser = userService.getUserByName(arguments[0]); + if (blUser != null) { + Tag tag = tagService.getTag(arguments[0], false); + if (tag != null) { + PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(from, tag); + if (result == PrivacyQueriesService.PrivacyResult.Added) { + return "Tag added to your blacklist"; + } else { + return "Tag removed from your blacklist"; + } + } + } + return "Tag not found"; + } + + @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs") + public String commandUsers(User currentUser, String... args) { + StringBuilder msg = new StringBuilder(); + msg.append("Recommended blogs"); + List recommendedUsers = showQueriesService.getRecommendedUsers(currentUser); + if (recommendedUsers.size() > 0) { + for (String user : recommendedUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo recommendations now. Subscribe to more blogs. ;)"); + } + msg.append("\n\nTop 10 personal blogs:"); + List topUsers = showQueriesService.getTopUsers(); + if (topUsers.size() > 0) { + for (String user : topUsers) { + msg.append("\n@").append(user); + } + } else { + msg.append("\nNo top users. Empty DB? ;)"); + } + return msg.toString(); + } + + @UserCommand(pattern = "\\*", help = "* - Show your tags") + public String commandTags(User currentUser, String... args) { + List tags = tagService.getUserTagStats(currentUser.getUid()); + String msg = "Your tags: (tag - messages)\n" + + tags.stream() + .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining()); + return msg; + } + + @UserCommand(pattern = "S", help = "S - Show your subscriptions") + public String commandSubscriptions(User currentUser, String... args) { + List friends = userService.getUserFriends(currentUser.getUid()); + List tags = subscriptionService.getSubscribedTags(currentUser); + String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName()) + .collect(Collectors.joining()) + : "You are not subscribed to any user."; + msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t) + .collect(Collectors.joining()) + : "\nYou are not subscribed to any tag."; + return msg; + } + + @UserCommand(pattern = "!", help = "! - Show your favorite messages") + public String commandFavorites(User currentUser, String... args) { + List mids = messagesService.getUserRecommendations(currentUser.getUid(), 0); + if (mids.size() > 0) { + List messages = messagesService.getMessages(mids); + return "Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) + .collect(Collectors.toList())); + } + return "No favorite messages, try to \"like\" something ;)"; + } + + @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", + help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") + public String commandUser(User user, String... arguments) { + User blogUser = userService.getUserByName(arguments[0]); + int page = arguments[1].length(); + if (blogUser.getUid() > 0) { + List mids = messagesService.getUserBlog(blogUser.getUid(), 0, page); + List messages = messagesService.getMessages(mids); + return String.format("Last messages from @%s:\n%s", arguments[0], + String.join("\n", messages.stream() + .map(PlainTextFormatter::formatPost).collect(Collectors.toList()))); + } + return "User not found"; + } + + @UserCommand(pattern = "^d\\s*\\#([0-9]+)$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "D #12345 - delete the message") + public String commandDel(User user, String... args) { + int mid = NumberUtils.toInt(args[0], 0); + if (messagesService.deleteMessage(user.getUid(), mid)) { + return String.format("Message %s deleted", mid); + } + return "Error"; + } + + @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "LOGIN - log in to Juick website") + public String commandLogin(User user, String... arguments) { + return baseUri + "?" + userService.getHashByUID(user.getUid()); + } + + @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)") + public String commandMyFeed(User user, String... arguments) { + // number of # is the page count + int page = arguments[0].length() - 1; + List mids = messagesService.getMyFeed(user.getUid(), page); + List messages = messagesService.getMessages(mids); + // TODO: add instructions for empty feed + return "Your feed: \n" + String.join("\n", + messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); + } + + @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "ON/OFF - Enable/disable subscriptions delivery") + public String commandOnOff(User user, String[] input) { + UserService.ActiveStatus newStatus; + String retValUpdated; + if (input[0].toLowerCase().equals("on")) { + newStatus = UserService.ActiveStatus.Active; + retValUpdated = "Notifications are activated for " + user.getJid(); + } else { + newStatus = UserService.ActiveStatus.Inactive; + retValUpdated = "Notifications are disabled for " + user.getJid(); + } + + if (userService.setActiveStatusForJID(user.getJid(), newStatus)) { + return retValUpdated; + } else { + return String.format("Subscriptions status for %s was not changed", user.getJid()); + } + } + + @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "PING - returns you a PONG") + public String commandPing(User user, String[] input) { + return "PONG"; + } + + @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username") + public String commandPM(User user_from, String... arguments) { + String user_to = arguments[0]; + String body = arguments[1]; + + User toUser = userService.getUserByName(user_to); + + if (toUser.getUid() > 0) { + if (!userService.isInBLAny(toUser.getUid(), user_from.getUid())) { + if (pmQueriesService.createPM(user_from.getUid(), toUser.getUid(), body)) { + listener.privateMessage(user_from, toUser, body); + return "Private message sent"; + } + } + } + return "Error"; + } + + @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)") + public String commandShow(User user, String... arguments) { + boolean showReplies = arguments[1].length() > 0; + int mid = NumberUtils.toInt(arguments[0], 0); + if (mid == 0) { + return "Error"; + } + Message msg = messagesService.getMessage(mid); + if (msg != null) { + if (showReplies) { + List replies = messagesService.getReplies(mid); + replies.add(0, msg); + return String.join("\n", + replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); + } + return PlainTextFormatter.formatPost(msg); + } + return "Message not found"; + } + @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", + help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") + public String EditOrReply(User user, String... args) { + int mid = NumberUtils.toInt(args[1]); + int rid = NumberUtils.toInt(args[4], 0); + String txt = args[5]; + List messageTags = tagService.fromString(txt, true); + if (messageTags.size() > 0) { + if (user.getUid() != messagesService.getMessageAuthor(mid).getUid()) { + return "It is not your message"; + } + tagService.updateTags(mid, messageTags); + return "Tags are updated"; + } else { + int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, null); + listener.messagePosted(messagesService.getReply(mid, newrid)); + return "Reply posted.\n#" + mid + "/" + newrid + " " + + baseUri + mid + "#" + newrid; + } + } + + @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments", + patternFlags = Pattern.CASE_INSENSITIVE) + public String commandSubscribeMessage(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + int mid = NumberUtils.toInt(args[1], 0); + if (messagesService.getMessage(mid) != null) { + if (subscribe) { + if (subscriptionService.subscribeMessage(mid, user.getUid())) { + return "Subscribed"; + } + } else { + if (subscriptionService.unSubscribeMessage(mid, user.getUid())) { + return "Unsubscribed from #" + mid; + } + return "You was not subscribed to #" + mid; + } + } + return "Error"; + } + @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @user - subscribe to user's posts", + patternFlags = Pattern.CASE_INSENSITIVE) + public String commandSubscribeUser(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + User toUser = userService.getUserByName(args[1]); + if (toUser.getUid() > 0) { + if (subscribe) { + if (subscriptionService.subscribeUser(user, toUser)) { + listener.userSubscribed(user, toUser); + return "Subscribed"; + // TODO: already subscribed case + } + } else { + if (subscriptionService.unSubscribeUser(user, toUser)) { + return "Unsubscribed from @" + toUser.getName(); + } + return "You was not subscribed to @" + toUser.getName(); + } + } + return "Error"; + } + @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" + + "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE) + public String commandSubscribeTag(User user, String... args) { + boolean subscribe = args[0].equalsIgnoreCase("s"); + Tag tag = tagService.getTag(args[1], true); + if (subscribe) { + if (subscriptionService.subscribeTag(user, tag)) { + return "Subscribed"; + } + } else { + if (subscriptionService.unSubscribeTag(user, tag)) { + return "Unsubscribed from " + tag.getName(); + } + return "You was not subscribed to " + tag.getName(); + } + return "Error"; + } + + @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE, + help = "HELP - returns this help message") + public String commandHelp(User user, String[] input) { + return Arrays.stream(getClass().getDeclaredMethods()) + .filter(m -> m.isAnnotationPresent(UserCommand.class)) + .map(m -> m.getAnnotation(UserCommand.class).help()) + .collect(Collectors.joining("\n")); + } + + public String getBaseUri() { + return baseUri; + } + + public ProtocolListener getListener() { + return listener; + } + + public void setListener(ProtocolListener listener) { + this.listener = listener; + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java b/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java new file mode 100644 index 00000000..11231e04 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/protocol/ProtocolListener.java @@ -0,0 +1,13 @@ +package com.juick.server.protocol; + +import com.juick.Message; +import com.juick.User; + +/** + * Created by vitalyster on 19.12.2016. + */ +public interface ProtocolListener { + void privateMessage(User from, User to, String body); + void userSubscribed(User from, User to); + void messagePosted(Message msg); +} diff --git a/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java b/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java new file mode 100644 index 00000000..42a9bb59 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/protocol/annotation/UserCommand.java @@ -0,0 +1,33 @@ +package com.juick.server.protocol.annotation; + +import org.apache.commons.lang3.StringUtils; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Created by oxpa on 22.03.16. + */ +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface UserCommand { + /** + * + * @return a command pattern + */ + String pattern() default StringUtils.EMPTY; + + /** + * + * @return pattern flags + */ + int patternFlags() default 0; + + /** + * + * @return a string used in HELP command output. Basically, only 1 string + */ + String help() default StringUtils.EMPTY; +} diff --git a/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java b/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java new file mode 100644 index 00000000..7e166d43 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/util/HashUtils.java @@ -0,0 +1,19 @@ +package com.juick.server.util; + +import java.util.Random; + +/** + * Created by vitalyster on 29.06.2017. + */ +public class HashUtils { + private static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public static String generateHash(final int len) { + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); + } + return sb.toString(); + } +} diff --git a/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java b/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java new file mode 100644 index 00000000..1a92d6d1 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/server/util/TagUtils.java @@ -0,0 +1,25 @@ +package com.juick.server.util; + +import com.juick.Tag; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by aalexeev on 11/13/16. + */ +public class TagUtils { + private TagUtils() { + throw new IllegalStateException(); + } + + public static String toString(final List tags) { + if (CollectionUtils.isEmpty(tags)) + return StringUtils.EMPTY; + + return tags.stream().map(t -> " *" + t.getName()) + .collect(Collectors.joining()); + } +} diff --git a/juick-server-core/src/main/java/com/juick/service/CrosspostService.java b/juick-server-core/src/main/java/com/juick/service/CrosspostService.java new file mode 100644 index 00000000..467d1cbe --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/CrosspostService.java @@ -0,0 +1,58 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface CrosspostService { + + Optional> getTwitterTokens(int uid); + + boolean deleteTwitterToken(Integer uid); + + Optional getFacebookToken(int uid); + + ApplicationStatus getFbCrossPostStatus(int uid); + + boolean enableFBCrosspost(Integer uid); + + void disableFBCrosspost(Integer uid); + + String getTwitterName(int uid); + + String getTelegramName(int uid); + + Optional> getVkTokens(int uid); + + void deleteVKUser(Integer uid); + + int getUIDbyFBID(long fbID); + + boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink); + + boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink); + + int getUIDbyVKID(long vkID); + + boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink); + + String getFacebookNameByHash(String hash); + + String getTelegramNameByHash(String hash); + + boolean setFacebookUser(String hash, int uid); + + String getVKNameByHash(String hash); + + boolean setVKUser(String hash, int uid); + + boolean setTelegramUser(String hash, int uid); + + String getJIDByHash(String hash); + + boolean setJIDUser(String hash, int uid); +} diff --git a/juick-server-core/src/main/java/com/juick/service/EmailService.java b/juick-server-core/src/main/java/com/juick/service/EmailService.java new file mode 100644 index 00000000..67925ec1 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/EmailService.java @@ -0,0 +1,11 @@ +package com.juick.service; + +/** + * Created by vitalyster on 09.12.2016. + */ +public interface EmailService { + boolean verifyAddressByCode(Integer userId, String code); + boolean addVerificationCode(Integer userId, String account, String code); + boolean deleteEmail(Integer userId, String account); + boolean setSubscriptionHour(Integer userId, String account, String hour); +} diff --git a/juick-server-core/src/main/java/com/juick/service/MessagesService.java b/juick-server-core/src/main/java/com/juick/service/MessagesService.java new file mode 100644 index 00000000..37ca5eac --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/MessagesService.java @@ -0,0 +1,86 @@ +package com.juick.service; + +import com.juick.User; +import com.juick.server.helpers.ResponseReply; + +import java.util.Collection; +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface MessagesService { + int createMessage(int uid, String txt, String attachment, Collection tags); + + int createReply(int mid, int rid, int uid, String txt, String attachment); + + int getReplyIDIncrement(int mid); + + boolean recommendMessage(int mid, int vuid); + + boolean canViewThread(int mid, int uid); + + boolean isReadOnly(int mid); + + boolean isSubscribed(int uid, int mid); + + int getMessagePrivacy(int mid); + + com.juick.Message getMessage(int mid); + + com.juick.Message getReply(int mid, int rid); + + User getMessageAuthor(int mid); + + List getMessageRecommendations(int mid); + + List getAll(int visitorUid, int before); + + List getTag(int tid, int visitorUid, int before, int cnt); + + List getTags(String tids, int visitorUid, int before, int cnt); + + List getPlace(int placeId, int visitorUid, int before); + + List getMyFeed(int uid, int before); + + List getPrivate(int uid, int before); + + List getDiscussions(int uid, int before); + + List getRecommended(int uid, int before); + + List getPopular(int visitorUid, int before); + + List getPhotos(int visitorUid, int before); + + List getSearch(String search, int before); + + List getUserBlog(int uid, int privacy, int before); + + List getUserTag(int uid, int tid, int privacy, int before); + + List getUserBlogAtDay(int uid, int privacy, int daysback); + + List getUserBlogWithRecommendations(int uid, int privacy, int before); + + List getUserRecommendations(int uid, int before); + + List getUserPhotos(int uid, int privacy, int before); + + List getUserSearch(int UID, String search, int privacy, int before); + + List getMessages(Collection mids); + + List getReplies(int mid); + + boolean setMessagePopular(int mid, int popular); + + boolean setMessagePrivacy(int mid); + + boolean deleteMessage(int uid, int mid); + + List getLastMessages(int hours); + + List getLastReplies(int hours); +} diff --git a/juick-server-core/src/main/java/com/juick/service/PMQueriesService.java b/juick-server-core/src/main/java/com/juick/service/PMQueriesService.java new file mode 100644 index 00000000..e20bb3a5 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/PMQueriesService.java @@ -0,0 +1,28 @@ +package com.juick.service; + +import com.juick.User; + +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PMQueriesService { + boolean createPM(int uidFrom, int uid_to, String body); + + boolean addPMinRoster(int uid, String jid); + + boolean removePMinRoster(int uid, String jid); + + boolean havePMinRoster(int uid, String jid); + + String getLastView(int uidFrom, int uidTo); + + List getPMLastConversationsUsers(int uid, int cnt); + + List getPMMessages(int uid, int uidTo); + + List getLastPMInbox(int uid); + + List getLastPMSent(int uid); +} diff --git a/juick-server-core/src/main/java/com/juick/service/PrivacyQueriesService.java b/juick-server-core/src/main/java/com/juick/service/PrivacyQueriesService.java new file mode 100644 index 00000000..61eb199b --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/PrivacyQueriesService.java @@ -0,0 +1,17 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.User; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PrivacyQueriesService { + enum PrivacyResult { + Removed, Added + } + + PrivacyResult blacklistUser(User user, User target); + + PrivacyResult blacklistTag(User user, Tag tag); +} diff --git a/juick-server-core/src/main/java/com/juick/service/PushQueriesService.java b/juick-server-core/src/main/java/com/juick/service/PushQueriesService.java new file mode 100644 index 00000000..7d4bc295 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/PushQueriesService.java @@ -0,0 +1,33 @@ +package com.juick.service; + +import java.util.Collection; +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PushQueriesService { + List getGCMRegID(int uid); + + List getGCMTokens(Collection uids); + + boolean addGCMToken(Integer uid, String token); + + boolean deleteGCMToken(String token); + + List getMPNSURL(int uid); + + List getMPNSTokens(Collection uids); + + boolean addMPNSToken(Integer uid, String token); + + boolean deleteMPNSToken(String token); + + List getAPNSToken(int uid); + + List getAPNSTokens(Collection uids); + + boolean addAPNSToken(Integer uid, String token); + + boolean deleteAPNSToken(String token); +} diff --git a/juick-server-core/src/main/java/com/juick/service/ShowQueriesService.java b/juick-server-core/src/main/java/com/juick/service/ShowQueriesService.java new file mode 100644 index 00000000..a7e1c364 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/ShowQueriesService.java @@ -0,0 +1,14 @@ +package com.juick.service; + +import com.juick.User; + +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface ShowQueriesService { + List getRecommendedUsers(User forUser); + + List getTopUsers(); +} diff --git a/juick-server-core/src/main/java/com/juick/service/SubscriptionService.java b/juick-server-core/src/main/java/com/juick/service/SubscriptionService.java new file mode 100644 index 00000000..074c73f5 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/SubscriptionService.java @@ -0,0 +1,38 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.helpers.NotifyOpts; + +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface SubscriptionService { + List getJIDSubscribedToUser(int uid, boolean friendsonly); + + List getSubscribedUsers(int uid, int mid); + + List getUsersSubscribedToComments(int mid, int ignore_uid); + + List getUsersSubscribedToUserRecommendations(int uid, int mid, int muid); + + boolean subscribeMessage(int mid, int vuid); + + boolean unSubscribeMessage(int mid, int vuid); + + boolean subscribeUser(User user, User toUser); + + boolean unSubscribeUser(User user, User fromUser); + + boolean subscribeTag(User user, Tag toTag); + + boolean unSubscribeTag(User user, Tag toTag); + + List getSubscribedTags(User user); + + NotifyOpts getNotifyOptions(User user); + + boolean setNotifyOptions(User user, NotifyOpts options); +} diff --git a/juick-server-core/src/main/java/com/juick/service/TagService.java b/juick-server-core/src/main/java/com/juick/service/TagService.java new file mode 100644 index 00000000..2fcc7097 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/TagService.java @@ -0,0 +1,37 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.server.helpers.TagStats; + +import java.util.Collection; +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface TagService { + com.juick.Tag getTag(int tid); + + com.juick.Tag getTag(String tag, boolean autoCreate); + + List getTags(String[] tags, boolean autoCreate); + + boolean getTagNoIndex(int tagId); + + int createTag(String name); + + List getUserTagStats(int uid); + + List getUserBLTags(int uid); + + List getPopularTags(); + List getTagStats(); + + List updateTags(int mid, Collection newTags); + + List fromString(String txt, boolean tagsOnly); + + List getMessageTags(int mid); + + List getMessageTagsIDs(int mid); +} diff --git a/juick-server-core/src/main/java/com/juick/service/TelegramService.java b/juick-server-core/src/main/java/com/juick/service/TelegramService.java new file mode 100644 index 00000000..b23e3405 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/TelegramService.java @@ -0,0 +1,22 @@ +package com.juick.service; + +import java.util.List; + +/** + * Created by vt on 24/11/2016. + */ +public interface TelegramService { + boolean addChat(Long id); + + List getChats(); + + int getUser(long tgId); + + boolean createTelegramUser(long tgID, String tgName); + + boolean deleteTelegramUser(Integer uid); + + List getSubscribers(int uid); + + List getSubscribersToComments(int mid, int ignore_uid); +} diff --git a/juick-server-core/src/main/java/com/juick/service/UserService.java b/juick-server-core/src/main/java/com/juick/service/UserService.java new file mode 100644 index 00000000..a6db9f82 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/UserService.java @@ -0,0 +1,126 @@ +package com.juick.service; + +import com.juick.User; +import com.juick.server.helpers.Auth; +import com.juick.server.helpers.EmailOpts; +import com.juick.server.helpers.UserInfo; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface UserService { + enum ActiveStatus { + Inactive, + Active + } + + String getSignUpHashByJID(String jid); + + String getSignUpHashByTelegramID(Long telegramId, String username); + + int createUser(String username, String password); + + Optional getUserByUID(int uid); + + User getUserByName(String username); + + User getFullyUserByName(String username); + + User getUserByEmail(String email); + + List getFullyUsersByNames(Collection usernames); + + User getUserByJID(String jid); + + List getUsersByName(Collection unames); + + List getUsersByID(Collection uids); + + List getUsersByJID(Collection jids); + + List getJIDsbyUID(int uid); + + int getUIDbyJID(String jid); + + int getUIDbyName(String uname); + + int getUIDbyHash(String hash); + + com.juick.User getUserByHash(String hash); + + String getHashByUID(int uid); + + int getUIDByHttpAuth(String header); + + int checkPassword(String username, String password); + + boolean updatePassword(User user, String newPassword); + + int getUserOptionInt(int uid, String option, int defaultValue); + + int setUserOptionInt(int uid, String option, int value); + + UserInfo getUserInfo(User user); + + boolean updateUserInfo(User user, UserInfo info); + + boolean getCanMedia(int uid); + + boolean isInWL(int uid, int check); + + boolean isInBL(int uid, int check); + + boolean isInBLAny(int uid, int uid2); + + List checkBL(int visitor, Collection uids); + + boolean isSubscribed(int uid, int check); + + List getUserRead(int uid); + + List getUserReadLeastPopular(int uid, int cnt); + + List getUserReaders(int uid); + + List getUserFriends(int uid); + + List getUserBLUsers(int uid); + + boolean linkTwitterAccount(User user, String accessToken, String accessTokenSecret, String screenName); + + int getStatsIRead(int uid); + + int getStatsMyReaders(int uid); + + int getStatsMessages(int uid); + + int getStatsReplies(int uid); + + boolean setActiveStatusForJID(String JID, ActiveStatus jidStatus); + + List getAllJIDs(User user); + + List getAuthCodes(User user); + + List getEmails(User user); + + EmailOpts getEmailOpts(User user); + + String getEmailHash(User user); + + int deleteLoginForUser(String name); + + int setLoginForUser(int uid, String loginHash); + + void logout(int uid); + + boolean deleteJID(int uid, String jid); + + boolean unauthJID(int uid, String jid); + + List getActiveJIDs(); +} diff --git a/juick-server-core/src/main/java/com/juick/service/search/SearchService.java b/juick-server-core/src/main/java/com/juick/service/search/SearchService.java new file mode 100644 index 00000000..21deb0b1 --- /dev/null +++ b/juick-server-core/src/main/java/com/juick/service/search/SearchService.java @@ -0,0 +1,14 @@ +package com.juick.service.search; + +import java.util.List; + +/** + * Created by aalexeev on 11/18/16. + */ +public interface SearchService { + void setMaxResult(int maxResult); + + List searchInAllMessages(String searchString, int messageIdBefore); + + List searchByStringAndUser(String searchString, final int userId, int messageIdBefore); +} diff --git a/juick-server-core/src/main/resources/juick.conf.example b/juick-server-core/src/main/resources/juick.conf.example new file mode 100644 index 00000000..f89ca82b --- /dev/null +++ b/juick-server-core/src/main/resources/juick.conf.example @@ -0,0 +1,86 @@ +### Main database JDBC connection settings ### +# Main database JDBC driver +datasource_driver=net.sf.log4jdbc.DriverSpy +!datasource_driver=com.mysql.jdbc.Driver + +# Main database JDBC Url +!datasource_url=jdbc:mysql://localhost:3306/juick?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8 +datasource_url=jdbc:log4jdbc:mysql://localhost:3306/juick?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8 + +# Main database username +datasource_user= + +# Main database password +datasource_password= + +### Sphinx search connection +# Sphinx search JDBC driver +sphinx_driver=com.mysql.jdbc.Driver + +# Sphinx search JDBC url +sphinx_url=jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000 + +# Sphinx search JDBC username +sphinx_user= + +# Sphinx search JDBC password +sphinx_password= + +# The domain name for Web (default value - "juick.com") +web_domain=juick.com + +# Authority remember-me key +auth_remember_me_key=3vHcy3OUDQlkpRDm + + +### Template Settings (web page templates) +# Show sponsors block +template.showSponsors=false + +# Show Sape scripts +template.showSape=true + +# Show Advertisement +template.showAdv=true + + +api_user=juick +api_password=secret + +ios_pkcs12_file= +ios_pkcs12_password=secret + + +twitter_consumer_key= +twitter_consumer_secret= + +crosspost_jid= +ws_jid= +push_jid= + +xmpp_host= +xmpp_port= +xmpp_password= + +push_xmpp_password= + +wns_application_sip= +wns_client_secret= +gcm_key= + +hostname= +componentname= +component_name= +component_host= +component_port= +s2s_port= +xmppbot_jid=juick@juick.com/Juick + +keystore= +keystore_password= +broken_ssl_hosts= +banned_hosts= + +upload_tmp_dir= + +xmpp_disabled=false \ No newline at end of file diff --git a/juick-server-core/src/main/resources/logback.xml.example b/juick-server-core/src/main/resources/logback.xml.example new file mode 100644 index 00000000..05a3cfdf --- /dev/null +++ b/juick-server-core/src/main/resources/logback.xml.example @@ -0,0 +1,35 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/juick-server-core/src/main/resources/schema.sql b/juick-server-core/src/main/resources/schema.sql new file mode 100644 index 00000000..90bb4ed4 --- /dev/null +++ b/juick-server-core/src/main/resources/schema.sql @@ -0,0 +1,885 @@ +-- MySQL dump 10.13 Distrib 5.5.44, for debian-linux-gnu (x86_64) +-- +-- Host: localhost Database: juick +-- ------------------------------------------------------ +-- Server version 5.5.44-0+deb8u1-log +use juick; +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `ads_messages` +-- + +DROP TABLE IF EXISTS `ads_messages`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `ads_messages` ( + `message_id` int(10) unsigned NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `ads_messages_log` +-- + +DROP TABLE IF EXISTS `ads_messages_log`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `ads_messages_log` ( + `user_id` int(10) unsigned NOT NULL, + `message_id` int(10) unsigned NOT NULL, + `ts` int(10) unsigned NOT NULL DEFAULT '0' +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `android` +-- + +DROP TABLE IF EXISTS `android`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `android` ( + `user_id` int(10) unsigned NOT NULL, + `regid` char(255) NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY `regid` (`regid`), + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `auth` +-- + +DROP TABLE IF EXISTS `auth`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `auth` ( + `user_id` int(10) unsigned NOT NULL, + `protocol` enum('xmpp','email','sms') NOT NULL, + `account` char(64) NOT NULL, + `authcode` char(8) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `bl_tags` +-- + +DROP TABLE IF EXISTS `bl_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `bl_tags` ( + `user_id` int(10) unsigned NOT NULL, + `tag_id` int(10) unsigned NOT NULL, + KEY `tag_id` (`tag_id`), + KEY `user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `bl_users` +-- + +DROP TABLE IF EXISTS `bl_users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `bl_users` ( + `user_id` int(10) unsigned NOT NULL, + `bl_user_id` int(10) unsigned NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`user_id`,`bl_user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `captcha` +-- + +DROP TABLE IF EXISTS `captcha`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `captcha` ( + `jid` char(64) NOT NULL, + `hash` char(16) NOT NULL, + `confirmed` tinyint(4) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `captchaimg` +-- + +DROP TABLE IF EXISTS `captchaimg`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `captchaimg` ( + `id` char(16) NOT NULL, + `txt` char(6) NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `ip` char(16) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `emails` +-- + +DROP TABLE IF EXISTS `emails`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `emails` ( + `user_id` int(10) unsigned NOT NULL, + `email` char(64) NOT NULL, + `subscr_hour` tinyint(4) DEFAULT NULL, + KEY `email` (`email`) USING HASH +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `facebook` +-- + +DROP TABLE IF EXISTS `facebook`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `facebook` ( + `user_id` int(10) unsigned DEFAULT NULL, + `fb_id` bigint(20) unsigned NOT NULL, + `loginhash` char(36) DEFAULT NULL, + `access_token` char(255) DEFAULT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `fb_name` char(64) NOT NULL, + `fb_link` char(64) NOT NULL, + `crosspost` tinyint(1) unsigned NOT NULL DEFAULT '1', + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `favorites` +-- + +DROP TABLE IF EXISTS `favorites`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `favorites` ( + `user_id` int(10) unsigned NOT NULL, + `message_id` int(10) unsigned NOT NULL, + `ts` datetime NOT NULL, + UNIQUE KEY `user_id_2` (`user_id`,`message_id`), + KEY `user_id` (`user_id`), + KEY `message_id` (`message_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `friends_facebook` +-- + +DROP TABLE IF EXISTS `friends_facebook`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `friends_facebook` ( + `user_id` int(10) unsigned NOT NULL, + `friend_id` bigint(20) unsigned NOT NULL, + UNIQUE KEY `user_id` (`user_id`,`friend_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `images` +-- + +DROP TABLE IF EXISTS `images`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `images` ( + `mid` int(10) unsigned NOT NULL, + `rid` int(10) unsigned NOT NULL, + `thumb` int(10) unsigned NOT NULL, + `small` int(10) unsigned NOT NULL, + `medium` int(10) unsigned NOT NULL, + `height` int(10) unsigned NOT NULL, + `width` int(10) unsigned NOT NULL, + PRIMARY KEY (`mid`,`rid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `ios` +-- + +DROP TABLE IF EXISTS `ios`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `ios` ( + `user_id` int(10) unsigned NOT NULL, + `token` char(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY `token` (`token`), + KEY `user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `jids` +-- + +DROP TABLE IF EXISTS `jids`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `jids` ( + `user_id` int(10) unsigned DEFAULT NULL, + `jid` char(64) NOT NULL, + `active` tinyint(1) NOT NULL DEFAULT '1', + `loginhash` char(36) DEFAULT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY `jid` (`jid`), + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `logins` +-- + +DROP TABLE IF EXISTS `logins`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `logins` ( + `user_id` int(10) unsigned NOT NULL, + `hash` char(16) NOT NULL, + UNIQUE KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `mail` +-- + +DROP TABLE IF EXISTS `mail`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `mail` ( + `user_id` int(10) unsigned NOT NULL, + `hash` char(16) NOT NULL, + PRIMARY KEY (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `meon` +-- + +DROP TABLE IF EXISTS `meon`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `meon` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `link` char(255) NOT NULL, + `name` char(32) NOT NULL, + `ico` smallint(5) unsigned DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `messages` +-- + +DROP TABLE IF EXISTS `messages`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `messages` ( + `message_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__', + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `replies` smallint(5) unsigned NOT NULL DEFAULT '0', + `maxreplyid` smallint(5) unsigned NOT NULL DEFAULT '0', + `privacy` tinyint(4) NOT NULL DEFAULT '1', + `readonly` tinyint(1) NOT NULL DEFAULT '0', + `attach` enum('jpg','mp4','png') DEFAULT NULL, + `place_id` int(10) unsigned DEFAULT NULL, + `lat` decimal(10,7) DEFAULT NULL, + `lon` decimal(10,7) DEFAULT NULL, + `popular` tinyint(4) NOT NULL DEFAULT '0', + `hidden` tinyint(3) unsigned NOT NULL DEFAULT '0', + `likes` smallint(6) NOT NULL DEFAULT '0', + PRIMARY KEY (`message_id`), + KEY `user_id` (`user_id`), + KEY `ts` (`ts`), + KEY `attach` (`attach`), + KEY `place_id` (`place_id`), + KEY `popular` (`popular`), + KEY `hidden` (`hidden`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `messages_access` +-- + +DROP TABLE IF EXISTS `messages_access`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `messages_access` ( + `message_id` int(10) unsigned NOT NULL, + `user_id` int(10) unsigned NOT NULL, + KEY `message_id` (`message_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `messages_tags` +-- + +DROP TABLE IF EXISTS `messages_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `messages_tags` ( + `message_id` int(10) unsigned NOT NULL, + `tag_id` int(10) unsigned NOT NULL, + UNIQUE KEY `message_id_2` (`message_id`,`tag_id`), + KEY `message_id` (`message_id`), + KEY `tag_id` (`tag_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `messages_txt` +-- + +DROP TABLE IF EXISTS `messages_txt`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `messages_txt` ( + `message_id` int(10) unsigned NOT NULL, + `tags` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `repliesby` varchar(96) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `txt` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`message_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `messages_votes` +-- + +DROP TABLE IF EXISTS `messages_votes`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `messages_votes` ( + `message_id` int(10) unsigned NOT NULL, + `user_id` int(10) unsigned NOT NULL, + `vote` tinyint(4) NOT NULL DEFAULT '1', + UNIQUE KEY `message_id` (`message_id`,`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `places` +-- + +DROP TABLE IF EXISTS `places`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `places` ( + `place_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `lat` decimal(10,7) NOT NULL, + `lon` decimal(10,7) NOT NULL, + `name` char(64) NOT NULL, + `descr` char(255) DEFAULT NULL, + `url` char(128) DEFAULT NULL, + `user_id` int(10) unsigned NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`place_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `places_tags` +-- + +DROP TABLE IF EXISTS `places_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `places_tags` ( + `place_id` int(10) unsigned NOT NULL, + `tag_id` int(10) unsigned NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `pm` +-- + +DROP TABLE IF EXISTS `pm`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `pm` ( + `user_id` int(10) unsigned NOT NULL, + `user_id_to` int(10) unsigned NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `txt` text NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `pm_inroster` +-- + +DROP TABLE IF EXISTS `pm_inroster`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `pm_inroster` ( + `user_id` int(10) unsigned NOT NULL, + `jid` char(64) NOT NULL, + UNIQUE KEY `user_id_2` (`user_id`,`jid`), + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `pm_streams` +-- + +DROP TABLE IF EXISTS `pm_streams`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `pm_streams` ( + `user_id` int(10) unsigned NOT NULL, + `user_id_to` int(10) unsigned NOT NULL, + `lastmessage` datetime NOT NULL, + `lastview` datetime DEFAULT NULL, + `unread` smallint(5) unsigned NOT NULL DEFAULT '0', + UNIQUE KEY `user_id` (`user_id`,`user_id_to`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `presence` +-- + +DROP TABLE IF EXISTS `presence`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `presence` ( + `user_id` int(10) unsigned NOT NULL, + `jid` char(64) DEFAULT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + UNIQUE KEY `jid` (`jid`) +) ENGINE=MEMORY DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `reader_links` +-- + +DROP TABLE IF EXISTS `reader_links`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `reader_links` ( + `link_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `rss_id` int(10) unsigned NOT NULL, + `url` char(255) NOT NULL, + `title` char(255) NOT NULL, + `ts` datetime NOT NULL, + PRIMARY KEY (`link_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `reader_rss` +-- + +DROP TABLE IF EXISTS `reader_rss`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `reader_rss` ( + `rss_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `url` char(255) NOT NULL, + `lastcheck` datetime NOT NULL, + PRIMARY KEY (`rss_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `replies` +-- + +DROP TABLE IF EXISTS `replies`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `replies` ( + `message_id` int(10) unsigned NOT NULL, + `reply_id` smallint(5) unsigned NOT NULL, + `user_id` int(10) unsigned NOT NULL, + `replyto` smallint(5) unsigned NOT NULL DEFAULT '0', + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `attach` enum('jpg','mp4','png') COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `txt` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, + KEY `message_id` (`message_id`), + KEY `user_id` (`user_id`), + KEY `ts` (`ts`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `sphinx` +-- + +DROP TABLE IF EXISTS `sphinx`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `sphinx` ( + `counter_id` tinyint(3) unsigned NOT NULL, + `max_id` int(10) unsigned NOT NULL, + PRIMARY KEY (`counter_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `subscr_messages` +-- + +DROP TABLE IF EXISTS `subscr_messages`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `subscr_messages` ( + `message_id` int(10) unsigned NOT NULL, + `suser_id` int(10) unsigned NOT NULL, + UNIQUE KEY `message_id` (`message_id`,`suser_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `subscr_tags` +-- + +DROP TABLE IF EXISTS `subscr_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `subscr_tags` ( + `tag_id` int(10) unsigned NOT NULL, + `suser_id` int(10) unsigned NOT NULL, + `jid` char(64) NOT NULL, + `active` bit(1) NOT NULL DEFAULT b'1', + UNIQUE KEY `tag_id` (`tag_id`,`suser_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `subscr_users` +-- + +DROP TABLE IF EXISTS `subscr_users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `subscr_users` ( + `user_id` int(10) unsigned NOT NULL, + `suser_id` int(10) unsigned NOT NULL, + `jid` char(64) DEFAULT NULL, + `active` bit(1) NOT NULL DEFAULT b'1', + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY `user_id` (`user_id`,`suser_id`), + KEY `suser_id` (`suser_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `tags` +-- + +DROP TABLE IF EXISTS `tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tags` ( + `tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `synonym_id` int(10) unsigned DEFAULT NULL, + `name` char(70) DEFAULT NULL, + `top` tinyint(1) unsigned NOT NULL DEFAULT '0', + `noindex` tinyint(1) unsigned NOT NULL DEFAULT '0', + `stat_messages` int(10) unsigned NOT NULL DEFAULT '0', + `stat_users` smallint(5) unsigned NOT NULL DEFAULT '0', + PRIMARY KEY (`tag_id`), + KEY `synonym_id` (`synonym_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `tags_ignore` +-- + +DROP TABLE IF EXISTS `tags_ignore`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tags_ignore` ( + `tag_id` int(10) unsigned NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `tags_synonyms` +-- + +DROP TABLE IF EXISTS `tags_synonyms`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tags_synonyms` ( + `name` char(64) NOT NULL, + `changeto` char(64) NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `telegram` +-- + +DROP TABLE IF EXISTS `telegram`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `telegram` ( + `user_id` int(10) unsigned DEFAULT NULL, + `tg_id` bigint(20) NOT NULL, + `tg_name` char(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `loginhash` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `telegram_chats` +-- + +DROP TABLE IF EXISTS `telegram_chats`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `telegram_chats` ( + `chat_id` bigint(20) DEFAULT NULL, + UNIQUE KEY `chat_id` (`chat_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `top_ignore_messages` +-- + +DROP TABLE IF EXISTS `top_ignore_messages`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `top_ignore_messages` ( + `message_id` int(10) unsigned NOT NULL +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `top_ignore_tags` +-- + +DROP TABLE IF EXISTS `top_ignore_tags`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `top_ignore_tags` ( + `tag_id` int(10) unsigned NOT NULL, + PRIMARY KEY (`tag_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `top_ignore_users` +-- + +DROP TABLE IF EXISTS `top_ignore_users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `top_ignore_users` ( + `user_id` int(10) unsigned NOT NULL, + PRIMARY KEY (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `twitter` +-- + +DROP TABLE IF EXISTS `twitter`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `twitter` ( + `user_id` int(10) unsigned NOT NULL, + `access_token` char(64) NOT NULL, + `access_token_secret` char(64) NOT NULL, + `uname` char(64) NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `crosspost` tinyint(1) unsigned NOT NULL DEFAULT '1', + PRIMARY KEY (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `useroptions` +-- + +DROP TABLE IF EXISTS `useroptions`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `useroptions` ( + `user_id` int(10) unsigned NOT NULL, + `jnotify` tinyint(1) NOT NULL DEFAULT '1', + `subscr_active` tinyint(1) NOT NULL DEFAULT '1', + `off_ts` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', + `xmppxhtml` tinyint(1) NOT NULL DEFAULT '0', + `subscr_notify` tinyint(1) NOT NULL DEFAULT '1', + `recommendations` tinyint(1) NOT NULL DEFAULT '1', + `privacy_view` tinyint(1) NOT NULL DEFAULT '1', + `privacy_reply` tinyint(1) NOT NULL DEFAULT '1', + `privacy_pm` tinyint(1) NOT NULL DEFAULT '1', + `repliesview` tinyint(1) NOT NULL DEFAULT '0', + PRIMARY KEY (`user_id`), + KEY `recommendations` (`recommendations`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `users` +-- + +DROP TABLE IF EXISTS `users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `users` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `nick` char(64) NOT NULL, + `passw` char(32) NOT NULL, + `lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__', + `banned` tinyint(3) unsigned NOT NULL DEFAULT '0', + `lastmessage` int(11) NOT NULL DEFAULT '0', + `lastpm` int(11) NOT NULL DEFAULT '0', + `lastphoto` int(11) NOT NULL DEFAULT '0', + `karma` smallint(6) NOT NULL DEFAULT '0', + PRIMARY KEY (`id`), + UNIQUE KEY `nick` (`nick`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `users_refs` +-- + +DROP TABLE IF EXISTS `users_refs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `users_refs` ( + `user_id` int(10) unsigned NOT NULL, + `ref` int(10) unsigned NOT NULL, + KEY `ref` (`ref`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `users_subscr` +-- + +DROP TABLE IF EXISTS `users_subscr`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `users_subscr` ( + `user_id` int(10) unsigned NOT NULL, + `cnt` smallint(5) unsigned NOT NULL DEFAULT '0', + PRIMARY KEY (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `usersinfo` +-- + +DROP TABLE IF EXISTS `usersinfo`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `usersinfo` ( + `user_id` int(10) unsigned NOT NULL, + `jid` char(32) DEFAULT NULL, + `fullname` char(32) DEFAULT NULL, + `country` char(32) DEFAULT NULL, + `url` char(64) DEFAULT NULL, + `gender` char(32) DEFAULT NULL, + `bday` char(10) DEFAULT NULL, + `descr` varchar(255) DEFAULT NULL, + PRIMARY KEY (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `vk` +-- + +DROP TABLE IF EXISTS `vk`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `vk` ( + `user_id` int(10) unsigned DEFAULT NULL, + `vk_id` bigint(20) NOT NULL, + `loginhash` char(36) DEFAULT NULL, + `access_token` char(128) NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `vk_name` char(64) NOT NULL, + `vk_link` char(64) NOT NULL, + `crosspost` tinyint(3) unsigned NOT NULL DEFAULT '1', + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `winphone` +-- + +DROP TABLE IF EXISTS `winphone`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `winphone` ( + `user_id` int(10) unsigned NOT NULL, + `url` char(255) NOT NULL, + `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY `url` (`url`), + KEY `user_id` (`user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Table structure for table `wl_users` +-- + +DROP TABLE IF EXISTS `wl_users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `wl_users` ( + `user_id` int(10) unsigned NOT NULL, + `wl_user_id` int(10) unsigned NOT NULL, + PRIMARY KEY (`user_id`,`wl_user_id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2016-09-03 14:32:11 diff --git a/juick-server-core/src/main/resources/update.sql b/juick-server-core/src/main/resources/update.sql new file mode 100644 index 00000000..13a62c3d --- /dev/null +++ b/juick-server-core/src/main/resources/update.sql @@ -0,0 +1,12 @@ +-- if version table not exists set up version = 0; +update version set version = 0; + +DROP TABLE IF EXISTS `version`; + +CREATE TABLE `version` ( + `version` bigint NOT NULL +); + +insert into version values (0); + +update version set version = 1; \ No newline at end of file diff --git a/juick-server-jdbc/build.gradle b/juick-server-jdbc/build.gradle new file mode 100644 index 00000000..fde15afe --- /dev/null +++ b/juick-server-jdbc/build.gradle @@ -0,0 +1,76 @@ +apply plugin: 'java' +apply plugin: 'war' + +sourceCompatibility = 1.8 + +dependencies { + compile project(':juick-server-core') + + compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${rootProject.jacksonVersion}" + + providedCompile "ch.qos.logback:logback-classic:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-core:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-access:${rootProject.logbackVersion}" + + providedCompile "org.slf4j:slf4j-api:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jcl-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:log4j-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jul-to-slf4j:${rootProject.slf4jVersion}" + + compile "org.apache.httpcomponents:httpclient:4.5.3" + providedCompile "org.apache.commons:commons-lang3:3.6" + providedCompile "org.apache.commons:commons-collections4:4.1" + providedCompile "org.apache.commons:commons-text:1.1" + providedCompile "commons-io:commons-io:2.5" + providedCompile "commons-codec:commons-codec:1.10" + + compile 'com.github.ben-manes.caffeine:caffeine:2.5.2' + + compile "org.springframework:spring-context:${rootProject.springFrameworkVersion}" + compile "org.springframework:spring-jdbc:${rootProject.springFrameworkVersion}" + + compile "org.springframework.security:spring-security-web:${rootProject.springSecurityVersion}" + compile "org.springframework.security:spring-security-config:${rootProject.springSecurityVersion}" + + providedCompile "org.apache.commons:commons-dbcp2:2.1.1" + compile "com.googlecode.log4jdbc:log4jdbc:1.2" + compile "javax.inject:javax.inject:1" + + compile "rocks.xmpp:xmpp-core-client:0.7.4" + compile "rocks.xmpp:xmpp-extensions-client:0.7.4" + + compile 'org.imgscalr:imgscalr-lib:4.2' + + providedCompile "javax.servlet:javax.servlet-api:3.1.0" + + providedRuntime "commons-fileupload:commons-fileupload:1.3.3" + + testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" + testCompile "junit:junit:${rootProject.junitVersion}" + testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" + testCompile "org.mockito:mockito-core:${rootProject.mockitoVersion}" + testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}" + testCompile "org.springframework.security:spring-security-test:${rootProject.springSecurityVersion}" + + testRuntime "mysql:mysql-connector-java:5.1.40" +} + +compileJava.options.encoding = 'UTF-8' + +configurations { + all*.exclude module: 'commons-logging' +} + +configurations { + testArtifacts.extendsFrom testRuntime +} +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output +} +artifacts { + testArtifacts testJar +} diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java new file mode 100644 index 00000000..b3c8c6bd --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/configuration/DataConfiguration.java @@ -0,0 +1,79 @@ +package com.juick.configuration; + +import com.juick.service.search.SearchService; +import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.StringUtils; +import org.springframework.context.annotation.*; +import org.springframework.core.env.Environment; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.annotation.TransactionManagementConfigurer; + +import javax.annotation.Resource; +import java.util.Collections; +import java.util.List; + +/** + * Created by aalexeev on 11/11/16. + */ +@Configuration +@EnableTransactionManagement +@PropertySource(value = {"classpath:juick.conf"}) +@ComponentScan(basePackages = {"com.juick.service"}) +public class DataConfiguration implements TransactionManagementConfigurer { + @Resource + private Environment env; + + // NOTE: The close() method will be called automatically with default @Bean settings + // But Datasource interface has no close() method + @Bean + public BasicDataSource dataSource() { + BasicDataSource dataSource = new BasicDataSource(); + + dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(env.getProperty("datasource_url")); + dataSource.setUsername(env.getProperty("datasource_user", StringUtils.EMPTY)); + dataSource.setPassword(env.getProperty("datasource_password", StringUtils.EMPTY)); + + dataSource.setValidationQuery("select 1"); + + return dataSource; + } + + @Bean + public PlatformTransactionManager transactionManager() { + return new DataSourceTransactionManager(dataSource()); + } + + @Override + public PlatformTransactionManager annotationDrivenTransactionManager() { + return transactionManager(); + } + + @Bean + @DependsOn("dataSource") + public JdbcTemplate jdbcTemplate() { + return new JdbcTemplate(dataSource()); + } + + @Bean + public SearchService emptySearchService() { + return new SearchService() { + @Override + public void setMaxResult(int maxResult) { + } + + @Override + public List searchInAllMessages(String searchString, int messageIdBefore) { + return Collections.emptyList(); + } + + @Override + public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { + return Collections.emptyList(); + } + }; + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java new file mode 100644 index 00000000..e6b446b6 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/configuration/SearchConfiguration.java @@ -0,0 +1,43 @@ +package com.juick.configuration; + +import com.juick.service.search.SearchService; +import com.juick.service.search.SphinxSearchServiceImpl; +import org.apache.commons.dbcp2.BasicDataSource; +import org.apache.commons.lang3.StringUtils; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; + +/** + * Created by aalexeev on 11/18/16. + */ +@Configuration +@PropertySource(value = {"classpath:juick.conf"}) +public class SearchConfiguration { + @Resource + private Environment env; + + // NOTE: The close() method will be called automatically with default @Bean settings + // But Datasource interface has no close() method + @Bean + public BasicDataSource searchDataSource() { + BasicDataSource dataSource = new BasicDataSource(); + + dataSource.setDriverClassName(env.getProperty("sphinx_driver", "com.mysql.jdbc.Driver")); + dataSource.setUrl(env.getProperty("sphinx_url")); + dataSource.setUsername(env.getProperty("sphinx_user", StringUtils.EMPTY)); + dataSource.setPassword(env.getProperty("sphinx_password", StringUtils.EMPTY)); + + return dataSource; + } + + @Bean + @DependsOn("searchDataSource") + public SearchService searchService() { + return new SphinxSearchServiceImpl(searchDataSource()); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java b/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java new file mode 100644 index 00000000..b3c0424c --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/configuration/UpdaterConfiguration.java @@ -0,0 +1,30 @@ +package com.juick.configuration; + +import com.juick.database.MySqlUpdater; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.PlatformTransactionManager; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 12/13/16. + */ +@Configuration +public class UpdaterConfiguration { + @Inject + JdbcTemplate jdbcTemplate; + @Inject + PlatformTransactionManager transactionManager; + + @Bean + @DependsOn({"jdbcTemplate", "transactionManager"}) + public MySqlUpdater updater() { + return new MySqlUpdater( + jdbcTemplate, + transactionManager, + "update.sql"); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/database/MySqlUpdater.java b/juick-server-jdbc/src/main/java/com/juick/database/MySqlUpdater.java new file mode 100644 index 00000000..2027d9f1 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/database/MySqlUpdater.java @@ -0,0 +1,135 @@ +package com.juick.database; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.util.Assert; + +import javax.annotation.PostConstruct; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED; + +/** + * Created by aalexeev on 12/13/16. + */ +public class MySqlUpdater { + private static final Pattern UPDATE_PATTERN = Pattern.compile( + "update\\s+(version|`version`)\\s+set\\s+(version|`version`)\\s+=\\s*(\\d+)", + Pattern.CASE_INSENSITIVE); + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final JdbcTemplate jdbcTemplate; + private final TransactionTemplate transactionTemplate; + private final String updateSqlResource; + + + public MySqlUpdater(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager, String updateSqlResource) { + Assert.notNull(jdbcTemplate, "JdbcTemplate must be initialized"); + Assert.notNull(transactionManager, "PlatformTransactionManager must be initialized"); + Assert.notNull(updateSqlResource, "sqlResource must be initialized"); + + this.jdbcTemplate = jdbcTemplate; + this.transactionTemplate = new TransactionTemplate(transactionManager); + this.updateSqlResource = updateSqlResource; + } + + @PostConstruct + public void init() { + try ( + InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(updateSqlResource); + ) { + if (is != null) { + String content = IOUtils.toString(is, StandardCharsets.UTF_8); + if (StringUtils.isNotEmpty(content)) { + String[] sqlArray = content.split(";"); + if (sqlArray.length > 0) { + List sqlList = new ArrayList<>(sqlArray.length); + for (String sql : sqlArray) + if (!sql.isEmpty()) { + String sqlTrimmed = sql.trim(); + if (!sqlTrimmed.isEmpty()) + sqlList.add(sqlTrimmed); + } + if (!sqlList.isEmpty()) + processingSql(sqlList); + } + } + } + } catch (Exception e) { + logger.error("MySqlUpdater initialization exception", e); + } + } + + private void processingSql(final List sqls) { + long currentDbVersion = getSingleResult(this::getVersionRaw); + long actualVersion; + + List changesSql = new ArrayList<>(); + + for (String sql : sqls) { + changesSql.add(sql); + + Matcher m = UPDATE_PATTERN.matcher(sql); + if (m.matches()) { + actualVersion = Long.valueOf(m.group(3)); + + if (actualVersion > currentDbVersion) { + updateInTransaction(changesSql); + currentDbVersion = actualVersion; + } + changesSql.clear(); + } + } + } + + private void updateInTransaction(final List sqls) { + transactionTemplate.setReadOnly(false); + transactionTemplate.setPropagationBehavior(PROPAGATION_REQUIRED); + transactionTemplate.execute(status -> { + for (String sql : sqls) + jdbcTemplate.execute(sql); + return 0; + }); + } + + private T getSingleResult(Supplier supplier) { + transactionTemplate.setReadOnly(true); + transactionTemplate.setPropagationBehavior(PROPAGATION_REQUIRED); + + return transactionTemplate.execute(status -> supplier.get()); + } + + private long getVersionRaw() { + int cnt = jdbcTemplate.query( + "SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?", + rs -> { + int result = 0; + if (rs.next()) + result = rs.getInt(1); + return result; + }, + "juick", "version"); + + long version = 0l; + + if (cnt == 1) { + List list = jdbcTemplate.queryForList("select version from version", Long.class); + if (!list.isEmpty()) + version = list.get(0); + } + + return version; + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java b/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java new file mode 100644 index 00000000..e3784345 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/BaseJdbcService.java @@ -0,0 +1,55 @@ +package com.juick.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; +import org.springframework.util.Assert; + +import javax.sql.DataSource; + +/** + * Created by aalexeev on 11/13/16. + */ +public abstract class BaseJdbcService { + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + private final JdbcTemplate jdbcTemplate; + private final TransactionTemplate transactionTemplate; + private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + + protected BaseJdbcService(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager) { + Assert.notNull(jdbcTemplate, "JdbcTemplate must be initialized"); + + this.jdbcTemplate = jdbcTemplate; + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + this.transactionTemplate = (transactionManager == null) ? + null : new TransactionTemplate(transactionManager); + + } + + protected BaseJdbcService(DataSource dataSource, PlatformTransactionManager transactionManager) { + Assert.notNull(dataSource, "DataSource must be initialized"); + + this.jdbcTemplate = new JdbcTemplate(dataSource); + this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + this.transactionTemplate = (transactionManager == null) ? + null : new TransactionTemplate(transactionManager); + + } + + protected JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + protected NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { + return namedParameterJdbcTemplate; + } + + protected TransactionTemplate getTransactionTemplate() { + return transactionTemplate; + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java new file mode 100644 index 00000000..965f2b51 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -0,0 +1,246 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; +import java.util.List; +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService { + + @Inject + public CrosspostServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Transactional(readOnly = true) + @Override + public Optional> getTwitterTokens(final int uid) { + List>> list = getJdbcTemplate().query( + "SELECT access_token,access_token_secret FROM twitter WHERE user_id = ? AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), + uid); + + return list.isEmpty() ? + Optional.empty() : list.get(0); + } + + @Transactional + @Override + public boolean deleteTwitterToken(Integer uid) { + return getJdbcTemplate().update("DELETE FROM twitter WHERE user_id=?", uid) > 0 + && getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=1741", uid) > 0; + } + + @Transactional(readOnly = true) + @Override + public Optional getFacebookToken(final int uid) { + List list = getJdbcTemplate().queryForList( + "SELECT access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1", + String.class, + uid); + return list.isEmpty() ? + Optional.empty() : Optional.of(list.get(0)); + } + + @Transactional(readOnly = true) + @Override + public ApplicationStatus getFbCrossPostStatus(final int uid) { + List 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 + && getJdbcTemplate().update( + "INSERT INTO subscr_users(user_id,suser_id,jid,active) VALUES (?,5863,'juick@facebook.juick.com',1)", + uid) > 0; + } + + @Transactional + @Override + public void disableFBCrosspost(Integer uid) { + getJdbcTemplate().update("UPDATE facebook SET crosspost=0 WHERE user_id=?", uid); + // TODO: stop using magic numbers for system users + getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=5863", uid); + } + + @Transactional(readOnly = true) + @Override + public String getTwitterName(final int uid) { + List 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 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> getVkTokens(final int uid) { + List>> 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("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)", + fbID, loginhash, token, fbName, fbLink) > 0; + } + + @Transactional + @Override + public boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink) { + return getJdbcTemplate().update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?", + token, fbName, fbLink, fbID) > 0; + } + + @Transactional(readOnly = true) + @Override + public int getUIDbyVKID(long vkID) { + try { + return getJdbcTemplate().queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID); + } catch (EmptyResultDataAccessException e) { + return 0; + } + } + + @Transactional + @Override + public boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink) { + return getJdbcTemplate().update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)", + vkID, loginhash, token, vkName, vkLink) > 0; + } + + @Transactional(readOnly = true) + @Override + public String getFacebookNameByHash(String hash) { + try { + List> 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 "" + fb.get(0).getLeft() + ""; + } + 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 "" + name + ""; + } 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> 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 "" + logins.get(0).getLeft() + ""; + } + 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-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java new file mode 100644 index 00000000..b0bf9e20 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java @@ -0,0 +1,52 @@ +package com.juick.service; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; + +/** + * Created by vitalyster on 09.12.2016. + */ +@Repository +@Transactional +public class EmailServiceImpl extends BaseJdbcService implements EmailService { + @Inject + public EmailServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @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); + getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", 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 deleteEmail(Integer userId, String account) { + return getJdbcTemplate().update("DELETE FROM emails WHERE user_id=? AND email=?", userId, account) > 0; + } + + @Override + public boolean setSubscriptionHour(Integer userId, String account, String hour) { + getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", userId); + return StringUtils.isNotEmpty(account) && getJdbcTemplate().update( + "UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?", hour, userId, account) > 0; + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java new file mode 100644 index 00000000..318b5805 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -0,0 +1,814 @@ +package com.juick.service; + +import com.juick.Message; +import com.juick.Tag; +import com.juick.User; +import com.juick.server.helpers.PrivacyOpts; +import com.juick.server.helpers.ResponseReply; +import com.juick.service.search.SearchService; +import com.juick.util.MessageUtils; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.jdbc.core.ConnectionCallback; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.SqlParameterSource; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +import javax.inject.Inject; +import java.sql.*; +import java.util.*; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class MessagesServiceImpl extends BaseJdbcService implements MessagesService { + private final UserService userService; + private final SearchService searchService; + + @Inject + public MessagesServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, SearchService searchService) { + super(jdbcTemplate, null); + + Assert.notNull(userService, "UserService must be initialized"); + this.userService = userService; + + Assert.notNull(searchService, "SearchService must be initialized"); + this.searchService = searchService; + } + + private class MessageMapper implements RowMapper { + @Override + public Message mapRow(ResultSet rs, int rowNum) throws SQLException { + Message msg = new Message(); + msg.setMid(rs.getInt(1)); + msg.setRid(rs.getInt(2)); + msg.setReplyto(rs.getInt(3)); + User user = new User(); + user.setUid(rs.getInt(4)); + user.setName(rs.getString(5)); + user.setBanned(rs.getBoolean(6)); + msg.setUser(user); + msg.TimeAgo = rs.getInt(7); + msg.setDate(rs.getTimestamp(8)); + msg.ReadOnly = rs.getBoolean(9); + msg.setPrivacy(rs.getInt(10)); + msg.FriendsOnly = msg.getPrivacy() < 0; + msg.setReplies(rs.getInt(11)); + msg.setAttachmentType(rs.getString(12)); + if (rs.getDouble(13) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(14); + msg.Place.lon = rs.getDouble(15); + } + msg.setLikes(rs.getInt(16)); + msg.Hidden = rs.getBoolean(17); + String tagsStr = rs.getString(18); + msg.parseTags(tagsStr); + msg.setRepliesBy(rs.getString(19)); + msg.setText(rs.getString(20)); + msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(21))); + return msg; + } + } + + /** + * @see Java, JDBC and MySQL Types + */ + @Transactional + @Override + public int createMessage(final int uid, final String txt, final String attachment, final Collection tags) { + KeyHolder holder = new GeneratedKeyHolder(); + getJdbcTemplate().update( + con -> { + PreparedStatement stmt = con.prepareStatement( + "INSERT INTO messages(user_id, attach) VALUES (?, ?)", + Statement.RETURN_GENERATED_KEYS); + stmt.setInt(1, uid); + if (attachment != null) + stmt.setString(2, attachment); + else + stmt.setNull(2, Types.CHAR); + + return stmt; + }, + holder); + + int mid = holder.getKey().intValue(); + + if (mid > 0) { + String tagsNames = StringUtils.EMPTY; + + if (CollectionUtils.isNotEmpty(tags)) { + StringBuilder tasNamesBuilder = new StringBuilder(); + List 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) VALUES (?, ?, ?)", + new Object[]{mid, tagsNames, txt}, + new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR}); + } + + return mid; + } + + /** + * @param mid + * @param rid + * @param uid + * @param txt + * @param attachment + * @return + * @see Java, JDBC and MySQL Types + */ + @Transactional + @Override + public int createReply(final int mid, final int rid, final int uid, final String txt, final String attachment) { + int ridnew = getReplyIDIncrement(mid); + + getJdbcTemplate().update( + "INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt) VALUES (?, ?, ?, ?, ?, ?)", + new Object[]{mid, ridnew, uid, rid, attachment, txt}, + new int[]{Types.INTEGER, Types.SMALLINT, Types.INTEGER, Types.SMALLINT, Types.CHAR, Types.VARCHAR}); + + if (ridnew > 0) + getJdbcTemplate().update( + "UPDATE messages SET replies = replies + 1 WHERE message_id = ?", + mid); + return ridnew; + } + + @Override + public int getReplyIDIncrement(final int mid) { + return getJdbcTemplate().execute((ConnectionCallback) conn -> { + conn.setAutoCommit(false); + final int replyNo; + try (PreparedStatement ps = conn.prepareStatement("START TRANSACTION")) { + ps.executeUpdate(); + } + try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) { + ps.setInt(1, mid); + try (ResultSet resultSet = ps.executeQuery()) { + if (resultSet.next()) { + replyNo = resultSet.getInt(1); + } else { + throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0); + } + } + } + try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) { + ps.setInt(1, replyNo); + ps.setInt(2, mid); + if (ps.executeUpdate() != 1) { + throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); + } + } + conn.commit(); + return replyNo; + }); + + } + + @Transactional + @Override + public boolean recommendMessage(final int mid, final int vuid) { + boolean res = getJdbcTemplate().update( + "INSERT IGNORE INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", + vuid, mid) == 1; + + if (res) + getJdbcTemplate().update( + "UPDATE messages SET likes = likes + 1 WHERE message_id = ?", + mid); + return res; + } + + @Transactional(readOnly = true) + @Override + public boolean canViewThread(final int mid, final int uid) { + List 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 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 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 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 list = getJdbcTemplate().query( + "SELECT messages.message_id, 0 as rid, 0 as replyto, " + + "messages.user_id, users.nick, users.banned as banned, " + + "TIMESTAMPDIFF(MINUTE, messages.ts, NOW())," + + "messages.ts," + + "messages.readonly, messages.privacy, messages.replies," + + "messages.attach, messages.place_id, messages.lat," + + "messages.lon, messages.likes, messages.hidden," + + "txt.tags, txt.repliesby, txt.txt, '' as q FROM messages " + + "INNER JOIN users ON messages.user_id = users.id " + + "INNER JOIN messages_txt AS txt " + + "ON messages.message_id = txt.message_id " + + "WHERE messages.message_id = ?", + new MessageMapper(), + mid); + + return list.isEmpty() ? null : list.get(0); + } + + @Transactional(readOnly = true) + @Override + public com.juick.Message getReply(final int mid, final int rid) { + List list = getJdbcTemplate().query( + "SELECT replies.user_id, users.nick," + + "replies.replyto, replies.ts," + + "replies.attach, replies.txt, IFNULL(q.txt,t.txt) as quote " + + "FROM replies INNER JOIN users ON replies.user_id = users.id " + + "LEFT JOIN replies q ON replies.message_id = q.message_id and replies.replyto = q.reply_id " + + "LEFT JOIN messages_txt t ON replies.message_id = t.message_id " + + "WHERE replies.message_id = ? AND replies.reply_id = ?", + (rs, num) -> { + Message msg = new Message(); + + msg.setMid(mid); + msg.setRid(rid); + msg.setUser(new User()); + msg.getUser().setUid(rs.getInt(1)); + msg.getUser().setName(rs.getString(2)); + msg.setReplyto(rs.getInt(3)); + msg.setDate(rs.getTimestamp(4)); + msg.setAttachmentType(rs.getString(5)); + msg.setText(rs.getString(6)); + String quote = rs.getString(7); + + if (!StringUtils.isEmpty(quote)) { + msg.setReplyQuote(MessageUtils.formatQuote(quote)); + } + + return msg; + }, + mid, rid); + + return list.isEmpty() ? null : list.get(0); + } + + @Transactional(readOnly = true) + @Override + public User getMessageAuthor(final int mid) { + List 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 getMessageRecommendations(final int mid) { + return getJdbcTemplate().queryForList( + "SELECT users.nick FROM favorites INNER JOIN users " + + "ON (favorites.message_id = ? AND favorites.user_id = users.id)", + new Object[]{mid}, + String.class); + } + + @Transactional(readOnly = true) + @Override + public List 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 users u WHERE u.banned = 1 and u.id = m.user_id) ORDER BY m.message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + } + + @Transactional(readOnly = true) + @Override + public List 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 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 USING(message_id) WHERE " + + (before > 0 ? + " messages.message_id < :before AND " : StringUtils.EMPTY) + + "(messages.privacy > 0 OR messages.user_id = :visitorUid) ORDER BY message_id DESC LIMIT :cnt", + sqlParameterSource, + Integer.class); + } + + @Transactional(readOnly = true) + @Override + public List 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 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 getMyFeed(final int uid, final int before) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("before", before); + + List mids = getNamedParameterJdbcTemplate().queryForList( + "(SELECT message_id FROM messages " + + " INNER JOIN subscr_users ON (subscr_users.suser_id = :uid AND subscr_users.user_id = messages.user_id) " + + " WHERE " + + (before > 0 ? + " message_id < :before AND " : 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)))) " + + " UNION " + + " (SELECT message_id FROM messages WHERE user_id=:uid " + + (before > 0 ? + " AND message_id < :before " : StringUtils.EMPTY) + + ") ORDER BY message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + + return mids; + } + + @Transactional(readOnly = true) + @Override + public List 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 getDiscussions(final int uid, final int before) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("before", before); + + return getNamedParameterJdbcTemplate().queryForList( + "SELECT message_id FROM subscr_messages WHERE suser_id = :uid" + + (before > 0 ? + " AND message_id < :before " : " ") + + "ORDER BY message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + } + + @Transactional(readOnly = true) + @Override + public List 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 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) " + + " ORDER BY m.message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + } + + @Transactional(readOnly = true) + @Override + public List 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 users u WHERE u.banned = 1 and u.id = m.user_id) " + + " 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 getSearch(final String search, final int before) { + List mids = searchService.searchInAllMessages(search, before); + + if (!mids.isEmpty()) + return getNamedParameterJdbcTemplate().queryForList( + "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy > 0 ORDER BY message_id DESC LIMIT 20", + new MapSqlParameterSource("ids", mids), + Integer.class); + return mids; + } + + @Transactional(readOnly = true) + @Override + public List getUserBlog(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 ORDER BY message_id DESC LIMIT 20", + sqlParameterSource, + Integer.class); + } + + @Transactional(readOnly = true) + @Override + public List 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); + + 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 getUserBlogAtDay(final int uid, final int privacy, final int daysback) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("privacy", privacy) + .addValue("daysback", daysback); + + 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 getUserBlogWithRecommendations(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 " + + "(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 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 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 getUserSearch(final int UID, final String search, final int privacy, final int before) { + List mids = searchService.searchByStringAndUser(search, UID, before); + + if (!mids.isEmpty()) { + return getNamedParameterJdbcTemplate().queryForList( + "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy >= :privacy ORDER BY message_id DESC", + new MapSqlParameterSource("ids", mids) + .addValue("privacy", privacy), + Integer.class); + } + return mids; + } + + @Transactional(readOnly = true) + @Override + public List getMessages(final Collection mids) { + if (CollectionUtils.isNotEmpty(mids)) { + return getNamedParameterJdbcTemplate().query( + "SELECT messages.message_id, 0 as rid, 0 as replyto, " + + "messages.user_id,users.nick, 0 as banned, " + + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + + "messages.ts," + + "messages.readonly,messages.privacy,messages.replies," + + "messages.attach,messages.place_id,messages.lat," + + "messages.lon,messages.likes,messages.hidden," + + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q " + + "FROM (messages INNER JOIN messages_txt " + + "ON messages.message_id=messages_txt.message_id) " + + "INNER JOIN users ON messages.user_id=users.id " + + "WHERE messages.message_id IN (:ids) ORDER BY messages.message_id DESC", + new MapSqlParameterSource("ids", mids), + new MessageMapper()); + } + return Collections.emptyList(); + } + + @Transactional(readOnly = true) + @Override + public List getReplies(final int mid) { + return getNamedParameterJdbcTemplate().query( + "SELECT replies.message_id as mid, replies.reply_id, replies.replyto, " + + "replies.user_id, users.nick, users.banned, " + + "TIMESTAMPDIFF(MINUTE, replies.ts, NOW()), replies.ts, " + + "0 as readonly, 0 as privacy, 0 as replies, " + + "replies.attach, 0 as place_id, 0 as lat, " + + "0 as lon, 0 as likes, 0 as hidden, " + + "NULL as tags, NULL as repliesby, replies.txt, " + + "IFNULL(qw.txt, t.txt) as q " + + "FROM replies INNER JOIN users " + + "ON replies.user_id = users.id " + + "LEFT JOIN replies qw ON replies.message_id = qw.message_id and replies.replyto = qw.reply_id " + + "LEFT JOIN messages_txt t on replies.message_id = t.message_id " + + "WHERE replies.message_id = :mid ORDER BY replies.reply_id ASC", + new MapSqlParameterSource("mid", mid), + new MessageMapper()); + } + + @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(readOnly = true) + @Override + public List 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 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 " + + "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)); + return reply; + }, -hours); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java new file mode 100644 index 00000000..e6a8617d --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -0,0 +1,160 @@ +package com.juick.service; + +import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; +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 javax.inject.Inject; +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesService { + + @Inject + public PMQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Transactional + @Override + public boolean createPM(final int uidFrom, final int uid_to, final String body) { + boolean ret = getJdbcTemplate().update( + "INSERT INTO pm(user_id, user_id_to, txt) VALUES (?, ?, ?)", + uidFrom, uid_to, body) > 0; + + if (ret) { + getJdbcTemplate().update( + "INSERT INTO pm_streams(user_id, user_id_to, lastmessage, unread) VALUES (?, ?, NOW(), 1) " + + "ON DUPLICATE KEY UPDATE lastmessage = NOW(), unread = unread + 1", + uidFrom, uid_to); + } + return ret; + } + + @Transactional + @Override + public boolean addPMinRoster(final int uid, final String jid) { + return getJdbcTemplate().update( + "INSERT INTO pm_inroster(user_id, jid) VALUES (?, ?)", uid, jid) > 0; + } + + @Transactional + @Override + public boolean removePMinRoster(final int uid, final String jid) { + return getJdbcTemplate().update( + "DELETE FROM pm_inroster WHERE user_id = ? AND jid = ?", uid, jid) > 0; + } + + @Transactional + @Override + public boolean havePMinRoster(final int uid, final String jid) { + List res = getJdbcTemplate().queryForList( + "SELECT 1 FROM pm_inroster WHERE user_id = ? AND jid = ?", + Integer.class, + uid, jid); + return res.size() > 0; + } + + @Transactional(readOnly = true) + @Override + public String getLastView(final int uidFrom, final int uidTo) { + List list = getJdbcTemplate().queryForList( + "SELECT lastview FROM pm_streams WHERE user_id = ? AND user_id_to = ?", + String.class, + uidFrom, uidTo); + + return list.isEmpty() ? + null : list.get(0); + } + + @Transactional(readOnly = true) + @Override + public List getPMLastConversationsUsers(final int uid, final int cnt) { + return getJdbcTemplate().query( + "SELECT pm_streams.user_id, users.nick, pm_streams.unread FROM pm_streams " + + "INNER JOIN users ON users.id = pm_streams.user_id " + + "WHERE pm_streams.user_id_to=? " + + "ORDER BY pm_streams.unread DESC, pm_streams.lastmessage DESC LIMIT ?", + (rs, rowNum) -> { + com.juick.User u = new com.juick.User(); + u.setUid(rs.getInt(1)); + u.setName(rs.getString(2)); + u.setUnreadCount(rs.getInt(3)); + return u; + }, + uid, cnt); + } + + @Transactional + @Override + public List getPMMessages(final int uid, final int uidTo) { + SqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("uidTo", uidTo); + + List msgs = getNamedParameterJdbcTemplate().query( + "SELECT user_id, txt,ts FROM pm 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); + msg.setUser(new User()); + msg.getUser().setUid(uuid); + msg.setText(rs.getString(2)); + msg.setDate(rs.getTimestamp(3)); + return msg; + }); + + getNamedParameterJdbcTemplate().update( + "UPDATE pm_streams SET lastview = NOW(), unread = 0 WHERE user_id_to = :uid AND user_id = :uidTo", + sqlParameterSource); + + return msgs; + } + + @Transactional(readOnly = true) + @Override + public List getLastPMInbox(final int uid) { + return getJdbcTemplate().query( + "SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts " + + "FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20", + (rs, num) -> { + com.juick.Message msg = new com.juick.Message(); + msg.setUser(new User()); + msg.getUser().setUid(rs.getInt(1)); + msg.getUser().setName(rs.getString(2)); + msg.setText(rs.getString(3)); + msg.TimeAgo = rs.getInt(4); + msg.setDate(rs.getTimestamp(5)); + return msg; + }, + uid); + } + + @Transactional(readOnly = true) + @Override + public List getLastPMSent(final int uid) { + return getJdbcTemplate().query( + "SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW())," + + "pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id " + + "WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20", + (rs, num) -> { + com.juick.Message msg = new com.juick.Message(); + msg.setUser(new User()); + msg.getUser().setUid(rs.getInt(1)); + msg.getUser().setName(rs.getString(2)); + msg.setText(rs.getString(3)); + msg.TimeAgo = rs.getInt(4); + msg.setDate(rs.getTimestamp(5)); + return msg; + }, + uid); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java new file mode 100644 index 00000000..4f19c70f --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java @@ -0,0 +1,54 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.User; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +@Transactional +public class PrivacyQueriesServiceImpl extends BaseJdbcService implements PrivacyQueriesService { + + @Inject + public PrivacyQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @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-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java new file mode 100644 index 00000000..73f98abf --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -0,0 +1,126 @@ +package com.juick.service; + +import org.apache.commons.collections4.CollectionUtils; +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 { + + @Inject + public PushQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Transactional(readOnly = true) + @Override + public List getGCMRegID(final int uid) { + return getJdbcTemplate().queryForList( + "SELECT regid FROM android WHERE user_id=?", + String.class, + uid); + } + + @Transactional(readOnly = true) + @Override + public List getGCMTokens(final Collection 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 getMPNSURL(final int uid) { + return getJdbcTemplate().queryForList( + "SELECT url FROM winphone WHERE user_id=?", + String.class, + uid); + } + + @Transactional(readOnly = true) + @Override + public List getMPNSTokens(final Collection 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 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 getAPNSTokens(final Collection 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) { + return getJdbcTemplate().update("INSERT IGNORE INTO ios(user_id,token) VALUES (?, ?)", + uid, token) > 0; + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/ShowQueriesServiceImpl.java new file mode 100644 index 00000000..2362e948 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/ShowQueriesServiceImpl.java @@ -0,0 +1,52 @@ +package com.juick.service; + +import com.juick.User; +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.Collections; +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +@Transactional(readOnly = true) +public class ShowQueriesServiceImpl extends BaseJdbcService implements ShowQueriesService { + + @Inject + public ShowQueriesServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Override + public List 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 getTopUsers() { + return getJdbcTemplate().queryForList( + "SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " + + "FROM (subscr_users INNER JOIN users ON subscr_users.user_id=users.id) " + + "INNER JOIN useroptions ON users.id=useroptions.user_id " + + "WHERE useroptions.privacy_view>0 AND users.lastmessage > UNIX_TIMESTAMP() - 259200 " + + "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10", + String.class); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java new file mode 100644 index 00000000..d1eef338 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -0,0 +1,219 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.User; +import com.juick.server.helpers.NotifyOpts; +import org.apache.commons.lang3.StringUtils; +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 org.springframework.util.Assert; + +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 { + private final UserService userService; + private final MessagesService messagesService; + private final TagService tagService; + + @Inject + public SubscriptionServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, + MessagesService messagesService, TagService tagService) { + super(jdbcTemplate, null); + + Assert.notNull(userService, "UserService must be initialized"); + this.userService = userService; + + Assert.notNull(messagesService,"MessagesService must be initialized"); + this.messagesService = messagesService; + + Assert.notNull(tagService, "TagService must be initialized"); + this.tagService = tagService; + } + + @Transactional(readOnly = true) + @Override + public List getJIDSubscribedToUser(final int uid, final boolean friendsonly) { + return getNamedParameterJdbcTemplate().queryForList( + "SELECT jids.jid FROM subscr_users INNER JOIN jids " + + "ON (subscr_users.user_id = :uid AND subscr_users.suser_id = jids.user_id) WHERE jids.active = 1 " + + (friendsonly ? + " AND EXISTS (SELECT 1 FROM wl_users w WHERE w.user_id = :uid and jids.user_id = w.wl_user_id)" : StringUtils.EMPTY), + new MapSqlParameterSource("uid", uid), + String.class); + } + + @Transactional(readOnly = true) + @Override + public List getSubscribedUsers(final int uid, final int mid) { + User author = messagesService.getMessageAuthor(mid); + + List userids = userService.getUserReaders(uid); + List tags = tagService.getMessageTagsIDs(mid); + List tagsStr = tagService.getMessageTags(mid).stream().map(t -> t.getTag().getName()).collect(Collectors.toList()); + + Set set = new HashSet<>(); + set.addAll( + userids.stream() + .map(User::getUid).filter(u -> Collections.disjoint(tagService.getUserBLTags(u), tagsStr)) + .collect(Collectors.toList())); + + + if (!tags.isEmpty()) { + List 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); + } + + @Transactional(readOnly = true) + @Override + public List getUsersSubscribedToComments(final int mid, final int ignore_uid) { + List userids = getJdbcTemplate().queryForList( + "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?", + Integer.class, + mid, ignore_uid); + + if (!userids.isEmpty()) + return userService.getUsersByID(userids); + + return Collections.emptyList(); + } + + @Transactional(readOnly = true) + @Override + public List getUsersSubscribedToUserRecommendations(final int uid, final int mid, final int muid) { + List tags = tagService.getMessageTagsIDs(mid); + + String query = "SELECT s.suser_id FROM subscr_users s WHERE s.user_id = :uid " + + " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.bl_user_id = :muid and b.user_id = s.user_id) " + + " AND NOT EXISTS (SELECT 1 FROM subscr_users s1 WHERE s1.user_id = :muid AND s.user_id = s1.suser_id) " + + " AND NOT EXISTS (SELECT 1 FROM subscr_messages sm WHERE sm.message_id = :mid AND s.user_id = sm.suser_id) " + + " AND NOT EXISTS (SELECT 1 FROM favorites WHERE favorites.message_id = :mid AND favorites.user_id = s.user_id) " + + " AND s.user_id NOT IN (SELECT s2.suser_id FROM subscr_users s2 " + + " INNER JOIN favorites f ON (f.message_id = :mid AND s2.user_id = f.user_id AND f.user_id != :uid))"; + + MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("uid", uid) + .addValue("muid", muid) + .addValue("mid", mid); + + if (!tags.isEmpty()) { + sqlParameterSource.addValue("ids", tags); + query += " AND NOT EXISTS (SELECT 1 FROM subscr_tags st WHERE st.tag_id IN (:ids) AND s.user_id = st.suser_id) " + + " AND NOT EXISTS (SELECT 1 FROM bl_tags b WHERE b.tag_id IN (:ids) AND s.user_id = b.user_id)"; + } + + List userids = getNamedParameterJdbcTemplate().queryForList( + query, sqlParameterSource, Integer.class); + + return userService.getUsersByID(userids); + } + + @Transactional + @Override + public boolean subscribeMessage(final int mid, final int vuid) { + return getJdbcTemplate().update( + "INSERT IGNORE INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1; + } + + @Transactional + @Override + public boolean unSubscribeMessage(final int mid, final int vuid) { + return getJdbcTemplate().update( + "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", mid, vuid) > 0; + } + + @Transactional + @Override + public boolean subscribeUser(final User user, final User toUser) { + return getJdbcTemplate().update( + "INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1; + } + + @Transactional + @Override + public boolean unSubscribeUser(final User user, final User fromUser) { + return getJdbcTemplate().update( + "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", user.getUid(), fromUser.getUid()) > 0; + } + + @Transactional + @Override + public boolean subscribeTag(final User user, final Tag toTag) { + return getJdbcTemplate().update( + "INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID, user.getUid()) == 1; + } + + @Transactional + @Override + public boolean unSubscribeTag(final User user, final Tag toTag) { + return getJdbcTemplate().update( + "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0; + } + + @Transactional(readOnly = true) + @Override + public List getSubscribedTags(User user) { + return getJdbcTemplate().queryForList("SELECT tags.name FROM subscr_tags INNER JOIN tags USING(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 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-jdbc/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java new file mode 100644 index 00000000..b5bd0ae8 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java @@ -0,0 +1,260 @@ +package com.juick.service; + +import com.juick.Tag; +import com.juick.server.helpers.TagStats; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.jdbc.core.JdbcTemplate; +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 org.springframework.util.Assert; + +import javax.inject.Inject; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class TagServiceImpl extends BaseJdbcService implements TagService { + private static final Pattern TAGS_PATTERN1 = Pattern.compile("^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$"); + private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"); + private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); + + private final MessagesService messagesService; + + @Inject + public TagServiceImpl(JdbcTemplate jdbcTemplate, MessagesService messagesService) { + super(jdbcTemplate, null); + Assert.notNull(messagesService, "MessagesService must be initialized"); + this.messagesService = messagesService; + } + + @Transactional(readOnly = true) + @Override + public com.juick.Tag getTag(final int tid) { + List 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 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 getTags(final String[] tags, final boolean autoCreate) { + if (ArrayUtils.isEmpty(tags)) + return Collections.emptyList(); + + List ret = new ArrayList<>(); + + for (String tag : tags) { + if (!tag.isEmpty()) { + Tag t = getTag(tag, autoCreate); + + if (t != null) + ret.add(t); + } + } + + return ret.stream().distinct().collect(Collectors.toList()); + } + + @Transactional(readOnly = true) + @Override + public boolean getTagNoIndex(final int tagId) { + List 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 { + + @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 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 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 getPopularTags() { + return getJdbcTemplate().queryForList( + "SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class) + .stream() + .collect(Collectors.toList()); + } + + @Transactional(readOnly = true) + @Override + public List 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 updateTags(final int mid, final Collection newTags) { + List currentTags = getMessageTags(mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); + + if (CollectionUtils.isEmpty(newTags)) + return currentTags; + + List idsForDelete = newTags.stream() + .filter(currentTags::contains) + .map(tag -> tag.TID) + .collect(Collectors.toList()); + + 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)); + + return getMessageTags(mid).stream() + .map(TagStats::getTag).collect(Collectors.toList()); + } + + @Override + public List fromString(final String txt, final boolean tagsOnly) { + Pattern tagsPattern = tagsOnly ? TAGS_PATTERN1 : TAGS_PATTERN2; + + if (tagsPattern.matcher(txt).matches()) { + Matcher tagMatcher = TAG_PATTERN.matcher(txt); + + List tags = new ArrayList<>(); + + // TODO: process readonly, private, friends, public + while (tagMatcher.find()) { + for (int i = 1; i <= tagMatcher.groupCount(); i++) + tags.add(getTag(tagMatcher.group(i), true)); + } + return tags; + } + return Collections.emptyList(); + } + + @Transactional(readOnly = true) + @Override + public List 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 getMessageTagsIDs(final int mid) { + return getJdbcTemplate().queryForList( + "SELECT tag_id FROM messages_tags WHERE message_id = ?", + Integer.class, mid); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java new file mode 100644 index 00000000..a698e2e8 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/TelegramServiceImpl.java @@ -0,0 +1,73 @@ +package com.juick.service; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; +import java.util.List; +import java.util.UUID; + +/** + * Created by vt on 24/11/2016. + */ +@Repository +public class TelegramServiceImpl extends BaseJdbcService implements TelegramService { + + @Inject + public TelegramServiceImpl(JdbcTemplate jdbc) { + super(jdbc, null); + } + + @Transactional + @Override + public boolean addChat(final Long id) { + return getJdbcTemplate().update("INSERT IGNORE INTO telegram_chats(chat_id) VALUES(?)", id) > 0; + } + + @Transactional(readOnly = true) + @Override + public List getChats() { + return getJdbcTemplate().queryForList("SELECT chat_id FROM telegram_chats", Long.class); + } + + @Transactional(readOnly = true) + @Override + public int getUser(final long tgId) { + List 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 getSubscribers(final int uid) { + return getJdbcTemplate().queryForList("" + + "SELECT tg_id FROM telegram INNER JOIN subscr_users " + + "ON (subscr_users.user_id=? AND telegram.user_id=subscr_users.suser_id)", Long.class, uid); + } + + @Transactional(readOnly = true) + @Override + public List getSubscribersToComments(final int mid, final int ignore_uid) { + return getJdbcTemplate().queryForList( + "SELECT tg_id FROM telegram INNER JOIN subscr_messages " + + "ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java new file mode 100644 index 00000000..4013be59 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/UserServiceImpl.java @@ -0,0 +1,704 @@ +package com.juick.service; + +import com.juick.User; +import com.juick.server.helpers.AnonymousUser; +import com.juick.server.helpers.Auth; +import com.juick.server.helpers.EmailOpts; +import com.juick.server.helpers.UserInfo; +import com.juick.server.util.HashUtils; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.jdbc.core.JdbcTemplate; +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.inject.Inject; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.*; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class UserServiceImpl extends BaseJdbcService implements UserService { + + private class UserMapper implements RowMapper { + @Override + public User mapRow(ResultSet rs, int rowNum) throws SQLException { + User user = new User(); + + user.setUid(rs.getInt(1)); + user.setName(rs.getString(2)); + user.setBanned(rs.getBoolean(3)); + user.setLang(rs.getString(4)); + + return user; + } + } + + @Inject + public UserServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Transactional + @Override + public String getSignUpHashByJID(final String jid) { + List 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 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 getUserByUID(final int uid) { + List list = getJdbcTemplate().query( + "SELECT id, nick, banned, lang FROM users WHERE id = ?", new UserMapper(), uid); + + return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); + } + + @Transactional(readOnly = true) + @Override + public User getUserByName(final String username) { + if (StringUtils.isNotBlank(username)) { + List list = getJdbcTemplate().query( + "SELECT id, nick, banned, lang FROM users WHERE nick = ?", new UserMapper(), username); + + if (!list.isEmpty()) + return list.get(0); + } + // TODO: @NonNullable ? + return AnonymousUser.INSTANCE; + } + + @Override + // No need marks with @Transactional annotation + public User getFullyUserByName(final String username) { + if (StringUtils.isNotBlank(username)) { + List list = getFullyUsersByNames(Collections.singletonList(username)); + if (!list.isEmpty()) + return list.get(0); + } + return null; + } + + @Override + @Transactional(readOnly = true) + public User getUserByEmail(String email) { + if (StringUtils.isNotBlank(email)) { + List list = getJdbcTemplate().query( + "SELECT id, nick, banned, lang FROM users WHERE id = (SELECT user_id FROM emails WHERE email = ?)", + new UserMapper(), + email); + + if (!list.isEmpty()) + return list.get(0); + } + return AnonymousUser.INSTANCE; + } + + @Transactional(readOnly = true) + @Override + public List getFullyUsersByNames(final Collection usernames) { + if (CollectionUtils.isEmpty(usernames)) + return Collections.emptyList(); + + return getNamedParameterJdbcTemplate().query( + "SELECT id, nick, passw, lang, banned FROM users WHERE nick in (:names)", + new MapSqlParameterSource("names", usernames), + (rs, rowNum) -> { + User user = new User(); + + user.setUid(rs.getInt(1)); + user.setName(rs.getString(2)); + user.setCredentials(rs.getString(3)); + user.setLang(rs.getString(4)); + user.setBanned(rs.getBoolean(5)); + + return user; + }); + } + + @Transactional(readOnly = true) + @Override + public User getUserByJID(final String jid) { + User result = null; + + if (StringUtils.isNotBlank(jid)) { + List list = getJdbcTemplate().query( + "SELECT id, nick, banned, lang 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 getUsersByName(final Collection unames) { + if (CollectionUtils.isEmpty(unames)) + return Collections.emptyList(); + + return getNamedParameterJdbcTemplate().query( + "SELECT id, nick, banned, lang FROM users WHERE nick IN (:unames)", + new MapSqlParameterSource("unames", unames), + new UserMapper()); + } + + @Transactional(readOnly = true) + @Override + public List getUsersByID(final Collection uids) { + if (CollectionUtils.isEmpty(uids)) + return Collections.emptyList(); + + return getNamedParameterJdbcTemplate().query( + "SELECT id, nick, banned, lang FROM users WHERE id IN (:ids)", + new MapSqlParameterSource("ids", uids), + new UserMapper()); + } + + @Transactional(readOnly = true) + @Override + public List getUsersByJID(final Collection jids) { + if (CollectionUtils.isEmpty(jids)) + return Collections.emptyList(); + + return getNamedParameterJdbcTemplate().query( + "SELECT users.id, users.nick, jids.jid FROM users " + + " INNER JOIN jids ON jids.user_id = users.id " + + " WHERE jids.jid IN (:jids)", + new MapSqlParameterSource("jids", jids), + (rs, rowNum) -> { + com.juick.User user = new com.juick.User(); + user.setUid(rs.getInt(1)); + user.setName(rs.getString(2)); + user.setJid(rs.getString(3)); + return user; + }); + } + + @Transactional(readOnly = true) + @Override + public List 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 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 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 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 list = getJdbcTemplate().query( + "SELECT logins.user_id, users.nick, users.banned, users.lang 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 list = getJdbcTemplate().queryForList( + "SELECT hash FROM logins WHERE user_id = ?", String.class, uid); + + if (list.isEmpty()) { + String hash = HashUtils.generateHash(16); + getJdbcTemplate().update("INSERT INTO logins(user_id, hash) VALUES (?, ?)", uid, hash); + return hash; + } + return list.get(0); + } + + @Override + public int getUIDByHttpAuth(String auth) { + if (auth != null && auth.length() > 8 && auth.startsWith("Basic ")) { + Base64.Decoder dec = Base64.getDecoder(); + String loginpassw[] = new String(dec.decode(auth.substring(6))).split(":", 2); + if (loginpassw.length == 2 && loginpassw[0].length() > 1 && loginpassw[0].length() < 16 && loginpassw[0].matches("[a-zA-Z0-9\\-]+") && !loginpassw[1].isEmpty()) { + return checkPassword(loginpassw[0], loginpassw[1]); + } + } + return 0; + } + + @Transactional(readOnly = true) + @Override + public int checkPassword(final String username, final String password) { + if (StringUtils.isNotBlank(username)) { + List list = getJdbcTemplate().query( + "SELECT id, nick, banned, passw FROM users WHERE nick = ?", + (rs, rowNum) -> { + User user = new User(); + user.setUid(rs.getInt(1)); + user.setName(rs.getString(2)); + user.setBanned(rs.getBoolean(3)); + user.setCredentials(rs.getString(4)); + return user; + }, + 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 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 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 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 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 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 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 List checkBL(final int visitor, final Collection 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 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 getUserRead(final int uid) { + return getJdbcTemplate().queryForList( + "SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid); + } + + @Transactional(readOnly = true) + @Override + public List 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 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 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 List 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) { + if (getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + + "VALUES (?,?,?,?)" + + " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?", + user.getUid(), accessToken, accessTokenSecret, screenName, accessToken, accessTokenSecret, screenName) > 0) { + return getJdbcTemplate().update("INSERT INTO subscr_users(user_id,suser_id,jid) " + + "VALUES (?,1741,'juick\\@twitter.juick.com')", user.getUid()) > 0; + } + return false; + + } + + @Transactional(readOnly = true) + @Override + public int getStatsIRead(final int uid) { + List list = getJdbcTemplate().queryForList( + "SELECT COUNT(*) FROM subscr_users WHERE suser_id = ?", Integer.class, uid); + return list.isEmpty() ? 0 : list.get(0); + } + + @Transactional(readOnly = true) + @Override + public int getStatsMyReaders(final int uid) { + List 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 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 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 getAllJIDs(final User user) { + return getJdbcTemplate().queryForList( + "SELECT jid FROM jids WHERE user_id=?", String.class, user.getUid()); + } + + @Transactional(readOnly = true) + @Override + public List 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 getEmails(final User user) { + return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUid()); + } + + @Transactional(readOnly = true) + @Override + public EmailOpts getEmailOpts(final User user) { + List list = getJdbcTemplate().query( + "SELECT email,subscr_hour FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", + (rs, num) -> new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUid()); + return list.isEmpty() ? new EmailOpts("", 0) : list.get(0); + } + + @Transactional(readOnly = true) + @Override + public String getEmailHash(final User user) { + List 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 getJdbcTemplate().update("DELETE FROM jids WHERE user_id=? AND jid=?", uid, jid) > 0; + } + + @Transactional + @Override + public boolean unauthJID(int uid, String jid) { + return getJdbcTemplate() + .update("DELETE FROM auth WHERE user_id=? AND protocol='xmpp' AND account=?", uid, jid) > 0; + } + + @Transactional(readOnly = true) + @Override + public List getActiveJIDs() { + return getJdbcTemplate().queryForList("SELECT jid FROM jids WHERE active=1 AND loginhash IS NULL", String.class); + } +} diff --git a/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java new file mode 100644 index 00000000..aa903545 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java @@ -0,0 +1,85 @@ +package com.juick.service.search; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.util.Assert; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; + +/** + * Created by aalexeev on 11/18/16. + */ + +/* Note + * Don't use any spring's component annotation (such as @Repository, @Service, @Component, etc). + * This class directly used by spring's search configuration class + */ +public class SphinxSearchServiceImpl implements SearchService { + private static final int DEFAULT_MAX_RESULT = 25; + + private final NamedParameterJdbcTemplate namedParameterSearchJdbcTemplate; + + private int maxResult = DEFAULT_MAX_RESULT; + + + public SphinxSearchServiceImpl(JdbcTemplate searchJdbcTemplate) { + Assert.notNull(searchJdbcTemplate, "JdbcTemplate must be initialized"); + this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchJdbcTemplate); + } + + public SphinxSearchServiceImpl(DataSource searchDataSource) { + Assert.notNull(searchDataSource, "DataSource must be initialized"); + this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchDataSource); + } + + @Override + public List searchInAllMessages(final String searchString, final int messageIdBefore) { + if (StringUtils.isBlank(searchString)) + return Collections.emptyList(); + + MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("search", searchString) + .addValue("before", messageIdBefore) + .addValue("limit", maxResult); + + return namedParameterSearchJdbcTemplate.queryForList( + "SELECT id AS message_id FROM messages WHERE MATCH(:search) " + + (messageIdBefore > 0 ? + " AND id < :before " : StringUtils.EMPTY) + + " ORDER BY id DESC LIMIT :limit", + sqlParameterSource, + Integer.class); + } + + @Override + public List searchByStringAndUser(final String searchString, final int userId, int messageIdBefore) { + if (StringUtils.isBlank(searchString)) + return Collections.emptyList(); + + MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() + .addValue("search", searchString) + .addValue("userId", userId) + .addValue("before", messageIdBefore) + .addValue("limit", maxResult); + + return namedParameterSearchJdbcTemplate.queryForList( + "SELECT id AS message_id FROM messages WHERE user_id = :userId AND MATCH(:search) " + + (messageIdBefore > 0 ? + " AND id < :before " : StringUtils.EMPTY) + + " ORDER BY id DESC LIMIT :limit", + sqlParameterSource, + 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-jdbc/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java b/juick-server-jdbc/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java new file mode 100644 index 00000000..3da660b2 --- /dev/null +++ b/juick-server-jdbc/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java @@ -0,0 +1,39 @@ +package rocks.xmpp.core.session.debug; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import rocks.xmpp.core.session.XmppSession; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Created by vitalyster on 17.11.2016. + */ +public class LogbackDebugger implements XmppDebugger { + private static final Logger logger = LoggerFactory.getLogger(LogbackDebugger.class); + + @Override + public void initialize(XmppSession xmppSession) { + } + + @Override + public void writeStanza(String s, Object o) { + logger.info("OUT: {}", s); + } + + @Override + public void readStanza(String s, Object o) { + logger.info("IN: {}", s); + } + + @Override + public OutputStream createOutputStream(OutputStream outputStream) { + return outputStream; + } + + @Override + public InputStream createInputStream(InputStream inputStream) { + return inputStream; + } +} diff --git a/juick-server-jdbc/src/test/java/com/juick/configuration/MockDataConfiguration.java b/juick-server-jdbc/src/test/java/com/juick/configuration/MockDataConfiguration.java new file mode 100644 index 00000000..bd2f3f50 --- /dev/null +++ b/juick-server-jdbc/src/test/java/com/juick/configuration/MockDataConfiguration.java @@ -0,0 +1,47 @@ +package com.juick.configuration; + +import com.juick.service.*; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; + +/** + * Created by vitalyster on 12.01.2017. + */ +public class MockDataConfiguration { + @Bean + MessagesService messagesService() { + return Mockito.mock(MessagesService.class); + } + @Bean + UserService userService() { + return Mockito.mock(UserService.class); + } + @Bean + TagService tagService() { + return Mockito.mock(TagService.class); + } + @Bean + PushQueriesService pushQueriesService() { + return Mockito.mock(PushQueriesService.class); + } + @Bean + SubscriptionService subscriptionService() { + return Mockito.mock(SubscriptionService.class); + } + @Bean + PMQueriesService pmQueriesService() { + return Mockito.mock(PMQueriesService.class); + } + @Bean + TelegramService telegramService() { + return Mockito.mock(TelegramService.class); + } + @Bean + CrosspostService crosspostService() { + return Mockito.mock(CrosspostService.class); + } + @Bean + EmailService emailService() { + return Mockito.mock(EmailService.class); + } +} diff --git a/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java new file mode 100644 index 00000000..4a74349f --- /dev/null +++ b/juick-server-jdbc/src/test/java/com/juick/configuration/RepositoryConfiguration.java @@ -0,0 +1,65 @@ +package com.juick.configuration; + +import ch.vorburger.exec.ManagedProcessException; +import ch.vorburger.mariadb4j.DB; +import com.juick.service.search.SearchService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +import java.util.Collections; +import java.util.List; + +/** + * Created by aalexeev on 11/25/16. + */ +@Configuration +@ComponentScan(basePackages = "com.juick.service") +public class RepositoryConfiguration { + + @Bean(destroyMethod = "stop") + DB db() throws ManagedProcessException { + DB db = DB.newEmbeddedDB(33306); + + db.start(); + db.createDB("juick"); + db.source("schema.sql"); + + return db; + } + + @Bean + public DriverManagerDataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("net.sf.log4jdbc.DriverSpy"); + dataSource.setUrl("jdbc:log4jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); + + return dataSource; + } + + @Bean + public JdbcTemplate jdbcTemplate() { + return new JdbcTemplate(dataSource()); + } + + @Bean + public SearchService emptySearchService() { + return new SearchService() { + @Override + public void setMaxResult(int maxResult) { + } + + @Override + public List searchInAllMessages(String searchString, int messageIdBefore) { + return Collections.emptyList(); + } + + @Override + public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { + return Collections.emptyList(); + } + }; + } +} diff --git a/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java new file mode 100644 index 00000000..5f2c2b05 --- /dev/null +++ b/juick-server-jdbc/src/test/java/com/juick/service/MessageServiceTest.java @@ -0,0 +1,23 @@ +package com.juick.service; + +import com.juick.configuration.RepositoryConfiguration; +import org.junit.Test; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/25/16. + */ +@ContextConfiguration(classes = RepositoryConfiguration.class) +public class MessageServiceTest extends AbstractJUnit4SpringContextTests { + @Inject + private MessagesService messagesService; + + + @Test + public void getMyFeed() { + messagesService.getMyFeed(1, 1000000); + } +} diff --git a/juick-server-jdbc/src/test/java/com/juick/tests/util/MockUtils.java b/juick-server-jdbc/src/test/java/com/juick/tests/util/MockUtils.java new file mode 100644 index 00000000..5344dd23 --- /dev/null +++ b/juick-server-jdbc/src/test/java/com/juick/tests/util/MockUtils.java @@ -0,0 +1,34 @@ +package com.juick.tests.util; + +import com.juick.Message; +import com.juick.User; +import org.apache.commons.text.RandomStringGenerator; + +import java.util.Date; + +/** + * Created by vitalyster on 12.01.2017. + */ +public class MockUtils { + final static RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build(); + public static Message mockMessage(Integer mid, final User user, final String messageText) { + Message msg = new Message(); + + msg.setMid(mid); + msg.setUser(user); + msg.setText(messageText == null ? generator.generate(24) : messageText); + msg.setDate(new Date()); + return msg; + } + + public static User mockUser(final int uid, final String name, final String password) { + User user = new User(); + + user.setName(name); + user.setUid(uid); + user.setCredentials(password); + user.setBanned(false); + + return user; + } +} diff --git a/juick-server-web/build.gradle b/juick-server-web/build.gradle new file mode 100644 index 00000000..fde15afe --- /dev/null +++ b/juick-server-web/build.gradle @@ -0,0 +1,76 @@ +apply plugin: 'java' +apply plugin: 'war' + +sourceCompatibility = 1.8 + +dependencies { + compile project(':juick-server-core') + + compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" + compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${rootProject.jacksonVersion}" + + providedCompile "ch.qos.logback:logback-classic:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-core:${rootProject.logbackVersion}" + providedCompile "ch.qos.logback:logback-access:${rootProject.logbackVersion}" + + providedCompile "org.slf4j:slf4j-api:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jcl-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:log4j-over-slf4j:${rootProject.slf4jVersion}" + providedCompile "org.slf4j:jul-to-slf4j:${rootProject.slf4jVersion}" + + compile "org.apache.httpcomponents:httpclient:4.5.3" + providedCompile "org.apache.commons:commons-lang3:3.6" + providedCompile "org.apache.commons:commons-collections4:4.1" + providedCompile "org.apache.commons:commons-text:1.1" + providedCompile "commons-io:commons-io:2.5" + providedCompile "commons-codec:commons-codec:1.10" + + compile 'com.github.ben-manes.caffeine:caffeine:2.5.2' + + compile "org.springframework:spring-context:${rootProject.springFrameworkVersion}" + compile "org.springframework:spring-jdbc:${rootProject.springFrameworkVersion}" + + compile "org.springframework.security:spring-security-web:${rootProject.springSecurityVersion}" + compile "org.springframework.security:spring-security-config:${rootProject.springSecurityVersion}" + + providedCompile "org.apache.commons:commons-dbcp2:2.1.1" + compile "com.googlecode.log4jdbc:log4jdbc:1.2" + compile "javax.inject:javax.inject:1" + + compile "rocks.xmpp:xmpp-core-client:0.7.4" + compile "rocks.xmpp:xmpp-extensions-client:0.7.4" + + compile 'org.imgscalr:imgscalr-lib:4.2' + + providedCompile "javax.servlet:javax.servlet-api:3.1.0" + + providedRuntime "commons-fileupload:commons-fileupload:1.3.3" + + testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" + testCompile "junit:junit:${rootProject.junitVersion}" + testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" + testCompile "org.mockito:mockito-core:${rootProject.mockitoVersion}" + testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}" + testCompile "org.springframework.security:spring-security-test:${rootProject.springSecurityVersion}" + + testRuntime "mysql:mysql-connector-java:5.1.40" +} + +compileJava.options.encoding = 'UTF-8' + +configurations { + all*.exclude module: 'commons-logging' +} + +configurations { + testArtifacts.extendsFrom testRuntime +} +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output +} +artifacts { + testArtifacts testJar +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/HttpBadRequestException.java b/juick-server-web/src/main/java/com/juick/server/util/HttpBadRequestException.java new file mode 100644 index 00000000..1ba1aecb --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/HttpBadRequestException.java @@ -0,0 +1,15 @@ +package com.juick.server.util; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by vt on 24/11/2016. + */ +@ResponseStatus(value = HttpStatus.BAD_REQUEST) +public class HttpBadRequestException extends RuntimeException { + public HttpBadRequestException() { + super(StringUtils.EMPTY, null, false, false); + } +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/HttpForbiddenException.java b/juick-server-web/src/main/java/com/juick/server/util/HttpForbiddenException.java new file mode 100644 index 00000000..733453ba --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/HttpForbiddenException.java @@ -0,0 +1,16 @@ +package com.juick.server.util; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by vt on 24/11/2016. + */ +@ResponseStatus(value = HttpStatus.FORBIDDEN) +public class HttpForbiddenException extends RuntimeException { + public HttpForbiddenException() { + super(StringUtils.EMPTY, null, false, false); + } + +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/HttpNotFoundException.java b/juick-server-web/src/main/java/com/juick/server/util/HttpNotFoundException.java new file mode 100644 index 00000000..942d90e2 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/HttpNotFoundException.java @@ -0,0 +1,15 @@ +package com.juick.server.util; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by vt on 24/11/2016. + */ +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class HttpNotFoundException extends RuntimeException { + public HttpNotFoundException() { + super(StringUtils.EMPTY, null, false, false); + } +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/HttpUtils.java b/juick-server-web/src/main/java/com/juick/server/util/HttpUtils.java new file mode 100644 index 00000000..31a68962 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/HttpUtils.java @@ -0,0 +1,112 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.server.util; + +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.multipart.MultipartFile; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.UUID; + +/** + * + * @author Ugnich Anton + */ +public class HttpUtils { + private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); + + public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) { + if (attach !=null && !attach.isEmpty()) { + String partname = attach.getOriginalFilename(); + if (partname != null && partname.length() > 0) { + String attachmentType = partname.substring(partname.length() - 3).toLowerCase(); + if (attachmentType.equals("jpg") || attachmentType.equals("peg") || attachmentType.equals("png")) { + if (attachmentType.equals("peg")) { + attachmentType = "jpg"; + } + String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; + try { + Files.write(Paths.get(tmpDir, attachmentFName), + attach.getBytes()); + return attachmentFName; + } catch (IOException e) { + logger.warn("file receive error", e); + } + } + } + } + return StringUtils.EMPTY; + } + public static String downloadImage(URL url, String tmpDir) throws Exception { + String attachmentFName = null; + Exception ex = null; + + InputStream is = null; + FileOutputStream fos = null; + try { + URLConnection urlConn = url.openConnection(); + is = urlConn.getInputStream(); + String mime = urlConn.getContentType(); + + String attachmentType; + if (mime != null && mime.equals("image/jpeg")) { + attachmentType = "jpg"; + } else if (mime != null && mime.equals("image/png")) { + attachmentType = "png"; + } else { + throw new Exception("Wrong file type"); + } + + attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; + fos = new FileOutputStream(Paths.get(tmpDir, attachmentFName).toString()); + byte[] buffer = new byte[10240]; + int len; + while ((len = is.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + } catch (Exception e) { + ex = e; + attachmentFName = null; + } finally { + try { + if (is != null) { + is.close(); + } + } finally { + if (fos != null) { + fos.close(); + } + } + } + + if (ex != null) { + throw ex; + } else { + return attachmentFName; + } + } +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java new file mode 100644 index 00000000..61677750 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java @@ -0,0 +1,66 @@ + +package com.juick.server.util; + +import org.apache.commons.io.FilenameUtils; +import org.imgscalr.Scalr; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +public class ImageUtils { + + /** + * Move attached image from temp folder to image folder. + * Create preview images in corresponding folders. + * + * @param tempFilename Name of the image file in temp folder. + * @param outputFilename Name that will be used in in image folder. + * @param tmpDir Path string for the temp folder. + * @param imgDir Path string for the image folder. + */ + public static void saveImageWithPreviews(String tempFilename, String outputFilename, String tmpDir, String imgDir) + throws IOException { + String ext = FilenameUtils.getExtension(outputFilename); + + Path outputImagePath = Paths.get(imgDir, "p", outputFilename); + Files.move(Paths.get(tmpDir, tempFilename), outputImagePath); + BufferedImage originalImage = ImageIO.read(outputImagePath.toFile()); + + int width = originalImage.getWidth(); + int height = originalImage.getHeight(); + int maxDimension = (width > height) ? width : height; + BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage; + BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage; + BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage; + ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile()); + ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile()); + ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile()); + } + + /** + * Save new avatar in all required sizes. + * + * @param tempFilename Name of the image file in temp folder. + * @param uid User id that is used to build image file names. + * @param tmpDir Path string for the temp folder. + * @param imgDir Path string for the image folder. + */ + public static void saveAvatar(String tempFilename, int uid, String tmpDir, String imgDir) + throws IOException { + String ext = FilenameUtils.getExtension(tempFilename); + String originalName = String.format("%s.%s", uid, ext); + Path originalPath = Paths.get(imgDir, "ao", originalName); + Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING); + BufferedImage originalImage = ImageIO.read(originalPath.toFile()); + + String targetExt = "png"; + String targetName = String.format("%s.%s", uid, targetExt); + ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile()); + ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile()); + } +} \ No newline at end of file diff --git a/juick-server-web/src/main/java/com/juick/server/util/UserUtils.java b/juick-server-web/src/main/java/com/juick/server/util/UserUtils.java new file mode 100644 index 00000000..eb86370e --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/UserUtils.java @@ -0,0 +1,36 @@ +package com.juick.server.util; + +import com.juick.User; +import com.juick.server.helpers.AnonymousUser; +import com.juick.service.security.entities.JuickUser; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; + +/** + * Created by aalexeev on 11/14/16. + */ +public class UserUtils { + private UserUtils() { + throw new IllegalStateException(); + } + + public static Authentication getAuthentication() { + return SecurityContextHolder.getContext().getAuthentication(); + } + + public static Object getPrincipal(final Authentication authentication) { + return authentication == null ? null : authentication.getPrincipal(); + } + + public static User getCurrentUser() { + Object principal = getPrincipal(getAuthentication()); + + if (principal instanceof JuickUser) + return ((JuickUser) principal).getUser(); + + if (principal instanceof User) + return (User) principal; + + return AnonymousUser.INSTANCE; + } +} diff --git a/juick-server-web/src/main/java/com/juick/server/util/WebUtils.java b/juick-server-web/src/main/java/com/juick/server/util/WebUtils.java new file mode 100644 index 00000000..7f50c89c --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/util/WebUtils.java @@ -0,0 +1,45 @@ +package com.juick.server.util; + +import java.util.regex.Pattern; + +/** + * Created by aalexeev on 11/28/16. + */ +public class WebUtils { + private WebUtils() { + throw new IllegalStateException(); + } + + private static final Pattern USER_NAME_PATTERN = Pattern.compile("[a-zA-Z-_\\d]{2,16}"); + + private static final Pattern POST_NUMBER_PATTERN = Pattern.compile("-?\\d+"); + + private static final Pattern JID_PATTERN = Pattern.compile("^[a-zA-Z0-9\\\\-\\\\_\\\\@\\\\.]{6,64}$"); + + + public static boolean isPostNumber(final String aString) { + return aString != null && POST_NUMBER_PATTERN.matcher(aString).matches(); + } + + public static boolean isNotPostNumber(final String aString) { + return !isPostNumber(aString); + } + + public static boolean isUserName(final String aString) { + return aString != null && USER_NAME_PATTERN.matcher(aString).matches(); + } + + public static boolean isNotUserName(final String aString) { + return !isUserName(aString); + } + + public static boolean isJid(final String aString) { + return aString != null && JID_PATTERN.matcher(aString).matches(); + } + + public static boolean isNotJid(final String aString) { + return !isJid(aString); + } + + +} diff --git a/juick-server-web/src/main/java/com/juick/service/BaseRestService.java b/juick-server-web/src/main/java/com/juick/service/BaseRestService.java new file mode 100644 index 00000000..4ccc3959 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/BaseRestService.java @@ -0,0 +1,18 @@ +package com.juick.service; + +import org.springframework.web.client.RestTemplate; + +/** + * Created by vitalyster on 15.12.2016. + */ +public abstract class BaseRestService { + private RestTemplate rest; + + public BaseRestService(RestTemplate rest) { + this.rest = rest; + } + + public RestTemplate getRest() { + return rest; + } +} diff --git a/juick-server-web/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java b/juick-server-web/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java new file mode 100644 index 00000000..86e21d01 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java @@ -0,0 +1,83 @@ +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.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()) { + Authentication authentication = new RememberMeAuthenticationToken( + hash, new JuickUser(user), 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-web/src/main/java/com/juick/service/security/JuickUserDetailsService.java b/juick-server-web/src/main/java/com/juick/service/security/JuickUserDetailsService.java new file mode 100644 index 00000000..4e645ac0 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/JuickUserDetailsService.java @@ -0,0 +1,34 @@ +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.getFullyUserByName(username); + + if (user != null) + return new JuickUser(user); + + throw new UsernameNotFoundException("The username " + username + " is not found"); + } +} diff --git a/juick-server-web/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java b/juick-server-web/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java new file mode 100644 index 00000000..b456a3d0 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/NotAuthorizedAuthenticationEntryPoint.java @@ -0,0 +1,20 @@ +package com.juick.service.security; + +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Created by vitalyster on 25.11.2016. + */ +public class NotAuthorizedAuthenticationEntryPoint implements AuthenticationEntryPoint { + @Override + public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) + throws IOException, ServletException { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } +} diff --git a/juick-server-web/src/main/java/com/juick/service/security/NullUserDetailsService.java b/juick-server-web/src/main/java/com/juick/service/security/NullUserDetailsService.java new file mode 100644 index 00000000..49e9effc --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/NullUserDetailsService.java @@ -0,0 +1,16 @@ +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-web/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java b/juick-server-web/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java new file mode 100644 index 00000000..189877fd --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java @@ -0,0 +1,113 @@ +package com.juick.service.security.deprecated; + +import com.juick.User; +import com.juick.server.util.HashUtils; +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.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", "juick.com")); + 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 = HashUtils.generateHash(16); + + 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 userOptional = userService.getUserByUID(uid); + + Assert.isTrue(userOptional.isPresent()); + + return new JuickUser(userOptional.get()); + } + + @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-web/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java b/juick-server-web/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java new file mode 100644 index 00000000..4874ebcf --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java @@ -0,0 +1,71 @@ +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.getUid() > 0) + return new JuickUser(user); + } + throw new UsernameNotFoundException("User not found by hash " + hash); + } +} diff --git a/juick-server-web/src/main/java/com/juick/service/security/entities/JuickUser.java b/juick-server-web/src/main/java/com/juick/service/security/entities/JuickUser.java new file mode 100644 index 00000000..2c409a1d --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/security/entities/JuickUser.java @@ -0,0 +1,75 @@ +package com.juick.service.security.entities; + +import com.juick.User; +import com.juick.server.helpers.AnonymousUser; +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 USER_AUTHORITY = Collections.singletonList(ROLE_USER); + public static final List 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 authorities; + + public JuickUser(com.juick.User user) { + this(user, USER_AUTHORITY); + } + + public JuickUser(com.juick.User user, Collection authorities) { + this.user = user; + this.authorities = authorities; + } + + @Override + public Collection getAuthorities() { + return authorities; + } + + @Override + public String getPassword() { + return user.getCredentials(); + } + + @Override + public String getUsername() { + return user.getName(); + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return !user.isBanned(); + } + + public User getUser() { + return user; + } +} diff --git a/juick-server/build.gradle b/juick-server/build.gradle deleted file mode 100644 index 30e31541..00000000 --- a/juick-server/build.gradle +++ /dev/null @@ -1,76 +0,0 @@ -apply plugin: 'java' -apply plugin: 'war' - -sourceCompatibility = 1.8 - -dependencies { - compile project(':juick-core') - - compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" - compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" - compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" - compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${rootProject.jacksonVersion}" - - providedCompile "ch.qos.logback:logback-classic:${rootProject.logbackVersion}" - providedCompile "ch.qos.logback:logback-core:${rootProject.logbackVersion}" - providedCompile "ch.qos.logback:logback-access:${rootProject.logbackVersion}" - - providedCompile "org.slf4j:slf4j-api:${rootProject.slf4jVersion}" - providedCompile "org.slf4j:jcl-over-slf4j:${rootProject.slf4jVersion}" - providedCompile "org.slf4j:log4j-over-slf4j:${rootProject.slf4jVersion}" - providedCompile "org.slf4j:jul-to-slf4j:${rootProject.slf4jVersion}" - - compile "org.apache.httpcomponents:httpclient:4.5.3" - providedCompile "org.apache.commons:commons-lang3:3.6" - providedCompile "org.apache.commons:commons-collections4:4.1" - providedCompile "org.apache.commons:commons-text:1.1" - providedCompile "commons-io:commons-io:2.5" - providedCompile "commons-codec:commons-codec:1.10" - - compile 'com.github.ben-manes.caffeine:caffeine:2.5.2' - - compile "org.springframework:spring-context:${rootProject.springFrameworkVersion}" - compile "org.springframework:spring-jdbc:${rootProject.springFrameworkVersion}" - - compile "org.springframework.security:spring-security-web:${rootProject.springSecurityVersion}" - compile "org.springframework.security:spring-security-config:${rootProject.springSecurityVersion}" - - providedCompile "org.apache.commons:commons-dbcp2:2.1.1" - compile "com.googlecode.log4jdbc:log4jdbc:1.2" - compile "javax.inject:javax.inject:1" - - compile "rocks.xmpp:xmpp-core-client:0.7.4" - compile "rocks.xmpp:xmpp-extensions-client:0.7.4" - - compile 'org.imgscalr:imgscalr-lib:4.2' - - providedCompile "javax.servlet:javax.servlet-api:3.1.0" - - providedRuntime "commons-fileupload:commons-fileupload:1.3.3" - - testCompile "ch.vorburger.mariaDB4j:mariaDB4j:2.2.3" - testCompile "junit:junit:${rootProject.junitVersion}" - testCompile "org.hamcrest:hamcrest-all:${rootProject.hamcrestVersion}" - testCompile "org.mockito:mockito-core:${rootProject.mockitoVersion}" - testCompile "org.springframework:spring-test:${rootProject.springFrameworkVersion}" - testCompile "org.springframework.security:spring-security-test:${rootProject.springSecurityVersion}" - - testRuntime "mysql:mysql-connector-java:5.1.40" -} - -compileJava.options.encoding = 'UTF-8' - -configurations { - all*.exclude module: 'commons-logging' -} - -configurations { - testArtifacts.extendsFrom testRuntime -} -task testJar(type: Jar) { - classifier "test" - from sourceSets.test.output -} -artifacts { - testArtifacts testJar -} diff --git a/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java b/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java deleted file mode 100644 index 4258703d..00000000 --- a/juick-server/src/main/java/com/juick/configuration/DataConfiguration.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.juick.configuration; - -import com.juick.database.MySqlUpdater; -import com.juick.service.search.SearchService; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang3.StringUtils; -import org.springframework.context.annotation.*; -import org.springframework.core.env.Environment; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.transaction.annotation.TransactionManagementConfigurer; - -import javax.annotation.Resource; -import java.util.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/11/16. - */ -@Configuration -@EnableTransactionManagement -@PropertySource(value = {"classpath:juick.conf"}) -@ComponentScan(basePackages = {"com.juick.service"}) -public class DataConfiguration implements TransactionManagementConfigurer { - @Resource - private Environment env; - - // NOTE: The close() method will be called automatically with default @Bean settings - // But Datasource interface has no close() method - @Bean - public BasicDataSource dataSource() { - BasicDataSource dataSource = new BasicDataSource(); - - dataSource.setDriverClassName(env.getProperty("datasource_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("datasource_url")); - dataSource.setUsername(env.getProperty("datasource_user", StringUtils.EMPTY)); - dataSource.setPassword(env.getProperty("datasource_password", StringUtils.EMPTY)); - - dataSource.setValidationQuery("select 1"); - - return dataSource; - } - - @Bean - public PlatformTransactionManager transactionManager() { - return new DataSourceTransactionManager(dataSource()); - } - - @Override - public PlatformTransactionManager annotationDrivenTransactionManager() { - return transactionManager(); - } - - @Bean - @DependsOn("dataSource") - public JdbcTemplate jdbcTemplate() { - return new JdbcTemplate(dataSource()); - } - - @Bean - public SearchService emptySearchService() { - return new SearchService() { - @Override - public void setMaxResult(int maxResult) { - } - - @Override - public List searchInAllMessages(String searchString, int messageIdBefore) { - return Collections.emptyList(); - } - - @Override - public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { - return Collections.emptyList(); - } - }; - } -} diff --git a/juick-server/src/main/java/com/juick/configuration/SearchConfiguration.java b/juick-server/src/main/java/com/juick/configuration/SearchConfiguration.java deleted file mode 100644 index e6b446b6..00000000 --- a/juick-server/src/main/java/com/juick/configuration/SearchConfiguration.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.juick.configuration; - -import com.juick.service.search.SearchService; -import com.juick.service.search.SphinxSearchServiceImpl; -import org.apache.commons.dbcp2.BasicDataSource; -import org.apache.commons.lang3.StringUtils; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; - -import javax.annotation.Resource; - -/** - * Created by aalexeev on 11/18/16. - */ -@Configuration -@PropertySource(value = {"classpath:juick.conf"}) -public class SearchConfiguration { - @Resource - private Environment env; - - // NOTE: The close() method will be called automatically with default @Bean settings - // But Datasource interface has no close() method - @Bean - public BasicDataSource searchDataSource() { - BasicDataSource dataSource = new BasicDataSource(); - - dataSource.setDriverClassName(env.getProperty("sphinx_driver", "com.mysql.jdbc.Driver")); - dataSource.setUrl(env.getProperty("sphinx_url")); - dataSource.setUsername(env.getProperty("sphinx_user", StringUtils.EMPTY)); - dataSource.setPassword(env.getProperty("sphinx_password", StringUtils.EMPTY)); - - return dataSource; - } - - @Bean - @DependsOn("searchDataSource") - public SearchService searchService() { - return new SphinxSearchServiceImpl(searchDataSource()); - } -} diff --git a/juick-server/src/main/java/com/juick/configuration/UpdaterConfiguration.java b/juick-server/src/main/java/com/juick/configuration/UpdaterConfiguration.java deleted file mode 100644 index b3c0424c..00000000 --- a/juick-server/src/main/java/com/juick/configuration/UpdaterConfiguration.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.juick.configuration; - -import com.juick.database.MySqlUpdater; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.transaction.PlatformTransactionManager; - -import javax.inject.Inject; - -/** - * Created by aalexeev on 12/13/16. - */ -@Configuration -public class UpdaterConfiguration { - @Inject - JdbcTemplate jdbcTemplate; - @Inject - PlatformTransactionManager transactionManager; - - @Bean - @DependsOn({"jdbcTemplate", "transactionManager"}) - public MySqlUpdater updater() { - return new MySqlUpdater( - jdbcTemplate, - transactionManager, - "update.sql"); - } -} diff --git a/juick-server/src/main/java/com/juick/database/MySqlUpdater.java b/juick-server/src/main/java/com/juick/database/MySqlUpdater.java deleted file mode 100644 index 2027d9f1..00000000 --- a/juick-server/src/main/java/com/juick/database/MySqlUpdater.java +++ /dev/null @@ -1,135 +0,0 @@ -package com.juick.database; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.support.TransactionTemplate; -import org.springframework.util.Assert; - -import javax.annotation.PostConstruct; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Supplier; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED; - -/** - * Created by aalexeev on 12/13/16. - */ -public class MySqlUpdater { - private static final Pattern UPDATE_PATTERN = Pattern.compile( - "update\\s+(version|`version`)\\s+set\\s+(version|`version`)\\s+=\\s*(\\d+)", - Pattern.CASE_INSENSITIVE); - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - private final JdbcTemplate jdbcTemplate; - private final TransactionTemplate transactionTemplate; - private final String updateSqlResource; - - - public MySqlUpdater(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager, String updateSqlResource) { - Assert.notNull(jdbcTemplate, "JdbcTemplate must be initialized"); - Assert.notNull(transactionManager, "PlatformTransactionManager must be initialized"); - Assert.notNull(updateSqlResource, "sqlResource must be initialized"); - - this.jdbcTemplate = jdbcTemplate; - this.transactionTemplate = new TransactionTemplate(transactionManager); - this.updateSqlResource = updateSqlResource; - } - - @PostConstruct - public void init() { - try ( - InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(updateSqlResource); - ) { - if (is != null) { - String content = IOUtils.toString(is, StandardCharsets.UTF_8); - if (StringUtils.isNotEmpty(content)) { - String[] sqlArray = content.split(";"); - if (sqlArray.length > 0) { - List sqlList = new ArrayList<>(sqlArray.length); - for (String sql : sqlArray) - if (!sql.isEmpty()) { - String sqlTrimmed = sql.trim(); - if (!sqlTrimmed.isEmpty()) - sqlList.add(sqlTrimmed); - } - if (!sqlList.isEmpty()) - processingSql(sqlList); - } - } - } - } catch (Exception e) { - logger.error("MySqlUpdater initialization exception", e); - } - } - - private void processingSql(final List sqls) { - long currentDbVersion = getSingleResult(this::getVersionRaw); - long actualVersion; - - List changesSql = new ArrayList<>(); - - for (String sql : sqls) { - changesSql.add(sql); - - Matcher m = UPDATE_PATTERN.matcher(sql); - if (m.matches()) { - actualVersion = Long.valueOf(m.group(3)); - - if (actualVersion > currentDbVersion) { - updateInTransaction(changesSql); - currentDbVersion = actualVersion; - } - changesSql.clear(); - } - } - } - - private void updateInTransaction(final List sqls) { - transactionTemplate.setReadOnly(false); - transactionTemplate.setPropagationBehavior(PROPAGATION_REQUIRED); - transactionTemplate.execute(status -> { - for (String sql : sqls) - jdbcTemplate.execute(sql); - return 0; - }); - } - - private T getSingleResult(Supplier supplier) { - transactionTemplate.setReadOnly(true); - transactionTemplate.setPropagationBehavior(PROPAGATION_REQUIRED); - - return transactionTemplate.execute(status -> supplier.get()); - } - - private long getVersionRaw() { - int cnt = jdbcTemplate.query( - "SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?", - rs -> { - int result = 0; - if (rs.next()) - result = rs.getInt(1); - return result; - }, - "juick", "version"); - - long version = 0l; - - if (cnt == 1) { - List list = jdbcTemplate.queryForList("select version from version", Long.class); - if (!list.isEmpty()) - version = list.get(0); - } - - return version; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/ApplicationStatus.java b/juick-server/src/main/java/com/juick/server/helpers/ApplicationStatus.java deleted file mode 100644 index 61109c47..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/ApplicationStatus.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.juick.server.helpers; - -import org.apache.commons.lang3.builder.ToStringBuilder; - -/** - * Created by vt on 03/09/16. - */ -public class ApplicationStatus { - private boolean connected; - private boolean crosspostEnabled; - - @Override - public String toString() { - return new ToStringBuilder(this) - .append("connected", connected) - .append("crosspostEnabled", crosspostEnabled) - .toString(); - } - - public boolean isConnected() { - return connected; - } - - public void setConnected(boolean connected) { - this.connected = connected; - } - - public boolean isCrosspostEnabled() { - return crosspostEnabled; - } - - public void setCrosspostEnabled(boolean crosspostEnabled) { - this.crosspostEnabled = crosspostEnabled; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/Auth.java b/juick-server/src/main/java/com/juick/server/helpers/Auth.java deleted file mode 100644 index 3e1f0bd9..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/Auth.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.juick.server.helpers; - -/** - * Created by vt on 09/02/16. - */ -public class Auth { - private String account; - private String authCode; - - public Auth(String account, String authCode) { - this.account = account; - this.authCode = authCode; - } - - public String getAccount() { - return account; - } - - public String getAuthCode() { - return authCode; - } -} \ No newline at end of file diff --git a/juick-server/src/main/java/com/juick/server/helpers/EmailOpts.java b/juick-server/src/main/java/com/juick/server/helpers/EmailOpts.java deleted file mode 100644 index 679d1a8d..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/EmailOpts.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.juick.server.helpers; - -import org.apache.commons.lang3.StringUtils; - -/** - * Created by vitalyster on 09.02.2016. - */ -public class EmailOpts { - private String email; - private String subscriptionHour; - - public EmailOpts(String email, int subscriptionHour) { - this.email = email; - this.subscriptionHour = StringUtils.leftPad(String.format("%d", subscriptionHour), 2, "0"); - } - - public String getSubscriptionHour() { - return subscriptionHour; - } - - public String getEmail() { - return email; - } -} \ No newline at end of file diff --git a/juick-server/src/main/java/com/juick/server/helpers/NotifyOpts.java b/juick-server/src/main/java/com/juick/server/helpers/NotifyOpts.java deleted file mode 100644 index 377b0a50..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/NotifyOpts.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.juick.server.helpers; - -/** - * Created by vt on 03/09/16. - */ -public class NotifyOpts { - private boolean repliesEnabled; - private boolean subscriptionsEnabled; - private boolean recommendationsEnabled; - - public boolean isRepliesEnabled() { - return repliesEnabled; - } - - public void setRepliesEnabled(boolean repliesEnabled) { - this.repliesEnabled = repliesEnabled; - } - - public boolean isSubscriptionsEnabled() { - return subscriptionsEnabled; - } - - public void setSubscriptionsEnabled(boolean subscriptionsEnabled) { - this.subscriptionsEnabled = subscriptionsEnabled; - } - - public boolean isRecommendationsEnabled() { - return recommendationsEnabled; - } - - public void setRecommendationsEnabled(boolean recommendationsEnabled) { - this.recommendationsEnabled = recommendationsEnabled; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/PrivacyOpts.java b/juick-server/src/main/java/com/juick/server/helpers/PrivacyOpts.java deleted file mode 100644 index 66cf9410..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/PrivacyOpts.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.juick.server.helpers; - -/** - * Created by vt on 16/01/16. - */ -public class PrivacyOpts { - private int uid; - private int privacy; - - public PrivacyOpts() { - - } - - public int getUid() { - return uid; - } - - public void setUid(int uid) { - this.uid = uid; - } - - public int getPrivacy() { - return privacy; - } - - public void setPrivacy(int privacy) { - this.privacy = privacy; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/PrivateChats.java b/juick-server/src/main/java/com/juick/server/helpers/PrivateChats.java deleted file mode 100644 index b1bfccf8..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/PrivateChats.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.juick.server.helpers; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.juick.User; - -import java.util.List; - -/** - * Created by vt on 24/11/2016. - */ -public class PrivateChats { - private List users; - - @JsonProperty("pms") - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java b/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java deleted file mode 100644 index f941c743..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/ResponseReply.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.juick.server.helpers; - -import java.util.Date; - -/** - * Created by vitalyster on 13.12.2016. - */ -public class ResponseReply { - private String muname; - private int mid; - private int rid; - private String uname; - private String description; - private Date pubDate; - private String attachmentType; - - public String getMuname() { - return muname; - } - - public void setMuname(String muname) { - this.muname = muname; - } - - public int getMid() { - return mid; - } - - public void setMid(int mid) { - this.mid = mid; - } - - public int getRid() { - return rid; - } - - public void setRid(int rid) { - this.rid = rid; - } - - public String getUname() { - return uname; - } - - public void setUname(String uname) { - this.uname = uname; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Date getPubDate() { - return pubDate; - } - - public void setPubDate(Date pubDate) { - this.pubDate = pubDate; - } - - public String getAttachmentType() { - return attachmentType; - } - - public void setAttachmentType(String attachmentType) { - this.attachmentType = attachmentType; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/TagStats.java b/juick-server/src/main/java/com/juick/server/helpers/TagStats.java deleted file mode 100644 index e8720991..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/TagStats.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.juick.server.helpers; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.juick.Tag; - -/** - * Created by vitalyster on 01.12.2016. - */ -public class TagStats { - private Tag tag; - private int usageCount; - - public Tag getTag() { - return tag; - } - - public void setTag(Tag tag) { - this.tag = tag; - } - - @JsonProperty("messages") - public int getUsageCount() { - return usageCount; - } - - public void setUsageCount(int usageCount) { - this.usageCount = usageCount; - } -} diff --git a/juick-server/src/main/java/com/juick/server/helpers/UserInfo.java b/juick-server/src/main/java/com/juick/server/helpers/UserInfo.java deleted file mode 100644 index 5a4b6894..00000000 --- a/juick-server/src/main/java/com/juick/server/helpers/UserInfo.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.juick.server.helpers; - -/** - * Created by vt on 03/09/16. - */ -public class UserInfo { - private String fullName; - private String country; - private String url; - private String description; - - public String getFullName() { - return fullName; - } - - public void setFullName(String fullName) { - this.fullName = fullName; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } -} diff --git a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java deleted file mode 100644 index ef8641f8..00000000 --- a/juick-server/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ /dev/null @@ -1,426 +0,0 @@ -package com.juick.server.protocol; - -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.formatters.PlainTextFormatter; -import com.juick.server.helpers.TagStats; -import com.juick.server.protocol.annotation.UserCommand; -import com.juick.service.*; -import com.juick.util.TagUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.math.NumberUtils; -import org.apache.commons.lang3.reflect.MethodUtils; - -import javax.inject.Inject; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -/** - * Created by oxpa on 22.03.16. - */ - -public class JuickProtocol { - - private String baseUri; - private ProtocolListener listener; - - @Inject - UserService userService; - @Inject - TagService tagService; - @Inject - MessagesService messagesService; - @Inject - SubscriptionService subscriptionService; - @Inject - PMQueriesService pmQueriesService; - @Inject - PrivacyQueriesService privacyQueriesService; - @Inject - ShowQueriesService showQueriesService; - - public JuickProtocol(String baseUri) { - this.baseUri = baseUri; - } - - /** - * find command by pattern and invoke - * @param user who send command - * @param userInput given by user - * @return command result - * @throws InvocationTargetException - * @throws IllegalAccessException - * @throws NoSuchMethodException - */ - public String getReply(User user, String userInput) throws InvocationTargetException, - IllegalAccessException, NoSuchMethodException { - Optional cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() - .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), - m.getAnnotation(UserCommand.class).patternFlags()).matcher(userInput).matches()) - .findFirst(); - if (!cmd.isPresent()) { - // default command - post as new message - return postMessage(user, userInput.trim()); - } else { - Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(), - cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(userInput.trim()); - List groups = new ArrayList<>(); - while (matcher.find()) { - for (int i = 1; i <= matcher.groupCount(); i++) { - groups.add(matcher.group(i)); - } - } - return (String) getClass().getMethod(cmd.get().getName(), User.class, String[].class) - .invoke(this, user, groups.toArray(new String[groups.size()])); - } - } - - public String postMessage(User user, String input) { - List tags = tagService.fromString(input, false); - String body = input.substring(TagUtils.toString(tags).length()); - int mid = messagesService.createMessage(user.getUid(), body, null, tags); - subscriptionService.subscribeMessage(mid, user.getUid()); - listener.messagePosted(messagesService.getMessage(mid)); - return "New message posted.\n#" + mid + " " + baseUri + mid; - } - - @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages") - public String commandLast(User user, String... arguments) { - List mids = messagesService.getAll(user.getUid(), 0); - List messages = messagesService.getMessages(mids); - return "Last messages: \n" - + messages.stream().sorted(Collections.reverseOrder()).map(PlainTextFormatter::formatPostSummary) - .collect(Collectors.joining("\n\n")); - } - - @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "BL - Show your blacklist") - public String commandBL(User user_from, String... arguments) { - List blusers; - List bltags; - - blusers = userService.getUserBLUsers(user_from.getUid()); - bltags = tagService.getUserBLTags(user_from.getUid()); - - - String txt = StringUtils.EMPTY; - if (bltags.size() > 0) { - for (String bltag : bltags) { - txt += "*" + bltag + "\n"; - } - - if (blusers.size() > 0) { - txt += "\n"; - } - } - if (blusers.size() > 0) { - for (User bluser : blusers) { - txt += "@" + bluser.getName() + "\n"; - } - } - if (txt.isEmpty()) { - txt = "You don't have any users or tags in your blacklist."; - } - return txt; - } - - @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE, - help = "BL @username - add @username to your blacklist") - public String blacklistUser(User from, String... arguments) { - User blUser = userService.getUserByName(arguments[0]); - if (blUser != null) { - PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(from, blUser); - if (result == PrivacyQueriesService.PrivacyResult.Added) { - return "User added to your blacklist"; - } else { - return "User removed from your blacklist"; - } - } - return "User not found"; - } - - @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "BL *tag - add *tag to your blacklist") - public String blacklistTag(User from, String... arguments) { - User blUser = userService.getUserByName(arguments[0]); - if (blUser != null) { - Tag tag = tagService.getTag(arguments[0], false); - if (tag != null) { - PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(from, tag); - if (result == PrivacyQueriesService.PrivacyResult.Added) { - return "Tag added to your blacklist"; - } else { - return "Tag removed from your blacklist"; - } - } - } - return "Tag not found"; - } - - @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs") - public String commandUsers(User currentUser, String... args) { - StringBuilder msg = new StringBuilder(); - msg.append("Recommended blogs"); - List recommendedUsers = showQueriesService.getRecommendedUsers(currentUser); - if (recommendedUsers.size() > 0) { - for (String user : recommendedUsers) { - msg.append("\n@").append(user); - } - } else { - msg.append("\nNo recommendations now. Subscribe to more blogs. ;)"); - } - msg.append("\n\nTop 10 personal blogs:"); - List topUsers = showQueriesService.getTopUsers(); - if (topUsers.size() > 0) { - for (String user : topUsers) { - msg.append("\n@").append(user); - } - } else { - msg.append("\nNo top users. Empty DB? ;)"); - } - return msg.toString(); - } - - @UserCommand(pattern = "\\*", help = "* - Show your tags") - public String commandTags(User currentUser, String... args) { - List tags = tagService.getUserTagStats(currentUser.getUid()); - String msg = "Your tags: (tag - messages)\n" + - tags.stream() - .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining()); - return msg; - } - - @UserCommand(pattern = "S", help = "S - Show your subscriptions") - public String commandSubscriptions(User currentUser, String... args) { - List friends = userService.getUserFriends(currentUser.getUid()); - List tags = subscriptionService.getSubscribedTags(currentUser); - String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName()) - .collect(Collectors.joining()) - : "You are not subscribed to any user."; - msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t) - .collect(Collectors.joining()) - : "\nYou are not subscribed to any tag."; - return msg; - } - - @UserCommand(pattern = "!", help = "! - Show your favorite messages") - public String commandFavorites(User currentUser, String... args) { - List mids = messagesService.getUserRecommendations(currentUser.getUid(), 0); - if (mids.size() > 0) { - List messages = messagesService.getMessages(mids); - return "Favorite messages: \n" + String.join("\n", messages.stream().map(PlainTextFormatter::formatPost) - .collect(Collectors.toList())); - } - return "No favorite messages, try to \"like\" something ;)"; - } - - @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", - help = "@username+ - Show user's info and last 10 messages (@username++ - second page, ..)") - public String commandUser(User user, String... arguments) { - User blogUser = userService.getUserByName(arguments[0]); - int page = arguments[1].length(); - if (blogUser.getUid() > 0) { - List mids = messagesService.getUserBlog(blogUser.getUid(), 0, page); - List messages = messagesService.getMessages(mids); - return String.format("Last messages from @%s:\n%s", arguments[0], - String.join("\n", messages.stream() - .map(PlainTextFormatter::formatPost).collect(Collectors.toList()))); - } - return "User not found"; - } - - @UserCommand(pattern = "^d\\s*\\#([0-9]+)$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "D #12345 - delete the message") - public String commandDel(User user, String... args) { - int mid = NumberUtils.toInt(args[0], 0); - if (messagesService.deleteMessage(user.getUid(), mid)) { - return String.format("Message %s deleted", mid); - } - return "Error"; - } - - @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "LOGIN - log in to Juick website") - public String commandLogin(User user, String... arguments) { - return baseUri + "?" + userService.getHashByUID(user.getUid()); - } - - @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)") - public String commandMyFeed(User user, String... arguments) { - // number of # is the page count - int page = arguments[0].length() - 1; - List mids = messagesService.getMyFeed(user.getUid(), page); - List messages = messagesService.getMessages(mids); - // TODO: add instructions for empty feed - return "Your feed: \n" + String.join("\n", - messages.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); - } - - @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "ON/OFF - Enable/disable subscriptions delivery") - public String commandOnOff(User user, String[] input) { - UserService.ActiveStatus newStatus; - String retValUpdated; - if (input[0].toLowerCase().equals("on")) { - newStatus = UserService.ActiveStatus.Active; - retValUpdated = "Notifications are activated for " + user.getJid(); - } else { - newStatus = UserService.ActiveStatus.Inactive; - retValUpdated = "Notifications are disabled for " + user.getJid(); - } - - if (userService.setActiveStatusForJID(user.getJid(), newStatus)) { - return retValUpdated; - } else { - return String.format("Subscriptions status for %s was not changed", user.getJid()); - } - } - - @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "PING - returns you a PONG") - public String commandPing(User user, String[] input) { - return "PONG"; - } - - @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username") - public String commandPM(User user_from, String... arguments) { - String user_to = arguments[0]; - String body = arguments[1]; - - User toUser = userService.getUserByName(user_to); - - if (toUser.getUid() > 0) { - if (!userService.isInBLAny(toUser.getUid(), user_from.getUid())) { - if (pmQueriesService.createPM(user_from.getUid(), toUser.getUid(), body)) { - listener.privateMessage(user_from, toUser, body); - return "Private message sent"; - } - } - } - return "Error"; - } - - @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)") - public String commandShow(User user, String... arguments) { - boolean showReplies = arguments[1].length() > 0; - int mid = NumberUtils.toInt(arguments[0], 0); - if (mid == 0) { - return "Error"; - } - Message msg = messagesService.getMessage(mid); - if (msg != null) { - if (showReplies) { - List replies = messagesService.getReplies(mid); - replies.add(0, msg); - return String.join("\n", - replies.stream().map(PlainTextFormatter::formatPost).collect(Collectors.toList())); - } - return PlainTextFormatter.formatPost(msg); - } - return "Message not found"; - } - @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)", - help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") - public String EditOrReply(User user, String... args) { - int mid = NumberUtils.toInt(args[1]); - int rid = NumberUtils.toInt(args[4], 0); - String txt = args[5]; - List messageTags = tagService.fromString(txt, true); - if (messageTags.size() > 0) { - if (user.getUid() != messagesService.getMessageAuthor(mid).getUid()) { - return "It is not your message"; - } - tagService.updateTags(mid, messageTags); - return "Tags are updated"; - } else { - int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, null); - listener.messagePosted(messagesService.getReply(mid, newrid)); - return "Reply posted.\n#" + mid + "/" + newrid + " " - + baseUri + mid + "#" + newrid; - } - } - - @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments", - patternFlags = Pattern.CASE_INSENSITIVE) - public String commandSubscribeMessage(User user, String... args) { - boolean subscribe = args[0].equalsIgnoreCase("s"); - int mid = NumberUtils.toInt(args[1], 0); - if (messagesService.getMessage(mid) != null) { - if (subscribe) { - if (subscriptionService.subscribeMessage(mid, user.getUid())) { - return "Subscribed"; - } - } else { - if (subscriptionService.unSubscribeMessage(mid, user.getUid())) { - return "Unsubscribed from #" + mid; - } - return "You was not subscribed to #" + mid; - } - } - return "Error"; - } - @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @user - subscribe to user's posts", - patternFlags = Pattern.CASE_INSENSITIVE) - public String commandSubscribeUser(User user, String... args) { - boolean subscribe = args[0].equalsIgnoreCase("s"); - User toUser = userService.getUserByName(args[1]); - if (toUser.getUid() > 0) { - if (subscribe) { - if (subscriptionService.subscribeUser(user, toUser)) { - listener.userSubscribed(user, toUser); - return "Subscribed"; - // TODO: already subscribed case - } - } else { - if (subscriptionService.unSubscribeUser(user, toUser)) { - return "Unsubscribed from @" + toUser.getName(); - } - return "You was not subscribed to @" + toUser.getName(); - } - } - return "Error"; - } - @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" + - "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE) - public String commandSubscribeTag(User user, String... args) { - boolean subscribe = args[0].equalsIgnoreCase("s"); - Tag tag = tagService.getTag(args[1], true); - if (subscribe) { - if (subscriptionService.subscribeTag(user, tag)) { - return "Subscribed"; - } - } else { - if (subscriptionService.unSubscribeTag(user, tag)) { - return "Unsubscribed from " + tag.getName(); - } - return "You was not subscribed to " + tag.getName(); - } - return "Error"; - } - - @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE, - help = "HELP - returns this help message") - public String commandHelp(User user, String[] input) { - return Arrays.stream(getClass().getDeclaredMethods()) - .filter(m -> m.isAnnotationPresent(UserCommand.class)) - .map(m -> m.getAnnotation(UserCommand.class).help()) - .collect(Collectors.joining("\n")); - } - - public String getBaseUri() { - return baseUri; - } - - public ProtocolListener getListener() { - return listener; - } - - public void setListener(ProtocolListener listener) { - this.listener = listener; - } -} diff --git a/juick-server/src/main/java/com/juick/server/protocol/ProtocolListener.java b/juick-server/src/main/java/com/juick/server/protocol/ProtocolListener.java deleted file mode 100644 index 11231e04..00000000 --- a/juick-server/src/main/java/com/juick/server/protocol/ProtocolListener.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.juick.server.protocol; - -import com.juick.Message; -import com.juick.User; - -/** - * Created by vitalyster on 19.12.2016. - */ -public interface ProtocolListener { - void privateMessage(User from, User to, String body); - void userSubscribed(User from, User to); - void messagePosted(Message msg); -} diff --git a/juick-server/src/main/java/com/juick/server/protocol/annotation/UserCommand.java b/juick-server/src/main/java/com/juick/server/protocol/annotation/UserCommand.java deleted file mode 100644 index 42a9bb59..00000000 --- a/juick-server/src/main/java/com/juick/server/protocol/annotation/UserCommand.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.juick.server.protocol.annotation; - -import org.apache.commons.lang3.StringUtils; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Created by oxpa on 22.03.16. - */ -@Target({ElementType.TYPE, ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface UserCommand { - /** - * - * @return a command pattern - */ - String pattern() default StringUtils.EMPTY; - - /** - * - * @return pattern flags - */ - int patternFlags() default 0; - - /** - * - * @return a string used in HELP command output. Basically, only 1 string - */ - String help() default StringUtils.EMPTY; -} diff --git a/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java b/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java deleted file mode 100644 index b384a8c8..00000000 --- a/juick-server/src/main/java/com/juick/server/security/HashParamAuthenticationFilter.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.juick.server.security; - -import com.juick.User; -import com.juick.server.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.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()) { - Authentication authentication = new RememberMeAuthenticationToken( - hash, new JuickUser(user), 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/server/security/NotAuthorizedAuthenticationEntryPoint.java b/juick-server/src/main/java/com/juick/server/security/NotAuthorizedAuthenticationEntryPoint.java deleted file mode 100644 index d9dc6d61..00000000 --- a/juick-server/src/main/java/com/juick/server/security/NotAuthorizedAuthenticationEntryPoint.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.juick.server.security; - -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.AuthenticationEntryPoint; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - * Created by vitalyster on 25.11.2016. - */ -public class NotAuthorizedAuthenticationEntryPoint implements AuthenticationEntryPoint { - @Override - public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) - throws IOException, ServletException { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - } -} diff --git a/juick-server/src/main/java/com/juick/server/security/entities/AnonymousUser.java b/juick-server/src/main/java/com/juick/server/security/entities/AnonymousUser.java deleted file mode 100644 index 5ee9527f..00000000 --- a/juick-server/src/main/java/com/juick/server/security/entities/AnonymousUser.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.juick.server.security.entities; - -import com.juick.User; - -/** - * Created by aalexeev on 12/11/16. - */ -public final class AnonymousUser extends User { - public static final AnonymousUser INSTANCE = new AnonymousUser(); - - private AnonymousUser() { - super.setUid(getUid()); - super.setName(getName()); - super.setAvatar(getAvatar()); - super.setFullName(getFullName()); - super.setJid(getJid()); - super.setMessagesCount(getMessagesCount()); - super.setAuthHash(getAuthHash()); - super.setBanned(isBanned()); - super.setCredentials(getCredentials()); - super.setLang(getLang()); - } - - @Override - public boolean equals(Object obj) { - return obj == this || obj instanceof AnonymousUser; - } - - @Override - public int getUid() { - return 0; - } - - @Override - public String getName() { - return "Anonymous"; - } - - @Override - public String getFullName() { - return getName(); - } - - @Override - public String getJid() { - return "anonym@localhost"; - } - - @Override - public String getAuthHash() { - return null; - } - - @Override - public Integer getUnreadCount() { - return 0; - } - - @Override - public boolean isBanned() { - return false; - } - - @Override - public Object getAvatar() { - return null; - } - - @Override - public String getCredentials() { - return null; - } - - @Override - public String getLang() { - return "__"; - } - - @Override - public int getMessagesCount() { - return 0; - } - - @Override - public boolean isAnonymous() { - return true; - } - - @Override - public void setUid(int uid) { - } - - @Override - public void setName(String name) { - } - - @Override - public void setFullName(String fullName) { - } - - @Override - public void setJid(String jid) { - } - - @Override - public void setAuthHash(String authHash) { - } - - @Override - public void setUnreadCount(Integer count) { - } - - @Override - public void setBanned(boolean banned) { - } - - @Override - public void setAvatar(Object avatar) { - } - - @Override - public void setCredentials(String credentials) { - } - - @Override - public void setLang(String lang) { - } - - @Override - public void setMessagesCount(int messagesCount) { - } -} diff --git a/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java b/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java deleted file mode 100644 index b86d94dd..00000000 --- a/juick-server/src/main/java/com/juick/server/security/entities/JuickUser.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.juick.server.security.entities; - -import com.juick.User; -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 USER_AUTHORITY = Collections.singletonList(ROLE_USER); - public static final List 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 authorities; - - public JuickUser(com.juick.User user) { - this(user, USER_AUTHORITY); - } - - public JuickUser(com.juick.User user, Collection authorities) { - this.user = user; - this.authorities = authorities; - } - - @Override - public Collection getAuthorities() { - return authorities; - } - - @Override - public String getPassword() { - return user.getCredentials(); - } - - @Override - public String getUsername() { - return user.getName(); - } - - @Override - public boolean isAccountNonExpired() { - return true; - } - - @Override - public boolean isAccountNonLocked() { - return true; - } - - @Override - public boolean isCredentialsNonExpired() { - return true; - } - - @Override - public boolean isEnabled() { - return !user.isBanned(); - } - - public User getUser() { - return user; - } -} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java b/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java deleted file mode 100644 index 1ba1aecb..00000000 --- a/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.juick.server.util; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -/** - * Created by vt on 24/11/2016. - */ -@ResponseStatus(value = HttpStatus.BAD_REQUEST) -public class HttpBadRequestException extends RuntimeException { - public HttpBadRequestException() { - super(StringUtils.EMPTY, null, false, false); - } -} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java b/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java deleted file mode 100644 index 733453ba..00000000 --- a/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.juick.server.util; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -/** - * Created by vt on 24/11/2016. - */ -@ResponseStatus(value = HttpStatus.FORBIDDEN) -public class HttpForbiddenException extends RuntimeException { - public HttpForbiddenException() { - super(StringUtils.EMPTY, null, false, false); - } - -} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java b/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java deleted file mode 100644 index 942d90e2..00000000 --- a/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.juick.server.util; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -/** - * Created by vt on 24/11/2016. - */ -@ResponseStatus(value = HttpStatus.NOT_FOUND) -public class HttpNotFoundException extends RuntimeException { - public HttpNotFoundException() { - super(StringUtils.EMPTY, null, false, false); - } -} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpUtils.java b/juick-server/src/main/java/com/juick/server/util/HttpUtils.java deleted file mode 100644 index 31a68962..00000000 --- a/juick-server/src/main/java/com/juick/server/util/HttpUtils.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.juick.server.util; - -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.multipart.MultipartFile; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.UUID; - -/** - * - * @author Ugnich Anton - */ -public class HttpUtils { - private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); - - public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) { - if (attach !=null && !attach.isEmpty()) { - String partname = attach.getOriginalFilename(); - if (partname != null && partname.length() > 0) { - String attachmentType = partname.substring(partname.length() - 3).toLowerCase(); - if (attachmentType.equals("jpg") || attachmentType.equals("peg") || attachmentType.equals("png")) { - if (attachmentType.equals("peg")) { - attachmentType = "jpg"; - } - String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; - try { - Files.write(Paths.get(tmpDir, attachmentFName), - attach.getBytes()); - return attachmentFName; - } catch (IOException e) { - logger.warn("file receive error", e); - } - } - } - } - return StringUtils.EMPTY; - } - public static String downloadImage(URL url, String tmpDir) throws Exception { - String attachmentFName = null; - Exception ex = null; - - InputStream is = null; - FileOutputStream fos = null; - try { - URLConnection urlConn = url.openConnection(); - is = urlConn.getInputStream(); - String mime = urlConn.getContentType(); - - String attachmentType; - if (mime != null && mime.equals("image/jpeg")) { - attachmentType = "jpg"; - } else if (mime != null && mime.equals("image/png")) { - attachmentType = "png"; - } else { - throw new Exception("Wrong file type"); - } - - attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; - fos = new FileOutputStream(Paths.get(tmpDir, attachmentFName).toString()); - byte[] buffer = new byte[10240]; - int len; - while ((len = is.read(buffer)) > 0) { - fos.write(buffer, 0, len); - } - } catch (Exception e) { - ex = e; - attachmentFName = null; - } finally { - try { - if (is != null) { - is.close(); - } - } finally { - if (fos != null) { - fos.close(); - } - } - } - - if (ex != null) { - throw ex; - } else { - return attachmentFName; - } - } -} diff --git a/juick-server/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server/src/main/java/com/juick/server/util/ImageUtils.java deleted file mode 100644 index 61677750..00000000 --- a/juick-server/src/main/java/com/juick/server/util/ImageUtils.java +++ /dev/null @@ -1,66 +0,0 @@ - -package com.juick.server.util; - -import org.apache.commons.io.FilenameUtils; -import org.imgscalr.Scalr; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; - -public class ImageUtils { - - /** - * Move attached image from temp folder to image folder. - * Create preview images in corresponding folders. - * - * @param tempFilename Name of the image file in temp folder. - * @param outputFilename Name that will be used in in image folder. - * @param tmpDir Path string for the temp folder. - * @param imgDir Path string for the image folder. - */ - public static void saveImageWithPreviews(String tempFilename, String outputFilename, String tmpDir, String imgDir) - throws IOException { - String ext = FilenameUtils.getExtension(outputFilename); - - Path outputImagePath = Paths.get(imgDir, "p", outputFilename); - Files.move(Paths.get(tmpDir, tempFilename), outputImagePath); - BufferedImage originalImage = ImageIO.read(outputImagePath.toFile()); - - int width = originalImage.getWidth(); - int height = originalImage.getHeight(); - int maxDimension = (width > height) ? width : height; - BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage; - BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage; - BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage; - ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile()); - ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile()); - ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile()); - } - - /** - * Save new avatar in all required sizes. - * - * @param tempFilename Name of the image file in temp folder. - * @param uid User id that is used to build image file names. - * @param tmpDir Path string for the temp folder. - * @param imgDir Path string for the image folder. - */ - public static void saveAvatar(String tempFilename, int uid, String tmpDir, String imgDir) - throws IOException { - String ext = FilenameUtils.getExtension(tempFilename); - String originalName = String.format("%s.%s", uid, ext); - Path originalPath = Paths.get(imgDir, "ao", originalName); - Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING); - BufferedImage originalImage = ImageIO.read(originalPath.toFile()); - - String targetExt = "png"; - String targetName = String.format("%s.%s", uid, targetExt); - ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile()); - ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile()); - } -} \ No newline at end of file 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 e3784345..00000000 --- a/juick-server/src/main/java/com/juick/service/BaseJdbcService.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.juick.service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.support.TransactionTemplate; -import org.springframework.util.Assert; - -import javax.sql.DataSource; - -/** - * Created by aalexeev on 11/13/16. - */ -public abstract class BaseJdbcService { - protected final Logger logger = LoggerFactory.getLogger(getClass()); - - private final JdbcTemplate jdbcTemplate; - private final TransactionTemplate transactionTemplate; - private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; - - - protected BaseJdbcService(JdbcTemplate jdbcTemplate, PlatformTransactionManager transactionManager) { - Assert.notNull(jdbcTemplate, "JdbcTemplate must be initialized"); - - this.jdbcTemplate = jdbcTemplate; - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); - this.transactionTemplate = (transactionManager == null) ? - null : new TransactionTemplate(transactionManager); - - } - - protected BaseJdbcService(DataSource dataSource, PlatformTransactionManager transactionManager) { - Assert.notNull(dataSource, "DataSource must be initialized"); - - this.jdbcTemplate = new JdbcTemplate(dataSource); - this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); - this.transactionTemplate = (transactionManager == null) ? - null : new TransactionTemplate(transactionManager); - - } - - protected JdbcTemplate getJdbcTemplate() { - return jdbcTemplate; - } - - protected NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { - return namedParameterJdbcTemplate; - } - - protected TransactionTemplate getTransactionTemplate() { - return transactionTemplate; - } -} diff --git a/juick-server/src/main/java/com/juick/service/CrosspostService.java b/juick-server/src/main/java/com/juick/service/CrosspostService.java deleted file mode 100644 index 467d1cbe..00000000 --- a/juick-server/src/main/java/com/juick/service/CrosspostService.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.juick.service; - -import com.juick.server.helpers.ApplicationStatus; -import org.apache.commons.lang3.tuple.Pair; - -import java.util.Optional; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface CrosspostService { - - Optional> getTwitterTokens(int uid); - - boolean deleteTwitterToken(Integer uid); - - Optional getFacebookToken(int uid); - - ApplicationStatus getFbCrossPostStatus(int uid); - - boolean enableFBCrosspost(Integer uid); - - void disableFBCrosspost(Integer uid); - - String getTwitterName(int uid); - - String getTelegramName(int uid); - - Optional> getVkTokens(int uid); - - void deleteVKUser(Integer uid); - - int getUIDbyFBID(long fbID); - - boolean createFacebookUser(long fbID, String loginhash, String token, String fbName, String fbLink); - - boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink); - - int getUIDbyVKID(long vkID); - - boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink); - - String getFacebookNameByHash(String hash); - - String getTelegramNameByHash(String hash); - - boolean setFacebookUser(String hash, int uid); - - String getVKNameByHash(String hash); - - boolean setVKUser(String hash, int uid); - - boolean setTelegramUser(String hash, int uid); - - String getJIDByHash(String hash); - - boolean setJIDUser(String hash, int uid); -} diff --git a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java b/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java deleted file mode 100644 index 965f2b51..00000000 --- a/juick-server/src/main/java/com/juick/service/CrosspostServiceImpl.java +++ /dev/null @@ -1,246 +0,0 @@ -package com.juick.service; - -import com.juick.server.helpers.ApplicationStatus; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.tuple.Pair; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import javax.inject.Inject; -import java.util.List; -import java.util.Optional; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostService { - - @Inject - public CrosspostServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @Transactional(readOnly = true) - @Override - public Optional> getTwitterTokens(final int uid) { - List>> list = getJdbcTemplate().query( - "SELECT access_token,access_token_secret FROM twitter WHERE user_id = ? AND crosspost = 1", - (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), - uid); - - return list.isEmpty() ? - Optional.empty() : list.get(0); - } - - @Transactional - @Override - public boolean deleteTwitterToken(Integer uid) { - return getJdbcTemplate().update("DELETE FROM twitter WHERE user_id=?", uid) > 0 - && getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=1741", uid) > 0; - } - - @Transactional(readOnly = true) - @Override - public Optional getFacebookToken(final int uid) { - List list = getJdbcTemplate().queryForList( - "SELECT access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1", - String.class, - uid); - return list.isEmpty() ? - Optional.empty() : Optional.of(list.get(0)); - } - - @Transactional(readOnly = true) - @Override - public ApplicationStatus getFbCrossPostStatus(final int uid) { - List 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 - && getJdbcTemplate().update( - "INSERT INTO subscr_users(user_id,suser_id,jid,active) VALUES (?,5863,'juick@facebook.juick.com',1)", - uid) > 0; - } - - @Transactional - @Override - public void disableFBCrosspost(Integer uid) { - getJdbcTemplate().update("UPDATE facebook SET crosspost=0 WHERE user_id=?", uid); - // TODO: stop using magic numbers for system users - getJdbcTemplate().update("DELETE FROM subscr_users WHERE user_id=? AND suser_id=5863", uid); - } - - @Transactional(readOnly = true) - @Override - public String getTwitterName(final int uid) { - List 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 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> getVkTokens(final int uid) { - List>> 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("INSERT INTO facebook(fb_id,loginhash,access_token,fb_name,fb_link) VALUES (?,?,?,?,?)", - fbID, loginhash, token, fbName, fbLink) > 0; - } - - @Transactional - @Override - public boolean updateFacebookUser(long fbID, String token, String fbName, String fbLink) { - return getJdbcTemplate().update("UPDATE facebook SET access_token=?,fb_name=?,fb_link=? WHERE fb_id=?", - token, fbName, fbLink, fbID) > 0; - } - - @Transactional(readOnly = true) - @Override - public int getUIDbyVKID(long vkID) { - try { - return getJdbcTemplate().queryForObject("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL", Integer.class, vkID); - } catch (EmptyResultDataAccessException e) { - return 0; - } - } - - @Transactional - @Override - public boolean createVKUser(long vkID, String loginhash, String token, String vkName, String vkLink) { - return getJdbcTemplate().update("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)", - vkID, loginhash, token, vkName, vkLink) > 0; - } - - @Transactional(readOnly = true) - @Override - public String getFacebookNameByHash(String hash) { - try { - List> 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 "" + fb.get(0).getLeft() + ""; - } - 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 "" + name + ""; - } 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> 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 "" + logins.get(0).getLeft() + ""; - } - 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/EmailService.java b/juick-server/src/main/java/com/juick/service/EmailService.java deleted file mode 100644 index 67925ec1..00000000 --- a/juick-server/src/main/java/com/juick/service/EmailService.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.juick.service; - -/** - * Created by vitalyster on 09.12.2016. - */ -public interface EmailService { - boolean verifyAddressByCode(Integer userId, String code); - boolean addVerificationCode(Integer userId, String account, String code); - boolean deleteEmail(Integer userId, String account); - boolean setSubscriptionHour(Integer userId, String account, String hour); -} 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 b0bf9e20..00000000 --- a/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.juick.service; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import javax.inject.Inject; - -/** - * Created by vitalyster on 09.12.2016. - */ -@Repository -@Transactional -public class EmailServiceImpl extends BaseJdbcService implements EmailService { - @Inject - public EmailServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @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); - getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", 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 deleteEmail(Integer userId, String account) { - return getJdbcTemplate().update("DELETE FROM emails WHERE user_id=? AND email=?", userId, account) > 0; - } - - @Override - public boolean setSubscriptionHour(Integer userId, String account, String hour) { - getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", userId); - return StringUtils.isNotEmpty(account) && getJdbcTemplate().update( - "UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?", hour, userId, account) > 0; - } -} diff --git a/juick-server/src/main/java/com/juick/service/MessagesService.java b/juick-server/src/main/java/com/juick/service/MessagesService.java deleted file mode 100644 index 8e9d916b..00000000 --- a/juick-server/src/main/java/com/juick/service/MessagesService.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.juick.service; - -import com.juick.Message; -import com.juick.User; -import com.juick.server.helpers.ResponseReply; -import org.springframework.jdbc.core.JdbcTemplate; - -import java.util.Collection; -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface MessagesService { - int createMessage(int uid, String txt, String attachment, Collection tags); - - int createReply(int mid, int rid, int uid, String txt, String attachment); - - int getReplyIDIncrement(int mid); - - boolean recommendMessage(int mid, int vuid); - - boolean canViewThread(int mid, int uid); - - boolean isReadOnly(int mid); - - boolean isSubscribed(int uid, int mid); - - int getMessagePrivacy(int mid); - - com.juick.Message getMessage(int mid); - - com.juick.Message getReply(int mid, int rid); - - User getMessageAuthor(int mid); - - List getMessageRecommendations(int mid); - - List getAll(int visitorUid, int before); - - List getTag(int tid, int visitorUid, int before, int cnt); - - List getTags(String tids, int visitorUid, int before, int cnt); - - List getPlace(int placeId, int visitorUid, int before); - - List getMyFeed(int uid, int before); - - List getPrivate(int uid, int before); - - List getDiscussions(int uid, int before); - - List getRecommended(int uid, int before); - - List getPopular(int visitorUid, int before); - - List getPhotos(int visitorUid, int before); - - List getSearch(String search, int before); - - List getUserBlog(int uid, int privacy, int before); - - List getUserTag(int uid, int tid, int privacy, int before); - - List getUserBlogAtDay(int uid, int privacy, int daysback); - - List getUserBlogWithRecommendations(int uid, int privacy, int before); - - List getUserRecommendations(int uid, int before); - - List getUserPhotos(int uid, int privacy, int before); - - List getUserSearch(int UID, String search, int privacy, int before); - - List getMessages(Collection mids); - - List getReplies(int mid); - - boolean setMessagePopular(int mid, int popular); - - boolean setMessagePrivacy(int mid); - - boolean deleteMessage(int uid, int mid); - - List getLastMessages(int hours); - - List getLastReplies(int hours); -} 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 318b5805..00000000 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ /dev/null @@ -1,814 +0,0 @@ -package com.juick.service; - -import com.juick.Message; -import com.juick.Tag; -import com.juick.User; -import com.juick.server.helpers.PrivacyOpts; -import com.juick.server.helpers.ResponseReply; -import com.juick.service.search.SearchService; -import com.juick.util.MessageUtils; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.dao.IncorrectResultSizeDataAccessException; -import org.springframework.jdbc.core.ConnectionCallback; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.SqlParameterSource; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.Assert; - -import javax.inject.Inject; -import java.sql.*; -import java.util.*; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -public class MessagesServiceImpl extends BaseJdbcService implements MessagesService { - private final UserService userService; - private final SearchService searchService; - - @Inject - public MessagesServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, SearchService searchService) { - super(jdbcTemplate, null); - - Assert.notNull(userService, "UserService must be initialized"); - this.userService = userService; - - Assert.notNull(searchService, "SearchService must be initialized"); - this.searchService = searchService; - } - - private class MessageMapper implements RowMapper { - @Override - public Message mapRow(ResultSet rs, int rowNum) throws SQLException { - Message msg = new Message(); - msg.setMid(rs.getInt(1)); - msg.setRid(rs.getInt(2)); - msg.setReplyto(rs.getInt(3)); - User user = new User(); - user.setUid(rs.getInt(4)); - user.setName(rs.getString(5)); - user.setBanned(rs.getBoolean(6)); - msg.setUser(user); - msg.TimeAgo = rs.getInt(7); - msg.setDate(rs.getTimestamp(8)); - msg.ReadOnly = rs.getBoolean(9); - msg.setPrivacy(rs.getInt(10)); - msg.FriendsOnly = msg.getPrivacy() < 0; - msg.setReplies(rs.getInt(11)); - msg.setAttachmentType(rs.getString(12)); - if (rs.getDouble(13) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(14); - msg.Place.lon = rs.getDouble(15); - } - msg.setLikes(rs.getInt(16)); - msg.Hidden = rs.getBoolean(17); - String tagsStr = rs.getString(18); - msg.parseTags(tagsStr); - msg.setRepliesBy(rs.getString(19)); - msg.setText(rs.getString(20)); - msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(21))); - return msg; - } - } - - /** - * @see Java, JDBC and MySQL Types - */ - @Transactional - @Override - public int createMessage(final int uid, final String txt, final String attachment, final Collection tags) { - KeyHolder holder = new GeneratedKeyHolder(); - getJdbcTemplate().update( - con -> { - PreparedStatement stmt = con.prepareStatement( - "INSERT INTO messages(user_id, attach) VALUES (?, ?)", - Statement.RETURN_GENERATED_KEYS); - stmt.setInt(1, uid); - if (attachment != null) - stmt.setString(2, attachment); - else - stmt.setNull(2, Types.CHAR); - - return stmt; - }, - holder); - - int mid = holder.getKey().intValue(); - - if (mid > 0) { - String tagsNames = StringUtils.EMPTY; - - if (CollectionUtils.isNotEmpty(tags)) { - StringBuilder tasNamesBuilder = new StringBuilder(); - List 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) VALUES (?, ?, ?)", - new Object[]{mid, tagsNames, txt}, - new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR}); - } - - return mid; - } - - /** - * @param mid - * @param rid - * @param uid - * @param txt - * @param attachment - * @return - * @see Java, JDBC and MySQL Types - */ - @Transactional - @Override - public int createReply(final int mid, final int rid, final int uid, final String txt, final String attachment) { - int ridnew = getReplyIDIncrement(mid); - - getJdbcTemplate().update( - "INSERT INTO replies(message_id, reply_id, user_id, replyto, attach, txt) VALUES (?, ?, ?, ?, ?, ?)", - new Object[]{mid, ridnew, uid, rid, attachment, txt}, - new int[]{Types.INTEGER, Types.SMALLINT, Types.INTEGER, Types.SMALLINT, Types.CHAR, Types.VARCHAR}); - - if (ridnew > 0) - getJdbcTemplate().update( - "UPDATE messages SET replies = replies + 1 WHERE message_id = ?", - mid); - return ridnew; - } - - @Override - public int getReplyIDIncrement(final int mid) { - return getJdbcTemplate().execute((ConnectionCallback) conn -> { - conn.setAutoCommit(false); - final int replyNo; - try (PreparedStatement ps = conn.prepareStatement("START TRANSACTION")) { - ps.executeUpdate(); - } - try (PreparedStatement ps = conn.prepareStatement("SELECT maxreplyid+1 FROM messages WHERE message_id=? FOR UPDATE")) { - ps.setInt(1, mid); - try (ResultSet resultSet = ps.executeQuery()) { - if (resultSet.next()) { - replyNo = resultSet.getInt(1); - } else { - throw new IncorrectResultSizeDataAccessException("while getting getReplyIDIncrement, mid=" + mid, 1, 0); - } - } - } - try (PreparedStatement ps = conn.prepareStatement("UPDATE messages SET maxreplyid=? WHERE message_id=?")) { - ps.setInt(1, replyNo); - ps.setInt(2, mid); - if (ps.executeUpdate() != 1) { - throw new IncorrectResultSizeDataAccessException("Cannot find a message to update: " + mid, 1, 0); - } - } - conn.commit(); - return replyNo; - }); - - } - - @Transactional - @Override - public boolean recommendMessage(final int mid, final int vuid) { - boolean res = getJdbcTemplate().update( - "INSERT IGNORE INTO favorites(user_id, message_id, ts) VALUES (?, ?, NOW())", - vuid, mid) == 1; - - if (res) - getJdbcTemplate().update( - "UPDATE messages SET likes = likes + 1 WHERE message_id = ?", - mid); - return res; - } - - @Transactional(readOnly = true) - @Override - public boolean canViewThread(final int mid, final int uid) { - List 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 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 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 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 list = getJdbcTemplate().query( - "SELECT messages.message_id, 0 as rid, 0 as replyto, " - + "messages.user_id, users.nick, users.banned as banned, " - + "TIMESTAMPDIFF(MINUTE, messages.ts, NOW())," - + "messages.ts," - + "messages.readonly, messages.privacy, messages.replies," - + "messages.attach, messages.place_id, messages.lat," - + "messages.lon, messages.likes, messages.hidden," - + "txt.tags, txt.repliesby, txt.txt, '' as q FROM messages " - + "INNER JOIN users ON messages.user_id = users.id " - + "INNER JOIN messages_txt AS txt " - + "ON messages.message_id = txt.message_id " - + "WHERE messages.message_id = ?", - new MessageMapper(), - mid); - - return list.isEmpty() ? null : list.get(0); - } - - @Transactional(readOnly = true) - @Override - public com.juick.Message getReply(final int mid, final int rid) { - List list = getJdbcTemplate().query( - "SELECT replies.user_id, users.nick," - + "replies.replyto, replies.ts," - + "replies.attach, replies.txt, IFNULL(q.txt,t.txt) as quote " - + "FROM replies INNER JOIN users ON replies.user_id = users.id " - + "LEFT JOIN replies q ON replies.message_id = q.message_id and replies.replyto = q.reply_id " - + "LEFT JOIN messages_txt t ON replies.message_id = t.message_id " - + "WHERE replies.message_id = ? AND replies.reply_id = ?", - (rs, num) -> { - Message msg = new Message(); - - msg.setMid(mid); - msg.setRid(rid); - msg.setUser(new User()); - msg.getUser().setUid(rs.getInt(1)); - msg.getUser().setName(rs.getString(2)); - msg.setReplyto(rs.getInt(3)); - msg.setDate(rs.getTimestamp(4)); - msg.setAttachmentType(rs.getString(5)); - msg.setText(rs.getString(6)); - String quote = rs.getString(7); - - if (!StringUtils.isEmpty(quote)) { - msg.setReplyQuote(MessageUtils.formatQuote(quote)); - } - - return msg; - }, - mid, rid); - - return list.isEmpty() ? null : list.get(0); - } - - @Transactional(readOnly = true) - @Override - public User getMessageAuthor(final int mid) { - List 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 getMessageRecommendations(final int mid) { - return getJdbcTemplate().queryForList( - "SELECT users.nick FROM favorites INNER JOIN users " + - "ON (favorites.message_id = ? AND favorites.user_id = users.id)", - new Object[]{mid}, - String.class); - } - - @Transactional(readOnly = true) - @Override - public List 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 users u WHERE u.banned = 1 and u.id = m.user_id) ORDER BY m.message_id DESC LIMIT 20", - sqlParameterSource, - Integer.class); - } - - @Transactional(readOnly = true) - @Override - public List 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 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 USING(message_id) WHERE " + - (before > 0 ? - " messages.message_id < :before AND " : StringUtils.EMPTY) + - "(messages.privacy > 0 OR messages.user_id = :visitorUid) ORDER BY message_id DESC LIMIT :cnt", - sqlParameterSource, - Integer.class); - } - - @Transactional(readOnly = true) - @Override - public List 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 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 getMyFeed(final int uid, final int before) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("before", before); - - List mids = getNamedParameterJdbcTemplate().queryForList( - "(SELECT message_id FROM messages " + - " INNER JOIN subscr_users ON (subscr_users.suser_id = :uid AND subscr_users.user_id = messages.user_id) " + - " WHERE " + - (before > 0 ? - " message_id < :before AND " : 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)))) " + - " UNION " + - " (SELECT message_id FROM messages WHERE user_id=:uid " + - (before > 0 ? - " AND message_id < :before " : StringUtils.EMPTY) + - ") ORDER BY message_id DESC LIMIT 20", - sqlParameterSource, - Integer.class); - - return mids; - } - - @Transactional(readOnly = true) - @Override - public List 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 getDiscussions(final int uid, final int before) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("before", before); - - return getNamedParameterJdbcTemplate().queryForList( - "SELECT message_id FROM subscr_messages WHERE suser_id = :uid" + - (before > 0 ? - " AND message_id < :before " : " ") + - "ORDER BY message_id DESC LIMIT 20", - sqlParameterSource, - Integer.class); - } - - @Transactional(readOnly = true) - @Override - public List 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 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) " + - " ORDER BY m.message_id DESC LIMIT 20", - sqlParameterSource, - Integer.class); - } - - @Transactional(readOnly = true) - @Override - public List 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 users u WHERE u.banned = 1 and u.id = m.user_id) " + - " 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 getSearch(final String search, final int before) { - List mids = searchService.searchInAllMessages(search, before); - - if (!mids.isEmpty()) - return getNamedParameterJdbcTemplate().queryForList( - "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy > 0 ORDER BY message_id DESC LIMIT 20", - new MapSqlParameterSource("ids", mids), - Integer.class); - return mids; - } - - @Transactional(readOnly = true) - @Override - public List getUserBlog(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 ORDER BY message_id DESC LIMIT 20", - sqlParameterSource, - Integer.class); - } - - @Transactional(readOnly = true) - @Override - public List 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); - - 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 getUserBlogAtDay(final int uid, final int privacy, final int daysback) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("privacy", privacy) - .addValue("daysback", daysback); - - 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 getUserBlogWithRecommendations(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 " + - "(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 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 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 getUserSearch(final int UID, final String search, final int privacy, final int before) { - List mids = searchService.searchByStringAndUser(search, UID, before); - - if (!mids.isEmpty()) { - return getNamedParameterJdbcTemplate().queryForList( - "SELECT message_id FROM messages WHERE message_id IN (:ids) AND privacy >= :privacy ORDER BY message_id DESC", - new MapSqlParameterSource("ids", mids) - .addValue("privacy", privacy), - Integer.class); - } - return mids; - } - - @Transactional(readOnly = true) - @Override - public List getMessages(final Collection mids) { - if (CollectionUtils.isNotEmpty(mids)) { - return getNamedParameterJdbcTemplate().query( - "SELECT messages.message_id, 0 as rid, 0 as replyto, " - + "messages.user_id,users.nick, 0 as banned, " - + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," - + "messages.ts," - + "messages.readonly,messages.privacy,messages.replies," - + "messages.attach,messages.place_id,messages.lat," - + "messages.lon,messages.likes,messages.hidden," - + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q " - + "FROM (messages INNER JOIN messages_txt " - + "ON messages.message_id=messages_txt.message_id) " - + "INNER JOIN users ON messages.user_id=users.id " - + "WHERE messages.message_id IN (:ids) ORDER BY messages.message_id DESC", - new MapSqlParameterSource("ids", mids), - new MessageMapper()); - } - return Collections.emptyList(); - } - - @Transactional(readOnly = true) - @Override - public List getReplies(final int mid) { - return getNamedParameterJdbcTemplate().query( - "SELECT replies.message_id as mid, replies.reply_id, replies.replyto, " + - "replies.user_id, users.nick, users.banned, " + - "TIMESTAMPDIFF(MINUTE, replies.ts, NOW()), replies.ts, " + - "0 as readonly, 0 as privacy, 0 as replies, " + - "replies.attach, 0 as place_id, 0 as lat, " + - "0 as lon, 0 as likes, 0 as hidden, " + - "NULL as tags, NULL as repliesby, replies.txt, " + - "IFNULL(qw.txt, t.txt) as q " + - "FROM replies INNER JOIN users " + - "ON replies.user_id = users.id " + - "LEFT JOIN replies qw ON replies.message_id = qw.message_id and replies.replyto = qw.reply_id " + - "LEFT JOIN messages_txt t on replies.message_id = t.message_id " + - "WHERE replies.message_id = :mid ORDER BY replies.reply_id ASC", - new MapSqlParameterSource("mid", mid), - new MessageMapper()); - } - - @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(readOnly = true) - @Override - public List 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 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 " + - "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)); - return reply; - }, -hours); - } -} diff --git a/juick-server/src/main/java/com/juick/service/PMQueriesService.java b/juick-server/src/main/java/com/juick/service/PMQueriesService.java deleted file mode 100644 index e20bb3a5..00000000 --- a/juick-server/src/main/java/com/juick/service/PMQueriesService.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.juick.service; - -import com.juick.User; - -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface PMQueriesService { - boolean createPM(int uidFrom, int uid_to, String body); - - boolean addPMinRoster(int uid, String jid); - - boolean removePMinRoster(int uid, String jid); - - boolean havePMinRoster(int uid, String jid); - - String getLastView(int uidFrom, int uidTo); - - List getPMLastConversationsUsers(int uid, int cnt); - - List getPMMessages(int uid, int uidTo); - - List getLastPMInbox(int uid); - - List getLastPMSent(int uid); -} 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 e6a8617d..00000000 --- a/juick-server/src/main/java/com/juick/service/PMQueriesServiceImpl.java +++ /dev/null @@ -1,160 +0,0 @@ -package com.juick.service; - -import com.juick.User; -import org.springframework.jdbc.core.JdbcTemplate; -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 javax.inject.Inject; -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -public class PMQueriesServiceImpl extends BaseJdbcService implements PMQueriesService { - - @Inject - public PMQueriesServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @Transactional - @Override - public boolean createPM(final int uidFrom, final int uid_to, final String body) { - boolean ret = getJdbcTemplate().update( - "INSERT INTO pm(user_id, user_id_to, txt) VALUES (?, ?, ?)", - uidFrom, uid_to, body) > 0; - - if (ret) { - getJdbcTemplate().update( - "INSERT INTO pm_streams(user_id, user_id_to, lastmessage, unread) VALUES (?, ?, NOW(), 1) " - + "ON DUPLICATE KEY UPDATE lastmessage = NOW(), unread = unread + 1", - uidFrom, uid_to); - } - return ret; - } - - @Transactional - @Override - public boolean addPMinRoster(final int uid, final String jid) { - return getJdbcTemplate().update( - "INSERT INTO pm_inroster(user_id, jid) VALUES (?, ?)", uid, jid) > 0; - } - - @Transactional - @Override - public boolean removePMinRoster(final int uid, final String jid) { - return getJdbcTemplate().update( - "DELETE FROM pm_inroster WHERE user_id = ? AND jid = ?", uid, jid) > 0; - } - - @Transactional - @Override - public boolean havePMinRoster(final int uid, final String jid) { - List res = getJdbcTemplate().queryForList( - "SELECT 1 FROM pm_inroster WHERE user_id = ? AND jid = ?", - Integer.class, - uid, jid); - return res.size() > 0; - } - - @Transactional(readOnly = true) - @Override - public String getLastView(final int uidFrom, final int uidTo) { - List list = getJdbcTemplate().queryForList( - "SELECT lastview FROM pm_streams WHERE user_id = ? AND user_id_to = ?", - String.class, - uidFrom, uidTo); - - return list.isEmpty() ? - null : list.get(0); - } - - @Transactional(readOnly = true) - @Override - public List getPMLastConversationsUsers(final int uid, final int cnt) { - return getJdbcTemplate().query( - "SELECT pm_streams.user_id, users.nick, pm_streams.unread FROM pm_streams " - + "INNER JOIN users ON users.id = pm_streams.user_id " - + "WHERE pm_streams.user_id_to=? " - + "ORDER BY pm_streams.unread DESC, pm_streams.lastmessage DESC LIMIT ?", - (rs, rowNum) -> { - com.juick.User u = new com.juick.User(); - u.setUid(rs.getInt(1)); - u.setName(rs.getString(2)); - u.setUnreadCount(rs.getInt(3)); - return u; - }, - uid, cnt); - } - - @Transactional - @Override - public List getPMMessages(final int uid, final int uidTo) { - SqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("uidTo", uidTo); - - List msgs = getNamedParameterJdbcTemplate().query( - "SELECT user_id, txt,ts FROM pm 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); - msg.setUser(new User()); - msg.getUser().setUid(uuid); - msg.setText(rs.getString(2)); - msg.setDate(rs.getTimestamp(3)); - return msg; - }); - - getNamedParameterJdbcTemplate().update( - "UPDATE pm_streams SET lastview = NOW(), unread = 0 WHERE user_id_to = :uid AND user_id = :uidTo", - sqlParameterSource); - - return msgs; - } - - @Transactional(readOnly = true) - @Override - public List getLastPMInbox(final int uid) { - return getJdbcTemplate().query( - "SELECT pm.user_id,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW()),pm.ts " + - "FROM pm INNER JOIN users ON pm.user_id=users.id WHERE pm.user_id_to=? ORDER BY pm.ts DESC LIMIT 20", - (rs, num) -> { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - msg.getUser().setUid(rs.getInt(1)); - msg.getUser().setName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - return msg; - }, - uid); - } - - @Transactional(readOnly = true) - @Override - public List getLastPMSent(final int uid) { - return getJdbcTemplate().query( - "SELECT pm.user_id_to,users.nick,pm.txt,TIMESTAMPDIFF(MINUTE,pm.ts,NOW())," + - "pm.ts FROM pm INNER JOIN users ON pm.user_id_to=users.id " + - "WHERE pm.user_id=? ORDER BY pm.ts DESC LIMIT 20", - (rs, num) -> { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - msg.getUser().setUid(rs.getInt(1)); - msg.getUser().setName(rs.getString(2)); - msg.setText(rs.getString(3)); - msg.TimeAgo = rs.getInt(4); - msg.setDate(rs.getTimestamp(5)); - return msg; - }, - uid); - } -} diff --git a/juick-server/src/main/java/com/juick/service/PrivacyQueriesService.java b/juick-server/src/main/java/com/juick/service/PrivacyQueriesService.java deleted file mode 100644 index 61eb199b..00000000 --- a/juick-server/src/main/java/com/juick/service/PrivacyQueriesService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.User; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface PrivacyQueriesService { - enum PrivacyResult { - Removed, Added - } - - PrivacyResult blacklistUser(User user, User target); - - PrivacyResult blacklistTag(User user, Tag tag); -} diff --git a/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 4f19c70f..00000000 --- a/juick-server/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.User; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import javax.inject.Inject; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -@Transactional -public class PrivacyQueriesServiceImpl extends BaseJdbcService implements PrivacyQueriesService { - - @Inject - public PrivacyQueriesServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @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/PushQueriesService.java b/juick-server/src/main/java/com/juick/service/PushQueriesService.java deleted file mode 100644 index 7d4bc295..00000000 --- a/juick-server/src/main/java/com/juick/service/PushQueriesService.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.juick.service; - -import java.util.Collection; -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface PushQueriesService { - List getGCMRegID(int uid); - - List getGCMTokens(Collection uids); - - boolean addGCMToken(Integer uid, String token); - - boolean deleteGCMToken(String token); - - List getMPNSURL(int uid); - - List getMPNSTokens(Collection uids); - - boolean addMPNSToken(Integer uid, String token); - - boolean deleteMPNSToken(String token); - - List getAPNSToken(int uid); - - List getAPNSTokens(Collection uids); - - boolean addAPNSToken(Integer uid, String token); - - boolean deleteAPNSToken(String token); -} 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 73f98abf..00000000 --- a/juick-server/src/main/java/com/juick/service/PushQueriesServiceImpl.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.juick.service; - -import org.apache.commons.collections4.CollectionUtils; -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 { - - @Inject - public PushQueriesServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @Transactional(readOnly = true) - @Override - public List getGCMRegID(final int uid) { - return getJdbcTemplate().queryForList( - "SELECT regid FROM android WHERE user_id=?", - String.class, - uid); - } - - @Transactional(readOnly = true) - @Override - public List getGCMTokens(final Collection 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 getMPNSURL(final int uid) { - return getJdbcTemplate().queryForList( - "SELECT url FROM winphone WHERE user_id=?", - String.class, - uid); - } - - @Transactional(readOnly = true) - @Override - public List getMPNSTokens(final Collection 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 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 getAPNSTokens(final Collection 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) { - return getJdbcTemplate().update("INSERT IGNORE INTO ios(user_id,token) VALUES (?, ?)", - uid, token) > 0; - } -} diff --git a/juick-server/src/main/java/com/juick/service/ShowQueriesService.java b/juick-server/src/main/java/com/juick/service/ShowQueriesService.java deleted file mode 100644 index a7e1c364..00000000 --- a/juick-server/src/main/java/com/juick/service/ShowQueriesService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.juick.service; - -import com.juick.User; - -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface ShowQueriesService { - List getRecommendedUsers(User forUser); - - List getTopUsers(); -} 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 2362e948..00000000 --- a/juick-server/src/main/java/com/juick/service/ShowQueriesServiceImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.juick.service; - -import com.juick.User; -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.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -@Transactional(readOnly = true) -public class ShowQueriesServiceImpl extends BaseJdbcService implements ShowQueriesService { - - @Inject - public ShowQueriesServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @Override - public List 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 getTopUsers() { - return getJdbcTemplate().queryForList( - "SELECT users.nick,COUNT(subscr_users.suser_id) AS cnt " + - "FROM (subscr_users INNER JOIN users ON subscr_users.user_id=users.id) " + - "INNER JOIN useroptions ON users.id=useroptions.user_id " + - "WHERE useroptions.privacy_view>0 AND users.lastmessage > UNIX_TIMESTAMP() - 259200 " + - "AND users.id!=2 GROUP BY subscr_users.user_id ORDER BY cnt DESC LIMIT 10", - String.class); - } -} diff --git a/juick-server/src/main/java/com/juick/service/SubscriptionService.java b/juick-server/src/main/java/com/juick/service/SubscriptionService.java deleted file mode 100644 index 074c73f5..00000000 --- a/juick-server/src/main/java/com/juick/service/SubscriptionService.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.User; -import com.juick.server.helpers.NotifyOpts; - -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface SubscriptionService { - List getJIDSubscribedToUser(int uid, boolean friendsonly); - - List getSubscribedUsers(int uid, int mid); - - List getUsersSubscribedToComments(int mid, int ignore_uid); - - List getUsersSubscribedToUserRecommendations(int uid, int mid, int muid); - - boolean subscribeMessage(int mid, int vuid); - - boolean unSubscribeMessage(int mid, int vuid); - - boolean subscribeUser(User user, User toUser); - - boolean unSubscribeUser(User user, User fromUser); - - boolean subscribeTag(User user, Tag toTag); - - boolean unSubscribeTag(User user, Tag toTag); - - List getSubscribedTags(User user); - - NotifyOpts getNotifyOptions(User user); - - boolean setNotifyOptions(User user, NotifyOpts options); -} 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 d1eef338..00000000 --- a/juick-server/src/main/java/com/juick/service/SubscriptionServiceImpl.java +++ /dev/null @@ -1,219 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.User; -import com.juick.server.helpers.NotifyOpts; -import org.apache.commons.lang3.StringUtils; -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 org.springframework.util.Assert; - -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 { - private final UserService userService; - private final MessagesService messagesService; - private final TagService tagService; - - @Inject - public SubscriptionServiceImpl(JdbcTemplate jdbcTemplate, UserService userService, - MessagesService messagesService, TagService tagService) { - super(jdbcTemplate, null); - - Assert.notNull(userService, "UserService must be initialized"); - this.userService = userService; - - Assert.notNull(messagesService,"MessagesService must be initialized"); - this.messagesService = messagesService; - - Assert.notNull(tagService, "TagService must be initialized"); - this.tagService = tagService; - } - - @Transactional(readOnly = true) - @Override - public List getJIDSubscribedToUser(final int uid, final boolean friendsonly) { - return getNamedParameterJdbcTemplate().queryForList( - "SELECT jids.jid FROM subscr_users INNER JOIN jids " + - "ON (subscr_users.user_id = :uid AND subscr_users.suser_id = jids.user_id) WHERE jids.active = 1 " + - (friendsonly ? - " AND EXISTS (SELECT 1 FROM wl_users w WHERE w.user_id = :uid and jids.user_id = w.wl_user_id)" : StringUtils.EMPTY), - new MapSqlParameterSource("uid", uid), - String.class); - } - - @Transactional(readOnly = true) - @Override - public List getSubscribedUsers(final int uid, final int mid) { - User author = messagesService.getMessageAuthor(mid); - - List userids = userService.getUserReaders(uid); - List tags = tagService.getMessageTagsIDs(mid); - List tagsStr = tagService.getMessageTags(mid).stream().map(t -> t.getTag().getName()).collect(Collectors.toList()); - - Set set = new HashSet<>(); - set.addAll( - userids.stream() - .map(User::getUid).filter(u -> Collections.disjoint(tagService.getUserBLTags(u), tagsStr)) - .collect(Collectors.toList())); - - - if (!tags.isEmpty()) { - List 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); - } - - @Transactional(readOnly = true) - @Override - public List getUsersSubscribedToComments(final int mid, final int ignore_uid) { - List userids = getJdbcTemplate().queryForList( - "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?", - Integer.class, - mid, ignore_uid); - - if (!userids.isEmpty()) - return userService.getUsersByID(userids); - - return Collections.emptyList(); - } - - @Transactional(readOnly = true) - @Override - public List getUsersSubscribedToUserRecommendations(final int uid, final int mid, final int muid) { - List tags = tagService.getMessageTagsIDs(mid); - - String query = "SELECT s.suser_id FROM subscr_users s WHERE s.user_id = :uid " + - " AND NOT EXISTS (SELECT 1 FROM bl_users b WHERE b.bl_user_id = :muid and b.user_id = s.user_id) " + - " AND NOT EXISTS (SELECT 1 FROM subscr_users s1 WHERE s1.user_id = :muid AND s.user_id = s1.suser_id) " + - " AND NOT EXISTS (SELECT 1 FROM subscr_messages sm WHERE sm.message_id = :mid AND s.user_id = sm.suser_id) " + - " AND NOT EXISTS (SELECT 1 FROM favorites WHERE favorites.message_id = :mid AND favorites.user_id = s.user_id) " + - " AND s.user_id NOT IN (SELECT s2.suser_id FROM subscr_users s2 " + - " INNER JOIN favorites f ON (f.message_id = :mid AND s2.user_id = f.user_id AND f.user_id != :uid))"; - - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("uid", uid) - .addValue("muid", muid) - .addValue("mid", mid); - - if (!tags.isEmpty()) { - sqlParameterSource.addValue("ids", tags); - query += " AND NOT EXISTS (SELECT 1 FROM subscr_tags st WHERE st.tag_id IN (:ids) AND s.user_id = st.suser_id) " + - " AND NOT EXISTS (SELECT 1 FROM bl_tags b WHERE b.tag_id IN (:ids) AND s.user_id = b.user_id)"; - } - - List userids = getNamedParameterJdbcTemplate().queryForList( - query, sqlParameterSource, Integer.class); - - return userService.getUsersByID(userids); - } - - @Transactional - @Override - public boolean subscribeMessage(final int mid, final int vuid) { - return getJdbcTemplate().update( - "INSERT IGNORE INTO subscr_messages(suser_id, message_id) VALUES (?, ?)", vuid, mid) == 1; - } - - @Transactional - @Override - public boolean unSubscribeMessage(final int mid, final int vuid) { - return getJdbcTemplate().update( - "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", mid, vuid) > 0; - } - - @Transactional - @Override - public boolean subscribeUser(final User user, final User toUser) { - return getJdbcTemplate().update( - "INSERT IGNORE INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(), user.getUid()) == 1; - } - - @Transactional - @Override - public boolean unSubscribeUser(final User user, final User fromUser) { - return getJdbcTemplate().update( - "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", user.getUid(), fromUser.getUid()) > 0; - } - - @Transactional - @Override - public boolean subscribeTag(final User user, final Tag toTag) { - return getJdbcTemplate().update( - "INSERT IGNORE INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID, user.getUid()) == 1; - } - - @Transactional - @Override - public boolean unSubscribeTag(final User user, final Tag toTag) { - return getJdbcTemplate().update( - "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0; - } - - @Transactional(readOnly = true) - @Override - public List getSubscribedTags(User user) { - return getJdbcTemplate().queryForList("SELECT tags.name FROM subscr_tags INNER JOIN tags USING(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 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/TagService.java b/juick-server/src/main/java/com/juick/service/TagService.java deleted file mode 100644 index 2fcc7097..00000000 --- a/juick-server/src/main/java/com/juick/service/TagService.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.server.helpers.TagStats; - -import java.util.Collection; -import java.util.List; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface TagService { - com.juick.Tag getTag(int tid); - - com.juick.Tag getTag(String tag, boolean autoCreate); - - List getTags(String[] tags, boolean autoCreate); - - boolean getTagNoIndex(int tagId); - - int createTag(String name); - - List getUserTagStats(int uid); - - List getUserBLTags(int uid); - - List getPopularTags(); - List getTagStats(); - - List updateTags(int mid, Collection newTags); - - List fromString(String txt, boolean tagsOnly); - - List getMessageTags(int mid); - - List getMessageTagsIDs(int mid); -} 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 b5bd0ae8..00000000 --- a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java +++ /dev/null @@ -1,260 +0,0 @@ -package com.juick.service; - -import com.juick.Tag; -import com.juick.server.helpers.TagStats; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.ArrayUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; -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 org.springframework.util.Assert; - -import javax.inject.Inject; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -public class TagServiceImpl extends BaseJdbcService implements TagService { - private static final Pattern TAGS_PATTERN1 = Pattern.compile("^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$"); - private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)\\s+([\\s\\S]+)"); - private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); - - private final MessagesService messagesService; - - @Inject - public TagServiceImpl(JdbcTemplate jdbcTemplate, MessagesService messagesService) { - super(jdbcTemplate, null); - Assert.notNull(messagesService, "MessagesService must be initialized"); - this.messagesService = messagesService; - } - - @Transactional(readOnly = true) - @Override - public com.juick.Tag getTag(final int tid) { - List 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 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 getTags(final String[] tags, final boolean autoCreate) { - if (ArrayUtils.isEmpty(tags)) - return Collections.emptyList(); - - List ret = new ArrayList<>(); - - for (String tag : tags) { - if (!tag.isEmpty()) { - Tag t = getTag(tag, autoCreate); - - if (t != null) - ret.add(t); - } - } - - return ret.stream().distinct().collect(Collectors.toList()); - } - - @Transactional(readOnly = true) - @Override - public boolean getTagNoIndex(final int tagId) { - List 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 { - - @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 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 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 getPopularTags() { - return getJdbcTemplate().queryForList( - "SELECT name FROM tags WHERE top=1 ORDER BY name ASC", String.class) - .stream() - .collect(Collectors.toList()); - } - - @Transactional(readOnly = true) - @Override - public List 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 updateTags(final int mid, final Collection newTags) { - List currentTags = getMessageTags(mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); - - if (CollectionUtils.isEmpty(newTags)) - return currentTags; - - List idsForDelete = newTags.stream() - .filter(currentTags::contains) - .map(tag -> tag.TID) - .collect(Collectors.toList()); - - 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)); - - return getMessageTags(mid).stream() - .map(TagStats::getTag).collect(Collectors.toList()); - } - - @Override - public List fromString(final String txt, final boolean tagsOnly) { - Pattern tagsPattern = tagsOnly ? TAGS_PATTERN1 : TAGS_PATTERN2; - - if (tagsPattern.matcher(txt).matches()) { - Matcher tagMatcher = TAG_PATTERN.matcher(txt); - - List tags = new ArrayList<>(); - - // TODO: process readonly, private, friends, public - while (tagMatcher.find()) { - for (int i = 1; i <= tagMatcher.groupCount(); i++) - tags.add(getTag(tagMatcher.group(i), true)); - } - return tags; - } - return Collections.emptyList(); - } - - @Transactional(readOnly = true) - @Override - public List 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 getMessageTagsIDs(final int mid) { - return getJdbcTemplate().queryForList( - "SELECT tag_id FROM messages_tags WHERE message_id = ?", - Integer.class, mid); - } -} diff --git a/juick-server/src/main/java/com/juick/service/TelegramService.java b/juick-server/src/main/java/com/juick/service/TelegramService.java deleted file mode 100644 index b23e3405..00000000 --- a/juick-server/src/main/java/com/juick/service/TelegramService.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.juick.service; - -import java.util.List; - -/** - * Created by vt on 24/11/2016. - */ -public interface TelegramService { - boolean addChat(Long id); - - List getChats(); - - int getUser(long tgId); - - boolean createTelegramUser(long tgID, String tgName); - - boolean deleteTelegramUser(Integer uid); - - List getSubscribers(int uid); - - List getSubscribersToComments(int mid, int ignore_uid); -} diff --git a/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java b/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java deleted file mode 100644 index a698e2e8..00000000 --- a/juick-server/src/main/java/com/juick/service/TelegramServiceImpl.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.juick.service; - -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import javax.inject.Inject; -import java.util.List; -import java.util.UUID; - -/** - * Created by vt on 24/11/2016. - */ -@Repository -public class TelegramServiceImpl extends BaseJdbcService implements TelegramService { - - @Inject - public TelegramServiceImpl(JdbcTemplate jdbc) { - super(jdbc, null); - } - - @Transactional - @Override - public boolean addChat(final Long id) { - return getJdbcTemplate().update("INSERT IGNORE INTO telegram_chats(chat_id) VALUES(?)", id) > 0; - } - - @Transactional(readOnly = true) - @Override - public List getChats() { - return getJdbcTemplate().queryForList("SELECT chat_id FROM telegram_chats", Long.class); - } - - @Transactional(readOnly = true) - @Override - public int getUser(final long tgId) { - List 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 getSubscribers(final int uid) { - return getJdbcTemplate().queryForList("" + - "SELECT tg_id FROM telegram INNER JOIN subscr_users " + - "ON (subscr_users.user_id=? AND telegram.user_id=subscr_users.suser_id)", Long.class, uid); - } - - @Transactional(readOnly = true) - @Override - public List getSubscribersToComments(final int mid, final int ignore_uid) { - return getJdbcTemplate().queryForList( - "SELECT tg_id FROM telegram INNER JOIN subscr_messages " + - "ON (telegram.user_id=subscr_messages.suser_id) WHERE message_id=? AND suser_id!=?", Long.class, mid, ignore_uid); - } -} diff --git a/juick-server/src/main/java/com/juick/service/UserService.java b/juick-server/src/main/java/com/juick/service/UserService.java deleted file mode 100644 index a6db9f82..00000000 --- a/juick-server/src/main/java/com/juick/service/UserService.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.juick.service; - -import com.juick.User; -import com.juick.server.helpers.Auth; -import com.juick.server.helpers.EmailOpts; -import com.juick.server.helpers.UserInfo; - -import java.util.Collection; -import java.util.List; -import java.util.Optional; - -/** - * Created by aalexeev on 11/13/16. - */ -public interface UserService { - enum ActiveStatus { - Inactive, - Active - } - - String getSignUpHashByJID(String jid); - - String getSignUpHashByTelegramID(Long telegramId, String username); - - int createUser(String username, String password); - - Optional getUserByUID(int uid); - - User getUserByName(String username); - - User getFullyUserByName(String username); - - User getUserByEmail(String email); - - List getFullyUsersByNames(Collection usernames); - - User getUserByJID(String jid); - - List getUsersByName(Collection unames); - - List getUsersByID(Collection uids); - - List getUsersByJID(Collection jids); - - List getJIDsbyUID(int uid); - - int getUIDbyJID(String jid); - - int getUIDbyName(String uname); - - int getUIDbyHash(String hash); - - com.juick.User getUserByHash(String hash); - - String getHashByUID(int uid); - - int getUIDByHttpAuth(String header); - - int checkPassword(String username, String password); - - boolean updatePassword(User user, String newPassword); - - int getUserOptionInt(int uid, String option, int defaultValue); - - int setUserOptionInt(int uid, String option, int value); - - UserInfo getUserInfo(User user); - - boolean updateUserInfo(User user, UserInfo info); - - boolean getCanMedia(int uid); - - boolean isInWL(int uid, int check); - - boolean isInBL(int uid, int check); - - boolean isInBLAny(int uid, int uid2); - - List checkBL(int visitor, Collection uids); - - boolean isSubscribed(int uid, int check); - - List getUserRead(int uid); - - List getUserReadLeastPopular(int uid, int cnt); - - List getUserReaders(int uid); - - List getUserFriends(int uid); - - List getUserBLUsers(int uid); - - boolean linkTwitterAccount(User user, String accessToken, String accessTokenSecret, String screenName); - - int getStatsIRead(int uid); - - int getStatsMyReaders(int uid); - - int getStatsMessages(int uid); - - int getStatsReplies(int uid); - - boolean setActiveStatusForJID(String JID, ActiveStatus jidStatus); - - List getAllJIDs(User user); - - List getAuthCodes(User user); - - List getEmails(User user); - - EmailOpts getEmailOpts(User user); - - String getEmailHash(User user); - - int deleteLoginForUser(String name); - - int setLoginForUser(int uid, String loginHash); - - void logout(int uid); - - boolean deleteJID(int uid, String jid); - - boolean unauthJID(int uid, String jid); - - List getActiveJIDs(); -} 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 6a3bb40c..00000000 --- a/juick-server/src/main/java/com/juick/service/UserServiceImpl.java +++ /dev/null @@ -1,704 +0,0 @@ -package com.juick.service; - -import com.juick.User; -import com.juick.server.helpers.Auth; -import com.juick.server.helpers.EmailOpts; -import com.juick.server.helpers.UserInfo; -import com.juick.server.security.entities.AnonymousUser; -import com.juick.util.UserUtils; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.dao.DuplicateKeyException; -import org.springframework.jdbc.core.JdbcTemplate; -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.inject.Inject; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.*; - -/** - * Created by aalexeev on 11/13/16. - */ -@Repository -public class UserServiceImpl extends BaseJdbcService implements UserService { - - private class UserMapper implements RowMapper { - @Override - public User mapRow(ResultSet rs, int rowNum) throws SQLException { - User user = new User(); - - user.setUid(rs.getInt(1)); - user.setName(rs.getString(2)); - user.setBanned(rs.getBoolean(3)); - user.setLang(rs.getString(4)); - - return user; - } - } - - @Inject - public UserServiceImpl(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate, null); - } - - @Transactional - @Override - public String getSignUpHashByJID(final String jid) { - List 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 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 getUserByUID(final int uid) { - List list = getJdbcTemplate().query( - "SELECT id, nick, banned, lang FROM users WHERE id = ?", new UserMapper(), uid); - - return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); - } - - @Transactional(readOnly = true) - @Override - public User getUserByName(final String username) { - if (StringUtils.isNotBlank(username)) { - List list = getJdbcTemplate().query( - "SELECT id, nick, banned, lang FROM users WHERE nick = ?", new UserMapper(), username); - - if (!list.isEmpty()) - return list.get(0); - } - // TODO: @NonNullable ? - return AnonymousUser.INSTANCE; - } - - @Override - // No need marks with @Transactional annotation - public User getFullyUserByName(final String username) { - if (StringUtils.isNotBlank(username)) { - List list = getFullyUsersByNames(Collections.singletonList(username)); - if (!list.isEmpty()) - return list.get(0); - } - return null; - } - - @Override - @Transactional(readOnly = true) - public User getUserByEmail(String email) { - if (StringUtils.isNotBlank(email)) { - List list = getJdbcTemplate().query( - "SELECT id, nick, banned, lang FROM users WHERE id = (SELECT user_id FROM emails WHERE email = ?)", - new UserMapper(), - email); - - if (!list.isEmpty()) - return list.get(0); - } - return AnonymousUser.INSTANCE; - } - - @Transactional(readOnly = true) - @Override - public List getFullyUsersByNames(final Collection usernames) { - if (CollectionUtils.isEmpty(usernames)) - return Collections.emptyList(); - - return getNamedParameterJdbcTemplate().query( - "SELECT id, nick, passw, lang, banned FROM users WHERE nick in (:names)", - new MapSqlParameterSource("names", usernames), - (rs, rowNum) -> { - User user = new User(); - - user.setUid(rs.getInt(1)); - user.setName(rs.getString(2)); - user.setCredentials(rs.getString(3)); - user.setLang(rs.getString(4)); - user.setBanned(rs.getBoolean(5)); - - return user; - }); - } - - @Transactional(readOnly = true) - @Override - public User getUserByJID(final String jid) { - User result = null; - - if (StringUtils.isNotBlank(jid)) { - List list = getJdbcTemplate().query( - "SELECT id, nick, banned, lang 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 getUsersByName(final Collection unames) { - if (CollectionUtils.isEmpty(unames)) - return Collections.emptyList(); - - return getNamedParameterJdbcTemplate().query( - "SELECT id, nick, banned, lang FROM users WHERE nick IN (:unames)", - new MapSqlParameterSource("unames", unames), - new UserMapper()); - } - - @Transactional(readOnly = true) - @Override - public List getUsersByID(final Collection uids) { - if (CollectionUtils.isEmpty(uids)) - return Collections.emptyList(); - - return getNamedParameterJdbcTemplate().query( - "SELECT id, nick, banned, lang FROM users WHERE id IN (:ids)", - new MapSqlParameterSource("ids", uids), - new UserMapper()); - } - - @Transactional(readOnly = true) - @Override - public List getUsersByJID(final Collection jids) { - if (CollectionUtils.isEmpty(jids)) - return Collections.emptyList(); - - return getNamedParameterJdbcTemplate().query( - "SELECT users.id, users.nick, jids.jid FROM users " + - " INNER JOIN jids ON jids.user_id = users.id " + - " WHERE jids.jid IN (:jids)", - new MapSqlParameterSource("jids", jids), - (rs, rowNum) -> { - com.juick.User user = new com.juick.User(); - user.setUid(rs.getInt(1)); - user.setName(rs.getString(2)); - user.setJid(rs.getString(3)); - return user; - }); - } - - @Transactional(readOnly = true) - @Override - public List 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 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 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 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 list = getJdbcTemplate().query( - "SELECT logins.user_id, users.nick, users.banned, users.lang 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 list = getJdbcTemplate().queryForList( - "SELECT hash FROM logins WHERE user_id = ?", String.class, uid); - - if (list.isEmpty()) { - String hash = UserUtils.generateHash(16); - getJdbcTemplate().update("INSERT INTO logins(user_id, hash) VALUES (?, ?)", uid, hash); - return hash; - } - return list.get(0); - } - - @Override - public int getUIDByHttpAuth(String auth) { - if (auth != null && auth.length() > 8 && auth.startsWith("Basic ")) { - Base64.Decoder dec = Base64.getDecoder(); - String loginpassw[] = new String(dec.decode(auth.substring(6))).split(":", 2); - if (loginpassw.length == 2 && loginpassw[0].length() > 1 && loginpassw[0].length() < 16 && loginpassw[0].matches("[a-zA-Z0-9\\-]+") && !loginpassw[1].isEmpty()) { - return checkPassword(loginpassw[0], loginpassw[1]); - } - } - return 0; - } - - @Transactional(readOnly = true) - @Override - public int checkPassword(final String username, final String password) { - if (StringUtils.isNotBlank(username)) { - List list = getJdbcTemplate().query( - "SELECT id, nick, banned, passw FROM users WHERE nick = ?", - (rs, rowNum) -> { - User user = new User(); - user.setUid(rs.getInt(1)); - user.setName(rs.getString(2)); - user.setBanned(rs.getBoolean(3)); - user.setCredentials(rs.getString(4)); - return user; - }, - 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 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 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 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 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 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 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 List checkBL(final int visitor, final Collection 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 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 getUserRead(final int uid) { - return getJdbcTemplate().queryForList( - "SELECT user_id FROM subscr_users WHERE suser_id=?", Integer.class, uid); - } - - @Transactional(readOnly = true) - @Override - public List 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 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 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 List 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) { - if (getJdbcTemplate().update("INSERT INTO twitter(user_id,access_token,access_token_secret,uname) " + - "VALUES (?,?,?,?)" + - " ON DUPLICATE KEY UPDATE access_token=?,access_token_secret=?,uname=?", - user.getUid(), accessToken, accessTokenSecret, screenName, accessToken, accessTokenSecret, screenName) > 0) { - return getJdbcTemplate().update("INSERT INTO subscr_users(user_id,suser_id,jid) " + - "VALUES (?,1741,'juick\\@twitter.juick.com')", user.getUid()) > 0; - } - return false; - - } - - @Transactional(readOnly = true) - @Override - public int getStatsIRead(final int uid) { - List list = getJdbcTemplate().queryForList( - "SELECT COUNT(*) FROM subscr_users WHERE suser_id = ?", Integer.class, uid); - return list.isEmpty() ? 0 : list.get(0); - } - - @Transactional(readOnly = true) - @Override - public int getStatsMyReaders(final int uid) { - List 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 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 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 getAllJIDs(final User user) { - return getJdbcTemplate().queryForList( - "SELECT jid FROM jids WHERE user_id=?", String.class, user.getUid()); - } - - @Transactional(readOnly = true) - @Override - public List 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 getEmails(final User user) { - return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=?", String.class, user.getUid()); - } - - @Transactional(readOnly = true) - @Override - public EmailOpts getEmailOpts(final User user) { - List list = getJdbcTemplate().query( - "SELECT email,subscr_hour FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", - (rs, num) -> new EmailOpts(rs.getString(1), rs.getInt(2)), user.getUid()); - return list.isEmpty() ? new EmailOpts("", 0) : list.get(0); - } - - @Transactional(readOnly = true) - @Override - public String getEmailHash(final User user) { - List 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 getJdbcTemplate().update("DELETE FROM jids WHERE user_id=? AND jid=?", uid, jid) > 0; - } - - @Transactional - @Override - public boolean unauthJID(int uid, String jid) { - return getJdbcTemplate() - .update("DELETE FROM auth WHERE user_id=? AND protocol='xmpp' AND account=?", uid, jid) > 0; - } - - @Transactional(readOnly = true) - @Override - public List getActiveJIDs() { - return getJdbcTemplate().queryForList("SELECT jid FROM jids WHERE active=1 AND loginhash IS NULL", String.class); - } -} diff --git a/juick-server/src/main/java/com/juick/service/search/SearchService.java b/juick-server/src/main/java/com/juick/service/search/SearchService.java deleted file mode 100644 index 21deb0b1..00000000 --- a/juick-server/src/main/java/com/juick/service/search/SearchService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.juick.service.search; - -import java.util.List; - -/** - * Created by aalexeev on 11/18/16. - */ -public interface SearchService { - void setMaxResult(int maxResult); - - List searchInAllMessages(String searchString, int messageIdBefore); - - List searchByStringAndUser(String searchString, final int userId, int messageIdBefore); -} diff --git a/juick-server/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java b/juick-server/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java deleted file mode 100644 index aa903545..00000000 --- a/juick-server/src/main/java/com/juick/service/search/SphinxSearchServiceImpl.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.juick.service.search; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; -import org.springframework.util.Assert; - -import javax.sql.DataSource; -import java.util.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/18/16. - */ - -/* Note - * Don't use any spring's component annotation (such as @Repository, @Service, @Component, etc). - * This class directly used by spring's search configuration class - */ -public class SphinxSearchServiceImpl implements SearchService { - private static final int DEFAULT_MAX_RESULT = 25; - - private final NamedParameterJdbcTemplate namedParameterSearchJdbcTemplate; - - private int maxResult = DEFAULT_MAX_RESULT; - - - public SphinxSearchServiceImpl(JdbcTemplate searchJdbcTemplate) { - Assert.notNull(searchJdbcTemplate, "JdbcTemplate must be initialized"); - this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchJdbcTemplate); - } - - public SphinxSearchServiceImpl(DataSource searchDataSource) { - Assert.notNull(searchDataSource, "DataSource must be initialized"); - this.namedParameterSearchJdbcTemplate = new NamedParameterJdbcTemplate(searchDataSource); - } - - @Override - public List searchInAllMessages(final String searchString, final int messageIdBefore) { - if (StringUtils.isBlank(searchString)) - return Collections.emptyList(); - - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("before", messageIdBefore) - .addValue("limit", maxResult); - - return namedParameterSearchJdbcTemplate.queryForList( - "SELECT id AS message_id FROM messages WHERE MATCH(:search) " + - (messageIdBefore > 0 ? - " AND id < :before " : StringUtils.EMPTY) + - " ORDER BY id DESC LIMIT :limit", - sqlParameterSource, - Integer.class); - } - - @Override - public List searchByStringAndUser(final String searchString, final int userId, int messageIdBefore) { - if (StringUtils.isBlank(searchString)) - return Collections.emptyList(); - - MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource() - .addValue("search", searchString) - .addValue("userId", userId) - .addValue("before", messageIdBefore) - .addValue("limit", maxResult); - - return namedParameterSearchJdbcTemplate.queryForList( - "SELECT id AS message_id FROM messages WHERE user_id = :userId AND MATCH(:search) " + - (messageIdBefore > 0 ? - " AND id < :before " : StringUtils.EMPTY) + - " ORDER BY id DESC LIMIT :limit", - sqlParameterSource, - 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/security/JuickUserDetailsService.java b/juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java deleted file mode 100644 index 6f9ece7e..00000000 --- a/juick-server/src/main/java/com/juick/service/security/JuickUserDetailsService.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.juick.service.security; - -import com.juick.server.security.entities.JuickUser; -import com.juick.service.UserService; -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.getFullyUserByName(username); - - if (user != null) - 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 49e9effc..00000000 --- a/juick-server/src/main/java/com/juick/service/security/NullUserDetailsService.java +++ /dev/null @@ -1,16 +0,0 @@ -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 25ea645b..00000000 --- a/juick-server/src/main/java/com/juick/service/security/deprecated/CookieSimpleHashRememberMeServices.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.juick.service.security.deprecated; - -import com.juick.User; -import com.juick.server.security.entities.JuickUser; -import com.juick.service.UserService; -import com.juick.service.security.NullUserDetailsService; -import com.juick.util.UserUtils; -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", "juick.com")); - 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 = UserUtils.generateHash(16); - - 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 userOptional = userService.getUserByUID(uid); - - Assert.isTrue(userOptional.isPresent()); - - return new JuickUser(userOptional.get()); - } - - @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 0e635927..00000000 --- a/juick-server/src/main/java/com/juick/service/security/deprecated/RequestParamHashRememberMeServices.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.juick.service.security.deprecated; - -import com.juick.User; -import com.juick.server.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.getUid() > 0) - return new JuickUser(user); - } - throw new UsernameNotFoundException("User not found by hash " + hash); - } -} diff --git a/juick-server/src/main/java/com/juick/util/TagUtils.java b/juick-server/src/main/java/com/juick/util/TagUtils.java deleted file mode 100644 index fec59f29..00000000 --- a/juick-server/src/main/java/com/juick/util/TagUtils.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.juick.util; - -import com.juick.Tag; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; - -import java.util.List; -import java.util.stream.Collectors; - -/** - * Created by aalexeev on 11/13/16. - */ -public class TagUtils { - private TagUtils() { - throw new IllegalStateException(); - } - - public static String toString(final List tags) { - if (CollectionUtils.isEmpty(tags)) - return StringUtils.EMPTY; - - return tags.stream().map(t -> " *" + t.getName()) - .collect(Collectors.joining()); - } -} diff --git a/juick-server/src/main/java/com/juick/util/UserUtils.java b/juick-server/src/main/java/com/juick/util/UserUtils.java deleted file mode 100644 index 3d78b5b5..00000000 --- a/juick-server/src/main/java/com/juick/util/UserUtils.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.juick.util; - -import com.juick.User; -import com.juick.server.security.entities.AnonymousUser; -import com.juick.server.security.entities.JuickUser; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; - -import java.util.Random; - -/** - * Created by aalexeev on 11/14/16. - */ -public class UserUtils { - private UserUtils() { - throw new IllegalStateException(); - } - - private static final String ABCDEF = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - - public static String generateHash(final int len) { - Random rnd = new Random(); - StringBuilder sb = new StringBuilder(len); - for (int i = 0; i < len; i++) { - sb.append(ABCDEF.charAt(rnd.nextInt(ABCDEF.length()))); - } - return sb.toString(); - } - - public static Authentication getAuthentication() { - return SecurityContextHolder.getContext().getAuthentication(); - } - - public static Object getPrincipal(final Authentication authentication) { - return authentication == null ? null : authentication.getPrincipal(); - } - - public static User getCurrentUser() { - Object principal = getPrincipal(getAuthentication()); - - if (principal instanceof JuickUser) - return ((JuickUser) principal).getUser(); - - if (principal instanceof User) - return (User) principal; - - return AnonymousUser.INSTANCE; - } -} diff --git a/juick-server/src/main/java/com/juick/util/WebUtils.java b/juick-server/src/main/java/com/juick/util/WebUtils.java deleted file mode 100644 index 8e240c5d..00000000 --- a/juick-server/src/main/java/com/juick/util/WebUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.juick.util; - -import java.util.regex.Pattern; - -/** - * Created by aalexeev on 11/28/16. - */ -public class WebUtils { - private WebUtils() { - throw new IllegalStateException(); - } - - private static final Pattern USER_NAME_PATTERN = Pattern.compile("[a-zA-Z-_\\d]{2,16}"); - - private static final Pattern POST_NUMBER_PATTERN = Pattern.compile("-?\\d+"); - - private static final Pattern JID_PATTERN = Pattern.compile("^[a-zA-Z0-9\\\\-\\\\_\\\\@\\\\.]{6,64}$"); - - - public static boolean isPostNumber(final String aString) { - return aString != null && POST_NUMBER_PATTERN.matcher(aString).matches(); - } - - public static boolean isNotPostNumber(final String aString) { - return !isPostNumber(aString); - } - - public static boolean isUserName(final String aString) { - return aString != null && USER_NAME_PATTERN.matcher(aString).matches(); - } - - public static boolean isNotUserName(final String aString) { - return !isUserName(aString); - } - - public static boolean isJid(final String aString) { - return aString != null && JID_PATTERN.matcher(aString).matches(); - } - - public static boolean isNotJid(final String aString) { - return !isJid(aString); - } - - -} diff --git a/juick-server/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java b/juick-server/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java deleted file mode 100644 index 3da660b2..00000000 --- a/juick-server/src/main/java/rocks/xmpp/core/session/debug/LogbackDebugger.java +++ /dev/null @@ -1,39 +0,0 @@ -package rocks.xmpp.core.session.debug; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import rocks.xmpp.core.session.XmppSession; - -import java.io.InputStream; -import java.io.OutputStream; - -/** - * Created by vitalyster on 17.11.2016. - */ -public class LogbackDebugger implements XmppDebugger { - private static final Logger logger = LoggerFactory.getLogger(LogbackDebugger.class); - - @Override - public void initialize(XmppSession xmppSession) { - } - - @Override - public void writeStanza(String s, Object o) { - logger.info("OUT: {}", s); - } - - @Override - public void readStanza(String s, Object o) { - logger.info("IN: {}", s); - } - - @Override - public OutputStream createOutputStream(OutputStream outputStream) { - return outputStream; - } - - @Override - public InputStream createInputStream(InputStream inputStream) { - return inputStream; - } -} diff --git a/juick-server/src/main/resources/juick.conf.example b/juick-server/src/main/resources/juick.conf.example deleted file mode 100644 index f89ca82b..00000000 --- a/juick-server/src/main/resources/juick.conf.example +++ /dev/null @@ -1,86 +0,0 @@ -### Main database JDBC connection settings ### -# Main database JDBC driver -datasource_driver=net.sf.log4jdbc.DriverSpy -!datasource_driver=com.mysql.jdbc.Driver - -# Main database JDBC Url -!datasource_url=jdbc:mysql://localhost:3306/juick?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8 -datasource_url=jdbc:log4jdbc:mysql://localhost:3306/juick?autoReconnect=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8 - -# Main database username -datasource_user= - -# Main database password -datasource_password= - -### Sphinx search connection -# Sphinx search JDBC driver -sphinx_driver=com.mysql.jdbc.Driver - -# Sphinx search JDBC url -sphinx_url=jdbc:mysql://127.0.0.1:9306?autoReconnect=true&useUnicode=yes&characterEncoding=utf8&maxAllowedPacket=512000 - -# Sphinx search JDBC username -sphinx_user= - -# Sphinx search JDBC password -sphinx_password= - -# The domain name for Web (default value - "juick.com") -web_domain=juick.com - -# Authority remember-me key -auth_remember_me_key=3vHcy3OUDQlkpRDm - - -### Template Settings (web page templates) -# Show sponsors block -template.showSponsors=false - -# Show Sape scripts -template.showSape=true - -# Show Advertisement -template.showAdv=true - - -api_user=juick -api_password=secret - -ios_pkcs12_file= -ios_pkcs12_password=secret - - -twitter_consumer_key= -twitter_consumer_secret= - -crosspost_jid= -ws_jid= -push_jid= - -xmpp_host= -xmpp_port= -xmpp_password= - -push_xmpp_password= - -wns_application_sip= -wns_client_secret= -gcm_key= - -hostname= -componentname= -component_name= -component_host= -component_port= -s2s_port= -xmppbot_jid=juick@juick.com/Juick - -keystore= -keystore_password= -broken_ssl_hosts= -banned_hosts= - -upload_tmp_dir= - -xmpp_disabled=false \ No newline at end of file diff --git a/juick-server/src/main/resources/logback.xml.example b/juick-server/src/main/resources/logback.xml.example deleted file mode 100644 index 05a3cfdf..00000000 --- a/juick-server/src/main/resources/logback.xml.example +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/juick-server/src/main/resources/schema.sql b/juick-server/src/main/resources/schema.sql deleted file mode 100644 index 90bb4ed4..00000000 --- a/juick-server/src/main/resources/schema.sql +++ /dev/null @@ -1,885 +0,0 @@ --- MySQL dump 10.13 Distrib 5.5.44, for debian-linux-gnu (x86_64) --- --- Host: localhost Database: juick --- ------------------------------------------------------ --- Server version 5.5.44-0+deb8u1-log -use juick; -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8mb4 */; -/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; -/*!40103 SET TIME_ZONE='+00:00' */; -/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; -/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; -/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; - --- --- Table structure for table `ads_messages` --- - -DROP TABLE IF EXISTS `ads_messages`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `ads_messages` ( - `message_id` int(10) unsigned NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `ads_messages_log` --- - -DROP TABLE IF EXISTS `ads_messages_log`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `ads_messages_log` ( - `user_id` int(10) unsigned NOT NULL, - `message_id` int(10) unsigned NOT NULL, - `ts` int(10) unsigned NOT NULL DEFAULT '0' -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `android` --- - -DROP TABLE IF EXISTS `android`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `android` ( - `user_id` int(10) unsigned NOT NULL, - `regid` char(255) NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE KEY `regid` (`regid`), - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `auth` --- - -DROP TABLE IF EXISTS `auth`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `auth` ( - `user_id` int(10) unsigned NOT NULL, - `protocol` enum('xmpp','email','sms') NOT NULL, - `account` char(64) NOT NULL, - `authcode` char(8) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `bl_tags` --- - -DROP TABLE IF EXISTS `bl_tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `bl_tags` ( - `user_id` int(10) unsigned NOT NULL, - `tag_id` int(10) unsigned NOT NULL, - KEY `tag_id` (`tag_id`), - KEY `user_id` (`user_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `bl_users` --- - -DROP TABLE IF EXISTS `bl_users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `bl_users` ( - `user_id` int(10) unsigned NOT NULL, - `bl_user_id` int(10) unsigned NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - PRIMARY KEY (`user_id`,`bl_user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `captcha` --- - -DROP TABLE IF EXISTS `captcha`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `captcha` ( - `jid` char(64) NOT NULL, - `hash` char(16) NOT NULL, - `confirmed` tinyint(4) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `captchaimg` --- - -DROP TABLE IF EXISTS `captchaimg`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `captchaimg` ( - `id` char(16) NOT NULL, - `txt` char(6) NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `ip` char(16) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `emails` --- - -DROP TABLE IF EXISTS `emails`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `emails` ( - `user_id` int(10) unsigned NOT NULL, - `email` char(64) NOT NULL, - `subscr_hour` tinyint(4) DEFAULT NULL, - KEY `email` (`email`) USING HASH -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `facebook` --- - -DROP TABLE IF EXISTS `facebook`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `facebook` ( - `user_id` int(10) unsigned DEFAULT NULL, - `fb_id` bigint(20) unsigned NOT NULL, - `loginhash` char(36) DEFAULT NULL, - `access_token` char(255) DEFAULT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `fb_name` char(64) NOT NULL, - `fb_link` char(64) NOT NULL, - `crosspost` tinyint(1) unsigned NOT NULL DEFAULT '1', - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `favorites` --- - -DROP TABLE IF EXISTS `favorites`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `favorites` ( - `user_id` int(10) unsigned NOT NULL, - `message_id` int(10) unsigned NOT NULL, - `ts` datetime NOT NULL, - UNIQUE KEY `user_id_2` (`user_id`,`message_id`), - KEY `user_id` (`user_id`), - KEY `message_id` (`message_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `friends_facebook` --- - -DROP TABLE IF EXISTS `friends_facebook`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `friends_facebook` ( - `user_id` int(10) unsigned NOT NULL, - `friend_id` bigint(20) unsigned NOT NULL, - UNIQUE KEY `user_id` (`user_id`,`friend_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `images` --- - -DROP TABLE IF EXISTS `images`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `images` ( - `mid` int(10) unsigned NOT NULL, - `rid` int(10) unsigned NOT NULL, - `thumb` int(10) unsigned NOT NULL, - `small` int(10) unsigned NOT NULL, - `medium` int(10) unsigned NOT NULL, - `height` int(10) unsigned NOT NULL, - `width` int(10) unsigned NOT NULL, - PRIMARY KEY (`mid`,`rid`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `ios` --- - -DROP TABLE IF EXISTS `ios`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `ios` ( - `user_id` int(10) unsigned NOT NULL, - `token` char(64) COLLATE utf8mb4_unicode_ci NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE KEY `token` (`token`), - KEY `user_id` (`user_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `jids` --- - -DROP TABLE IF EXISTS `jids`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `jids` ( - `user_id` int(10) unsigned DEFAULT NULL, - `jid` char(64) NOT NULL, - `active` tinyint(1) NOT NULL DEFAULT '1', - `loginhash` char(36) DEFAULT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE KEY `jid` (`jid`), - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `logins` --- - -DROP TABLE IF EXISTS `logins`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `logins` ( - `user_id` int(10) unsigned NOT NULL, - `hash` char(16) NOT NULL, - UNIQUE KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `mail` --- - -DROP TABLE IF EXISTS `mail`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `mail` ( - `user_id` int(10) unsigned NOT NULL, - `hash` char(16) NOT NULL, - PRIMARY KEY (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `meon` --- - -DROP TABLE IF EXISTS `meon`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `meon` ( - `id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `user_id` int(10) unsigned NOT NULL, - `link` char(255) NOT NULL, - `name` char(32) NOT NULL, - `ico` smallint(5) unsigned DEFAULT NULL, - PRIMARY KEY (`id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `messages` --- - -DROP TABLE IF EXISTS `messages`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `messages` ( - `message_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `user_id` int(10) unsigned NOT NULL, - `lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__', - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `replies` smallint(5) unsigned NOT NULL DEFAULT '0', - `maxreplyid` smallint(5) unsigned NOT NULL DEFAULT '0', - `privacy` tinyint(4) NOT NULL DEFAULT '1', - `readonly` tinyint(1) NOT NULL DEFAULT '0', - `attach` enum('jpg','mp4','png') DEFAULT NULL, - `place_id` int(10) unsigned DEFAULT NULL, - `lat` decimal(10,7) DEFAULT NULL, - `lon` decimal(10,7) DEFAULT NULL, - `popular` tinyint(4) NOT NULL DEFAULT '0', - `hidden` tinyint(3) unsigned NOT NULL DEFAULT '0', - `likes` smallint(6) NOT NULL DEFAULT '0', - PRIMARY KEY (`message_id`), - KEY `user_id` (`user_id`), - KEY `ts` (`ts`), - KEY `attach` (`attach`), - KEY `place_id` (`place_id`), - KEY `popular` (`popular`), - KEY `hidden` (`hidden`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `messages_access` --- - -DROP TABLE IF EXISTS `messages_access`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `messages_access` ( - `message_id` int(10) unsigned NOT NULL, - `user_id` int(10) unsigned NOT NULL, - KEY `message_id` (`message_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `messages_tags` --- - -DROP TABLE IF EXISTS `messages_tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `messages_tags` ( - `message_id` int(10) unsigned NOT NULL, - `tag_id` int(10) unsigned NOT NULL, - UNIQUE KEY `message_id_2` (`message_id`,`tag_id`), - KEY `message_id` (`message_id`), - KEY `tag_id` (`tag_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `messages_txt` --- - -DROP TABLE IF EXISTS `messages_txt`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `messages_txt` ( - `message_id` int(10) unsigned NOT NULL, - `tags` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - `repliesby` varchar(96) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - `txt` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, - PRIMARY KEY (`message_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `messages_votes` --- - -DROP TABLE IF EXISTS `messages_votes`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `messages_votes` ( - `message_id` int(10) unsigned NOT NULL, - `user_id` int(10) unsigned NOT NULL, - `vote` tinyint(4) NOT NULL DEFAULT '1', - UNIQUE KEY `message_id` (`message_id`,`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `places` --- - -DROP TABLE IF EXISTS `places`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `places` ( - `place_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `lat` decimal(10,7) NOT NULL, - `lon` decimal(10,7) NOT NULL, - `name` char(64) NOT NULL, - `descr` char(255) DEFAULT NULL, - `url` char(128) DEFAULT NULL, - `user_id` int(10) unsigned NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - PRIMARY KEY (`place_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `places_tags` --- - -DROP TABLE IF EXISTS `places_tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `places_tags` ( - `place_id` int(10) unsigned NOT NULL, - `tag_id` int(10) unsigned NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `pm` --- - -DROP TABLE IF EXISTS `pm`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `pm` ( - `user_id` int(10) unsigned NOT NULL, - `user_id_to` int(10) unsigned NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - `txt` text NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `pm_inroster` --- - -DROP TABLE IF EXISTS `pm_inroster`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `pm_inroster` ( - `user_id` int(10) unsigned NOT NULL, - `jid` char(64) NOT NULL, - UNIQUE KEY `user_id_2` (`user_id`,`jid`), - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `pm_streams` --- - -DROP TABLE IF EXISTS `pm_streams`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `pm_streams` ( - `user_id` int(10) unsigned NOT NULL, - `user_id_to` int(10) unsigned NOT NULL, - `lastmessage` datetime NOT NULL, - `lastview` datetime DEFAULT NULL, - `unread` smallint(5) unsigned NOT NULL DEFAULT '0', - UNIQUE KEY `user_id` (`user_id`,`user_id_to`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `presence` --- - -DROP TABLE IF EXISTS `presence`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `presence` ( - `user_id` int(10) unsigned NOT NULL, - `jid` char(64) DEFAULT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - UNIQUE KEY `jid` (`jid`) -) ENGINE=MEMORY DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `reader_links` --- - -DROP TABLE IF EXISTS `reader_links`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `reader_links` ( - `link_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `rss_id` int(10) unsigned NOT NULL, - `url` char(255) NOT NULL, - `title` char(255) NOT NULL, - `ts` datetime NOT NULL, - PRIMARY KEY (`link_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `reader_rss` --- - -DROP TABLE IF EXISTS `reader_rss`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `reader_rss` ( - `rss_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `url` char(255) NOT NULL, - `lastcheck` datetime NOT NULL, - PRIMARY KEY (`rss_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `replies` --- - -DROP TABLE IF EXISTS `replies`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `replies` ( - `message_id` int(10) unsigned NOT NULL, - `reply_id` smallint(5) unsigned NOT NULL, - `user_id` int(10) unsigned NOT NULL, - `replyto` smallint(5) unsigned NOT NULL DEFAULT '0', - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `attach` enum('jpg','mp4','png') COLLATE utf8mb4_unicode_ci DEFAULT NULL, - `txt` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL, - KEY `message_id` (`message_id`), - KEY `user_id` (`user_id`), - KEY `ts` (`ts`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `sphinx` --- - -DROP TABLE IF EXISTS `sphinx`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `sphinx` ( - `counter_id` tinyint(3) unsigned NOT NULL, - `max_id` int(10) unsigned NOT NULL, - PRIMARY KEY (`counter_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `subscr_messages` --- - -DROP TABLE IF EXISTS `subscr_messages`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `subscr_messages` ( - `message_id` int(10) unsigned NOT NULL, - `suser_id` int(10) unsigned NOT NULL, - UNIQUE KEY `message_id` (`message_id`,`suser_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `subscr_tags` --- - -DROP TABLE IF EXISTS `subscr_tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `subscr_tags` ( - `tag_id` int(10) unsigned NOT NULL, - `suser_id` int(10) unsigned NOT NULL, - `jid` char(64) NOT NULL, - `active` bit(1) NOT NULL DEFAULT b'1', - UNIQUE KEY `tag_id` (`tag_id`,`suser_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `subscr_users` --- - -DROP TABLE IF EXISTS `subscr_users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `subscr_users` ( - `user_id` int(10) unsigned NOT NULL, - `suser_id` int(10) unsigned NOT NULL, - `jid` char(64) DEFAULT NULL, - `active` bit(1) NOT NULL DEFAULT b'1', - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE KEY `user_id` (`user_id`,`suser_id`), - KEY `suser_id` (`suser_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `tags` --- - -DROP TABLE IF EXISTS `tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `tags` ( - `tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `synonym_id` int(10) unsigned DEFAULT NULL, - `name` char(70) DEFAULT NULL, - `top` tinyint(1) unsigned NOT NULL DEFAULT '0', - `noindex` tinyint(1) unsigned NOT NULL DEFAULT '0', - `stat_messages` int(10) unsigned NOT NULL DEFAULT '0', - `stat_users` smallint(5) unsigned NOT NULL DEFAULT '0', - PRIMARY KEY (`tag_id`), - KEY `synonym_id` (`synonym_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `tags_ignore` --- - -DROP TABLE IF EXISTS `tags_ignore`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `tags_ignore` ( - `tag_id` int(10) unsigned NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `tags_synonyms` --- - -DROP TABLE IF EXISTS `tags_synonyms`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `tags_synonyms` ( - `name` char(64) NOT NULL, - `changeto` char(64) NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `telegram` --- - -DROP TABLE IF EXISTS `telegram`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `telegram` ( - `user_id` int(10) unsigned DEFAULT NULL, - `tg_id` bigint(20) NOT NULL, - `tg_name` char(64) COLLATE utf8mb4_unicode_ci NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `loginhash` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `telegram_chats` --- - -DROP TABLE IF EXISTS `telegram_chats`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `telegram_chats` ( - `chat_id` bigint(20) DEFAULT NULL, - UNIQUE KEY `chat_id` (`chat_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `top_ignore_messages` --- - -DROP TABLE IF EXISTS `top_ignore_messages`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `top_ignore_messages` ( - `message_id` int(10) unsigned NOT NULL -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `top_ignore_tags` --- - -DROP TABLE IF EXISTS `top_ignore_tags`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `top_ignore_tags` ( - `tag_id` int(10) unsigned NOT NULL, - PRIMARY KEY (`tag_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `top_ignore_users` --- - -DROP TABLE IF EXISTS `top_ignore_users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `top_ignore_users` ( - `user_id` int(10) unsigned NOT NULL, - PRIMARY KEY (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `twitter` --- - -DROP TABLE IF EXISTS `twitter`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `twitter` ( - `user_id` int(10) unsigned NOT NULL, - `access_token` char(64) NOT NULL, - `access_token_secret` char(64) NOT NULL, - `uname` char(64) NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `crosspost` tinyint(1) unsigned NOT NULL DEFAULT '1', - PRIMARY KEY (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `useroptions` --- - -DROP TABLE IF EXISTS `useroptions`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `useroptions` ( - `user_id` int(10) unsigned NOT NULL, - `jnotify` tinyint(1) NOT NULL DEFAULT '1', - `subscr_active` tinyint(1) NOT NULL DEFAULT '1', - `off_ts` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `xmppxhtml` tinyint(1) NOT NULL DEFAULT '0', - `subscr_notify` tinyint(1) NOT NULL DEFAULT '1', - `recommendations` tinyint(1) NOT NULL DEFAULT '1', - `privacy_view` tinyint(1) NOT NULL DEFAULT '1', - `privacy_reply` tinyint(1) NOT NULL DEFAULT '1', - `privacy_pm` tinyint(1) NOT NULL DEFAULT '1', - `repliesview` tinyint(1) NOT NULL DEFAULT '0', - PRIMARY KEY (`user_id`), - KEY `recommendations` (`recommendations`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `users` --- - -DROP TABLE IF EXISTS `users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `users` ( - `id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `nick` char(64) NOT NULL, - `passw` char(32) NOT NULL, - `lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__', - `banned` tinyint(3) unsigned NOT NULL DEFAULT '0', - `lastmessage` int(11) NOT NULL DEFAULT '0', - `lastpm` int(11) NOT NULL DEFAULT '0', - `lastphoto` int(11) NOT NULL DEFAULT '0', - `karma` smallint(6) NOT NULL DEFAULT '0', - PRIMARY KEY (`id`), - UNIQUE KEY `nick` (`nick`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `users_refs` --- - -DROP TABLE IF EXISTS `users_refs`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `users_refs` ( - `user_id` int(10) unsigned NOT NULL, - `ref` int(10) unsigned NOT NULL, - KEY `ref` (`ref`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `users_subscr` --- - -DROP TABLE IF EXISTS `users_subscr`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `users_subscr` ( - `user_id` int(10) unsigned NOT NULL, - `cnt` smallint(5) unsigned NOT NULL DEFAULT '0', - PRIMARY KEY (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `usersinfo` --- - -DROP TABLE IF EXISTS `usersinfo`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `usersinfo` ( - `user_id` int(10) unsigned NOT NULL, - `jid` char(32) DEFAULT NULL, - `fullname` char(32) DEFAULT NULL, - `country` char(32) DEFAULT NULL, - `url` char(64) DEFAULT NULL, - `gender` char(32) DEFAULT NULL, - `bday` char(10) DEFAULT NULL, - `descr` varchar(255) DEFAULT NULL, - PRIMARY KEY (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `vk` --- - -DROP TABLE IF EXISTS `vk`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `vk` ( - `user_id` int(10) unsigned DEFAULT NULL, - `vk_id` bigint(20) NOT NULL, - `loginhash` char(36) DEFAULT NULL, - `access_token` char(128) NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `vk_name` char(64) NOT NULL, - `vk_link` char(64) NOT NULL, - `crosspost` tinyint(3) unsigned NOT NULL DEFAULT '1', - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `winphone` --- - -DROP TABLE IF EXISTS `winphone`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `winphone` ( - `user_id` int(10) unsigned NOT NULL, - `url` char(255) NOT NULL, - `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - UNIQUE KEY `url` (`url`), - KEY `user_id` (`user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; - --- --- Table structure for table `wl_users` --- - -DROP TABLE IF EXISTS `wl_users`; -/*!40101 SET @saved_cs_client = @@character_set_client */; -/*!40101 SET character_set_client = utf8 */; -CREATE TABLE `wl_users` ( - `user_id` int(10) unsigned NOT NULL, - `wl_user_id` int(10) unsigned NOT NULL, - PRIMARY KEY (`user_id`,`wl_user_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8; -/*!40101 SET character_set_client = @saved_cs_client */; -/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; - -/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; -/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; -/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; -/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; - --- Dump completed on 2016-09-03 14:32:11 diff --git a/juick-server/src/main/resources/update.sql b/juick-server/src/main/resources/update.sql deleted file mode 100644 index 13a62c3d..00000000 --- a/juick-server/src/main/resources/update.sql +++ /dev/null @@ -1,12 +0,0 @@ --- if version table not exists set up version = 0; -update version set version = 0; - -DROP TABLE IF EXISTS `version`; - -CREATE TABLE `version` ( - `version` bigint NOT NULL -); - -insert into version values (0); - -update version set version = 1; \ No newline at end of file diff --git a/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java b/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java deleted file mode 100644 index bd2f3f50..00000000 --- a/juick-server/src/test/java/com/juick/configuration/MockDataConfiguration.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.juick.configuration; - -import com.juick.service.*; -import org.mockito.Mockito; -import org.springframework.context.annotation.Bean; - -/** - * Created by vitalyster on 12.01.2017. - */ -public class MockDataConfiguration { - @Bean - MessagesService messagesService() { - return Mockito.mock(MessagesService.class); - } - @Bean - UserService userService() { - return Mockito.mock(UserService.class); - } - @Bean - TagService tagService() { - return Mockito.mock(TagService.class); - } - @Bean - PushQueriesService pushQueriesService() { - return Mockito.mock(PushQueriesService.class); - } - @Bean - SubscriptionService subscriptionService() { - return Mockito.mock(SubscriptionService.class); - } - @Bean - PMQueriesService pmQueriesService() { - return Mockito.mock(PMQueriesService.class); - } - @Bean - TelegramService telegramService() { - return Mockito.mock(TelegramService.class); - } - @Bean - CrosspostService crosspostService() { - return Mockito.mock(CrosspostService.class); - } - @Bean - EmailService emailService() { - return Mockito.mock(EmailService.class); - } -} diff --git a/juick-server/src/test/java/com/juick/configuration/RepositoryConfiguration.java b/juick-server/src/test/java/com/juick/configuration/RepositoryConfiguration.java deleted file mode 100644 index 4a74349f..00000000 --- a/juick-server/src/test/java/com/juick/configuration/RepositoryConfiguration.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.juick.configuration; - -import ch.vorburger.exec.ManagedProcessException; -import ch.vorburger.mariadb4j.DB; -import com.juick.service.search.SearchService; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.datasource.DriverManagerDataSource; - -import java.util.Collections; -import java.util.List; - -/** - * Created by aalexeev on 11/25/16. - */ -@Configuration -@ComponentScan(basePackages = "com.juick.service") -public class RepositoryConfiguration { - - @Bean(destroyMethod = "stop") - DB db() throws ManagedProcessException { - DB db = DB.newEmbeddedDB(33306); - - db.start(); - db.createDB("juick"); - db.source("schema.sql"); - - return db; - } - - @Bean - public DriverManagerDataSource dataSource() { - DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName("net.sf.log4jdbc.DriverSpy"); - dataSource.setUrl("jdbc:log4jdbc:mysql://localhost:33306/juick?autoReconnect=true&user=root"); - - return dataSource; - } - - @Bean - public JdbcTemplate jdbcTemplate() { - return new JdbcTemplate(dataSource()); - } - - @Bean - public SearchService emptySearchService() { - return new SearchService() { - @Override - public void setMaxResult(int maxResult) { - } - - @Override - public List searchInAllMessages(String searchString, int messageIdBefore) { - return Collections.emptyList(); - } - - @Override - public List searchByStringAndUser(String searchString, int userId, int messageIdBefore) { - return Collections.emptyList(); - } - }; - } -} diff --git a/juick-server/src/test/java/com/juick/service/MessageServiceTest.java b/juick-server/src/test/java/com/juick/service/MessageServiceTest.java deleted file mode 100644 index 5f2c2b05..00000000 --- a/juick-server/src/test/java/com/juick/service/MessageServiceTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.juick.service; - -import com.juick.configuration.RepositoryConfiguration; -import org.junit.Test; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; - -import javax.inject.Inject; - -/** - * Created by aalexeev on 11/25/16. - */ -@ContextConfiguration(classes = RepositoryConfiguration.class) -public class MessageServiceTest extends AbstractJUnit4SpringContextTests { - @Inject - private MessagesService messagesService; - - - @Test - public void getMyFeed() { - messagesService.getMyFeed(1, 1000000); - } -} diff --git a/juick-server/src/test/java/com/juick/tests/util/MockUtils.java b/juick-server/src/test/java/com/juick/tests/util/MockUtils.java deleted file mode 100644 index 5344dd23..00000000 --- a/juick-server/src/test/java/com/juick/tests/util/MockUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.juick.tests.util; - -import com.juick.Message; -import com.juick.User; -import org.apache.commons.text.RandomStringGenerator; - -import java.util.Date; - -/** - * Created by vitalyster on 12.01.2017. - */ -public class MockUtils { - final static RandomStringGenerator generator = new RandomStringGenerator.Builder().withinRange('a', 'z').build(); - public static Message mockMessage(Integer mid, final User user, final String messageText) { - Message msg = new Message(); - - msg.setMid(mid); - msg.setUser(user); - msg.setText(messageText == null ? generator.generate(24) : messageText); - msg.setDate(new Date()); - return msg; - } - - public static User mockUser(final int uid, final String name, final String password) { - User user = new User(); - - user.setName(name); - user.setUid(uid); - user.setCredentials(password); - user.setBanned(false); - - return user; - } -} diff --git a/juick-ws/build.gradle b/juick-ws/build.gradle index a0987f5b..c704cbdb 100644 --- a/juick-ws/build.gradle +++ b/juick-ws/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" compile "org.springframework:spring-websocket:${rootProject.springFrameworkVersion}" providedRuntime 'mysql:mysql-connector-java:5.1.40' diff --git a/juick-www/build.gradle b/juick-www/build.gradle index 5a11e9e2..4c26f754 100644 --- a/juick-www/build.gradle +++ b/juick-www/build.gradle @@ -18,7 +18,8 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-web') + compile project(':juick-server-jdbc') compile 'com.github.scribejava:scribejava-apis:4.1.1' compile 'com.github.ooxi:serialized-php-parser:0.5.0' compile 'com.sun.mail:javax.mail:1.5.6' @@ -28,7 +29,7 @@ dependencies { providedCompile 'javax.servlet:javax.servlet-api:3.1.0' providedRuntime 'mysql:mysql-connector-java:5.1.40' - testCompile project(path: ':juick-server', configuration: 'testArtifacts') + testCompile project(path: ':juick-server-jdbc', configuration: 'testArtifacts') // htmlunit 2.25 isn't compatible with spring-test 4.3.6 testCompile ('net.sourceforge.htmlunit:htmlunit:2.24') diff --git a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java b/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java index c6357c90..757aeb06 100644 --- a/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java +++ b/juick-www/src/main/java/com/juick/www/configuration/WebSecurityConfig.java @@ -1,7 +1,7 @@ package com.juick.www.configuration; -import com.juick.server.security.HashParamAuthenticationFilter; -import com.juick.server.security.entities.JuickUser; +import com.juick.service.security.HashParamAuthenticationFilter; +import com.juick.service.security.entities.JuickUser; import com.juick.service.UserService; import com.juick.service.security.JuickUserDetailsService; import org.springframework.beans.factory.annotation.Value; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Help.java b/juick-www/src/main/java/com/juick/www/controllers/Help.java index 602850de..f40c3b96 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Help.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Help.java @@ -2,7 +2,7 @@ package com.juick.www.controllers; import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.HelpService; import com.juick.www.WebApp; import org.springframework.stereotype.Controller; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Home.java b/juick-www/src/main/java/com/juick/www/controllers/Home.java index ebf56b73..d42531e5 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Home.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Home.java @@ -21,8 +21,8 @@ import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; -import com.juick.util.UserUtils; -import com.juick.util.WebUtils; +import com.juick.server.util.UserUtils; +import com.juick.server.util.WebUtils; import com.juick.www.Utils; import com.juick.www.WebApp; import org.apache.commons.codec.CharEncoding; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Login.java b/juick-www/src/main/java/com/juick/www/controllers/Login.java index e664c1d9..ce9572fa 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Login.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Login.java @@ -18,7 +18,7 @@ package com.juick.www.controllers; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 24e100ce..e34207a0 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -26,7 +26,7 @@ import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.HttpUtils; import com.juick.server.util.ImageUtils; import com.juick.service.*; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.Utils; import com.juick.www.WebApp; import org.apache.commons.codec.CharEncoding; diff --git a/juick-www/src/main/java/com/juick/www/controllers/PM.java b/juick-www/src/main/java/com/juick/www/controllers/PM.java index 0afe5f14..7695226a 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/PM.java +++ b/juick-www/src/main/java/com/juick/www/controllers/PM.java @@ -23,9 +23,8 @@ import com.juick.service.MessagesService; import com.juick.service.PMQueriesService; import com.juick.service.TagService; import com.juick.service.UserService; -import com.juick.util.MessageUtils; -import com.juick.util.UserUtils; -import com.juick.util.WebUtils; +import com.juick.server.util.UserUtils; +import com.juick.server.util.WebUtils; import com.juick.www.WebApp; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index 6fa3fd2e..21df9d6e 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -19,11 +19,12 @@ package com.juick.www.controllers; import com.juick.server.helpers.NotifyOpts; import com.juick.server.helpers.UserInfo; +import com.juick.server.util.HashUtils; +import com.juick.service.*; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpUtils; import com.juick.server.util.ImageUtils; -import com.juick.service.*; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.WebApp; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -183,7 +184,7 @@ public class Settings { break; case "email-add": if (!emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("account"))) { - String authCode = UserUtils.generateHash(8); + String authCode = HashUtils.generateHash(8); if (emailService.addVerificationCode(visitor.getUid(), request.getParameter("account"), authCode)) { Session session = Session.getDefaultInstance(System.getProperties()); try { diff --git a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java index 77971165..cfbd9ca2 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java +++ b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java @@ -22,7 +22,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.service.CrosspostService; import com.juick.service.MessagesService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.WebApp; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Tags.java b/juick-www/src/main/java/com/juick/www/controllers/Tags.java index c3ef5153..16f71959 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Tags.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Tags.java @@ -19,7 +19,7 @@ package com.juick.www.controllers; import com.juick.service.MessagesService; import com.juick.service.TagService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.Utils; import com.juick.www.WebApp; import org.apache.commons.codec.CharEncoding; diff --git a/juick-www/src/main/java/com/juick/www/controllers/User.java b/juick-www/src/main/java/com/juick/www/controllers/User.java index a249d8d0..8bde9bee 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/User.java +++ b/juick-www/src/main/java/com/juick/www/controllers/User.java @@ -22,7 +22,7 @@ import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.Utils; import com.juick.www.WebApp; import org.apache.commons.codec.CharEncoding; diff --git a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java index 1d854b8f..cc476309 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/UserThread.java +++ b/juick-www/src/main/java/com/juick/www/controllers/UserThread.java @@ -24,7 +24,7 @@ import com.juick.service.MessagesService; import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.MessageUtils; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.WebApp; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java b/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java index 45fff7ce..8436fd14 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java +++ b/juick-www/src/main/java/com/juick/www/controllers/XMPPPost.java @@ -4,7 +4,7 @@ import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.HttpUtils; import com.juick.service.TagService; -import com.juick.util.UserUtils; +import com.juick.server.util.UserUtils; import com.juick.www.WebApp; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; diff --git a/juick-xmpp-bot/build.gradle b/juick-xmpp-bot/build.gradle index bc6dce3c..25199b05 100644 --- a/juick-xmpp-bot/build.gradle +++ b/juick-xmpp-bot/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" providedRuntime 'mysql:mysql-connector-java:5.1.40' } diff --git a/juick-xmpp/build.gradle b/juick-xmpp/build.gradle index 1758e97e..cd0e6ec2 100644 --- a/juick-xmpp/build.gradle +++ b/juick-xmpp/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'war' apply plugin: 'org.akhikhl.gretty' dependencies { - compile project(':juick-server') + compile project(':juick-server-jdbc') compile 'com.github.juick:com.juick.xmpp:483936e0bd' compile "org.slf4j:slf4j-api:${rootProject.slf4jVersion}" compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}" diff --git a/settings.gradle b/settings.gradle index 43dd810f..3ba77cfb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ rootProject.name = "Juick" -include ':juick-core', 'juick-server', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-bot' +include ':juick-core', ':juick-server-core', ':juick-server-jdbc', ':juick-server-web', ':juick-api', ':juick-www', ':juick-rss', ':juick-ws', ':juick-notifications', ':juick-crosspost', ':juick-xmpp', ':juick-xmpp-bot' -- cgit v1.2.3