...
 
Commits (4)
......@@ -25,4 +25,4 @@
/nbdist/
/.nb-gradle/
derby.log
/application.yml
......@@ -9,6 +9,7 @@ FROM openjdk:8-jre
#ENV PORT 4567
#EXPOSE 4567
COPY --from=BUILD /usr/src/app/build/libs /opt/target
COPY application.yml /opt/target
WORKDIR /opt/target
CMD ["/bin/bash", "-c", "find -type f -name 'socializer-*.jar' | xargs java -jar"]
......
......@@ -10,6 +10,15 @@ buildscript {
}
}
plugins {
id 'io.franzbecker.gradle-lombok' version '1.14'
}
lombok {
version = '1.18.4'
sha256 = ''
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
......@@ -22,8 +31,9 @@ repositories {
mavenCentral()
}
dependencies {
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-social-twitter:1.1.0.RELEASE'
......
package org.alcibiade.socializer.configuration;
import org.alcibiade.socializer.configuration.properties.SocializerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(SocializerProperties.class)
public class PropertiesConfiguration {
}
package org.alcibiade.socializer.config;
package org.alcibiade.socializer.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
......
package org.alcibiade.socializer.configuration.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Data
@Validated
@ConfigurationProperties(prefix = "socializer")
public class SocializerProperties {
@NotNull
private TwitterProperties twitter;
@Data
public static class TwitterProperties {
private Map<String, AccountProperties> accounts;
@Data
public static class AccountProperties {
@NotBlank
private String apiKey;
@NotBlank
private String apiSecretKey;
@NotBlank
private String accessTokens;
@NotBlank
private String accessTokenSecret;
}
}
}
package org.alcibiade.socializer.domain;
import javax.persistence.*;
@Entity
public class TwitterAccount {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(length = 64, nullable = false)
private String accessToken;
@Column(length = 64, nullable = false)
private String accessTokenSecret;
@Column(nullable = false)
private boolean master = false;
public Long getId() {
return id;
}
public String getAccessToken() {
return accessToken;
}
public String getAccessTokenSecret() {
return accessTokenSecret;
}
public boolean isMaster() {
return master;
}
}
package org.alcibiade.socializer.domain;
import javax.persistence.*;
@Entity
public class TwitterApi {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(length = 24, nullable = false)
private String apiKey;
@Column(length = 48, nullable = false)
private String apiSecretKey;
@Column(nullable = false)
private boolean master;
public Long getId() {
return id;
}
public String getApiKey() {
return apiKey;
}
public String getApiSecretKey() {
return apiSecretKey;
}
public boolean isMaster() {
return master;
}
}
package org.alcibiade.socializer.domain;
import javax.persistence.*;
@Entity
public class TwitterTrust {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private TwitterAccount account;
@ManyToOne
private TwitterUser trustedUser;
public Long getId() {
return id;
}
public TwitterAccount getAccount() {
return account;
}
public TwitterUser getTrustedUser() {
return trustedUser;
}
}
package org.alcibiade.socializer.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Entity
public class TwitterUser {
@Id
@Column(length = 64)
private String handle;
@Column(nullable = false, length = 256)
private String fullName;
@Column(nullable = false)
private LocalDateTime lastActivity;
public String getHandle() {
return handle;
}
public String getFullName() {
return fullName;
}
public LocalDateTime getLastActivity() {
return lastActivity;
}
@Override
public String toString() {
return handle;
}
}
......@@ -17,9 +17,9 @@ public class RetweetJob {
this.twitterService = twitterService;
}
@Scheduled(fixedRate = 3600L)
@Scheduled(initialDelay = 5_000L, fixedRate = 3600_000L)
@Transactional
public void retweet() {
twitterService.retweet();
}
}
......@@ -17,7 +17,7 @@ public class UpdateActivityJob {
this.twitterService = twitterService;
}
@Scheduled(fixedDelay = 1200L)
@Scheduled(fixedDelay = 60_000L)
@Transactional
public void updateActivity() {
twitterService.updateActivity();
......
package org.alcibiade.socializer.persistence;
import org.alcibiade.socializer.domain.TwitterAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
@Component
public interface TwitterAccoutRepository extends JpaRepository<TwitterAccount, Long> {
TwitterAccount getOneByMaster(boolean master);
default TwitterAccount getMasterAccount() {
return this.getOneByMaster(true);
}
}
package org.alcibiade.socializer.persistence;
import org.alcibiade.socializer.domain.TwitterApi;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
@Component
public interface TwitterApiRepository extends JpaRepository<TwitterApi, Long> {
TwitterApi getOneByMaster(boolean master);
default TwitterApi getMasterApi() {
return this.getOneByMaster(true);
}
}
package org.alcibiade.socializer.service;
import org.alcibiade.socializer.domain.TwitterAccount;
import org.alcibiade.socializer.domain.TwitterApi;
import org.alcibiade.socializer.persistence.TwitterAccoutRepository;
import org.alcibiade.socializer.persistence.TwitterApiRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
@Component
@ConfigurationProperties(prefix = "twitter.accounts")
public class TwitterFactory {
private TwitterApiRepository apiRepository;
private TwitterAccoutRepository accoutRepository;
@Autowired
public TwitterFactory(TwitterApiRepository apiRepository, TwitterAccoutRepository accoutRepository) {
this.apiRepository = apiRepository;
this.accoutRepository = accoutRepository;
}
public Twitter createTwitterClient(TwitterApi api, TwitterAccount account) {
Twitter twitter = new TwitterTemplate(
api.getApiKey(), api.getApiSecretKey(),
account.getAccessToken(), account.getAccessTokenSecret()
);
return twitter;
}
@Transactional
public Twitter createTwitterClient() {
TwitterApi api = apiRepository.getMasterApi();
TwitterAccount account = accoutRepository.getMasterAccount();
return this.createTwitterClient(api, account);
}
}
package org.alcibiade.socializer.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.alcibiade.socializer.configuration.properties.SocializerProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Slf4j
@Service
public class TwitterService {
private Logger logger = LoggerFactory.getLogger(TwitterService.class);
private final TwitterFactory twitterFactory;
private final SocializerProperties socializerProperties;
@Autowired
public TwitterService(TwitterFactory twitterFactory) {
this.twitterFactory = twitterFactory;
public TwitterService(SocializerProperties socializerProperties) {
this.socializerProperties = socializerProperties;
}
@Transactional
public void updateActivity() {
logger.info("Updating activity timestamps for users...");
Twitter twitter = twitterFactory.createTwitterClient();
public void retweet() {
log.debug("Retweeting for {} twitter accounts",
socializerProperties.getTwitter().getAccounts().size());
}
public void updateActivity() {
log.debug("Updating Activity...");
}
}
package org.alcibiade.socializer;
import org.alcibiade.socializer.persistence.TwitterAccoutRepository;
import org.alcibiade.socializer.configuration.properties.SocializerProperties;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -8,19 +8,21 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.transaction.Transactional;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SocializerApplicationTests {
@Autowired
public TwitterAccoutRepository twitterAccoutRepository;
public SocializerProperties properties;
@Test
@Transactional
public void contextLoads() {
Assertions.assertThat(twitterAccoutRepository.findAll()).isEmpty();
Assertions.assertThat(properties).isNotNull();
Assertions.assertThat(properties.getTwitter()).isNotNull();
Assertions.assertThat(properties.getTwitter().getAccounts()).hasSize(1);
Assertions.assertThat(
properties.getTwitter().getAccounts().get("account1").getAccessTokenSecret()
).isEqualTo("dddddddddddddddddddddddddddddddddd");
}
}
......
......@@ -3,4 +3,13 @@ spring:
url: jdbc:h2:mem:testdb
username: sa
password:
driver-class-name: org.h2.Driver
\ No newline at end of file
driver-class-name: org.h2.Driver
socializer:
twitter:
accounts:
account1:
apiKey: 'aaaaaaaaaaaaaaaaaaaa'
apiSecretKey: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
accessTokens: 'ccccccccccccc-cccccccccccccccccc'
accessTokenSecret: 'dddddddddddddddddddddddddddddddddd'