From e2a018e998e125ee2ec983962059c4d2b733a4b4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 9 Dec 2016 17:35:49 +0300 Subject: juick-spring-www: WIP --- .../src/main/java/com/juick/www/WebApp.java | 79 ++++++++++++ .../www/configuration/WebAppConfiguration.java | 5 + .../com/juick/www/controllers/LoginController.java | 65 ++++++++++ .../com/juick/www/controllers/PMController.java | 138 ++++++++++++++++++++ .../main/webapp/WEB-INF/templates/views/login.html | 139 +++++++++++++++++++++ .../WEB-INF/templates/views/partial/footer.html | 8 +- .../templates/views/partial/homecolumn.html | 6 +- .../templates/views/partial/navigation.html | 45 +++---- .../webapp/WEB-INF/templates/views/pm_inbox.html | 44 +++++++ .../webapp/WEB-INF/templates/views/pm_sent.html | 42 +++++++ .../WEB-INF/templates/views/settings_about.html | 8 +- 11 files changed, 544 insertions(+), 35 deletions(-) create mode 100644 juick-spring-www/src/main/java/com/juick/www/WebApp.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java create mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java create mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/views/login.html create mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_inbox.html create mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_sent.html diff --git a/juick-spring-www/src/main/java/com/juick/www/WebApp.java b/juick-spring-www/src/main/java/com/juick/www/WebApp.java new file mode 100644 index 00000000..6b26ec03 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/WebApp.java @@ -0,0 +1,79 @@ +package com.juick.www; + +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.env.Environment; +import rocks.xmpp.core.XmppException; +import rocks.xmpp.core.session.Extension; +import rocks.xmpp.core.session.XmppSessionConfiguration; +import rocks.xmpp.extensions.component.accept.ExternalComponent; + +import javax.annotation.PostConstruct; + +/** + * Created by vitalyster on 09.12.2016. + */ +public class WebApp implements AutoCloseable { + private static Logger logger = LoggerFactory.getLogger(WebApp.class); + + private ExternalComponent xmpp; + + public String tmpDir; + public String imgDir; + + private String xmppHost, xmppPassword, xmppJid; + private int xmppPort; + private boolean isXmppDisabled; + + + public WebApp(Environment conf) { + tmpDir = conf.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/"); + imgDir = conf.getProperty("img_path", "/var/www/juick.com/i/"); + isXmppDisabled = BooleanUtils.toBoolean(conf.getProperty("xmpp_disabled")); + xmppHost = conf.getProperty("xmpp_host", "localhost"); + xmppPort = NumberUtils.toInt(conf.getProperty("xmpp_port", "5347"), 5347); + xmppJid = conf.getProperty("xmpp_jid", "www.localhost"); + xmppPassword = conf.getProperty("xmpp_password"); + } + + @PostConstruct + public void init() { + if (!isXmppDisabled) { + setupXmppComponent(xmppHost, xmppPort, xmppJid, xmppPassword); + } + } + + @Override + public void close() { + try { + if (getXmpp() != null) + getXmpp().close(); + + logger.info("ExternalComponent on juick-www destroyed"); + } catch (Exception e) { + logger.warn("Exception occurs on juick-www destroy", e); + } + } + + public void setupXmppComponent(final String host, final int port, final String jid, final String password) { + XmppSessionConfiguration configuration = XmppSessionConfiguration.builder() + .extensions(Extension.of(com.juick.Message.class)) + .build(); + setXmpp(ExternalComponent.create(jid, password, configuration, host, port)); + try { + getXmpp().connect(); + } catch (XmppException e) { + logger.warn("xmpp extension", e); + } + } + + public ExternalComponent getXmpp() { + return xmpp; + } + + public void setXmpp(ExternalComponent xmpp) { + this.xmpp = xmpp; + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java index 5fb7848d..eaed8ebd 100644 --- a/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java +++ b/juick-spring-www/src/main/java/com/juick/www/configuration/WebAppConfiguration.java @@ -1,5 +1,6 @@ package com.juick.www.configuration; +import com.juick.www.WebApp; import com.juick.www.settings.TemplateSettingsHolder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,4 +35,8 @@ public class WebAppConfiguration { public TemplateSettingsHolder settingsHolder() { return new TemplateSettingsHolder(env); } + @Bean + public WebApp webApp() { + return new WebApp(env); + } } diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java new file mode 100644 index 00000000..8a474c9b --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/LoginController.java @@ -0,0 +1,65 @@ +package com.juick.www.controllers; + +import com.juick.User; +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.service.UserService; +import com.juick.util.UserUtils; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.inject.Inject; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; +import java.net.URI; +import java.security.Principal; +import java.util.Optional; + +/** + * Created by vitalyster on 09.12.2016. + */ +@Controller +public class LoginController { + @Inject + UserService userService; + @Inject + Environment env; + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String doGetLoginForm(Principal principal) { + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + if (visitor.getUid() > 0) { + return "redirect:/login"; + } + return "views/login"; + } + @RequestMapping(value = "/login", method = RequestMethod.POST) + protected String doPostLogin( + @RequestParam("username") Optional username, + @RequestParam("password") Optional password, + @RequestHeader("Referer") Optional referer, + HttpServletResponse response) { + if (!username.isPresent() && password.isPresent()) { + throw new HttpBadRequestException(); + } + + int uid = userService.checkPassword(username.get(), password.get()); + if (uid > 0) { + if (referer.isPresent()) { + URI refererURI = URI.create(referer.get()); + if (refererURI.getHost().equals(env.getProperty("web_domain")) + && !refererURI.getPath().equals("/login")) { + return "redirect:" + referer.get(); + } else { + return "redirect:/"; + } + } + } + throw new HttpForbiddenException(); + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java new file mode 100644 index 00000000..c3b37cf6 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/PMController.java @@ -0,0 +1,138 @@ +package com.juick.www.controllers; + +import com.juick.Message; +import com.juick.User; +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.service.PMQueriesService; +import com.juick.service.TagService; +import com.juick.service.UserService; +import com.juick.util.MessageUtils; +import com.juick.util.UserUtils; +import com.juick.www.WebApp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import rocks.xmpp.addr.Jid; + +import javax.inject.Inject; +import java.security.Principal; +import java.util.List; + +/** + * Created by vitalyster on 09.12.2016. + */ +@Controller +public class PMController { + + private static final Logger logger = LoggerFactory.getLogger(PMController.class); + + @Inject + PMQueriesService pmQueriesService; + @Inject + UserService userService; + @Inject + TagService tagService; + @Inject + WebApp webApp; + + @RequestMapping("/pm/inbox") + public String doGetInbox(Principal principal, ModelMap context) { + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + String title = "PM: Inbox"; + List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); + msgs.forEach(m -> m.setText(MessageUtils.formatMessage(m.getText()))); + context.put("title", title); + context.put("visitor", visitor); + context.put("msgs", msgs); + context.put("tags", tagService.getPopularTags()); + return "views/pm_inbox"; + } + + @RequestMapping(value = "/pm/sent", method = RequestMethod.GET) + public String doGetSent( + Principal principal, + @RequestParam String uname, + ModelMap context) { + String title = "PM: Sent"; + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); + + if (!UserUtils.checkUserNameValid(uname)) { + uname = ""; + } + context.put("title", title); + context.put("visitor", visitor); + context.put("msgs", msgs); + context.put("tags", tagService.getPopularTags()); + context.put("uname", uname); + return "views/pm_sent"; + } + + @RequestMapping(value = "/pm/sent", method = RequestMethod.POST) + public String doPostPM( + Principal principal, + @RequestParam String uname, + @RequestParam String body, + ModelMap context) { + String name = UserUtils.getUsername(principal, null); + User visitor = userService.getUserByName(name); + if (uname.startsWith("@")) { + uname = uname.substring(1); + } + int uid = 0; + if (UserUtils.checkUserNameValid(uname)) { + uid = userService.getUIDbyName(uname); + } + + if (uid == 0 || body == null || body.length() < 1 || body.length() > 10240) { + throw new HttpBadRequestException(); + } + + if (userService.isInBLAny(uid, visitor.getUid())) { + throw new HttpForbiddenException(); + } + + if (pmQueriesService.createPM(visitor.getUid(), uid, body)) { + if (webApp.getXmpp() != null) { + rocks.xmpp.core.stanza.model.Message msg = new rocks.xmpp.core.stanza.model.Message(); + msg.setFrom(Jid.of("juick@juick.com")); + msg.setTo(Jid.of(String.format("%d@push.juick.com", uid))); + com.juick.Message jmsg = new com.juick.Message(); + jmsg.setUser(visitor); + jmsg.setText(body); + msg.addExtension(jmsg); + webApp.getXmpp().send(msg); + + msg.setTo(Jid.of(String.format("%d@ws.juick.com", uid))); + webApp.getXmpp().send(msg); + + List jids = userService.getJIDsbyUID(uid); + for (String jid : jids) { + rocks.xmpp.core.stanza.model.Message mm = new rocks.xmpp.core.stanza.model.Message(); + mm.setTo(Jid.of(jid)); + mm.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT); + if (pmQueriesService.havePMinRoster(visitor.getUid(), jid)) { + mm.setFrom(Jid.of(jmsg.getUser().getName(), "juick.com", "Juick")); + mm.setBody(body); + } else { + mm.setFrom(Jid.of("juick", "juick.com", "Juick")); + mm.setBody("Private message from @" + jmsg.getUser().getName() + ":\n" + body); + } + webApp.getXmpp().send(mm); + } + } else { + logger.warn("XMPP unavailable"); + } + return "redirect:/pm/sent"; + } else { + throw new HttpBadRequestException(); + } + } +} diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/login.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/login.html new file mode 100644 index 00000000..ae3aede6 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/login.html @@ -0,0 +1,139 @@ + + + + Juick + + + + + + + + + +
juick.com © 2008-2016   Контакты · Помощь
+ +
+ Зарегистрироваться: + + +
XMPP +
Отправьте LOGIN на juick@@juick.com
+
+
+ + + + \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/footer.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/footer.html index 6978a2c0..6fbaecf2 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/footer.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/footer.html @@ -11,9 +11,7 @@ Facebook \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/homecolumn.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/homecolumn.html index 5293918d..534e0368 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/homecolumn.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/homecolumn.html @@ -1,6 +1,4 @@

- {% include "views/partial/tags.html" %} - {% if showAdv %} - конструктор сайтов - {% endif %} +

Tags
+ конструктор сайтов

\ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/navigation.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/navigation.html index 2863d0c3..bd6359d7 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/navigation.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/partial/navigation.html @@ -9,29 +9,30 @@ -
- {% if visitor.getUID() > 0 %} - - - {% else %} -

Чтобы добавлять сообщения и комментарии, представьтесь. -

- {% endif %} +
+ + + + + +

Чтобы добавлять сообщения и комментарии, представьтесь. +

+
diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_inbox.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_inbox.html new file mode 100644 index 00000000..7ab557f5 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_inbox.html @@ -0,0 +1,44 @@ + + + + Inbox title + + + +
+
    +
  • +
    +
    + @ugnich: +
    + + ${msg.getUser().getName()} + +
    +
    01.01.1970
    +
    + +
    Lorem ipsum
    + +
    + +
    +
    + +
    +
    +
    + +
    +
  • +
+
+ + + + \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_sent.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_sent.html new file mode 100644 index 00000000..f2c05ad3 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/pm_sent.html @@ -0,0 +1,42 @@ + + + + Settings title + + + +
+
+
+
+
To:
+
+
+
+
+ +
+
+ + + + diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/settings_about.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/settings_about.html index ff82b542..b93a699e 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/views/settings_about.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/views/settings_about.html @@ -10,12 +10,12 @@
-

Full name:

-

Country:

-

URL:
+

Full name:

+

Country:

+

URL:
Please, start with "http://"

About:
-
+
Max. 255 symbols

Avatar:
Recommendations: PNG, 96x96, <50Kb. Also, JPG and GIF supported.

-- cgit v1.2.3