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 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 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 msgs = messagesService.getMessages(visitor.getUid(), mids); msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarWebPath(m.getUser()))); if (!visitor.isAnonymous()) { List unread = messagesService.getUnread(visitor); visitor.setUnreadCount(unread.size()); List 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; } }