From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- .../main/java/com/juick/www/controllers/Login.java | 2 +- .../com/juick/www/controllers/MessagesWWW.java | 6 ++-- .../java/com/juick/www/controllers/NewMessage.java | 6 ++-- .../java/com/juick/www/controllers/Settings.java | 28 ++++++++-------- .../java/com/juick/www/controllers/SignUp.java | 35 ++++++++++---------- juick-www/src/test/java/com/juick/WebAppTests.java | 38 ++++++++++++---------- 6 files changed, 59 insertions(+), 56 deletions(-) (limited to 'juick-www/src') 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 cb7df833..783430b2 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 @@ -44,7 +44,7 @@ public class Login { } @GetMapping("/login/success") public String getSuccessLogin(ModelMap model) { - model.addAttribute("hash", userService.getHashByUID(UserUtils.getCurrentUser().getUid())); + model.addAttribute("hash", userService.getHashForUser(UserUtils.getCurrentUser())); return "views/login_success"; } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java index f416cb86..dc0729e0 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java +++ b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java @@ -456,7 +456,7 @@ public class MessagesWWW { return "redirect:/login"; } String title = "PM: Inbox"; - List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); + List msgs = pmQueriesService.getLastPMInbox(visitor); fillUserModel(model, visitor, visitor); model.addAttribute("title", title); model.addAttribute("visitor", visitor); @@ -473,7 +473,7 @@ public class MessagesWWW { return "redirect:/login"; } String title = "PM: Sent"; - List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); + List msgs = pmQueriesService.getLastPMSent(visitor); if (WebUtils.isNotUserName(uname)) { uname = StringUtils.EMPTY; @@ -549,7 +549,7 @@ public class MessagesWWW { "\n" + "\n" + "\n"; - String twitterName = crosspostService.getTwitterName(msg.getUser().getUid()); + String twitterName = crosspostService.getTwitterName(msg.getUser()); if (StringUtils.isNotEmpty(twitterName)) { headers += "\n"; } 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 a464add2..a88698af 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 @@ -161,7 +161,7 @@ public class NewMessage { params.add("body", rid == 0 ? String.format("#%d %s", mid, body) : String.format("#%d/%d %s", mid, rid, body)); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); if (StringUtils.isNotEmpty(attachmentFName.toString())) { params.add("img", attachmentFName.toASCIIString()); } @@ -203,7 +203,7 @@ public class NewMessage { HttpEntity> request = new HttpEntity<>(params, headers); params.add("body", String.format("@%s %s", userTo.getName(), body)); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); URI postUri = UriComponentsBuilder.fromUriString(apiUrl).path("/post").build().toUri(); ResponseEntity result = rest.postForEntity( postUri, @@ -240,7 +240,7 @@ public class NewMessage { HttpEntity> request = new HttpEntity<>(params, headers); params.add("body", body); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); if (StringUtils.isNotEmpty(attachmentFName.toString())) { params.add("img", attachmentFName.toASCIIString()); } 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 f9527423..702f52af 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 @@ -96,20 +96,20 @@ public class Settings { model.addAttribute("visitor", visitor); model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("auths", userService.getAuthCodes(visitor)); - model.addAttribute("email_active", emailService.getNotificationsEmail(visitor.getUid())); + model.addAttribute("email_active", emailService.getNotificationsEmail(visitor)); model.addAttribute("ehash", userService.getEmailHash(visitor)); model.addAttribute("emails", userService.getEmails(visitor)); model.addAttribute("jids", userService.getAllJIDs(visitor)); List hours = IntStream.rangeClosed(0, 23).boxed() .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList()); model.addAttribute("hours", hours); - model.addAttribute("fbstatus", crosspostService.getFbCrossPostStatus(visitor.getUid())); - model.addAttribute("twitter_name", crosspostService.getTwitterName(visitor.getUid())); - model.addAttribute("telegram_name", crosspostService.getTelegramName(visitor.getUid())); + model.addAttribute("fbstatus", crosspostService.getFbCrossPostStatus(visitor)); + model.addAttribute("twitter_name", crosspostService.getTwitterName(visitor)); + model.addAttribute("telegram_name", crosspostService.getTelegramName(visitor)); model.addAttribute("notify_options", subscriptionService.getNotifyOptions(visitor)); model.addAttribute("userinfo", userService.getUserInfo(visitor)); if (page.equals("auth-email")) { - if (emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("code"))) { + if (emailService.verifyAddressByCode(visitor, request.getParameter("code"))) { ; model.addAttribute("result", "OK!"); } else { @@ -140,7 +140,7 @@ public class Settings { case "password": if (userService.updatePassword(visitor, request.getParameter("password"))) { result = "

Password has been changed.

"; - String hash = userService.getHashByUID(visitor.getUid()); + String hash = userService.getHashForUser(visitor); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); @@ -186,9 +186,9 @@ public class Settings { } break; case "email-add": - if (!emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("account"))) { + if (!emailService.verifyAddressByCode(visitor, request.getParameter("account"))) { String authCode = RandomStringUtils.randomAlphanumeric(8).toUpperCase(); - if (emailService.addVerificationCode(visitor.getUid(), request.getParameter("account"), authCode)) { + if (emailService.addVerificationCode(visitor, request.getParameter("account"), authCode)) { Session session = Session.getDefaultInstance(System.getProperties()); try { MimeMessage message = new MimeMessage(session); @@ -210,14 +210,14 @@ public class Settings { } break; case "email-del": - if (emailService.deleteEmail(visitor.getUid(), request.getParameter("account"))) { + if (emailService.deleteEmail(visitor, request.getParameter("account"))) { result = "

Deleted. Back.

"; } else { result = "

An error occured while deleting.

"; } break; case "email-subscr": - if (emailService.setNotificationsEmail(visitor.getUid(), request.getParameter("account"))) { + if (emailService.setNotificationsEmail(visitor, request.getParameter("account"))) { result = String.format("

Saved! Will send notifications to %s." + "

Back

", request.getParameter("account")); } else { @@ -225,7 +225,7 @@ public class Settings { } break; case "twitter-del": - crosspostService.deleteTwitterToken(visitor.getUid()); + crosspostService.deleteTwitterToken(visitor); for (Cookie cookie : request.getCookies()) { if (cookie.getName().equals("request_token")) { cookie.setMaxAge(0); @@ -246,15 +246,15 @@ public class Settings { result = "

Back

"; break; case "facebook-disable": - crosspostService.disableFBCrosspost(visitor.getUid()); + crosspostService.disableFBCrosspost(visitor); result = "

Back

"; break; case "facebook-enable": - crosspostService.enableFBCrosspost(visitor.getUid()); + crosspostService.enableFBCrosspost(visitor); result = "

Back

"; break; case "vk-del": - crosspostService.deleteVKUser(visitor.getUid()); + crosspostService.deleteVKUser(visitor); result = "

Back

"; break; default: 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 02a8006b..11f02169 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 @@ -16,6 +16,7 @@ */ package com.juick.www.controllers; +import com.juick.User; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.UserUtils; @@ -95,7 +96,7 @@ public class SignUp { @RequestParam(required = false) String username, @RequestParam(required = false) String password) { com.juick.User visitor = UserUtils.getCurrentUser(); - int uid = 0; + User signedUser = visitor; if (hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { throw new HttpBadRequestException(); @@ -107,20 +108,20 @@ public class SignUp { if (username.length() > 32) { throw new HttpBadRequestException(); } - uid = userService.checkPassword(username, password); + signedUser = userService.checkPassword(username, password); } else { - uid = visitor.getUid(); + signedUser = visitor; } - if (uid <= 0) { + if (signedUser.isAnonymous()) { throw new HttpForbiddenException(); } - if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid)) - && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid)) - && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid)) - && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid)) - && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, uid))) { + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, signedUser)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, signedUser)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, signedUser)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, signedUser)) + && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, signedUser.getUid()))) { throw new HttpBadRequestException(); } @@ -131,22 +132,22 @@ public class SignUp { // CHECK USERNAME - uid = userService.createUser(username, password); - if (uid <= 0) { + signedUser = userService.createUser(username, password); + if (signedUser.isAnonymous()) { throw new HttpBadRequestException(); } - if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid)) - && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid)) - && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid)) - && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid)) - && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, uid))) { + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, signedUser)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, signedUser)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, signedUser)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, signedUser)) + && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, signedUser.getUid()))) { throw new HttpBadRequestException(); } } if (visitor.isAnonymous()) { - hash = userService.getHashByUID(uid); + hash = userService.getHashForUser(signedUser); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); diff --git a/juick-www/src/test/java/com/juick/WebAppTests.java b/juick-www/src/test/java/com/juick/WebAppTests.java index 74be2c92..3b85b585 100644 --- a/juick-www/src/test/java/com/juick/WebAppTests.java +++ b/juick-www/src/test/java/com/juick/WebAppTests.java @@ -23,6 +23,7 @@ import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.css.StyleElement; import com.gargoylesoftware.htmlunit.html.DomElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.juick.server.helpers.AnonymousUser; import com.juick.service.*; import com.juick.util.MessageUtils; import com.juick.www.Utils; @@ -126,8 +127,7 @@ public class WebAppTests { userService.createUser(ugnichName, ugnichPassword); ugnich = userService.getUserByName(ugnichName); - int freefdId = userService.createUser(freefdName, freefdPassword); - freefd = userService.getUserByUID(freefdId).orElseThrow(IllegalStateException::new); + freefd = userService.createUser(freefdName, freefdPassword); isSetUp = true; } @@ -148,7 +148,7 @@ public class WebAppTests { @Test public void postWithoutTagsShouldNotHaveAsteriskInTitle() throws Exception { String msgText = "Привет, я - Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); + int mid = messagesService.createMessage(ugnich, msgText, null, null); HtmlPage threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getTitleText(), equalTo("ugnich:")); } @@ -159,8 +159,9 @@ public class WebAppTests { String msgText = "автор этого поста был забанен"; String hash = "12345678"; - User isilmine = userService.getUserByUID(userService.createUser(userName, userPassword)).orElseThrow(IllegalStateException::new); - int mid = messagesService.createMessage(isilmine.getUid(), msgText, null, null); + User isilmine = userService.createUser(userName, userPassword); + assertThat(isilmine, not(AnonymousUser.INSTANCE)); + int mid = messagesService.createMessage(isilmine, msgText, null, null); jdbcTemplate.update("UPDATE users SET banned=1 WHERE id=?", isilmine.getUid()); Page blogPage = webClient.getPage("http://localhost:8080/isilmine"); Page threadPage = webClient.getPage(String.format("http://localhost:8080/isilmine/%d", mid)); @@ -169,7 +170,7 @@ public class WebAppTests { } @Test public void repliesList() throws IOException { - int mid = messagesService.createMessage(ugnich.getUid(), "hello", null, null); + int mid = messagesService.createMessage(ugnich, "hello", null, null); IntStream.range(1, 15).forEach(i -> messagesService.createReply(mid, i-1, freefd, String.valueOf(i-1), null )); @@ -184,7 +185,7 @@ public class WebAppTests { } @Test public void userShouldNotSeeReplyButtonToBannedUser() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "freefd bl me", null, null); + int mid = messagesService.createMessage(ugnich, "freefd bl me", null, null); messagesService.createReply(mid, 0, ugnich, "yo", null); MvcResult loginResult = mockMvc.perform(post("/login") .param("username", freefdName) @@ -201,8 +202,9 @@ public class WebAppTests { assertThat(threadPage.querySelectorAll(".a-thread-comment").isEmpty(), equalTo(false)); privacyQueriesService.blacklistUser(freefd, ugnich); assertThat(userService.isInBLAny(freefd.getUid(), ugnich.getUid()), equalTo(true)); - int renhaId = userService.createUser("renha", "secret"); - messagesService.createReply(mid, 0, userService.getUserByUID(renhaId).orElseThrow(IllegalStateException::new), + User renha = userService.createUser("renha", "secret"); + assertThat(renha, not(AnonymousUser.INSTANCE)); + messagesService.createReply(mid, 0, renha, "people", null); threadPage = webClient.getPage(String.format("http://localhost:8080/ugnich/%d", mid)); assertThat(threadPage.getWebResponse().getStatusCode(), equalTo(200)); @@ -229,11 +231,11 @@ public class WebAppTests { @Test public void testTwitterCards() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "without image", null, null); + int mid = messagesService.createMessage(ugnich, "without image", null, null); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card") .getAttribute("content"), equalTo("summary")); - int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null); + int mid2 = messagesService.createMessage(ugnich, "with image", "png", null); Message message = messagesService.getMessage(mid2); assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card") .getAttribute("content"), equalTo("summary_large_image")); @@ -266,7 +268,7 @@ public class WebAppTests { mockMvc.perform(multipart("/post2") .file(file) .cookie(loginResult.getResponse().getCookies())).andExpect(status().isFound()); - int mid = messagesService.createMessage(ugnich.getUid(), "dummy message", null, null); + int mid = messagesService.createMessage(ugnich, "dummy message", null, null); mockMvc.perform(post("/comment") .param("mid", String.valueOf(mid)) .param("body", "yo")).andExpect(redirectedUrl("http://localhost/login")); @@ -297,7 +299,7 @@ public class WebAppTests { } @Test public void hashLoginShouldNotUseSession() throws Exception { - String hash = userService.getHashByUID(ugnich.getUid()); + String hash = userService.getHashForUser(ugnich); MvcResult hashLoginResult = mockMvc.perform(get("/?show=my&hash=" + hash)) .andExpect(status().isOk()) .andExpect(model().attribute("visitor", hasProperty("authHash", equalTo(hash)))) @@ -322,8 +324,8 @@ public class WebAppTests { @Test public void discussionsShouldBePageableByTimestamp() throws Exception { String msgText = "Привет, я снова Угнич"; - int mid = messagesService.createMessage(ugnich.getUid(), msgText, null, null); - int midNew = messagesService.createMessage(ugnich.getUid(), "Я более новый Угнич", null, null); + int mid = messagesService.createMessage(ugnich, msgText, null, null); + int midNew = messagesService.createMessage(ugnich, "Я более новый Угнич", null, null); MvcResult loginResult = mockMvc.perform(post("/login") .param("username", freefdName) .param("password", freefdPassword)).andReturn(); @@ -351,7 +353,7 @@ public class WebAppTests { HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "&to=" + msg.getUpdated().toEpochMilli()); assertThat(discussionsOld.querySelectorAll("article").size(), is(1)); assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew))); - List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); + List newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich, String.valueOf(i), null, null)).boxed().collect(Collectors.toList()); for (Integer m : newMids) { subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd); } @@ -384,7 +386,7 @@ public class WebAppTests { } @Test public void anythingRedirects() throws Exception { - int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, null); + int mid = messagesService.createMessage(ugnich, "yo", null, null); mockMvc.perform(get(String.format("/%d", mid))) .andExpect(status().isMovedPermanently()) .andExpect(redirectedUrl(String.format("/%s/%d", ugnich.getName(), mid))); @@ -407,7 +409,7 @@ public class WebAppTests { new com.gargoylesoftware.htmlunit.util.Cookie(loginCookie.getDomain(), loginCookie.getName(), loginCookie.getValue())); - int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null); + int mid = messagesService.createMessage(ugnich, "new test", null, null); subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd); int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null); HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss"); -- cgit v1.2.3