aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/api
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-05-18 15:33:38 +0300
committerGravatar Vitaly Takmazov2018-07-24 14:35:02 +0300
commit8a0fbbd35c13054a947ea0d27ca117542bc452b9 (patch)
tree4266fbfaac3f1e268e780bc1cfa3cdc23ccd955c /juick-server/src/main/java/com/juick/server/api
parent3fa18888d5ca53b59c778de3f829870614d05c27 (diff)
JPA WIP
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/api')
-rw-r--r--juick-server/src/main/java/com/juick/server/api/PM.java12
-rw-r--r--juick-server/src/main/java/com/juick/server/api/SocialLogin.java33
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Users.java2
3 files changed, 25 insertions, 22 deletions
diff --git a/juick-server/src/main/java/com/juick/server/api/PM.java b/juick-server/src/main/java/com/juick/server/api/PM.java
index cbd70bed..0985e7c4 100644
--- a/juick-server/src/main/java/com/juick/server/api/PM.java
+++ b/juick-server/src/main/java/com/juick/server/api/PM.java
@@ -54,16 +54,16 @@ public class PM {
if (visitor.isAnonymous()) {
throw new HttpForbiddenException();
}
- int uid = 0;
+ User user = AnonymousUser.INSTANCE;
if (uname != null && uname.matches("^[a-zA-Z0-9\\-]{2,16}$")) {
- uid = userService.getUIDbyName(uname);
+ user = userService.getUserByName(uname);
}
- if (uid == 0) {
+ if (user.getUid() == 0) {
throw new HttpBadRequestException();
}
- return pmQueriesService.getPMMessages(visitor.getUid(), uid);
+ return pmQueriesService.getPMMessages(visitor, user);
}
@RequestMapping(value = "/pm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@@ -87,7 +87,7 @@ public class PM {
throw new HttpForbiddenException();
}
- if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) {
+ if (pmQueriesService.createPM(visitor, userTo, body)) {
com.juick.Message jmsg = new com.juick.Message();
jmsg.setUser(visitor);
jmsg.setText(body);
@@ -112,7 +112,7 @@ public class PM {
cnt = 10;
}
- List<User> lastconv = pmQueriesService.getPMLastConversationsUsers(visitor.getUid(), cnt);
+ List<User> lastconv = pmQueriesService.getPMLastConversationsUsers(visitor, cnt);
PrivateChats pms = new PrivateChats();
pms.setUsers(lastconv);
return pms;
diff --git a/juick-server/src/main/java/com/juick/server/api/SocialLogin.java b/juick-server/src/main/java/com/juick/server/api/SocialLogin.java
index dc7425e1..195d1217 100644
--- a/juick-server/src/main/java/com/juick/server/api/SocialLogin.java
+++ b/juick-server/src/main/java/com/juick/server/api/SocialLogin.java
@@ -25,6 +25,7 @@ import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.juick.facebook.User;
+import com.juick.server.helpers.AnonymousUser;
import com.juick.server.util.HttpBadRequestException;
import com.juick.service.CrosspostService;
import com.juick.service.EmailService;
@@ -44,6 +45,8 @@ import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
+import java.util.Map;
+import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
@@ -137,22 +140,22 @@ public class SocialLogin {
throw new HttpBadRequestException();
}
- int uid = crosspostService.getUIDbyFBID(fbID);
- if (uid > 0) {
+ com.juick.User user = crosspostService.getUserByFBID(fbID).orElse(AnonymousUser.INSTANCE);
+ if (!user.isAnonymous()) {
if (!crosspostService.updateFacebookUser(fbID, token.getAccessToken(), fb.getName(), fb.getLink())) {
logger.error("error updating facebook user, id: {}, token: {}", fbID, token.getAccessToken());
throw new HttpBadRequestException();
}
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl);
- uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid));
+ uriComponentsBuilder.queryParam("hash", userService.getHashForUser(user));
return "redirect:" + uriComponentsBuilder.build().toUriString();
} else if (fb.getVerified()) {
if (!crosspostService.createFacebookUser(fbID, state, token.getAccessToken(), fb.getName(), fb.getLink())) {
if (StringUtils.isNotEmpty(fb.getEmail())) {
logger.info("found {} for facebook user {}", fb.getEmail(), fb.getLink());
- Integer userId = crosspostService.getUIDbyFBID(fbID);
- if (!emailService.getEmails(userId, false).contains(fb.getEmail())) {
- emailService.addEmail(userId, fb.getEmail());
+ user = crosspostService.getUserByFBID(fbID).orElse(AnonymousUser.INSTANCE);
+ if (!emailService.getEmails(user, false).contains(fb.getEmail())) {
+ emailService.addEmail(user, fb.getEmail());
}
}
logger.info("email not found for facebook user {}", fb.getLink());
@@ -254,10 +257,10 @@ public class SocialLogin {
}
Long vkID = NumberUtils.toLong(jsonUser.getId(), 0);
- int uid = crosspostService.getUIDbyVKID(vkID);
- if (uid > 0) {
+ com.juick.User vkUser = crosspostService.getUserByVKID(vkID).orElse(AnonymousUser.INSTANCE);
+ if (!vkUser.isAnonymous()) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(redirectUrl);
- uriComponentsBuilder.queryParam("hash", userService.getHashByUID(uid));
+ uriComponentsBuilder.queryParam("hash", userService.getHashForUser(vkUser));
return "redirect:" + uriComponentsBuilder.build().toUriString();
} else {
String loginhash = UUID.randomUUID().toString();
@@ -282,17 +285,17 @@ public class SocialLogin {
byte[] secretKey = DigestUtils.sha256(telegramToken);
String resultString = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secretKey).hmacHex(dataCheckString);
if (hash.equals(resultString)) {
- Long tgUser = Long.valueOf(params.get("id"));
- int uid = telegramService.getUser(tgUser);
- if (uid > 0) {
- Cookie c = new Cookie("hash", userService.getHashByUID(uid));
+ Long tgUserId = Long.valueOf(params.get("id"));
+ com.juick.User tgUser = telegramService.getUser(tgUserId);
+ if (!tgUser.isAnonymous()) {
+ Cookie c = new Cookie("hash", userService.getHashForUser(tgUser));
c.setMaxAge(50 * 24 * 60 * 60);
response.addCookie(c);
return Utils.getPreviousPageByRequest(request).orElse("redirect:/");
} else {
String username = StringUtils.defaultString(params.get("username"), params.get("first_name"));
- telegramService.createTelegramUser(tgUser, username);
- return "redirect:/signup?type=durov&hash=" + userService.getSignUpHashByTelegramID(tgUser, username);
+ telegramService.createTelegramUser(tgUserId, username);
+ return "redirect:/signup?type=durov&hash=" + userService.getSignUpHashByTelegramID(tgUserId, username);
}
} else {
logger.warn("invalid tg hash {} for {}", resultString, hash);
diff --git a/juick-server/src/main/java/com/juick/server/api/Users.java b/juick-server/src/main/java/com/juick/server/api/Users.java
index 41deddb8..53d49e5b 100644
--- a/juick-server/src/main/java/com/juick/server/api/Users.java
+++ b/juick-server/src/main/java/com/juick/server/api/Users.java
@@ -46,7 +46,7 @@ public class Users {
@ApiOperation(value = "This returns user token", notes = "Pass login and password using HTTP Basic")
@RequestMapping(value = "/auth", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String getAuthToken() {
- return userService.getHashByUID(UserUtils.getCurrentUser().getUid());
+ return userService.getHashForUser(UserUtils.getCurrentUser());
}
@RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)