Compare commits

..

16 Commits

Author SHA1 Message Date
Tim
0fa08e2260 feat: allow closing posts 2025-08-19 22:18:53 +08:00
Tim
38a49f7414 fix: 信息展示效率低 #632 2025-08-19 21:58:02 +08:00
Tim
fb89c9fb25 fix: 弹出弹窗逻辑修改 2025-08-19 21:48:48 +08:00
Tim
e9458f5419 fix: hljs 优化导入 2025-08-19 21:24:27 +08:00
Tim
2d87c8f23d fix: 格式化问题修改 2025-08-19 21:17:13 +08:00
Tim
cb281e4030 Merge pull request #638 from nagisa77/feature/message_load_more
支持分页加载
2025-08-19 19:52:34 +08:00
Tim
fe84e3f2fa Merge pull request #639 from CH-122/fix/post-page-update
修复文章详情页面返回后不更新数据 & 优化 /reaction-types 接口重复调用
2025-08-19 17:02:30 +08:00
Tim
c307732696 Merge pull request #637 from zpaeng/main
fix:删帖需要给发帖者提示
2025-08-19 17:01:44 +08:00
CH-122
a29bf7d860 feat: 增加 useReactionTypes,优化 /reaction-types 接口重复调用 2025-08-19 16:52:33 +08:00
CH-122
27393c15f2 fix: 添加 onActivated 钩子以刷新帖子和评论 2025-08-19 16:51:22 +08:00
zpaeng
c91a787f29 Merge branch 'nagisa77:main' into main 2025-08-19 16:49:08 +08:00
zpaeng
6096712291 fix:删帖需要给发帖者提示 2025-08-19 16:45:47 +08:00
Tim
6d20addcde Merge pull request #634 from CH-122/fix/post-list-content
fix: 帖子描述与参与人员重叠
2025-08-19 15:47:21 +08:00
CH-122
d8f9fd670c fix: 帖子描述与参与人员重叠 2025-08-19 15:38:12 +08:00
Tim
5ebe739917 Merge pull request #631 from WoJiaoFuXiaoYun/main
style: 优化行内代码样式
2025-08-19 15:26:29 +08:00
WangHe
022edc866a style: 优化行内代码样式
Related #622
2025-08-19 15:03:08 +08:00
19 changed files with 345 additions and 45 deletions

View File

@@ -62,6 +62,16 @@ public class PostController {
postService.deletePost(id, auth.getName());
}
@PostMapping("/{id}/close")
public PostSummaryDto close(@PathVariable Long id, Authentication auth) {
return postMapper.toSummaryDto(postService.closePost(id, auth.getName()));
}
@PostMapping("/{id}/reopen")
public PostSummaryDto reopen(@PathVariable Long id, Authentication auth) {
return postMapper.toSummaryDto(postService.reopenPost(id, auth.getName()));
}
@GetMapping("/{id}")
public ResponseEntity<PostDetailDto> getPost(@PathVariable Long id, Authentication auth) {
String viewer = auth != null ? auth.getName() : null;

View File

@@ -32,5 +32,6 @@ public class PostSummaryDto {
private PostType type;
private LotteryDto lottery;
private boolean rssExcluded;
private boolean closed;
}

View File

@@ -64,6 +64,7 @@ public class PostMapper {
dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt());
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
dto.setClosed(post.isClosed());
List<ReactionDto> reactions = reactionService.getReactionsForPost(post.getId())
.stream()

View File

@@ -64,6 +64,9 @@ public class Post {
@Column(nullable = false)
private PostType type = PostType.NORMAL;
@Column(nullable = false)
private boolean closed = false;
@Column
private LocalDateTime pinnedAt;

View File

@@ -52,6 +52,9 @@ public class CommentService {
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
Post post = postRepository.findById(postId)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
if (post.isClosed()) {
throw new IllegalStateException("Post closed");
}
Comment comment = new Comment();
comment.setAuthor(author);
comment.setPost(post);
@@ -94,6 +97,9 @@ public class CommentService {
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
Comment parent = commentRepository.findById(parentId)
.orElseThrow(() -> new IllegalArgumentException("Comment not found"));
if (parent.getPost().isClosed()) {
throw new IllegalStateException("Post closed");
}
Comment comment = new Comment();
comment.setAuthor(author);
comment.setPost(parent.getPost());

View File

@@ -512,6 +512,30 @@ public class PostService {
return postRepository.save(post);
}
public Post closePost(Long id, String username) {
Post post = postRepository.findById(id)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
if (!user.getId().equals(post.getAuthor().getId()) && user.getRole() != Role.ADMIN) {
throw new IllegalArgumentException("Unauthorized");
}
post.setClosed(true);
return postRepository.save(post);
}
public Post reopenPost(Long id, String username) {
Post post = postRepository.findById(id)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
if (!user.getId().equals(post.getAuthor().getId()) && user.getRole() != Role.ADMIN) {
throw new IllegalArgumentException("Unauthorized");
}
post.setClosed(false);
return postRepository.save(post);
}
@org.springframework.transaction.annotation.Transactional
public Post updatePost(Long id,
String username,

View File

@@ -21,6 +21,7 @@
</div>
</div>
<GlobalPopups />
<ConfirmDialog />
</div>
</template>
@@ -28,6 +29,7 @@
import HeaderComponent from '~/components/HeaderComponent.vue'
import MenuComponent from '~/components/MenuComponent.vue'
import GlobalPopups from '~/components/GlobalPopups.vue'
import ConfirmDialog from '~/components/ConfirmDialog.vue'
import { useIsMobile } from '~/utils/screen'
const isMobile = useIsMobile()

View File

@@ -90,7 +90,8 @@ body {
}
.vditor-toolbar--pin {
top: var(--header-height) !important;
top: calc(var(--header-height) + 1px) !important;
z-index: 2000;
}
.vditor-panel {

View File

@@ -22,6 +22,7 @@ import {
getEditorTheme as getEditorThemeUtil,
getPreviewTheme as getPreviewThemeUtil,
} from '~/utils/vditor'
import '~/assets/global.css'
import LoginOverlay from '~/components/LoginOverlay.vue'
export default {

View File

@@ -57,7 +57,7 @@
v-if="showEditor"
@submit="submitReply"
:loading="isWaitingForReply"
:disabled="!loggedIn"
:disabled="!loggedIn || postClosed"
:show-login-overlay="!loggedIn"
:parent-user-name="comment.userName"
/>
@@ -76,6 +76,7 @@
:level="level + 1"
:default-show-replies="item.openReplies"
:post-author-id="postAuthorId"
:post-closed="postClosed"
/>
</template>
</BaseTimeline>
@@ -122,6 +123,10 @@ const props = defineProps({
type: [Number, String],
required: true,
},
postClosed: {
type: Boolean,
default: false,
},
})
const emit = defineEmits(['deleted'])
@@ -148,6 +153,7 @@ const toggleReplies = () => {
}
const toggleEditor = () => {
if (props.postClosed) return
showEditor.value = !showEditor.value
if (showEditor.value) {
setTimeout(() => {
@@ -213,6 +219,10 @@ const deleteComment = async () => {
}
const submitReply = async (parentUserName, text, clear) => {
if (!text.trim()) return
if (props.postClosed) {
toast.error('帖子已关闭')
return
}
isWaitingForReply.value = true
const token = getToken()
if (!token) {

View File

@@ -0,0 +1,71 @@
<template>
<BasePopup :visible="visible" @close="onCancel">
<div class="confirm-dialog" role="dialog" aria-modal="true">
<h3 class="confirm-title">{{ title }}</h3>
<p class="confirm-message">{{ message }}</p>
<div class="confirm-actions">
<div class="cancel-button" @click="onCancel">取消</div>
<div class="confirm-button" @click="onConfirm">确认</div>
</div>
</div>
</BasePopup>
</template>
<script setup lang="ts">
import BasePopup from '~/components/BasePopup.vue'
import { useConfirm } from '~/composables/useConfirm'
const { visible, title, message, onConfirm, onCancel } = useConfirm()
</script>
<style scoped>
.confirm-dialog {
padding: 20px;
text-align: center;
}
.confirm-title {
margin-top: 0;
font-size: 18px;
font-weight: 600;
}
.confirm-message {
margin: 16px 0 20px;
line-height: 1.6;
color: var(--text-secondary, #666);
}
.confirm-actions {
display: flex;
justify-content: center;
gap: 12px;
}
.confirm-button,
.cancel-button {
min-width: 88px;
height: 36px;
padding: 0 14px;
border-radius: 8px;
cursor: pointer;
border: 1px solid transparent;
}
.confirm-button {
background: var(--primary-color);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.confirm-button:hover {
background: var(--primary-color-hover);
}
.cancel-button {
background: transparent;
color: var(--primary-color);
border-color: currentColor;
display: flex;
align-items: center;
justify-content: center;
}
.cancel-button:hover {
opacity: 0.85;
}
</style>

View File

@@ -16,6 +16,7 @@ import {
getEditorTheme as getEditorThemeUtil,
getPreviewTheme as getPreviewThemeUtil,
} from '~/utils/vditor'
import '~/assets/global.css'
export default {
name: 'PostEditor',

View File

@@ -51,6 +51,10 @@ import { computed, onMounted, ref, watch } from 'vue'
import { toast } from '~/main'
import { authState, getToken } from '~/utils/auth'
import { reactionEmojiMap } from '~/utils/reactions'
import { useReactionTypes } from '~/composables/useReactionTypes'
const { reactionTypes, initialize } = useReactionTypes()
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
const emit = defineEmits(['update:modelValue'])
@@ -66,30 +70,6 @@ watch(
)
const reactions = ref(props.modelValue)
const reactionTypes = ref([])
let cachedTypes = null
const fetchTypes = async () => {
if (cachedTypes) return cachedTypes
try {
const token = getToken()
const res = await fetch(`${API_BASE_URL}/api/reaction-types`, {
headers: { Authorization: token ? `Bearer ${token}` : '' },
})
if (res.ok) {
cachedTypes = await res.json()
} else {
cachedTypes = []
}
} catch {
cachedTypes = []
}
return cachedTypes
}
onMounted(async () => {
reactionTypes.value = await fetchTypes()
})
const counts = computed(() => {
const c = {}
@@ -200,6 +180,10 @@ const toggleReaction = async (type) => {
toast.error('操作失败')
}
}
onMounted(async () => {
await initialize()
})
</script>
<style>
@@ -253,7 +237,7 @@ const toggleReaction = async (type) => {
.make-reaction-item {
cursor: pointer;
padding: 10px;
padding: 4px;
opacity: 0.5;
border-radius: 8px;
font-size: 20px;

View File

@@ -0,0 +1,52 @@
// composables/useConfirm.ts
import { ref } from 'vue'
// 全局单例SPA 下即可Nuxt/SSR 下见文末“SSR 提醒”)
const visible = ref(false)
const title = ref('')
const message = ref('')
let resolver: ((ok: boolean) => void) | null = null
function reset() {
visible.value = false
title.value = ''
message.value = ''
resolver = null
}
export function useConfirm() {
/**
* 打开确认框,返回 Promise<boolean>
* - 确认 => resolve(true)
* - 取消/关闭 => resolve(false)
* 若并发调用,以最后一次为准(更简单直观)
*/
const confirm = (t: string, m: string) => {
title.value = t
message.value = m
visible.value = true
return new Promise<boolean>((resolve) => {
resolver = resolve
})
}
const onConfirm = () => {
resolver?.(true)
reset()
}
const onCancel = () => {
resolver?.(false)
reset()
}
return {
visible,
title,
message,
confirm,
onConfirm,
onCancel,
}
}

View File

@@ -0,0 +1,52 @@
import { ref } from 'vue'
const reactionTypes = ref([])
let isLoading = false
let isInitialized = false
export function useReactionTypes() {
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
const fetchReactionTypes = async () => {
if (isInitialized || isLoading) {
reactionTypes.value = [...(window.reactionTypes || [])]
return reactionTypes.value
}
isLoading = true
try {
const token = getToken()
const res = await fetch(`${API_BASE_URL}/api/reaction-types`, {
headers: { Authorization: token ? `Bearer ${token}` : '' },
})
if (res.ok) {
reactionTypes.value = await res.json()
window.reactionTypes = [...reactionTypes.value]
isInitialized = true
} else {
reactionTypes.value = []
}
} catch (error) {
console.error('Failed to fetch reaction types:', error)
reactionTypes.value = []
} finally {
isLoading = false
}
return reactionTypes.value
}
const initialize = async () => {
if (!isInitialized) {
await fetchReactionTypes()
}
return reactionTypes.value
}
return {
reactionTypes: readonly(reactionTypes),
fetchReactionTypes,
initialize,
isInitialized: readonly(isInitialized)
}
}

View File

@@ -535,6 +535,7 @@ const sanitizeDescription = (text) => stripMarkdown(text)
}
.article-item-description {
max-width: 100%;
margin-top: 10px;
font-size: 14px;
color: gray;

View File

@@ -15,6 +15,7 @@
<div class="article-title-container-right">
<div v-if="status === 'PENDING'" class="article-pending-button">审核中</div>
<div v-if="status === 'REJECTED'" class="article-block-button">已拒绝</div>
<div v-if="closed" class="article-closed-button">已关闭</div>
<div
v-if="loggedIn && !isAuthor && !subscribed"
class="article-subscribe-button"
@@ -171,7 +172,7 @@
<CommentEditor
@submit="postComment"
:loading="isWaitingPostingComment"
:disabled="!loggedIn"
:disabled="!loggedIn || closed"
:show-login-overlay="!loggedIn"
:parent-user-name="author.username"
/>
@@ -196,6 +197,7 @@
:level="0"
:default-show-replies="item.openReplies"
:post-author-id="author.id"
:post-closed="closed"
@deleted="onCommentDeleted"
/>
</template>
@@ -232,7 +234,16 @@
</template>
<script setup>
import { ref, computed, onMounted, onBeforeUnmount, nextTick, watch, watchEffect } from 'vue'
import {
ref,
computed,
onMounted,
onBeforeUnmount,
nextTick,
watch,
watchEffect,
onActivated,
} from 'vue'
import VueEasyLightbox from 'vue-easy-lightbox'
import { useRoute } from 'vue-router'
import CommentItem from '~/components/CommentItem.vue'
@@ -251,6 +262,8 @@ import { useRouter } from 'vue-router'
import { useIsMobile } from '~/utils/screen'
import Dropdown from '~/components/Dropdown.vue'
import { ClientOnly } from '#components'
import { useConfirm } from '~/composables/useConfirm'
const { confirm } = useConfirm()
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
@@ -267,6 +280,7 @@ const tags = ref([])
const postReactions = ref([])
const comments = ref([])
const status = ref('PUBLISHED')
const closed = ref(false)
const pinnedAt = ref(null)
const rssExcluded = ref(false)
const isWaitingPostingComment = ref(false)
@@ -349,7 +363,12 @@ const articleMenuItems = computed(() => {
const items = []
if (isAuthor.value || isAdmin.value) {
items.push({ text: '编辑文章', onClick: () => editPost() })
items.push({ text: '删除文章', color: 'red', onClick: () => deletePost() })
items.push({ text: '删除文章', color: 'red', onClick: deletePost })
if (closed.value) {
items.push({ text: '重新打开帖子', onClick: () => reopenPost() })
} else {
items.push({ text: '关闭帖子', onClick: () => closePost() })
}
}
if (isAdmin.value) {
if (pinnedAt.value) {
@@ -485,6 +504,7 @@ watchEffect(() => {
postReactions.value = data.reactions || []
subscribed.value = !!data.subscribed
status.value = data.status
closed.value = data.closed
pinnedAt.value = data.pinnedAt
rssExcluded.value = data.rssExcluded
postTime.value = TimeManager.format(data.createdAt)
@@ -544,6 +564,10 @@ const onSliderInput = (e) => {
const postComment = async (parentUserName, text, clear) => {
if (!text.trim()) return
if (closed.value) {
toast.error('帖子已关闭')
return
}
console.debug('Posting comment', { postId, text })
isWaitingPostingComment.value = true
const token = getToken()
@@ -682,24 +706,62 @@ const includeRss = async () => {
}
}
const closePost = async () => {
const token = getToken()
if (!token) return
const res = await fetch(`${API_BASE_URL}/api/posts/${postId}/close`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
})
if (res.ok) {
closed.value = true
toast.success('已关闭')
await refreshPost()
} else {
toast.error('操作失败')
}
}
const reopenPost = async () => {
const token = getToken()
if (!token) return
const res = await fetch(`${API_BASE_URL}/api/posts/${postId}/reopen`, {
method: 'POST',
headers: { Authorization: `Bearer ${token}` },
})
if (res.ok) {
closed.value = false
toast.success('已重新打开')
await refreshPost()
} else {
toast.error('操作失败')
}
}
const editPost = () => {
navigateTo(`/posts/${postId}/edit`, { replace: true })
}
const deletePost = async () => {
const token = getToken()
if (!token) {
toast.error('请先登录')
return
}
const res = await fetch(`${API_BASE_URL}/api/posts/${postId}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${token}` },
})
if (res.ok) {
toast.success('已删除')
navigateTo('/', { replace: true })
} else {
try {
const ok = await confirm('删除帖子', '此操作不可恢复,确认要删除吗?')
if (!ok) return
const token = getToken()
if (!token) {
toast.error('请先登录')
return
}
const res = await fetch(`${API_BASE_URL}/api/posts/${postId}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${token}` },
})
if (res.ok) {
toast.success('已删除')
navigateTo('/', { replace: true })
} else {
toast.error('操作失败')
}
} catch (e) {
toast.error('操作失败')
}
}
@@ -812,6 +874,11 @@ const gotoProfile = () => {
navigateTo(`/users/${author.value.id}`, { replace: true })
}
onActivated(async () => {
await refreshPost()
await fetchComments()
})
onMounted(async () => {
await fetchComments()
const hash = location.hash
@@ -845,6 +912,10 @@ onMounted(async () => {
width: calc(85% - 40px);
}
.info-content-text p code {
padding: 2px 4px;
}
.post-page-scroller-container {
display: flex;
flex-direction: column;
@@ -1024,6 +1095,15 @@ onMounted(async () => {
font-size: 14px;
}
.article-closed-button {
background-color: var(--background-color);
color: gray;
border: 1px solid gray;
padding: 5px 10px;
border-radius: 8px;
font-size: 14px;
}
.article-title {
font-size: 30px;
font-weight: bold;

View File

@@ -1,4 +1,5 @@
import hljs from 'highlight.js'
import hljs from 'highlight.js/lib/common'
if (typeof window !== 'undefined') {
const theme =
document.documentElement.dataset.theme ||

View File

@@ -2,7 +2,6 @@ import Vditor from 'vditor'
import { getToken, authState } from './auth'
import { searchUsers, fetchFollowings, fetchAdmins } from './user'
import { tiebaEmoji } from './tiebaEmoji'
import '~/assets/global.css'
export function getEditorTheme() {
return document.documentElement.dataset.theme === 'dark' ? 'dark' : 'classic'