/*
* Copyright (C) 2008-2017, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.juick.www.controllers;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.UserUtils;
import com.juick.service.MessagesService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Ugnich Anton
*/
@Controller
public class Tags {
@Inject
private MessagesService messagesService;
@Inject
private TagService tagService;
@Inject
private UserService userService;
@GetMapping("/tag/{tagName}")
protected String tagAction(HttpServletRequest request,
@PathVariable String tagName,
@RequestParam(required = false, defaultValue = "0") int before,
ModelMap model) throws IOException {
com.juick.User visitor = UserUtils.getCurrentUser();
String paramTagStr = StringEscapeUtils.unescapeHtml4(tagName);
com.juick.Tag paramTag = tagService.getTag(paramTagStr, false);
if (paramTag == null) {
throw new HttpNotFoundException();
} else if (paramTag.SynonymID > 0 && paramTag.TID != paramTag.SynonymID) {
com.juick.Tag synTag = tagService.getTag(paramTag.SynonymID);
String url = "/tag/" + URLEncoder.encode(StringEscapeUtils.escapeHtml4(synTag.getName()), CharEncoding.UTF_8);
if (request.getQueryString() != null) {
url += "?" + request.getQueryString();
}
return "redirect:" + url;
} else if (!paramTag.getName().equals(paramTagStr)) {
String url = "/tag/" + URLEncoder.encode(StringEscapeUtils.escapeHtml4(paramTag.getName()), CharEncoding.UTF_8);
if (request.getQueryString() != null) {
url += "?" + request.getQueryString();
}
return "redirect:" + url;
}
int visitor_uid = visitor.getUid();
String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName());
model.addAttribute("title", title);
List mids = messagesService.getTag(paramTag.TID, visitor_uid, before, (visitor_uid == 0) ? 40 : 20);
String head = StringUtils.EMPTY;
if (tagService.getTagNoIndex(paramTag.TID)) {
head = "";
} else if (before > 0 || mids.size() < 5) {
head = "";
}
model.addAttribute("headers", head);
model.addAttribute("visitor", visitor);
List msgs = messagesService.getMessages(mids);
if (visitor.getUid() != 0) {
List blUIDs = userService.checkBL(visitor.getUid(),
msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList()));
msgs.forEach(m -> m.ReadOnly |= blUIDs.contains(m.getUser().getUid()));
}
model.addAttribute("msgs", msgs);
model.addAttribute("tags", tagService.getPopularTags());
model.addAttribute("headers", head);
model.addAttribute("noindex", before > 0);
model.addAttribute("showAdv",before == 0 && visitor.getUid() == 0);
model.addAttribute("isModerator", visitor.getUid() == 3694);
if (mids.size() >= 20) {
String nextpage = "/tag/" + URLEncoder.encode(paramTag.getName(), CharEncoding.UTF_8) + "?before=" + mids.get(mids.size() - 1);
model.addAttribute("nextpage", nextpage);
}
return "views/index";
}
}