aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2017-12-25 12:55:10 +0300
committerGravatar Vitaly Takmazov2017-12-25 12:55:10 +0300
commite3a9e8a42bf980751db5f75c63ef9e17ab1ea8dc (patch)
tree3e7106a4883907368e60bd6372e809c27606afc4
parentd767b84a27b6864897bbfbd8c9250550975c5d45 (diff)
add email from facebook
-rw-r--r--juick-api/src/main/java/com/juick/api/EmailManager.java4
-rw-r--r--juick-server-core/src/main/java/com/juick/service/EmailService.java3
-rw-r--r--juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java11
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java13
-rw-r--r--juick-www/src/main/java/com/juick/www/facebook/User.java5
5 files changed, 28 insertions, 8 deletions
diff --git a/juick-api/src/main/java/com/juick/api/EmailManager.java b/juick-api/src/main/java/com/juick/api/EmailManager.java
index c0cc1c37..a2ddbc3b 100644
--- a/juick-api/src/main/java/com/juick/api/EmailManager.java
+++ b/juick-api/src/main/java/com/juick/api/EmailManager.java
@@ -35,11 +35,11 @@ public class EmailManager implements ApplicationListener<MessageEvent> {
Message msg = event.getMessage();
if (msg.getRid() == 0) {
subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid())
- .forEach(user -> emailService.getEmails(user.getUid())
+ .forEach(user -> emailService.getEmails(user.getUid(), true)
.forEach(email -> emailNotify(email, msg)));
} else {
subscriptionService.getUsersSubscribedToComments(msg.getMid(), msg.getUser().getUid())
- .forEach(user -> emailService.getEmails(user.getUid())
+ .forEach(user -> emailService.getEmails(user.getUid(), true)
.forEach(email -> emailNotify(email, msg)));
}
}
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
index 369434e7..ac63f4da 100644
--- a/juick-server-core/src/main/java/com/juick/service/EmailService.java
+++ b/juick-server-core/src/main/java/com/juick/service/EmailService.java
@@ -25,7 +25,8 @@ import java.util.List;
public interface EmailService {
boolean verifyAddressByCode(Integer userId, String code);
boolean addVerificationCode(Integer userId, String account, String code);
+ boolean addEmail(Integer userId, String email);
boolean deleteEmail(Integer userId, String account);
boolean setNotificationsEmail(Integer userId, String account);
- List<String> getEmails(Integer userId);
+ List<String> getEmails(Integer userId, boolean active);
}
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
index a8c0595e..0cbaec00 100644
--- a/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java
+++ b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java
@@ -43,7 +43,7 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService {
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);
+ addEmail(userId, address);
getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", userId, code);
} catch (EmptyResultDataAccessException e) {
return false;
@@ -58,6 +58,11 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService {
}
@Override
+ public boolean addEmail(Integer userId, String email) {
+ return getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", userId, email) > 0;
+ }
+
+ @Override
public boolean deleteEmail(Integer userId, String account) {
return getNamedParameterJdbcTemplate().update("DELETE FROM emails " +
"WHERE (SELECT COUNT(*) cnt FROM (select user_id, email FROM emails e) c WHERE user_id=:uid) > 1 " +
@@ -76,8 +81,8 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService {
@Transactional(readOnly = true)
@Override
- public List<String> getEmails(Integer userId) {
+ public List<String> getEmails(Integer userId, boolean active) {
return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=? " +
- "AND subscr_hour IS NOT NULL", String.class, userId);
+ (active ? "AND subscr_hour IS NOT NULL" : ""), String.class, userId);
}
}
diff --git a/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java
index cb55bf82..95ed9a5b 100644
--- a/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java
+++ b/juick-www/src/main/java/com/juick/www/controllers/FacebookLogin.java
@@ -25,6 +25,7 @@ import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.juick.server.util.HttpBadRequestException;
import com.juick.service.CrosspostService;
+import com.juick.service.EmailService;
import com.juick.service.UserService;
import com.juick.www.Utils;
import com.juick.www.facebook.User;
@@ -69,6 +70,8 @@ public class FacebookLogin {
private CrosspostService crosspostService;
@Inject
private UserService userService;
+ @Inject
+ private EmailService emailService;
@PostConstruct
public void init() {
@@ -89,7 +92,7 @@ public class FacebookLogin {
OAuth20Service facebookAuthService = serviceBuilder
.apiSecret(FACEBOOK_SECRET)
.callback(FACEBOOK_REDIRECT)
- .scope("publish_actions")
+ .scope("publish_actions,email")
.state(fbstate)
.build(FacebookApi.instance());
return "redirect:" + facebookAuthService.getAuthorizationUrl();
@@ -109,7 +112,7 @@ public class FacebookLogin {
.state(state)
.build(FacebookApi.instance());
OAuth2AccessToken token = facebookService.getAccessToken(code);
- final OAuthRequest meRequest = new OAuthRequest(Verb.GET, "https://graph.facebook.com/v2.10/me?fields=id,name,link,verified");
+ final OAuthRequest meRequest = new OAuthRequest(Verb.GET, "https://graph.facebook.com/v2.10/me?fields=id,name,link,verified,email");
facebookService.signRequest(token, meRequest);
String graph = facebookService.execute(meRequest).getBody();
if (StringUtils.isBlank(graph)) {
@@ -134,6 +137,12 @@ public class FacebookLogin {
} else if (fb.getVerified()) {
String loginhash = UUID.randomUUID().toString();
if (!crosspostService.createFacebookUser(fbID, loginhash, token.getAccessToken(), fb.getName(), fb.getLink())) {
+ if (StringUtils.isNotEmpty(fb.getEmail())) {
+ Integer userId = crosspostService.getUIDbyFBID(fbID);
+ if (!emailService.getEmails(userId, false).contains(fb.getEmail())) {
+ emailService.addEmail(userId, fb.getEmail());
+ }
+ }
throw new HttpBadRequestException();
}
return "redirect:/signup?type=fb&hash=" + loginhash;
diff --git a/juick-www/src/main/java/com/juick/www/facebook/User.java b/juick-www/src/main/java/com/juick/www/facebook/User.java
index 77f23ac9..b56c4ee3 100644
--- a/juick-www/src/main/java/com/juick/www/facebook/User.java
+++ b/juick-www/src/main/java/com/juick/www/facebook/User.java
@@ -35,6 +35,7 @@ public class User {
private String locale;
private String timezone;
private String updatedTime;
+ private String email;
public String getId() {
return id;
@@ -117,4 +118,8 @@ public class User {
public void setUpdatedTime(String updatedTime) {
this.updatedTime = updatedTime;
}
+
+ public String getEmail() {
+ return email;
+ }
}