aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/api/activity/Profile.java
blob: c3f838ee553175665a85d6794bf4ae2e708709a5 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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.apache.commons.lang3.StringUtils;
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;
import java.util.stream.Stream;

@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()) {
            UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri);
            Person person = new Person();
            uri.replacePath(String.format("/u/%s", userName));
            person.setId(uri.toUriString());
            person.setName(userName);
            uri.replacePath("/post");
            person.setInbox(uri.toUriString());
            person.setOutbox(uri.replacePath(String.format("/u/%s/blog/toc", userName)).toUriString());
            person.setFollowers(uri.replacePath(String.format("/u/%s/followers/toc", userName)).toUriString());
            person.setFollowing(uri.replacePath(String.format("/u/%s/following/toc", userName)).toUriString());
            uri.replacePath(String.format("/a/%d.png", user.getUid()));
            Image avatar = new Image();
            avatar.setUrl(uri.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 user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
            OrderedCollection blog = new OrderedCollection();
            blog.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().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<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, before);
            List<Note> notes = messagesService.getMessages(visitor, mids).stream().map(m -> {
                Note note = new Note();
                note.setPublished(m.getTimestamp());
                note.setContent(m.getText());
                if (StringUtils.isNotBlank(m.getAttachmentType())) {
                    Link attachment = new Link();
                    attachment.setHref(m.getAttachment().getMedium().getUrl());
                    note.setAttachment(attachment);
                }
                return note;
            }).collect(Collectors.toList());
            Person person = new Person();
            person.setName(user.getName());
            OrderedCollectionPage page = new OrderedCollectionPage();
            page.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            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();
    }
    @GetMapping(value = "/u/{userName}/followers/toc", produces = ActivityObject.CONTENT_TYPE)
    public OrderedCollection getFollowers(@PathVariable String userName) {
        User user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
            OrderedCollection followers = new OrderedCollection();
            followers.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            followers.setTotalItems(userService.getStatsMyReaders(user.getUid()));
            Link first = new Link();
            uriComponentsBuilder.path(String.format("/u/%s/followers", userName));
            first.setHref(uriComponentsBuilder.toUriString());
            followers.setFirst(first);
            return followers;
        }
        throw new HttpNotFoundException();
    }
    @GetMapping(value = "/u/{userName}/followers", produces = ActivityObject.CONTENT_TYPE)
    public OrderedCollectionPage getFollowersPage(@PathVariable String userName,
                                               @RequestParam(required = false, defaultValue = "0") int page) {
        User user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
            uriComponentsBuilder.path(String.format("/u/%s/followers", userName));
            List<User> followers = userService.getUserReaders(user.getUid());
            Stream<User> followersPage = followers.stream().skip(20 * page).limit(20);

            Person person = new Person();
            person.setName(user.getName());
            OrderedCollectionPage result = new OrderedCollectionPage();
            result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            result.setOrderedItems(followersPage.map(a -> {
                Person follower = new Person();
                follower.setName(a.getName());
                Link url = new Link();
                url.setHref(uriComponentsBuilder.replacePath(String.format("/u/%s", a.getName())).toUriString());
                follower.setUrl(url);
                return follower;
            }).collect(Collectors.toList()));
            boolean hasNext = followers.size() <= 20 * page;
            if (hasNext) {
                Link next = new Link();
                next.setHref(uriComponentsBuilder.queryParam("page", page + 1).toUriString());
                result.setNext(next);
            }
            return result;
        }
        throw new HttpNotFoundException();
    }
    @GetMapping(value = "/u/{userName}/following/toc", produces = ActivityObject.CONTENT_TYPE)
    public OrderedCollection getFollowing(@PathVariable String userName) {
        User user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
            OrderedCollection following = new OrderedCollection();
            following.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            following.setTotalItems(userService.getUserFriends(user.getUid()).size());
            Link first = new Link();
            uriComponentsBuilder.path(String.format("/u/%s/followers", userName));
            first.setHref(uriComponentsBuilder.toUriString());
            following.setFirst(first);
            return following;
        }
        throw new HttpNotFoundException();
    }
    @GetMapping(value = "/u/{userName}/following", produces = ActivityObject.CONTENT_TYPE)
    public OrderedCollectionPage getFollowingPage(@PathVariable String userName,
                                                  @RequestParam(required = false, defaultValue = "0") int page) {
        User user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUriString(baseUri);
            uriComponentsBuilder.path(String.format("/u/%s/following", userName));
            List<User> following = userService.getUserFriends(user.getUid());
            Stream<User> followingPage = following.stream().skip(20 * page).limit(20);

            Person person = new Person();
            person.setName(user.getName());
            OrderedCollectionPage result = new OrderedCollectionPage();
            result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            result.setOrderedItems(followingPage.map(a -> {
                Person follower = new Person();
                follower.setName(a.getName());
                Link url = new Link();
                url.setHref(uriComponentsBuilder.replacePath(String.format("/u/%s", a.getName())).toUriString());
                follower.setUrl(url);
                return follower;
            }).collect(Collectors.toList()));
            boolean hasNext = following.size() <= 20 * page;
            if (hasNext) {
                Link next = new Link();
                next.setHref(uriComponentsBuilder.queryParam("page", page + 1).toUriString());
                result.setNext(next);
            }
            return result;
        }
        throw new HttpNotFoundException();
    }
}