Skip to content

Commit

Permalink
feat: Add a new sorting rule by comments to the blog list in the console
Browse files Browse the repository at this point in the history
Signed-off-by: ZJamss <zjamss@qq.com>
  • Loading branch information
ZJamss committed Mar 29, 2024
1 parent 4cb3495 commit 79b756e
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Post extends AbstractExtension {

public static final String COUNTER_VISIT_ANNO = "counter.visit";

public static final String COUNTER_COMMENT_ANNO = "counter.comment";

public static final String DELETED_LABEL = "content.halo.run/deleted";
public static final String PUBLISHED_LABEL = "content.halo.run/published";
public static final String OWNER_LABEL = "content.halo.run/owner";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import run.halo.app.core.extension.Counter;
import run.halo.app.core.extension.content.Post;
import run.halo.app.event.post.PostEvent;
import run.halo.app.event.post.PostVisitUpdatedEvent;
import run.halo.app.event.post.PostCounterUpdatedEvent;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.index.Indexer;
import run.halo.app.extension.index.IndexerFactoryImpl;
import run.halo.app.metrics.MeterUtils;


@Component
Expand All @@ -21,13 +23,20 @@ public class PostCounterIndexUpdater {

@EventListener(PostEvent.class)
public void onPostUpdated(PostEvent postEvent) {
if(postEvent instanceof PostVisitUpdatedEvent postVisitUpdatedEvent){
client.fetch(Post.class, postEvent.getName().split("/")[1])
.map(post -> {
post.getMetadata().getAnnotations()
.put(Post.COUNTER_VISIT_ANNO, postVisitUpdatedEvent.getNewVisit().toString());
return post;
}).doOnNext(post -> {
if (postEvent instanceof PostCounterUpdatedEvent) {
client.fetch(Post.class, postEvent.getName())
.flatMap(post -> client.fetch(Counter.class,
MeterUtils.nameOf(Post.class, post.getMetadata().getName()))
.map(counter -> {
post.getMetadata().getAnnotations()
.put(Post.COUNTER_VISIT_ANNO,
counter.getVisit().toString());
post.getMetadata().getAnnotations()
.put(Post.COUNTER_COMMENT_ANNO,
counter.getTotalComment().toString());
return post;
}))
.doOnNext(post -> {
GroupVersionKind gvk = GroupVersionKind.fromExtension(Post.class);
Indexer indexer = indexerFactory.getIndexer(gvk);
indexer.updateRecord(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
Expand All @@ -22,6 +23,7 @@
import run.halo.app.core.extension.notification.Reason;
import run.halo.app.core.extension.notification.Subscription;
import run.halo.app.event.post.CommentCreatedEvent;
import run.halo.app.event.post.PostCounterUpdatedEvent;
import run.halo.app.event.post.ReplyCreatedEvent;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.GroupVersionKind;
Expand Down Expand Up @@ -51,6 +53,8 @@ public class CommentNotificationReasonPublisher {
private final NewCommentOnPageReasonPublisher newCommentOnPageReasonPublisher;
private final NewReplyReasonPublisher newReplyReasonPublisher;

private final ApplicationEventPublisher eventPublisher;

/**
* On new comment.
*/
Expand All @@ -63,6 +67,7 @@ public void onNewComment(CommentCreatedEvent event) {
} else if (isPageComment(comment)) {
newCommentOnPageReasonPublisher.publishReasonBy(comment);
}
eventPublisher.publishEvent(new PostCounterUpdatedEvent(this,comment.getSpec().getSubjectRef().getName()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import run.halo.app.core.extension.content.Constant;
import run.halo.app.event.post.CommentCreatedEvent;
import run.halo.app.event.post.CommentUnreadReplyCountChangedEvent;
import run.halo.app.event.post.PostCounterUpdatedEvent;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ListOptions;
Expand Down Expand Up @@ -62,6 +63,8 @@ public Result reconcile(Request request) {
if (removeFinalizers(comment.getMetadata(), Set.of(FINALIZER_NAME))) {
cleanUpResources(comment);
client.update(comment);
eventPublisher.publishEvent(new PostCounterUpdatedEvent(this,
comment.getSpec().getSubjectRef().getName()));
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package run.halo.app.event.post;

import org.springframework.context.ApplicationEvent;

/**
* @author ZJamss
*/
public class PostCounterUpdatedEvent extends ApplicationEvent implements PostEvent{
private final String name;

public PostCounterUpdatedEvent(Object source, String post) {
super(source);
this.name = post;
}

@Override
public String getName() {
return this.name;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ public void visit(Post post) {
.map(extensionStore ->
converter.convertFrom(Counter.class, extensionStore))
.defaultIfEmpty(Counter.emptyCounter(post.getMetadata().getName()))
.doOnNext(counter -> post.getMetadata().getAnnotations()
.put(Post.COUNTER_VISIT_ANNO, counter.getVisit().toString()))
.doOnNext(counter -> {
post.getMetadata().getAnnotations()
.put(Post.COUNTER_VISIT_ANNO, counter.getVisit().toString());
post.getMetadata().getAnnotations()
.put(Post.COUNTER_COMMENT_ANNO, counter.getTotalComment().toString());
})
.subscribe();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ public void onApplicationEvent(@NonNull ApplicationContextInitializedEvent event
simpleAttribute(Post.class,
post -> post.getMetadata().getAnnotations().get(Post.COUNTER_VISIT_ANNO))));

indexSpecs.add(new IndexSpec()
.setName("counter.comment")
.setIndexFunc(
simpleAttribute(Post.class,
post -> post.getMetadata().getAnnotations().get(Post.COUNTER_COMMENT_ANNO))));

indexSpecs.add(new IndexSpec()
.setName(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME)
.setIndexFunc(simpleAttribute(Post.class, post -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import run.halo.app.core.extension.Counter;
import run.halo.app.event.post.PostVisitUpdatedEvent;
import run.halo.app.event.post.PostCounterUpdatedEvent;
import run.halo.app.event.post.VisitedEvent;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.GroupVersionKind;
Expand Down Expand Up @@ -48,7 +48,8 @@ public class VisitedEventReconciler
private final Controller visitedEventController;
private final ApplicationEventPublisher eventPublisher;

public VisitedEventReconciler(ExtensionClient client, ApplicationEventPublisher eventPublisher) {
public VisitedEventReconciler(ExtensionClient client,
ApplicationEventPublisher eventPublisher) {
this.client = client;
this.eventPublisher = eventPublisher;
visitedEventQueue = new DefaultQueue<>(Instant::now);
Expand All @@ -68,13 +69,13 @@ private void createOrUpdateVisits(String name, Integer visits) {
counter.setVisit(existingVisit + visits);
client.update(counter);
eventPublisher.publishEvent(
new PostVisitUpdatedEvent(this, name, counter.getVisit()));
new PostCounterUpdatedEvent(this, name.split("/")[1]));
}, () -> {
Counter counter = Counter.emptyCounter(name);
counter.setVisit(visits);
client.create(counter);
eventPublisher.publishEvent(
new PostVisitUpdatedEvent(this, name, counter.getVisit()));
new PostCounterUpdatedEvent(this, name.split("/")[1]));
});
}

Expand Down
4 changes: 4 additions & 0 deletions ui/console-src/modules/contents/posts/PostList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ watch(selectedPostNames, (newValue) => {
label: t('core.post.filters.sort.items.visit_desc'),
value: 'counter.visit,desc',
},
{
label: t('core.post.filters.sort.items.comment_desc'),
value: 'counter.comment,desc',
},
]"
/>
<div class="flex flex-row gap-2">
Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ core:
create_time_desc: Latest Created
create_time_asc: Earliest Created
visit_desc: Most Visit
comment_desc: Most Comment
list:
fields:
categories: "Categories:"
Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ core:
create_time_desc: Creado más reciente
create_time_asc: Creado más antiguo
visit_desc: Máximo de visitas
comment_desc: Número máximo de comentarios
list:
fields:
categories: "Categorías:"
Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ core:
create_time_desc: 较近创建
create_time_asc: 较早创建
visit_desc: 最多访问量
comment_desc: 最多评论量
list:
fields:
categories: 分类:
Expand Down
1 change: 1 addition & 0 deletions ui/src/locales/zh-TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ core:
create_time_desc: 較近創建
create_time_asc: 較早創建
visit_desc: 最多訪問量
comment_desc: 最多評論量
list:
fields:
categories: 分類:
Expand Down

0 comments on commit 79b756e

Please sign in to comment.