Commit 62959551 by 段启岩

延时发布解析

parent ed209994
...@@ -78,7 +78,7 @@ public class PostApi { ...@@ -78,7 +78,7 @@ public class PostApi {
//发布动态 //发布动态
try { try {
postService.publishPost(post, postForm.getPublishTime()); postService.publishPost(post);
return Response.success(); return Response.success();
} catch (ProjectServiceException e) { } catch (ProjectServiceException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -27,6 +27,4 @@ public class PostForm { ...@@ -27,6 +27,4 @@ public class PostForm {
@NullOrNotBlank(message = "请传入有效的视频内容") @NullOrNotBlank(message = "请传入有效的视频内容")
private String video; private String video;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date publishTime;
} }
...@@ -23,10 +23,9 @@ public interface IPostService extends IService<Post> { ...@@ -23,10 +23,9 @@ public interface IPostService extends IService<Post> {
* 发布动态 * 发布动态
* *
* @param post * @param post
* @param publishTime
* @throws ProjectServiceException * @throws ProjectServiceException
*/ */
void publishPost(Post post, Date publishTime) throws ProjectServiceException; void publishPost(Post post) throws ProjectServiceException;
/** /**
* 删除动态 * 删除动态
......
...@@ -127,7 +127,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP ...@@ -127,7 +127,7 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
* @throws ProjectServiceException * @throws ProjectServiceException
*/ */
@Override @Override
public void publishPost(Post post, Date publishTime) throws ProjectServiceException { public void publishPost(Post post) throws ProjectServiceException {
//1.判断是否视频和图片都传了 //1.判断是否视频和图片都传了
if (null != post.getPictures() && null != post.getVideo()) { if (null != post.getPictures() && null != post.getVideo()) {
throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME); throw new ProjectServiceException(PostErrorCode.NOT_APPEAR_SAME_TIME);
...@@ -153,10 +153,11 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP ...@@ -153,10 +153,11 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IP
User user = userService.getById(post.getUserId()); User user = userService.getById(post.getUserId());
post.setUserNick(user.getNickName()); post.setUserNick(user.getNickName());
post.setUserAvatar(user.getUserAvatar()); post.setUserAvatar(user.getUserAvatar());
post.setContent(TopicUtils.encodeTopic(post.getContent())); Integer delay = TopicUtils.getDelay(post.getContent());
if (null != publishTime && publishTime.getTime() > System.currentTimeMillis()) { if (null != delay && delay > 0) {
post.setCreateTime(publishTime); post.setCreateTime(new Date(System.currentTimeMillis() + delay * 1000 * 60));
} }
post.setContent(TopicUtils.encodeTopic(post.getContent()));
//2.保存动态 //2.保存动态
save(post); save(post);
......
...@@ -21,6 +21,8 @@ public class TopicUtils { ...@@ -21,6 +21,8 @@ public class TopicUtils {
private static final Pattern topicPattern = Pattern.compile("(#([^#]+?)#)"); private static final Pattern topicPattern = Pattern.compile("(#([^#]+?)#)");
private static final Pattern delayPattern = Pattern.compile("(delay\\((\\d+)\\);)$");
public static String encodeTopic(String str) { public static String encodeTopic(String str) {
Assert.hasText(str, "str must not be empty"); Assert.hasText(str, "str must not be empty");
...@@ -37,7 +39,12 @@ public class TopicUtils { ...@@ -37,7 +39,12 @@ public class TopicUtils {
matcher.appendReplacement(buffer, tmp); matcher.appendReplacement(buffer, tmp);
} }
matcher.appendTail(buffer); matcher.appendTail(buffer);
return buffer.toString().replace("\n", "<br>"); String result = buffer.toString().replace("\n", "<br>");
matcher = delayPattern.matcher(result);
if (matcher.find()) {
result = matcher.replaceAll("");
}
return result;
} }
public static List<String> parseTopics(String str) { public static List<String> parseTopics(String str) {
...@@ -58,10 +65,20 @@ public class TopicUtils { ...@@ -58,10 +65,20 @@ public class TopicUtils {
return topicNames; return topicNames;
} }
public static Integer getDelay(String str) {
Matcher matcher = delayPattern.matcher(str);
if (matcher.find()) {
return Integer.valueOf(matcher.group(2));
} else {
return null;
}
}
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(TopicUtils.parseTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######")); // System.out.println(TopicUtils.parseTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######"));
System.out.println(TopicUtils.encodeTopic("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######哈哈#a#b")); System.out.println(TopicUtils.encodeTopic("#哈哈哈#今#哈哈delay(5);哈#天你吃饭了吗#吃了#######哈哈#a#bdelay(5);"));
System.out.println(TopicUtils.clearLockedTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######哈哈#a#b", List.of("哈哈哈"))); System.out.println(TopicUtils.getDelay("#哈哈哈#今#哈哈delay(5);哈#天你吃饭了吗#吃了#######哈哈#a#bdelay(5);"));
// System.out.println(TopicUtils.clearLockedTopics("#哈哈哈#今#哈哈哈#天你吃饭了吗#吃了#######哈哈#a#b", List.of("哈哈哈")));
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment