From 725f2fde72df4bdd92a2262fbb335f40e0a81a1c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Dec 2022 20:40:59 +0300 Subject: Extract profile URI helpers into ProfileUriBuilder --- .../api/activity/helpers/ProfileUriBuilder.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/com/juick/www/api/activity/helpers/ProfileUriBuilder.java (limited to 'src/main/java/com/juick/www/api/activity/helpers') diff --git a/src/main/java/com/juick/www/api/activity/helpers/ProfileUriBuilder.java b/src/main/java/com/juick/www/api/activity/helpers/ProfileUriBuilder.java new file mode 100644 index 00000000..8f5e7566 --- /dev/null +++ b/src/main/java/com/juick/www/api/activity/helpers/ProfileUriBuilder.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2008-2022, Juick + * + * 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.www.api.activity.helpers; + +import com.juick.model.Message; +import com.juick.model.Tag; +import com.juick.model.User; +import lombok.AllArgsConstructor; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; + +@AllArgsConstructor +public class ProfileUriBuilder { + private String baseUri; + + public String getBaseUri() { + return baseUri; + } + + public String inboxUri() { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath("/api/inbox").toUriString(); + } + + public String outboxUri(User user) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath(String.format("/u/%s/blog/toc", user.getName())).toUriString(); + } + + 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(); + } + + public String personWebUri(User user) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath(String.format("/%s/", user.getName())).toUriString(); + } + + public String followersUri(User user) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath(String.format("/u/%s/followers/toc", user.getName())).toUriString(); + } + + public String followingUri(User user) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath(String.format("/u/%s/following/toc", user.getName())).toUriString(); + } + + public String messageUri(Message msg) { + return messageUri(msg.getMid(), msg.getRid()); + } + + public String messageUri(int mid, int rid) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + uri.replacePath(String.format("/n/%d-%d", mid, rid)); + return uri.toUriString(); + } + + public String tagUri(Tag tag) { + UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); + return uri.replacePath(String.format("/t/%s", tag.getName())).toUriString(); + } + + public String postId(String messageUri) { + UriComponents uri = UriComponentsBuilder.fromUriString(messageUri).build(); + return uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1) + .replace("-", "/"); + } +} -- cgit v1.2.3