package com.juick.server.api.activity; import com.juick.User; import com.juick.server.api.activity.model.*; import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.UserUtils; import com.juick.service.MessagesService; import com.juick.service.UserService; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder; import javax.inject.Inject; import java.util.List; import java.util.stream.Collectors; @RestController public class Profile { @Inject private UserService userService; @Inject private MessagesService messagesService; @Value("${web_domain:localhost}") private String domain; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; @Value("${img_url:http://localhost:8080/i/}") private String baseImagesUri; @GetMapping(value = "/u/{userName}", produces = ActivityObject.CONTENT_TYPE) public Person getUser(@PathVariable String userName) { User user = userService.getUserByName(userName); if (!user.isAnonymous()) { Person person = new Person(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(baseUri); builder.path(String.format("/u/%s", userName)); person.setId(builder.toUriString()); person.setName(userName); UriComponentsBuilder inboxBuilder = UriComponentsBuilder.fromUriString(baseUri); inboxBuilder.path("/post"); person.setInbox(inboxBuilder.toUriString()); UriComponentsBuilder outboxBuilder = UriComponentsBuilder.fromUriString(baseUri); person.setOutbox(outboxBuilder.path(String.format("/u/%s/blog", userName)).toUriString()); UriComponentsBuilder imgBuilder = UriComponentsBuilder.fromUriString(baseImagesUri); imgBuilder.path(String.format("/a/%d.png", user.getUid())); Image avatar = new Image(); avatar.setUrl(imgBuilder.toUriString()); avatar.setMediaType("image/png"); person.setIcon(avatar); return person; } throw new HttpNotFoundException(); } @GetMapping(value = "/u/{userName}/blog/toc", produces = ActivityObject.CONTENT_TYPE) public OrderedCollection getOutbox(@PathVariable String userName) { User visitor = UserUtils.getCurrentUser(); User user = userService.getUserByName(userName); if (!user.isAnonymous()) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); OrderedCollection blog = new OrderedCollection(); blog.setId(uriComponentsBuilder.toUriString()); blog.setTotalItems(userService.getStatsMessages(user.getUid())); Link first = new Link(); uriComponentsBuilder.path(String.format("/u/%s/blog", userName)); first.setHref(uriComponentsBuilder.toUriString()); blog.setFirst(first); return blog; } throw new HttpNotFoundException(); } @GetMapping(value = "/u/{userName}/blog", produces = ActivityObject.CONTENT_TYPE) public OrderedCollectionPage getOutboxPage(@PathVariable String userName, @RequestParam(required = false, defaultValue = "0") int before) { User visitor = UserUtils.getCurrentUser(); User user = userService.getUserByName(userName); if (!user.isAnonymous()) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri); uriComponentsBuilder.path(String.format("/u/%s/blog", userName)); List mids = messagesService.getUserBlog(user.getUid(), 0, before); List notes = messagesService.getMessages(visitor, mids).stream().map(m -> { Note note = new Note(); note.setPublished(m.getTimestamp()); note.setContent(m.getText()); return note; }).collect(Collectors.toList()); Person person = new Person(); person.setName(user.getName()); OrderedCollectionPage page = new OrderedCollectionPage(); page.setOrderedItems(notes.stream().map(a -> { Create create = new Create(); create.setActor(person); create.setObject(a); create.setPublished(a.getPublished()); return create; }).collect(Collectors.toList())); int beforeNext = mids.stream().reduce((fst, second) -> second).orElse(0); if (beforeNext > 0) { Link next = new Link(); next.setHref(uriComponentsBuilder.queryParam("before", beforeNext).toUriString()); page.setNext(next); } return page; } throw new HttpNotFoundException(); } }