/* * Copyright (C) 2008-2017, Juick * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package com.juick.configuration; import com.juick.service.ImagesService; import com.juick.service.MockImagesService; 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.context.annotation.DependsOn; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; 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 public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;mode=MYSQL"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_users (user_id int(10) unsigned NOT NULL, " + "bl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS bl_tags (user_id int(10) unsigned NOT NULL, " + "tag_id int(10) unsigned NOT NULL)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS wl_users (user_id int(10) unsigned NOT NULL, " + "wl_user_id int(10) unsigned NOT NULL, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages (" + "message_id int(10) unsigned NOT NULL AUTO_INCREMENT," + "user_id int(10) unsigned NOT NULL," + "place_id int(10) unsigned DEFAULT NULL," + "lat decimal(10,7) DEFAULT NULL," + "lon decimal(10,7) DEFAULT NULL," + "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'," + "attach nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + "readonly tinyint(1) NOT NULL DEFAULT 0," + "likes smallint(6) NOT NULL DEFAULT 0," + "`popular` tinyint(4) NOT NULL DEFAULT '0'," + "hidden tinyint(3) unsigned NOT NULL DEFAULT 0," + "updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ")"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_tags (" + "message_id int(10) unsigned NOT NULL," + "tag_id int(10) unsigned NOT NULL" + ")"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS users (" + "id int(10) unsigned NOT NULL AUTO_INCREMENT," + "nick char(64) NOT NULL," + "`lang` enum('en','ru','fr','fa','__') NOT NULL DEFAULT '__'," + "passw char(32) NOT NULL," + "banned tinyint(3) unsigned NOT NULL DEFAULT 0, " + "PRIMARY KEY(id), UNIQUE KEY(nick))"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS useroptions (" + "user_id int(10) unsigned NOT NULL," + "jnotify tinyint(1) NOT NULL DEFAULT 1," + "`repliesview` tinyint(1) NOT NULL DEFAULT '0'," + "subscr_active tinyint(1) NOT NULL DEFAULT 1)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_users (" + "user_id int(10) unsigned NOT NULL," + "suser_id int(10) unsigned NOT NULL," + "jid char(64) DEFAULT NULL," + "active tinyint(1) NOT NULL DEFAULT 1," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, " + "PRIMARY KEY (suser_id, user_id))"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_messages (" + "message_id int(10) unsigned NOT NULL," + "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, message_id))"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS subscr_tags (" + "tag_id int(10) unsigned NOT NULL," + "suser_id int(10) unsigned NOT NULL, PRIMARY KEY (suser_id, tag_id))"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS messages_txt (" + "message_id int(10) unsigned NOT NULL," + "tags varchar(255)," + "repliesby varchar(96)," + "txt TEXT)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS tags(" + "tag_id int(10) unsigned NOT NULL AUTO_INCREMENT," + "synonym_id int(10) unsigned DEFAULT NULL," + "name char(48) NOT NULL," + "top 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)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS jids(" + "user_id int(10) unsigned NOT NULL AUTO_INCREMENT," + "jid char(64) NOT NULL," + "active tinyint(1) unsigned NOT NULL DEFAULT '1'," + "loginhash char(36) unsigned DEFAULT NULL," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS 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 nchar(3) check (attach in ('jpg', 'mp4', 'png'))," + "txt text)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS twitter(" + "user_id int(10) unsigned NOT NULL," + "access_token char(64) NOT NULL," + "access_token_secret char(64) NOT NULL," + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + "uname char(64) NOT NULL," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS facebook(" + "user_id int(10) unsigned NOT NULL," + "fb_id int(20) unsigned NOT NULL," + "access_token char(255) NOT NULL," + "loginhash char(36) NOT NULL," + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + "fb_name char(64) NOT NULL," + "fb_link char(64) NOT NULL," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS vk(" + "user_id int(10) unsigned NOT NULL," + "vk_id int(20) unsigned NOT NULL," + "access_token char(128) NOT NULL," + "loginhash char(36) NOT NULL," + "crosspost tinyint(1) unsigned NOT NULL DEFAULT '1'," + "vk_name char(64) NOT NULL," + "vk_link char(64) NOT NULL," + "ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS emails (" + " user_id int(10) unsigned NOT NULL," + " email char(64) NOT NULL," + " subscr_hour tinyint(4) DEFAULT NULL," + " PRIMARY KEY (email)" + ")"); jdbcTemplate.execute("CREATE TABLE favorites (" + " user_id int(10) unsigned NOT NULL," + " message_id int(10) unsigned NOT NULL," + " ts datetime NOT NULL" + ")"); jdbcTemplate.execute("CREATE TABLE logins (" + " user_id int(10) unsigned NOT NULL," + " hash char(16) NOT NULL," + " PRIMARY KEY (user_id)" + ")"); jdbcTemplate.execute("CREATE TABLE `telegram` (" + " `user_id` int(10) unsigned DEFAULT NULL," + " `tg_id` bigint(20) NOT NULL," + " `tg_name` char(64) NOT NULL," + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP," + " `loginhash` char(36) DEFAULT NULL" + ")"); jdbcTemplate.execute("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," + " `txt` text NOT NULL" + ")"); jdbcTemplate.execute("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'" + ")"); jdbcTemplate.execute("CREATE TABLE `android` (" + " `user_id` int(10) unsigned NOT NULL," + " `regid` char(255) NOT NULL," + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ")"); jdbcTemplate.execute("CREATE TABLE `ios` (" + " `user_id` int(10) unsigned NOT NULL," + " `token` char(64) NOT NULL," + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ")"); jdbcTemplate.execute("CREATE TABLE `winphone` (" + " `user_id` int(10) unsigned NOT NULL," + " `url` char(255) NOT NULL," + " `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP" + ")"); jdbcTemplate.execute("CREATE TABLE `tags_ignore` (" + " `tag_id` int(10) unsigned NOT NULL" + ")"); jdbcTemplate.execute("CREATE TABLE `users_subscr` (" + " `user_id` int(10) unsigned NOT NULL," + " `cnt` smallint(5) unsigned NOT NULL DEFAULT '0'," + " PRIMARY KEY (`user_id`)" + ")"); return jdbcTemplate; } @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(); } }; } @Bean public ImagesService imagesService() { return new MockImagesService(); } }