From b6bb0dc8a3391442fad0124baa12799982cd6d8d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 20 Nov 2018 19:07:59 +0300 Subject: fix api avatar urls --- src/main/java/com/juick/server/XMPPConnection.java | 2 +- src/main/java/com/juick/server/api/Messages.java | 10 +++++----- src/main/java/com/juick/server/api/PM.java | 4 ++-- src/main/java/com/juick/server/api/Users.java | 12 ++++++------ src/main/java/com/juick/server/api/activity/Profile.java | 6 +----- src/main/java/com/juick/server/api/rss/MessagesView.java | 2 +- src/main/java/com/juick/server/www/WebApp.java | 12 ++++++++++++ 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/juick/server/XMPPConnection.java b/src/main/java/com/juick/server/XMPPConnection.java index 444e2512..44cc4a30 100644 --- a/src/main/java/com/juick/server/XMPPConnection.java +++ b/src/main/java/com/juick/server/XMPPConnection.java @@ -173,7 +173,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { userVCard.setFormattedName(info.getFullName()); userVCard.setNickname(user.getName()); try { - userVCard.setPhoto(new VCard.Image(URI.create(webApp.getAvatarWebPath(user)))); + userVCard.setPhoto(new VCard.Image(URI.create(webApp.getAvatarUrl(user)))); if (info.getUrl() != null) { userVCard.setUrl(new URL(info.getUrl())); } diff --git a/src/main/java/com/juick/server/api/Messages.java b/src/main/java/com/juick/server/api/Messages.java index 4ce1ea13..e105890f 100644 --- a/src/main/java/com/juick/server/api/Messages.java +++ b/src/main/java/com/juick/server/api/Messages.java @@ -78,7 +78,7 @@ public class Messages { int vuid = visitor.getUid(); List mids = messagesService.getMyFeed(vuid, before_mid, true); List msgs = messagesService.getMessages(visitor, mids); - msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); return ResponseEntity.ok(msgs); } return FORBIDDEN; @@ -141,7 +141,7 @@ public class Messages { } } List msgs = messagesService.getMessages(visitor, mids); - msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); return ResponseEntity.ok(msgs); } @DeleteMapping("/api/messages") @@ -161,7 +161,7 @@ public class Messages { public List getDiscussions( @RequestParam(required = false, defaultValue = "0") Long to) { List msgs = messagesService.getMessages(UserUtils.getCurrentUser(), messagesService.getDiscussions(UserUtils.getCurrentUser().getUid(), to)); - msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); return msgs; } @GetMapping("/api/thread") @@ -177,10 +177,10 @@ public class Messages { if (userService.getUserByName(msg.getUser().getName()).isBanned()) { throw new HttpNotFoundException(); } - msg.getUser().setAvatar(webApp.getAvatarWebPath(msg.getUser())); + msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser())); msg.setRecommendations(new HashSet<>(messagesService.getMessageRecommendations(msg.getMid()))); List replies = messagesService.getReplies(visitor, mid); - replies.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + replies.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); if (!visitor.isAnonymous()) { userService.updateLastSeen(visitor); applicationEventPublisher.publishEvent( diff --git a/src/main/java/com/juick/server/api/PM.java b/src/main/java/com/juick/server/api/PM.java index 05f46d8d..a80ad0dc 100644 --- a/src/main/java/com/juick/server/api/PM.java +++ b/src/main/java/com/juick/server/api/PM.java @@ -69,7 +69,7 @@ public class PM { } List msgs = pmQueriesService.getPMMessages(visitor.getUid(), uid); - msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); return msgs; } @@ -115,7 +115,7 @@ public class PM { // TODO: ignore cnt param for now but make sure paging param will not be cnt List lastconv = pmQueriesService.getLastChats(visitor); - lastconv.forEach(c -> c.setAvatar(webApp.getAvatarWebPath(c))); + lastconv.forEach(c -> c.setAvatar(webApp.getAvatarUrl(c))); PrivateChats pms = new PrivateChats(); pms.setUsers(lastconv); return pms; diff --git a/src/main/java/com/juick/server/api/Users.java b/src/main/java/com/juick/server/api/Users.java index c1cee8b5..6f9ab290 100644 --- a/src/main/java/com/juick/server/api/Users.java +++ b/src/main/java/com/juick/server/api/Users.java @@ -69,12 +69,12 @@ public class Users { if (!unames.isEmpty() && unames.size() < 20) users.addAll(userService.getUsersByName(unames)); } - users.forEach(u -> u.setAvatar(webApp.getAvatarWebPath(u))); + users.forEach(u -> u.setAvatar(webApp.getAvatarUrl(u))); if (!users.isEmpty()) return users; if (!UserUtils.getCurrentUser().isAnonymous()) { User visitor = UserUtils.getCurrentUser(); - visitor.setAvatar(webApp.getAvatarWebPath(visitor)); + visitor.setAvatar(webApp.getAvatarUrl(visitor)); return Collections.singletonList(visitor); } @@ -93,7 +93,7 @@ public class Users { me.setUnreadCount(unread.size()); me.setRead(userService.getUserFriends(visitor.getUid())); me.setReaders(userService.getUserReaders(visitor.getUid())); - me.setAvatar(webApp.getAvatarWebPath(visitor)); + me.setAvatar(webApp.getAvatarUrl(visitor)); return me; } @@ -118,7 +118,7 @@ public class Users { if (uid > 0) { List friends = userService.getUserFriends(uid); - friends.forEach(f -> f.setAvatar(webApp.getAvatarWebPath(f))); + friends.forEach(f -> f.setAvatar(webApp.getAvatarUrl(f))); return friends; } throw new HttpNotFoundException(); @@ -145,7 +145,7 @@ public class Users { if (uid > 0) { List readers = userService.getUserReaders(uid); - readers.forEach(r -> r.setAvatar(webApp.getAvatarWebPath(r))); + readers.forEach(r -> r.setAvatar(webApp.getAvatarUrl(r))); return readers; } throw new HttpNotFoundException(); @@ -155,7 +155,7 @@ public class Users { public UserInfo getUserInfo(@PathVariable String uname) { User user = userService.getUserByName(uname); if (!user.isBanned()) { - user.setAvatar(webApp.getAvatarWebPath(user)); + user.setAvatar(webApp.getAvatarUrl(user)); return userService.getUserInfo(user); } throw new HttpNotFoundException(); diff --git a/src/main/java/com/juick/server/api/activity/Profile.java b/src/main/java/com/juick/server/api/activity/Profile.java index 979f6265..d8a8fd90 100644 --- a/src/main/java/com/juick/server/api/activity/Profile.java +++ b/src/main/java/com/juick/server/api/activity/Profile.java @@ -78,8 +78,6 @@ public class Profile { private String domain; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; - @Value("${img_url:http://localhost:8080/i/}") - private String baseImagesUri; @Inject private ObjectMapper jsonMapper; @Inject @@ -103,10 +101,8 @@ public class Profile { person.setOutbox(activityPubManager.outboxUri(user)); person.setFollowers(activityPubManager.followersUri(user)); person.setFollowing(activityPubManager.followingUri(user)); - UriComponentsBuilder image = UriComponentsBuilder.fromUriString(baseImagesUri); - image.path(webApp.getAvatarWebPath(user)); Image avatar = new Image(); - avatar.setUrl(image.toUriString()); + avatar.setUrl(webApp.getAvatarUrl(user)); avatar.setMediaType("image/png"); person.setIcon(avatar); return (Person) Context.build(person); diff --git a/src/main/java/com/juick/server/api/rss/MessagesView.java b/src/main/java/com/juick/server/api/rss/MessagesView.java index 2dd9f53b..05bc0bd6 100644 --- a/src/main/java/com/juick/server/api/rss/MessagesView.java +++ b/src/main/java/com/juick/server/api/rss/MessagesView.java @@ -87,7 +87,7 @@ public class MessagesView extends AbstractRssFeedView { String link = String.format("http://juick.com/%s/", user.getName()); feed.setLink(link); Image rssImage = new Image(); - rssImage.setUrl(webApp.getAvatarWebPath(user)); + rssImage.setUrl(webApp.getAvatarUrl(user)); rssImage.setTitle(title); rssImage.setLink(link); feed.setImage(rssImage); diff --git a/src/main/java/com/juick/server/www/WebApp.java b/src/main/java/com/juick/server/www/WebApp.java index 08c6188d..7282eeeb 100644 --- a/src/main/java/com/juick/server/www/WebApp.java +++ b/src/main/java/com/juick/server/www/WebApp.java @@ -25,6 +25,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.servlet.resource.ResourceUrlProvider; import org.springframework.web.util.UriComponentsBuilder; +import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.ArrayList; import java.util.Arrays; @@ -43,6 +44,12 @@ public class WebApp { private ResourceUrlProvider resourceUrlProvider; @Value("${img_url:http://localhost:8080/i/}") private String baseImagesUri; + UriComponentsBuilder avatarBuilder; + + @PostConstruct + public void init() { + avatarBuilder = UriComponentsBuilder.fromUriString(baseImagesUri); + } public List parseTags(String tagsStr) { List tags = new ArrayList<>(); @@ -79,4 +86,9 @@ public class WebApp { } return resourceUrlProvider.getForLookupPath("/av-96.png"); } + + public String getAvatarUrl(User user) { + avatarBuilder.replacePath(getAvatarWebPath(user)); + return avatarBuilder.build().toUriString(); + } } -- cgit v1.2.3