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 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())); person.setIcon(imgBuilder.toUriString()); return person; } throw new HttpNotFoundException(); } @GetMapping(value = "/u/{userName}/blog", produces = ActivityObject.CONTENT_TYPE) public OrderedCollection getOutbox( @PathVariable String userName, @RequestParam(required = false, defaultValue = "0") int before) { User visitor = UserUtils.getCurrentUser(); User user = userService.getUserByName(userName); if (!user.isAnonymous()) { ServletUriComponentsBuilder uriComponentsBuilder = ServletUriComponentsBuilder.fromCurrentRequestUri(); OrderedCollection blog = new OrderedCollection(); blog.setId(uriComponentsBuilder.toUriString()); List mids = messagesService.getUserBlog(user.getUid(), 0, before); List
articles = messagesService.getMessages(visitor, mids).stream().map(m -> { Article article = new Article(); article.setContent(m.getText()); return article; }).collect(Collectors.toList()); OrderedCollectionPage first = new OrderedCollectionPage(); first.setId(uriComponentsBuilder.toUriString()); Person person = new Person(); person.setName(user.getName()); first.setOrderedItems(articles.stream().map(a -> { Create create = new Create(); create.setActor(person); create.setObject(a); 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()); first.setNext(next); } blog.setFirst(first); return blog; } throw new HttpNotFoundException(); } }