aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/api/activity/Profile.java
blob: 8311c862d63d52b27d0709492ee5d5f3f8abf35e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, before);
            List<Article> 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();
    }
}