aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/api/activity/Profile.java
blob: 404f0f84e17b4a8ca0ee214b6a4dd02b04f2a6b8 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.juick.server.api.activity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.Message;
import com.juick.User;
import com.juick.model.CommandResult;
import com.juick.server.ActivityPubManager;
import com.juick.server.CommandsManager;
import com.juick.server.KeystoreManager;
import com.juick.server.SignatureManager;
import com.juick.server.api.activity.model.Activity;
import com.juick.server.api.activity.model.Context;
import com.juick.server.api.activity.model.activities.Announce;
import com.juick.server.api.activity.model.activities.Create;
import com.juick.server.api.activity.model.activities.Delete;
import com.juick.server.api.activity.model.activities.Follow;
import com.juick.server.api.activity.model.activities.Like;
import com.juick.server.api.activity.model.activities.Undo;
import com.juick.server.api.activity.model.objects.Image;
import com.juick.server.api.activity.model.objects.Key;
import com.juick.server.api.activity.model.objects.Note;
import com.juick.server.api.activity.model.objects.OrderedCollection;
import com.juick.server.api.activity.model.objects.OrderedCollectionPage;
import com.juick.server.api.activity.model.objects.Person;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.UserUtils;
import com.juick.server.www.WebApp;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.service.activities.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.inject.Inject;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
public class Profile {
    private static final Logger logger = LoggerFactory.getLogger(Profile.class);
    @Inject
    private UserService userService;
    @Inject
    private MessagesService messagesService;
    @Inject
    private KeystoreManager keystoreManager;
    @Inject
    private SignatureManager signatureManager;
    @Inject
    private ActivityPubManager activityPubManager;
    @Inject
    private ApplicationEventPublisher applicationEventPublisher;
    @Inject
    private CommandsManager commandsManager;
    @Value("${web_domain:localhost}")
    private String domain;
    @Value("${ap_base_uri:http://localhost:8080/}")
    private String baseUri;
    @Inject
    private ObjectMapper jsonMapper;
    @Inject
    private WebApp webApp;

    @GetMapping(value = "/u/{userName}", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE})
    public Person getUser(@PathVariable String userName) {
        User user = userService.getUserByName(userName);
        if (!user.isAnonymous()) {
            Person person = new Person();
            person.setId(activityPubManager.personUri(user));
            person.setUrl(activityPubManager.personWebUri(user));
            person.setName(userName);
            person.setPreferredUsername(userName);
            Key publicKey = new Key();
            publicKey.setId(person.getId() + "#main-key");
            publicKey.setOwner(person.getId());
            publicKey.setPublicKeyPem(keystoreManager.getPublicKeyPem());
            person.setPublicKey(publicKey);
            person.setInbox(activityPubManager.inboxUri());
            person.setOutbox(activityPubManager.outboxUri(user));
            person.setFollowers(activityPubManager.followersUri(user));
            person.setFollowing(activityPubManager.followingUri(user));
            Image avatar = new Image();
            avatar.setUrl(webApp.getAvatarUrl(user));
            avatar.setMediaType("image/png");
            person.setIcon(avatar);
            return (Person) Context.build(person);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/blog/toc", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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()));
            blog.setFirst(uriComponentsBuilder.path(String.format("/u/%s/blog", userName)).toUriString());
            return (OrderedCollection) Context.build(blog);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/blog", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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 uri = UriComponentsBuilder.fromUriString(baseUri);
            String personUri = uri.path(String.format("/u/%s", userName)).toUriString();
            List<Integer> mids = messagesService.getUserBlog(user.getUid(), 0, before);
            List<Note> notes = messagesService.getMessages(visitor, mids).stream().map(activityPubManager::makeNote).collect(Collectors.toList());
            OrderedCollectionPage page = new OrderedCollectionPage();
            page.setPartOf(uri.replacePath(String.format("/u/%s/blog/toc", userName)).toUriString());
            page.setFirst(uri.replacePath(String.format("/u/%s/blog", userName)).toUriString());
            page.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            page.setOrderedItems(notes.stream().map(a -> {
                Create create = new Create();
                create.setId(a.getId() + "#Create");
                create.setTo(a.getTo());
                create.setActor(personUri);
                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) {
                page.setNext(uri.queryParam("before", beforeNext).toUriString());
            }
            page.setLast(uri.replaceQueryParam("before", "1").toUriString());
            return (OrderedCollectionPage) Context.build(page);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/followers/toc", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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()));
            followers.setFirst(uriComponentsBuilder.path(String.format("/u/%s/followers", userName)).toUriString());
            return (OrderedCollection) Context.build(followers);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/followers", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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);

            OrderedCollectionPage result = new OrderedCollectionPage();
            result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            result.setOrderedItems(followersPage.map(a -> {
                Person follower = new Person();
                follower.setName(a.getName());
                follower.setPreferredUsername(a.getName());
                follower.setUrl(activityPubManager.personWebUri(a));
                return follower;
            }).collect(Collectors.toList()));
            boolean hasNext = followers.size() <= 20 * page;
            if (hasNext) {
                result.setNext(uriComponentsBuilder.queryParam("page", page + 1).toUriString());
            }
            return (OrderedCollectionPage) Context.build(result);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/following/toc", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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());
            following.setFirst(uriComponentsBuilder.path(String.format("/u/%s/followers", userName)).toUriString());
            return (OrderedCollection) Context.build(following);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/u/{userName}/following", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_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);

            OrderedCollectionPage result = new OrderedCollectionPage();
            result.setId(ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString());
            result.setOrderedItems(followingPage.map(a -> {
                Person follower = new Person();
                follower.setName(a.getName());
                follower.setPreferredUsername(a.getName());
                follower.setUrl(activityPubManager.personWebUri(a));
                return follower;
            }).collect(Collectors.toList()));
            boolean hasNext = following.size() <= 20 * page;
            if (hasNext) {
                result.setNext(uriComponentsBuilder.queryParam("page", page + 1).toUriString());
            }
            return (OrderedCollectionPage) Context.build(result);
        }
        throw new HttpNotFoundException();
    }

    @GetMapping(value = "/n/{mid}-{rid}", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE})
    public Context showNote(@PathVariable int mid, @PathVariable int rid) {
        if (rid > 0) {
            // reply
            return Context.build(activityPubManager.makeNote(
                    messagesService.getReply(mid, rid)));
        }
        return Context.build(activityPubManager.makeNote(
                messagesService.getMessage(mid).orElseThrow(IllegalStateException::new)));
    }

    @PostMapping(value = "/api/inbox", consumes = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE})
    public ResponseEntity<Void> processInbox(@RequestBody Activity activity) throws Exception {
        User visitor = UserUtils.getCurrentUser();
        if ((StringUtils.isNotEmpty(visitor.getUri().toString()) && visitor.getUri().equals(URI.create(activity.getActor()))) || !visitor.isAnonymous()) {
            if (activity instanceof Follow) {
                Follow followRequest = (Follow) activity;
                applicationEventPublisher.publishEvent(
                        new FollowEvent(this, followRequest));
                return new ResponseEntity<>(HttpStatus.ACCEPTED);

            }
            if (activity instanceof Undo) {
                Map object = (Map) activity.getObject();
                String objectType = (String) object.get("type");
                String objectObject = (String) object.get("object");
                if (objectType.equals("Follow")) {
                    applicationEventPublisher.publishEvent(new UndoFollowEvent(this, activity.getActor(), objectObject));
                    return new ResponseEntity<>(HttpStatus.OK);
                } else if (objectType.equals("Like") || objectType.equals("Announce")) {
                    applicationEventPublisher.publishEvent(new UndoAnnounceEvent(this, activity.getActor(), objectObject));
                    return new ResponseEntity<>(HttpStatus.OK);
                }
            }
            if (activity instanceof Delete) {
                if (activity.getObject() instanceof String) {
                    // Delete user
                    applicationEventPublisher.publishEvent(new DeleteUserEvent(this, (String)activity.getObject()));
                    return new ResponseEntity<>(HttpStatus.OK);
                }
            }
            if (activity instanceof Create) {
                if (activity.getObject() instanceof Map) {
                    Map<String, Object> note = (Map<String, Object>) activity.getObject();
                    if (note.get("type").equals("Note")) {
                        URI noteId = URI.create((String) note.get("id"));
                        if (messagesService.replyExists(noteId)) {
                            return new ResponseEntity<>(HttpStatus.OK);
                        } else {
                            String inReplyTo = (String) note.get("inReplyTo");
                            if (StringUtils.isNotBlank(inReplyTo)) {
                                if (inReplyTo.startsWith(baseUri)) {
                                    String postId = activityPubManager.postId(inReplyTo);
                                    User user = new User();
                                    user.setUri(URI.create(activity.getActor()));
                                    String attachment = StringUtils.EMPTY;
                                    if (note.get("attachment") != null && ((List) note.get("attachment")).size() > 0) {
                                        Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
                                        attachment = (String) attachmentObj.get("url");
                                    }
                                    CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, note.get("content")), URI.create(attachment));
                                    logger.info(jsonMapper.writeValueAsString(result));
                                    if (result.getNewMessage().isPresent()) {
                                        messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
                                        return new ResponseEntity<>(HttpStatus.OK);
                                    } else {
                                        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
                                    }
                                } else {
                                    Message reply = messagesService.getReplyByUri(inReplyTo);
                                    if (reply != null) {
                                        User user = new User();
                                        user.setUri(URI.create(activity.getActor()));
                                        String attachment = StringUtils.EMPTY;
                                        if (note.get("attachment") != null && ((List) note.get("attachment")).size() > 0) {
                                            Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
                                            attachment = (String) attachmentObj.get("url");
                                        }
                                        CommandResult result = commandsManager.processCommand(user, String.format("#%d/%d %s", reply.getMid(), reply.getRid(), note.get("content")), URI.create(attachment));
                                        logger.info(jsonMapper.writeValueAsString(result));
                                        if (result.getNewMessage().isPresent()) {
                                            messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
                                            return new ResponseEntity<>(HttpStatus.OK);
                                        } else {
                                            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (activity instanceof Delete) {
                Map<String, Object> tombstone = (Map<String, Object>) activity.getObject();
                if (tombstone.get("type").equals("Tombstone")) {
                    URI actor = URI.create(activity.getActor());
                    URI reply = URI.create((String)tombstone.get("id"));
                    messagesService.deleteReply(actor, reply);
                    return new ResponseEntity<>(HttpStatus.OK);
                }
            }
            if (activity instanceof Like || activity instanceof Announce) {
                applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), (String)activity.getObject()));
                return new ResponseEntity<>(HttpStatus.OK);
            }
            logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity));
            return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
        }
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }
    @PostMapping(value = "/u/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public User fetchUser(@RequestParam URI uri) {
        Person person = (Person) signatureManager.getContext(uri).orElseThrow(HttpBadRequestException::new);
        User user = new User();
        user.setUri(URI.create(person.getUrl()));
        user.setName(person.getPreferredUsername());
        if (person.getIcon() != null) {
            user.setAvatar(person.getIcon().getUrl());
        }
        return user;
    }
}