From 312b2408d72d11caf4d88cfbf07157ead220347f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 6 Oct 2018 21:26:54 +0300 Subject: Replies formatting for anonymous users --- juick-server/src/main/assets/scripts.js | 12 +++++++ .../java/com/juick/server/ActivityPubManager.java | 3 ++ .../com/juick/service/MessagesServiceImpl.java | 38 +++++++++++++--------- .../extension/filters/FormatMessageFilter.java | 16 +++++---- 4 files changed, 48 insertions(+), 21 deletions(-) (limited to 'juick-server/src') diff --git a/juick-server/src/main/assets/scripts.js b/juick-server/src/main/assets/scripts.js index 89c5c45f..1f2efd33 100644 --- a/juick-server/src/main/assets/scripts.js +++ b/juick-server/src/main/assets/scripts.js @@ -884,6 +884,18 @@ ready(function() { }); } }); + document.querySelectorAll('[data-user-uri]').forEach(el => { + let dataUri = el.getAttribute('href'); + if (dataUri) { + fetchUserUri(dataUri, user => { + let textNode = el.childNodes[0]; + if (textNode.nodeType === Node.TEXT_NODE && textNode.nodeValue.trim().length > 0) { + let uname = document.createTextNode(`@${user.uname}`); + el.replaceChild(uname, el.firstChild); + } + }); + } + }); initWS(); window.addEventListener('pagehide', wsShutdown); diff --git a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java index 2d6d06bc..3d296d8f 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -148,6 +148,9 @@ public class ActivityPubManager implements ActivityListener, NotificationListene } public String personUri(User user) { + if (user.getUri().toString().length() > 0) { + return user.getUri().toASCIIString(); + } UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/u/%s", user.getName())).toUriString(); } diff --git a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java index b357ff92..d556865d 100644 --- a/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -90,12 +90,14 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.setReplyQuote(MessageUtils.formatQuote(rs.getString(17))); msg.setUpdated(rs.getTimestamp(18).toInstant()); int quoteUid = rs.getInt(19); - if (quoteUid > 0) { - User quoteUser = new User(); - quoteUser.setUid(quoteUid); - quoteUser.setName(rs.getString(20)); - msg.setTo(quoteUser); + User quoteUser = new User(); + quoteUser.setUid(quoteUid); + quoteUser.setName(rs.getString(20)); + if (quoteUid == 0) { + quoteUser.setName(AnonymousUser.INSTANCE.getName()); + quoteUser.setUri(URI.create(Optional.ofNullable(rs.getString(23)).orElse(StringUtils.EMPTY))); } + msg.setTo(quoteUser); msg.setUpdatedAt(rs.getTimestamp(21).toInstant()); if (StringUtils.isNotEmpty(msg.getAttachmentType())) { try { @@ -334,7 +336,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "messages.readonly, messages.privacy, messages.replies," + "messages.attach, COUNT(DISTINCT favorites.user_id) as likes, messages.hidden," + "txt.tags, txt.repliesby, txt.txt, '' as q, messages.updated as updated, 0 as to_uid, " - + "NULL as to_name, txt.updated_at, '' as uri FROM messages " + + "NULL as to_name, txt.updated_at, '' as uri, '' as to_uri FROM messages " + "INNER JOIN users ON messages.user_id = users.id " + "INNER JOIN messages_txt AS txt " + "ON messages.message_id = txt.message_id " @@ -362,7 +364,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "replies.replyto, replies.ts," + "replies.attach, replies.txt, IFNULL(q.txt,t.txt) as quote, " + "COALESCE(q.user_id, m.user_id) AS to_uid, COALESCE(qu.nick, mu.nick) AS to_name, " - + "replies.updated_at, replies.user_uri as uri " + + "replies.updated_at, replies.user_uri as uri, " + + "q.user_uri AS to_uri " + "FROM replies LEFT JOIN users ON replies.user_id = users.id " + "LEFT JOIN replies q ON replies.message_id = q.message_id and replies.replyto = q.reply_id " + "LEFT JOIN messages_txt t ON replies.message_id = t.message_id " @@ -378,6 +381,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.setUser(new User()); msg.getUser().setUid(rs.getInt(1)); msg.getUser().setName(rs.getString(2)); + if (msg.getUser().getUid() == 0) { + msg.getUser().setName(AnonymousUser.INSTANCE.getName()); + msg.getUser().setUri( + URI.create(Optional.ofNullable(rs.getString(11)).orElse(StringUtils.EMPTY))); + } msg.setReplyto(rs.getInt(3)); msg.setTimestamp(rs.getTimestamp(4).toInstant()); msg.setAttachmentType(rs.getString(5)); @@ -388,12 +396,11 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ msg.setReplyQuote(MessageUtils.formatQuote(quote)); } int quoteUid = rs.getInt(8); - if (quoteUid > 0) { - User quoteUser = new User(); - quoteUser.setUid(quoteUid); - quoteUser.setName(rs.getString(9)); - msg.setTo(quoteUser); - } + User quoteUser = new User(); + quoteUser.setUid(quoteUid); + quoteUser.setName(Optional.ofNullable(rs.getString(9)).orElse(AnonymousUser.INSTANCE.getName())); + quoteUser.setUri(URI.create(Optional.ofNullable(rs.getString(11)).orElse(StringUtils.EMPTY))); + msg.setTo(quoteUser); msg.setUpdatedAt(rs.getTimestamp(10).toInstant()); if (StringUtils.isNotEmpty(msg.getAttachmentType())) { try { @@ -824,7 +831,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ + "messages.readonly,messages.privacy, messages.replies-COUNT(DISTINCT banned.reply_id) as replies," + "messages.attach,COUNT(DISTINCT favorites.user_id) AS likes,messages.hidden," + "messages_txt.tags,messages_txt.repliesby, messages_txt.txt, '' as q, " - + "messages.updated, 0 as to_uid, NULL as to_name, messages_txt.updated_at, '' as uri " + + "messages.updated, 0 as to_uid, NULL as to_name, messages_txt.updated_at, '' as uri, '' as to_uri " + "FROM (messages INNER JOIN messages_txt " + "ON messages.message_id=messages_txt.message_id) " + "INNER JOIN users ON messages.user_id=users.id " @@ -901,7 +908,8 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ "IFNULL(qw.txt, t.txt) as q, " + "NOW(), " + "COALESCE(qw.user_id, m.user_id) as to_uid, COALESCE(qu.nick, mu.nick) as to_name, " + - "replies.updated_at, replies.user_uri as uri " + + "replies.updated_at, replies.user_uri as uri, " + + "qw.user_uri as to_uri " + "FROM replies LEFT JOIN users " + "ON replies.user_id = users.id " + "LEFT JOIN replies qw ON replies.message_id = qw.message_id and replies.replyto = qw.reply_id " + diff --git a/juick-server/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java b/juick-server/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java index 5b5291f1..93f9fccb 100644 --- a/juick-server/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java +++ b/juick-server/src/main/java/com/mitchellbosecke/pebble/extension/filters/FormatMessageFilter.java @@ -18,6 +18,7 @@ package com.mitchellbosecke.pebble.extension.filters; import com.juick.Message; +import com.juick.model.AnonymousUser; import com.juick.util.MessageUtils; import com.mitchellbosecke.pebble.extension.Filter; import com.mitchellbosecke.pebble.extension.escaper.SafeString; @@ -27,6 +28,7 @@ import org.apache.commons.lang3.StringUtils; import java.util.List; import java.util.Map; +import java.util.Optional; /** * Created by vitalyster on 04.05.2017. @@ -37,12 +39,14 @@ public class FormatMessageFilter implements Filter { if (input instanceof Message) { Message msg = (Message) input; boolean isCode = msg.getTags().stream().anyMatch(t -> t.getName().equals("code")); - String formatString = MessageUtils.replyStartsWithQuote(msg) ? "@%s,\n%s" : "@%s, %s"; - String msgTxt = msg.getRid() > 0 ? String.format(formatString, msg.getTo().getName(), StringUtils.defaultString(msg.getText())) - : StringUtils.defaultString(msg.getText()); - String formattedMessage = isCode ? MessageUtils.formatMessageCode(msgTxt) - : MessageUtils.formatMessage(msgTxt); - return new SafeString(formattedMessage); + String toUserString = msg.getTo().getUid() == 0 ? String.format("@%s", + msg.getTo().getUri().toASCIIString(), msg.getTo().getName()) : String.format("@%s", msg.getTo().getName(), msg.getTo().getName()); + String formatString = MessageUtils.replyStartsWithQuote(msg) ? "%s,\n%s" : "%s, %s"; + String formattedMessage = isCode ? MessageUtils.formatMessageCode(StringUtils.defaultString(msg.getText())) + : MessageUtils.formatMessage(StringUtils.defaultString(msg.getText())); + String msgTxt = msg.getRid() > 0 ? String.format(formatString, toUserString, formattedMessage) + : formattedMessage; + return new SafeString(msgTxt); } throw new IllegalArgumentException("invalid input"); } -- cgit v1.2.3