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

import com.juick.User;
import com.juick.service.*;
import com.juick.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.inject.Inject;
import javax.servlet.ServletException;
import java.io.IOException;
import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Created by aalexeev on 11/21/16.
 */
@Controller
public class SettingsController {
    @Inject
    UserService userService;
    @Inject
    TagService tagService;
    @Inject
    CrosspostService crosspostService;
    @Inject
    SubscriptionService subscriptionService;
    @Inject
    EmailService emailService;

    @RequestMapping("settings")
    public String showSettings(
            Principal principal,
            @RequestParam(required = false) String page,
            @RequestParam(required = false) String code,
            ModelMap context
    ) throws ServletException, IOException {
        String name = UserUtils.getUsername(principal, null);
        User visitor = userService.getUserByName(name);
        if (visitor.getUid() == 0) {
            return "redirect:/login";
        }
        List<String> pages = Arrays.asList("main", "password", "about", "auth-email", "privacy");
        if (StringUtils.isEmpty(page) || !pages.contains(page)) {
            page = "main";
        }
        context.put("title", "Настройки");
        context.put("visitor", visitor);
        context.put("tags", tagService.getPopularTags());
        context.put("auths", userService.getAuthCodes(visitor));
        context.put("eopts", userService.getEmailOpts(visitor));
        context.put("ehash", userService.getEmailHash(visitor));
        context.put("emails", userService.getEmails(visitor));
        context.put("jids", userService.getAllJIDs(visitor));
        List<String> hours = IntStream.rangeClosed(0, 23).boxed()
                .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList());
        context.put("hours", hours);
        context.put("fbstatus", crosspostService.getFbCrossPostStatus(visitor.getUid()));
        context.put("twitter_name", crosspostService.getTwitterName(visitor.getUid()));
        context.put("telegram_name", crosspostService.getTelegramName(visitor.getUid()));
        context.put("notify_options", subscriptionService.getNotifyOptions(visitor));
        context.put("userinfo", userService.getUserInfo(visitor));
        if (page.equals("auth-email")) {
            String response = emailService.verifyAddressByCode(visitor.getUid(), code) ?
                    "OK!" : "Sorry, code unknown.";
            context.put("result", response);
        }
        return String.format("views/settings_%s", page);
    }
}