From ed878bfc46df42c56d13fb6f235f9e7f63a64d61 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Mon, 19 Oct 2015 22:57:41 +0700 Subject: + RSS, basic classes --- src/java/com/juick/Message.java | 171 ++++++++++++++++++++++++++++++++ src/java/com/juick/Tag.java | 35 +++++++ src/java/com/juick/User.java | 53 ++++++++++ src/java/com/juick/http/www/Main.java | 39 ++++---- src/java/com/juick/http/www/RSS.java | 113 +++++++++++++++++++++ src/java/com/juick/http/www/Reader.java | 111 --------------------- src/java/com/juick/http/www/Utils.java | 1 + 7 files changed, 395 insertions(+), 128 deletions(-) create mode 100644 src/java/com/juick/Message.java create mode 100644 src/java/com/juick/Tag.java create mode 100644 src/java/com/juick/User.java create mode 100644 src/java/com/juick/http/www/RSS.java delete mode 100644 src/java/com/juick/http/www/Reader.java (limited to 'src') diff --git a/src/java/com/juick/Message.java b/src/java/com/juick/Message.java new file mode 100644 index 00000000..65c9c015 --- /dev/null +++ b/src/java/com/juick/Message.java @@ -0,0 +1,171 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; + +/** + * + * @author Ugnich Anton + */ +public class Message { + + public int MID = 0; + public int RID = 0; + public int ReplyTo = 0; + public String Text = null; + public User User = null; + public ArrayList Tags = new ArrayList(); + public Date Timestamp = null; + public String TimestampString = null; + public int TimeAgo = 0; + public int Privacy = 1; + public boolean FriendsOnly = false; + public boolean ReadOnly = false; + public boolean Hidden = false; + public boolean VisitorCanComment = true; + public int Replies = 0; + public String RepliesBy = null; + public String AttachmentType = null; + public String Photo = null; + public String Video = null; + public int Likes = 0; + public boolean UserLike = false; + public ArrayList childs = new ArrayList(); + + public Message() { + } + + public Message(Message msg) { + MID = msg.MID; + RID = msg.RID; + ReplyTo = msg.ReplyTo; + Text = msg.Text; + User = msg.User; + Tags = msg.Tags; + Timestamp = msg.Timestamp; + TimestampString = msg.TimestampString; + TimeAgo = msg.TimeAgo; + Privacy = msg.Privacy; + FriendsOnly = msg.FriendsOnly; + ReadOnly = msg.ReadOnly; + Hidden = msg.Hidden; + Replies = msg.Replies; + AttachmentType = msg.AttachmentType; + Photo = msg.Photo; + Video = msg.Video; + Likes = msg.Likes; + UserLike = msg.UserLike; + childs = msg.childs; + } + + public void parseTags(String strTags) { + Tags.addAll(Arrays.asList(strTags.split(" "))); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Message)) { + return false; + } + Message jmsg = (Message) obj; + return (this.MID == jmsg.MID && this.RID == jmsg.RID); + } + + public int compareTo(Object obj) throws ClassCastException { + if (!(obj instanceof Message)) { + throw new ClassCastException(); + } + Message jmsg = (Message) obj; + + if (this.MID != jmsg.MID) { + if (this.MID > jmsg.MID) { + return -1; + } else { + return 1; + } + } + + if (this.RID != jmsg.RID) { + if (this.RID < jmsg.RID) { + return -1; + } else { + return 1; + } + } + + return 0; + } + + public int getChildsCount() { + int cnt = childs.size(); + for (int i = 0; i < childs.size(); i++) { + cnt += childs.get(i).getChildsCount(); + } + return cnt; + } + + public void cleanupChilds() { + if (!childs.isEmpty()) { + for (int i = 0; i < childs.size(); i++) { + childs.get(i).cleanupChilds(); + } + childs.clear(); + } + } + + public String getAttachmentURL() { + if (AttachmentType != null) { + String url = "http://i.juick.com/"; + url += AttachmentType.equals("mp4") ? "video" : "photos-1024"; + url += "/" + MID; + if (RID > 0) { + url += "-" + RID; + } + url += "." + AttachmentType; + return url; + } else { + return null; + } + } + + public String getTagsString() { + String ret = ""; + for (int i = 0; i < Tags.size(); i++) { + ret += " *" + Tags.get(i); + } + if (FriendsOnly) { + ret += " *friends"; + } + if (Privacy == -2) { + ret += " *private"; + } + if (Privacy == -1) { + ret += " *friends"; + } + if (Privacy == 2) { + ret += " *public"; + } + if (ReadOnly) { + ret += " *readonly"; + } + return ret; + } +} diff --git a/src/java/com/juick/Tag.java b/src/java/com/juick/Tag.java new file mode 100644 index 00000000..3cee3358 --- /dev/null +++ b/src/java/com/juick/Tag.java @@ -0,0 +1,35 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick; + +/** + * + * @author Ugnich Anton + */ +public class Tag implements Comparable { + + public String Name = null; + public int TID = 0; + public int SynonymID = 0; + public int UsageCnt = 0; + + @Override + public int compareTo(Tag o) { + return this.Name.compareTo(o.Name); + } +} diff --git a/src/java/com/juick/User.java b/src/java/com/juick/User.java new file mode 100644 index 00000000..e3fc0ae7 --- /dev/null +++ b/src/java/com/juick/User.java @@ -0,0 +1,53 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick; + +/** + * + * @author Ugnich Anton + */ +public class User { + + public int UID = 0; + public String UName = null; + public Object Avatar = null; + public String FullName = null; + public String JID = null; + public int MessagesCount = 0; + public String AuthHash = null; + public boolean Banned = false; + + public User() { + } + + public User(User u) { + UID = u.UID; + UName = u.UName; + Avatar = u.Avatar; + FullName = u.FullName; + JID = u.JID; + MessagesCount = u.MessagesCount; + AuthHash = u.AuthHash; + Banned = u.Banned; + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof User && ((User) obj).UID == this.UID); + } +} diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java index 0fe93173..56aa439f 100644 --- a/src/java/com/juick/http/www/Main.java +++ b/src/java/com/juick/http/www/Main.java @@ -17,6 +17,7 @@ */ package com.juick.http.www; +import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Stream; import com.juick.xmpp.StreamComponent; @@ -43,13 +44,12 @@ import ru.sape.Sape; @WebServlet(name = "Main", urlPatterns = {"/"}) @MultipartConfig(fileSizeThreshold = 1024 * 1024, maxRequestSize = 1024 * 1024 * 10) public class Main extends HttpServlet implements Stream.StreamListener { - + Connection sql; Connection sqlSearch; Stream xmpp; Home home = new Home(); Discover discover = new Discover(); - Reader reader = new Reader(); PM pm = new PM(); Login login = new Login(); Help help = new Help(); @@ -60,29 +60,30 @@ public class Main extends HttpServlet implements Stream.StreamListener { VKontakteLogin loginVK = new VKontakteLogin(); SignUp signup = new SignUp(); Settings settings = new Settings(); - + RSS rss = new RSS(); + @Override public void init() throws ServletException { super.init(); try { Properties conf = new Properties(); conf.load(new FileInputStream("/etc/juick/www.conf")); - + Class.forName("com.mysql.jdbc.Driver"); sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", "")); sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000", "", ""); - + setupXmppComponent(conf.getProperty("xmpp_password")); - + PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600); } catch (Exception e) { log(null, e); } } - + public void setupXmppComponent(final String password) { Thread thr = new Thread(new Runnable() { - + @Override public void run() { try { @@ -97,17 +98,17 @@ public class Main extends HttpServlet implements Stream.StreamListener { }); thr.start(); } - + @Override public void onStreamFail(String msg) { System.err.println("XMPP STREAM FAIL: " + msg); } - + @Override public void onStreamReady() { System.err.println("XMPP STREAM READY"); } - + @Override public void destroy() { super.destroy(); @@ -142,7 +143,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { request.setCharacterEncoding("UTF-8"); } String uri = request.getRequestURI(); - + if (uri.equals("/")) { String tag = request.getParameter("tag"); if (tag != null) { @@ -151,10 +152,6 @@ public class Main extends HttpServlet implements Stream.StreamListener { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); home.doGet(sql, sqlSearch, request, response, visitor); } - } else if (uri.equals("/reader")) { - reader.doGet(sql, request, response); - } else if (uri.equals("/_out")) { - reader.doGetOut(sql, request, response); } else if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); if (visitor != null) { @@ -181,6 +178,14 @@ public class Main extends HttpServlet implements Stream.StreamListener { Errors.doGet404(sql, request, response); } } + } else if (uri.startsWith("/rss/")) { + String uname = uri.substring(5); + int uid = UserQueries.getUIDbyName(sql, uname); + if (uid > 0) { + rss.doGet(sql, request, response, uid, uname); + } else { + response.sendError(404); + } } else if (uri.equals("/logout")) { login.doGetLogout(sql, request, response); } else if (uri.equals("/settings")) { @@ -274,7 +279,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { if (request.getCharacterEncoding() == null) { request.setCharacterEncoding("UTF-8"); } - + String uri = request.getRequestURI(); if (uri.equals("/post")) { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); diff --git a/src/java/com/juick/http/www/RSS.java b/src/java/com/juick/http/www/RSS.java new file mode 100644 index 00000000..ab96221e --- /dev/null +++ b/src/java/com/juick/http/www/RSS.java @@ -0,0 +1,113 @@ +/* + * Juick + * Copyright (C) 2008-2013, ugnich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.juick.http.www; + +import com.juick.Message; +import com.juick.server.MessagesQueries; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Connection; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Iterator; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author ugnich + */ +public class RSS { + + private static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private static final SimpleDateFormat sdfRSS = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); + + protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response, int uid, String uname) throws ServletException, IOException { + ArrayList mids = MessagesQueries.getUserBlog(sql, uid, 0, 0); + if (mids.isEmpty()) { + response.sendError(404); + return; + } + + ArrayList msgs = MessagesQueries.getMessages(sql, mids); + + response.setContentType("application/rss+xml; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + out.println(""); + out.println(""); + out.println(""); + out.println(""); + out.println("" + uname + " - Juick"); + out.println("http://juick.com/" + uname + "/"); + out.println("The latest messages by @" + uname + " at Juick"); + out.println("http://i.juick.com/a/" + uid + ".png" + uname + " - Juickhttp://juick.com/" + uname + "/"); + + Iterator i = msgs.iterator(); + while (i.hasNext()) { + Message msg = i.next(); + + out.println(""); + out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); + out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); + + out.print("<![CDATA[@" + msg.User.UName + ":"); + if (!msg.Tags.isEmpty()) { + for (int n = 0; n < msg.Tags.size(); n++) { + out.print(" *" + msg.Tags.get(n)); + } + } + out.println("]]>"); + out.println(""); + + synchronized (sdfSQL) { + try { + Date date = sdfSQL.parse(msg.TimestampString); + out.println("" + sdfRSS.format(date) + ""); + } catch (Exception e) { + System.err.println("PARSE EXCEPTION: " + msg.TimestampString); + } + } + + out.println("http://juick.com/" + msg.User.UName + "/" + msg.MID + ""); + if (!msg.Tags.isEmpty()) { + for (int n = 0; n < msg.Tags.size(); n++) { + out.println("" + msg.Tags.get(n) + ""); + } + } + if (msg.AttachmentType != null) { + if (msg.AttachmentType.equals("jpg")) { + out.println(""); + out.println(""); + } else if (msg.AttachmentType.equals("png")) { + out.println(""); + out.println(""); + } + } + out.println(""); + out.println(""); + } + + out.println(""); + } finally { + out.close(); + } + } +} diff --git a/src/java/com/juick/http/www/Reader.java b/src/java/com/juick/http/www/Reader.java deleted file mode 100644 index 75a0e774..00000000 --- a/src/java/com/juick/http/www/Reader.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Juick - * Copyright (C) 2008-2011, Ugnich Anton - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.juick.http.www; - -import com.juick.server.MessagesQueries; -import com.juick.server.TagQueries; -import java.io.IOException; -import java.io.PrintWriter; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * - * @author Ugnich Anton - */ -public class Reader { - - protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - - response.setContentType("text/html; charset=UTF-8"); - PrintWriter out = response.getWriter(); - try { - String head = ""; - PageTemplates.pageHead(out, "Ссылки", head); - PageTemplates.pageNavigation(out, visitor, null); - out.println("
"); - - out.println("
    "); - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT link_id,rss_id,url,title FROM reader_links ORDER BY ts DESC LIMIT 100"); - rs = stmt.executeQuery(); - rs.beforeFirst(); - while (rs.next()) { - out.println("
  • " + rs.getString(4) + "
  • "); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - out.println("
"); - - out.println("
"); - PageTemplates.pageFooter(request, out, visitor, true); - PageTemplates.pageEnd(out); - } finally { - out.close(); - } - } - - protected void doGetOut(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - com.juick.User visitor = Utils.getVisitorUser(sql, request, response); - int lid = Utils.parseInt(request.getParameter("lid"), 0); - if (lid == 0) { - response.sendError(404); - return; - } - - String url = null; - - PreparedStatement stmt = null; - ResultSet rs = null; - try { - stmt = sql.prepareStatement("SELECT url FROM reader_links WHERE link_id=?"); - stmt.setInt(1, lid); - rs = stmt.executeQuery(); - if (rs.first()) { - url = rs.getString(1); - } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); - } - - if (url == null) { - response.sendError(404); - return; - } - - Utils.sendTemporaryRedirect(response, url); - } -} diff --git a/src/java/com/juick/http/www/Utils.java b/src/java/com/juick/http/www/Utils.java index e20be893..afaf131e 100644 --- a/src/java/com/juick/http/www/Utils.java +++ b/src/java/com/juick/http/www/Utils.java @@ -188,6 +188,7 @@ public class Utils { in.close(); return b.toString(); } catch (Exception e) { + System.err.println("fetchURL: "+e.toString()); return null; } } -- cgit v1.2.3