diff options
Diffstat (limited to 'juick-core/src/main/java/com')
9 files changed, 127 insertions, 113 deletions
diff --git a/juick-core/src/main/java/com/juick/Message.java b/juick-core/src/main/java/com/juick/Message.java index c128b4f2..2b66e667 100644 --- a/juick-core/src/main/java/com/juick/Message.java +++ b/juick-core/src/main/java/com/juick/Message.java @@ -17,7 +17,10 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.*; +import java.util.stream.Collectors; /** * @@ -28,11 +31,12 @@ public class Message implements Comparable { private int MID = 0; private int RID = 0; - + + @JsonProperty("replyto") public int ReplyTo = 0; private String Text = null; private User User = null; - public List<String> Tags = new ArrayList<>(); + public List<Tag> Tags = new ArrayList<>(); private Date date; public int TimeAgo = 0; public int Privacy = 1; @@ -49,8 +53,8 @@ public class Message implements Comparable { public int Likes = 0; private boolean liked = false; public List<Message> childs = new ArrayList<>(); - private Optional<PM> PM = Optional.empty(); - private Optional<Recommendation> Recommendation = Optional.empty(); + private PM PM; + private Recommendation Recommendation; public Message() { } @@ -82,7 +86,8 @@ public class Message implements Comparable { public void parseTags(String strTags) { if (strTags != null) { - Tags.addAll(Arrays.asList(strTags.split(" "))); + List<String> tags = Arrays.asList(strTags.split(" ")); + Tags.addAll(tags.stream().map(Tag::new).collect(Collectors.toList())); } } @@ -156,8 +161,8 @@ public class Message implements Comparable { public String getTagsString() { String ret = ""; if (!Tags.isEmpty()) { - for (String Tag : Tags) { - ret += " *" + Tag; + for (Tag Tag : Tags) { + ret += " *" + Tag.getName(); } if (FriendsOnly) { ret += " *friends"; @@ -178,6 +183,7 @@ public class Message implements Comparable { return ret; } + @JsonProperty("mid") public int getMID() { return MID; } @@ -186,6 +192,7 @@ public class Message implements Comparable { this.MID = MID; } + @JsonProperty("rid") public int getRID() { return RID; } @@ -219,19 +226,19 @@ public class Message implements Comparable { this.date = date; } - public Optional<com.juick.PM> getPM() { + public com.juick.PM getPM() { return PM; } public void setPM(com.juick.PM PM) { - this.PM = Optional.ofNullable(PM); + this.PM = PM; } - public Optional<Recommendation> getRecommendation() { + public Recommendation getRecommendation() { return Recommendation; } public void setRecommendation(Recommendation recommendation) { - this.Recommendation = Optional.ofNullable(recommendation); + this.Recommendation = recommendation; } public boolean isLiked() { diff --git a/juick-core/src/main/java/com/juick/Tag.java b/juick-core/src/main/java/com/juick/Tag.java index 119aad99..8b790377 100644 --- a/juick-core/src/main/java/com/juick/Tag.java +++ b/juick-core/src/main/java/com/juick/Tag.java @@ -17,6 +17,9 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonValue; + import java.util.Objects; /** @@ -25,11 +28,15 @@ import java.util.Objects; */ public class Tag { - public String Name = null; + private String Name; public int TID = 0; public int SynonymID = 0; public int UsageCnt = 0; + public Tag(String name) { + this.Name = name; + } + @Override public boolean equals(Object o) { boolean equal = false; @@ -38,4 +45,13 @@ public class Tag { } return equal; } + @JsonIgnore + public String getName() { + return Name; + } + @Override + @JsonValue + public String toString() { + return Name; + } } diff --git a/juick-core/src/main/java/com/juick/User.java b/juick-core/src/main/java/com/juick/User.java index b9111d8f..a8109e50 100644 --- a/juick-core/src/main/java/com/juick/User.java +++ b/juick-core/src/main/java/com/juick/User.java @@ -17,6 +17,9 @@ */ package com.juick; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + /** * * @author Ugnich Anton @@ -25,11 +28,13 @@ public class User { private int UID = 0; private String UName = null; + @JsonIgnore public Object Avatar = null; private String FullName = null; private String JID = null; - public int MessagesCount = 0; + private int MessagesCount = 0; private String AuthHash = null; + @JsonIgnore public boolean Banned = false; public User() { @@ -51,6 +56,7 @@ public class User { return (obj instanceof User && ((User) obj).getUID() == this.getUID()); } + @JsonProperty("uid") public int getUID() { return UID; } @@ -59,6 +65,7 @@ public class User { this.UID = UID; } + @JsonProperty("uname") public String getUName() { return UName; } @@ -67,6 +74,7 @@ public class User { this.UName = UName; } + @JsonProperty("fullname") public String getFullName() { return FullName; } @@ -75,6 +83,7 @@ public class User { FullName = fullName; } + @JsonProperty("jid") public String getJID() { return JID; } @@ -83,6 +92,7 @@ public class User { this.JID = JID; } + @JsonIgnore public String getAuthHash() { return AuthHash; } @@ -90,7 +100,11 @@ public class User { public void setAuthHash(String authHash) { AuthHash = authHash; } + @JsonProperty("unreadCount") public Integer getUnreadCount() { return MessagesCount; } + public void setUnreadCount(Integer count) { + MessagesCount = count; + } } diff --git a/juick-core/src/main/java/com/juick/json/MessageSerializer.java b/juick-core/src/main/java/com/juick/json/MessageSerializer.java index 4238df60..d5adfa6e 100644 --- a/juick-core/src/main/java/com/juick/json/MessageSerializer.java +++ b/juick-core/src/main/java/com/juick/json/MessageSerializer.java @@ -18,6 +18,7 @@ package com.juick.json; import com.juick.Message; +import com.juick.Tag; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -27,6 +28,7 @@ import java.text.SimpleDateFormat; import java.util.TimeZone; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; /** * @@ -68,7 +70,7 @@ public class MessageSerializer extends JSONSerializer<Message> { if (json.has("tags")) { JSONArray tags = json.getJSONArray("tags"); for (int n = 0; n < tags.length(); n++) { - jmsg.Tags.add(tags.getString(n).replace(""", "\"")); + jmsg.Tags.add(new Tag(tags.getString(n).replace(""", "\""))); } } @@ -113,7 +115,7 @@ public class MessageSerializer extends JSONSerializer<Message> { json.put("user", userSerializer.serialize(msg.getUser())); } if (msg.Tags != null && msg.Tags.size() > 0) { - json.put("tags", new JSONArray(msg.Tags)); + json.put("tags", new JSONArray(msg.Tags.stream().map(Tag::getName).collect(Collectors.toList()))); } if (msg.Replies > 0) { json.put("replies", msg.Replies); diff --git a/juick-core/src/main/java/com/juick/server/MessagesQueries.java b/juick-core/src/main/java/com/juick/server/MessagesQueries.java index 3d0d734d..3c848f54 100644 --- a/juick-core/src/main/java/com/juick/server/MessagesQueries.java +++ b/juick-core/src/main/java/com/juick/server/MessagesQueries.java @@ -18,24 +18,22 @@ package com.juick.server; import com.juick.Message; +import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.PrivacyOpts; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.jdbc.core.ConnectionCallback; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import org.springframework.util.StringUtils; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; -import java.sql.Types; -import java.util.ArrayList; +import java.sql.*; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.logging.Logger; /** * @@ -43,7 +41,42 @@ import java.util.logging.Logger; */ public class MessagesQueries { - private static final Logger LOGGER = Logger.getLogger(MessagesQueries.class.getName()); + public static class MessageMapper implements RowMapper<Message> { + @Override + public Message mapRow(ResultSet rs, int rowNum) throws SQLException { + Message msg = new Message(); + msg.setMID(rs.getInt(1)); + msg.setUser(new User()); + msg.getUser().setUID(rs.getInt(2)); + msg.getUser().setUName(rs.getString(3)); + msg.TimeAgo = rs.getInt(4); + msg.setDate(rs.getTimestamp(5)); + msg.ReadOnly = rs.getBoolean(6); + msg.Privacy = rs.getInt(7); + msg.FriendsOnly = msg.Privacy < 0; + msg.Replies = rs.getInt(8); + msg.AttachmentType = rs.getString(9); + //if (rs.getInt(10) > 0) { + // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); + //} + if (rs.getDouble(10) != 0) { + msg.Place = new com.juick.Place(); + msg.Place.lat = rs.getDouble(11); + msg.Place.lon = rs.getDouble(12); + } + msg.Likes = rs.getInt(13); + msg.Hidden = rs.getBoolean(14); + // parse tags string + String tagsStr = rs.getString(15); + if (tagsStr != null) { + Arrays.asList(tagsStr.split(" ")) + .stream().forEach(t -> msg.Tags.add(new Tag(t))); + } + msg.RepliesBy = rs.getString(16); + msg.setText(rs.getString(17)); + return msg; + } + } public static int createMessage(JdbcTemplate sql, int uid, String txt, String attachment, List<com.juick.Tag> tags) { KeyHolder holder = new GeneratedKeyHolder(); @@ -72,7 +105,7 @@ public class MessagesQueries { tagsNames += " "; tagsIDs += ","; } - tagsNames += tags.get(i).Name; + tagsNames += tags.get(i).getName(); tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; } if (tags.size() > 0) { @@ -201,7 +234,7 @@ public class MessagesQueries { public static com.juick.Message getMessage(JdbcTemplate sql, int mid) { try { - return sql.queryForObject("SELECT messages.user_id,users.nick," + return sql.queryForObject("SELECT messages.message_id,messages.user_id,users.nick," + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," + "messages.ts," + "messages.readonly,messages.privacy,messages.replies," @@ -211,35 +244,8 @@ public class MessagesQueries { + "INNER JOIN users ON messages.user_id=users.id " + "INNER JOIN messages_txt AS txt " + "ON messages.message_id=txt.message_id " - + "WHERE messages.message_id=?", new Object[]{mid}, - (rs, rowNum) -> { - Message msg = new Message(); - msg.setMID(mid); - msg.setUser(new User()); - msg.getUser().setUID(rs.getInt(1)); - msg.getUser().setUName(rs.getString(2)); - msg.TimeAgo = rs.getInt(3); - msg.setDate(rs.getTimestamp(4)); - msg.ReadOnly = rs.getBoolean(5); - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.Replies = rs.getInt(7); - msg.AttachmentType = rs.getString(8); - //if (rs.getInt(9) > 0) { - // msg.Place = PlacesQueries.getPlace(sql, rs.getInt(9)); - //} - if (rs.getDouble(10) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(10); - msg.Place.lon = rs.getDouble(11); - } - msg.Likes = rs.getInt(12); - msg.Hidden = rs.getBoolean(13); - msg.parseTags(rs.getString(14)); - msg.RepliesBy = rs.getString(15); - msg.setText(rs.getString(16)); - return msg; - }); + + "WHERE messages.message_id=?", + new MessageMapper(), mid); } catch (EmptyResultDataAccessException e) { return null; } @@ -252,7 +258,7 @@ public class MessagesQueries { + "replies.attach,replies.txt FROM replies INNER JOIN users " + "ON replies.user_id=users.id " + "WHERE replies.message_id=? AND replies.reply_id=?", - new Object[]{mid, rid}, (rs, num) -> { + (rs, num) -> { Message msg = new Message(); msg.setMID(mid); msg.setRID(rid); @@ -264,7 +270,7 @@ public class MessagesQueries { msg.AttachmentType = rs.getString(5); msg.setText(rs.getString(6)); return msg; - }); + }, mid, rid); } catch (EmptyResultDataAccessException e) { return null; } @@ -290,10 +296,9 @@ public class MessagesQueries { return sql.query("SELECT tags.tag_id,synonym_id,name,stat_messages FROM tags " + "INNER JOIN messages_tags ON (messages_tags.message_id=? AND messages_tags.tag_id=tags.tag_id)", new Object[]{mid}, (rs, num) -> { - com.juick.Tag t = new com.juick.Tag(); + com.juick.Tag t = new com.juick.Tag(rs.getString(3)); t.TID = rs.getInt(1); t.SynonymID = rs.getInt(2); - t.Name = rs.getString(3); t.UsageCnt = rs.getInt(4); return t; }); @@ -479,10 +484,10 @@ public class MessagesQueries { if (before > 0) { mids = sqlSearch.queryForList("SELECT id AS message_id FROM messages WHERE MATCH(?) AND id<? " + - "ORDER BY id DESC LIMIT 25", new Object[]{search, before}, Integer.class); + "ORDER BY id DESC LIMIT 25", Integer.class, search, before); } else { mids = sqlSearch.queryForList("SELECT id AS message_id FROM messages WHERE MATCH(?) " + - "ORDER BY id DESC LIMIT 25", new Object[]{search}, Integer.class); + "ORDER BY id DESC LIMIT 25", Integer.class, search); } if (mids.size() > 0) { return sql.queryForList("SELECT message_id FROM messages WHERE message_id " + @@ -495,10 +500,10 @@ public class MessagesQueries { public static List<Integer> getUserBlog(JdbcTemplate sql, int UID, int privacy, int before) { if (before > 0) { return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND message_id<? AND privacy>=" + - privacy + " ORDER BY message_id DESC LIMIT 20", new Object[]{UID, before}, Integer.class); + privacy + " ORDER BY message_id DESC LIMIT 20", Integer.class, UID, before); } else { return sql.queryForList("SELECT message_id FROM messages WHERE user_id=? AND privacy>=" + privacy + - " ORDER BY message_id DESC LIMIT 20", new Object[]{UID}, Integer.class); + " ORDER BY message_id DESC LIMIT 20", Integer.class, UID); } } @@ -508,12 +513,12 @@ public class MessagesQueries { "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + "AND messages.message_id<? AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20", - new Object[] {UID, TID, before}, Integer.class); + Integer.class, UID, TID, before); } else { return sql.queryForList("SELECT messages.message_id FROM messages_tags INNER JOIN messages " + "USING(message_id) WHERE messages.user_id=? AND messages_tags.tag_id=? " + "AND messages.privacy>=" + privacy + " ORDER BY messages.message_id DESC LIMIT 20", - new Object[]{UID, TID}, Integer.class); + Integer.class, UID, TID); } } @@ -522,11 +527,11 @@ public class MessagesQueries { return sql.queryForList("SELECT message_id FROM favorites " + "WHERE user_id=? AND message_id<? " + "ORDER BY message_id DESC LIMIT 20", Integer.class, - new Object[]{UID, before}); + UID, before); } else { return sql.queryForList("SELECT message_id FROM favorites " + "WHERE user_id=? ORDER BY message_id DESC LIMIT 20", - Integer.class, new Object[]{UID}); + Integer.class, UID); } } @@ -566,43 +571,18 @@ public class MessagesQueries { public static List<com.juick.Message> getMessages(JdbcTemplate sql, List<Integer> mids) { if (!mids.isEmpty()) { return sql.query("SELECT messages.message_id,messages.user_id,users.nick," - + "messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt," + "TIMESTAMPDIFF(MINUTE,messages.ts,NOW())," - + "messages.ts,messages.replies," - + "messages_txt.repliesby,messages.attach,messages.lat," - + "messages.lon,messages.likes " + + "messages.ts," + + "messages.readonly,messages.privacy,messages.replies," + + "messages.attach,messages.place_id,messages.lat," + + "messages.lon,messages.likes,messages.hidden," + + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt " + "FROM (messages INNER JOIN messages_txt " + "ON messages.message_id=messages_txt.message_id) " + "INNER JOIN users ON messages.user_id=users.id " + "WHERE messages.message_id " + "IN (" + StringUtils.arrayToCommaDelimitedString(mids.toArray()) + ") " - + "ORDER BY messages.message_id DESC", (rs, rowNum) -> { - com.juick.Message msg = new com.juick.Message(); - msg.setUser(new User()); - - msg.setMID(rs.getInt(1)); - msg.getUser().setUID(rs.getInt(2)); - msg.getUser().setUName(rs.getString(3)); - if (rs.getString(4) != null) { - msg.parseTags(rs.getString(4)); - } - msg.ReadOnly = rs.getInt(5) == 1; - msg.Privacy = rs.getInt(6); - msg.FriendsOnly = msg.Privacy < 0; - msg.setText(rs.getString(7)); - msg.TimeAgo = rs.getInt(8); - msg.setDate(rs.getTimestamp(9)); - msg.Replies = rs.getInt(10); - msg.RepliesBy = rs.getString(11); - msg.AttachmentType = rs.getString(12); - if (rs.getDouble(13) != 0) { - msg.Place = new com.juick.Place(); - msg.Place.lat = rs.getDouble(13); - msg.Place.lon = rs.getDouble(14); - } - msg.Likes = rs.getInt(15); - return msg; - }); + + "ORDER BY messages.message_id DESC", new MessageMapper()); } return Collections.emptyList(); } diff --git a/juick-core/src/main/java/com/juick/server/PMQueries.java b/juick-core/src/main/java/com/juick/server/PMQueries.java index 875cbbd6..d7855002 100644 --- a/juick-core/src/main/java/com/juick/server/PMQueries.java +++ b/juick-core/src/main/java/com/juick/server/PMQueries.java @@ -75,7 +75,7 @@ public class PMQueries { com.juick.User u = new com.juick.User(); u.setUID(rs.getInt(1)); u.setUName(rs.getString(2)); - u.MessagesCount = rs.getInt(3); + u.setUnreadCount(rs.getInt(3)); return u; }, uid); } diff --git a/juick-core/src/main/java/com/juick/server/TagQueries.java b/juick-core/src/main/java/com/juick/server/TagQueries.java index 766f9bcd..76c12425 100644 --- a/juick-core/src/main/java/com/juick/server/TagQueries.java +++ b/juick-core/src/main/java/com/juick/server/TagQueries.java @@ -42,10 +42,9 @@ public class TagQueries { try { return sql.queryForObject("SELECT synonym_id,name FROM tags WHERE tag_id=?", (rs, num) -> { - Tag ret = new Tag(); + Tag ret = new Tag(rs.getString(2)); ret.TID = tid; ret.SynonymID = rs.getInt(1); - ret.Name = rs.getString(2); return ret; }, tid); } catch (EmptyResultDataAccessException e) { @@ -58,19 +57,17 @@ public class TagQueries { try { ret = sql.queryForObject("SELECT tag_id,synonym_id,name FROM tags WHERE name=?", (rs, rowNum) -> { - Tag ret1 = new Tag(); + Tag ret1 = new Tag(rs.getString(3)); ret1.TID = rs.getInt(1); ret1.SynonymID = rs.getInt(2); - ret1.Name = rs.getString(3); return ret1; }, tag); } catch (EmptyResultDataAccessException e) { // tag not found } if (ret == null && autoCreate) { - ret = new com.juick.Tag(); + ret = new com.juick.Tag(tag); ret.TID = createTag(sql, tag); - ret.Name = tag; } return ret; @@ -117,8 +114,7 @@ public class TagQueries { "AND messages.message_id=messages_tags.message_id)) " + "INNER JOIN tags ON messages_tags.tag_id=tags.tag_id GROUP BY tags.tag_id ORDER BY tags.name ASC", (rs, rowNum) -> { - Tag t = new Tag(); - t.Name = rs.getString(1); + Tag t = new Tag(rs.getString(1)); t.UsageCnt = rs.getInt(2); return t; }, uid); @@ -160,7 +156,7 @@ public class TagQueries { return Collections.emptyList(); } public static String toString(List<Tag> tags) { - return tags.stream().map(t -> " *" + t.Name) + return tags.stream().map(t -> " *" + t.getName()) .collect(Collectors.joining()); } } diff --git a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java index 033417a8..bb7615dd 100644 --- a/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java +++ b/juick-core/src/main/java/com/juick/server/protocol/JuickProtocol.java @@ -356,9 +356,9 @@ public class JuickProtocol { } } else { if (SubscriptionsQueries.unSubscribeTag(sql, user, tag)) { - return new ProtocolReply("Unsubscribed from " + tag.Name, Optional.empty()); + return new ProtocolReply("Unsubscribed from " + tag.getName(), Optional.empty()); } - return new ProtocolReply("You was not subscribed to " + tag.Name, Optional.empty()); + return new ProtocolReply("You was not subscribed to " + tag.getName(), Optional.empty()); } return new ProtocolReply("Error", Optional.empty()); } diff --git a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java index 510f7cc1..d259bc43 100644 --- a/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java +++ b/juick-core/src/main/java/com/juick/xmpp/extensions/JuickMessage.java @@ -17,6 +17,7 @@ */ package com.juick.xmpp.extensions; +import com.juick.Tag; import com.juick.xmpp.utils.XmlUtils; import com.juick.xmpp.*; import java.io.IOException; @@ -95,7 +96,7 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { } else if (tag.equals(JuickUser.TagName) && xmlns != null && xmlns.equals(JuickUser.XMLNS)) { jmsg.setUser(new JuickUser().parse(parser)); } else if (tag.equals("tag")) { - jmsg.Tags.add(XmlUtils.getTagText(parser)); + jmsg.Tags.add(new Tag(XmlUtils.getTagText(parser))); } else { XmlUtils.skip(parser); } @@ -137,10 +138,8 @@ public class JuickMessage extends com.juick.Message implements StanzaChild { if (getText() != null) { ret += "<body>" + XmlUtils.escape(getText()) + "</body>"; } - if (!Tags.isEmpty()) { - for (int i = 0; i < Tags.size(); i++) { - ret += "<tag>" + XmlUtils.escape(Tags.get(i)) + "</tag>"; - } + for (com.juick.Tag Tag : Tags) { + ret += "<tag>" + XmlUtils.escape(Tag.getName()) + "</tag>"; } ret += "</" + TagName + ">"; |