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 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 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); } }