aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/controllers/X.java
blob: b1b25dbcd6b35b2ee5f9f8d3effad46a44421889 (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
package com.juick.www.controllers;

import java.util.List;
import java.util.Locale;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.juick.model.Message;
import com.juick.model.User;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.util.HttpNotFoundException;
import com.juick.www.WebApp;

import io.swagger.v3.oas.annotations.Parameter;

@RestController
public class X {

    private MessagesService messagesService;

    private User serviceUser;

    private UserService userService;

    private WebApp webApp;

    public X(MessagesService messagesService, User serviceUser, UserService userService, WebApp webApp) {
        this.messagesService = messagesService;
        this.serviceUser = serviceUser;
        this.userService = userService;
        this.webApp = webApp;
    }

    @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
    protected List<Message> doGet(@Parameter(hidden = true) User visitor, Locale locale,
            @RequestParam(name = "show", required = false) String paramShow,
            @RequestParam(name = "search", required = false) String paramSearch,
            @RequestParam(name = "before", required = false, defaultValue = "0") Integer paramBefore,
            @RequestParam(name = "to", required = false, defaultValue = "0") Long paramTo,
            @RequestParam(name = "page", required = false, defaultValue = "0") Integer page) {
        visitor.setAvatar(webApp.getAvatarWebPath(visitor));

        if (paramSearch != null && paramSearch.length() > 64) {
            paramSearch = null;
        }

        List<Integer> mids;

        if (paramSearch != null) {
            mids = messagesService.getSearch(visitor, paramSearch, page);
        } else if (paramShow == null) {
            mids = messagesService.getDiscussions(visitor.getUid(), paramTo);
        } else if (paramShow.equals("top")) {
            mids = messagesService.getUserBlogWithRecommendations(serviceUser, visitor, 0, paramBefore);
        } else if (paramShow.equals("my") && !visitor.isAnonymous()) {
            mids = messagesService.getMyFeed(visitor.getUid(), paramBefore, true);
        } else if (paramShow.equals("private") && !visitor.isAnonymous()) {
            mids = messagesService.getPrivate(visitor.getUid(), paramBefore);
        } else if (paramShow.equals("recommended") && !visitor.isAnonymous()) {
            mids = messagesService.getRecommended(visitor.getUid(), paramBefore);
        } else if (paramShow.equals("photos")) {
            mids = messagesService.getPhotos(visitor.getUid(), paramBefore);
        } else if (paramShow.equals("all")) {
            mids = messagesService.getAll(visitor.getUid(), paramBefore);
        } else {
            throw new HttpNotFoundException();
        }
        List<Message> msgs = messagesService.getMessages(visitor.getUid(), mids);
        msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser())));
        if (!visitor.isAnonymous()) {
            List<Integer> unread = messagesService.getUnread(visitor);
            visitor.setUnreadCount(unread.size());
            List<Integer> blUIDs = userService.checkBL(visitor.getUid(),
                    msgs.stream().map(m -> m.getUser().getUid()).toList());
            msgs.forEach(m -> m.ReadOnly |= blUIDs.contains(m.getUser().getUid()));
        }
        return msgs;
    }
}