Skip to content

Commit

Permalink
feat: support subject sync create and remove for plugin. (#562)
Browse files Browse the repository at this point in the history
* feat: support subject sync create and remove for plugin.

* docs: update CHANGELOG.MD

* fix: checkstyle
  • Loading branch information
GuoHao authored Jun 15, 2024
1 parent ddf52ab commit 9d60fa2
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- 添加匹配移除条目剧集附件绑定 #554
- JWT令牌认证支持 #560

## 插件

- 插件接口添加条目同步平台相关接口 #557

## 优化

- 条目同步的平台ID输入框最大字符限制提升到了100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run.ikaros.server.core.subject.enums;
package run.ikaros.api.core.subject;

public enum SubjectSyncAction {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package run.ikaros.api.core.subject;

import jakarta.annotation.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.api.plugin.AllowPluginOperate;
import run.ikaros.api.store.enums.SubjectSyncPlatform;

public interface SubjectSyncPlatformOperate extends AllowPluginOperate {
Mono<Subject> sync(@Nullable Long subjectId, SubjectSyncPlatform platform, String platformId);

Mono<Subject> sync(PostSubjectSyncCondition condition);

Mono<SubjectSync> save(SubjectSync subjectSync);

Mono<Void> remove(SubjectSync subjectSync);

Flux<SubjectSync> findSubjectSyncsBySubjectId(long subjectId);

Mono<SubjectSync> findSubjectSyncBySubjectIdAndPlatform(long subjectId,
SubjectSyncPlatform platform);

Flux<SubjectSync> findSubjectSyncsByPlatformAndPlatformId(SubjectSyncPlatform platform,
String platformId);

Mono<SubjectSync> findBySubjectIdAndPlatformAndPlatformId(Long subjectId,
SubjectSyncPlatform platform,
String platformId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run.ikaros.server.core.subject.vo;
package run.ikaros.api.core.subject.vo;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package run.ikaros.server.core.subject.vo;
package run.ikaros.api.core.subject.vo;


import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package run.ikaros.server.core.subject.vo;
package run.ikaros.api.core.subject.vo;

import lombok.Builder;
import lombok.Data;
import run.ikaros.api.core.subject.SubjectSyncAction;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.server.core.subject.enums.SubjectSyncAction;

@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import reactor.core.publisher.Mono;
import run.ikaros.api.core.attachment.VideoSubtitle;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.vo.FindSubjectCondition;
import run.ikaros.api.store.enums.SubjectType;
import run.ikaros.server.core.attachment.service.AttachmentRelationService;
import run.ikaros.server.core.statics.StaticService;
import run.ikaros.server.core.subject.service.SubjectService;
import run.ikaros.server.core.subject.vo.FindSubjectCondition;
import run.ikaros.server.theme.ThemeService;

@Controller
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package run.ikaros.server.core.subject;

import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectSync;
import run.ikaros.api.core.subject.SubjectSyncPlatformOperate;
import run.ikaros.api.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.server.core.subject.service.SubjectSyncPlatformService;

@Slf4j
@Component
public class SubjectSyncPlatformOperator implements SubjectSyncPlatformOperate {
private final SubjectSyncPlatformService service;

public SubjectSyncPlatformOperator(SubjectSyncPlatformService service) {
this.service = service;
}

@Override
public Mono<Subject> sync(@Nullable Long subjectId, SubjectSyncPlatform platform,
String platformId) {
return service.sync(subjectId, platform, platformId);
}

@Override
public Mono<Subject> sync(PostSubjectSyncCondition condition) {
return service.sync(condition);
}

@Override
public Mono<SubjectSync> save(SubjectSync subjectSync) {
return service.save(subjectSync);
}

@Override
public Mono<Void> remove(SubjectSync subjectSync) {
return service.remove(subjectSync);
}

@Override
public Flux<SubjectSync> findSubjectSyncsBySubjectId(long subjectId) {
return service.findSubjectSyncsBySubjectId(subjectId);
}

@Override
public Mono<SubjectSync> findSubjectSyncBySubjectIdAndPlatform(long subjectId,
SubjectSyncPlatform platform) {
return service.findSubjectSyncBySubjectIdAndPlatform(subjectId, platform);
}

@Override
public Flux<SubjectSync> findSubjectSyncsByPlatformAndPlatformId(SubjectSyncPlatform platform,
String platformId) {
return service.findSubjectSyncsByPlatformAndPlatformId(platform, platformId);
}

@Override
public Mono<SubjectSync> findBySubjectIdAndPlatformAndPlatformId(Long subjectId,
SubjectSyncPlatform platform,
String platformId) {
return service.findBySubjectIdAndPlatformAndPlatformId(subjectId, platform, platformId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
import reactor.core.publisher.Mono;
import run.ikaros.api.constant.OpenApiConst;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.vo.FindSubjectCondition;
import run.ikaros.api.infra.exception.NotFoundException;
import run.ikaros.api.store.enums.SubjectType;
import run.ikaros.api.wrap.PagingWrap;
import run.ikaros.server.core.subject.service.SubjectService;
import run.ikaros.server.core.subject.vo.FindSubjectCondition;
import run.ikaros.server.endpoint.CoreEndpoint;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import reactor.core.publisher.Mono;
import run.ikaros.api.constant.OpenApiConst;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectSyncAction;
import run.ikaros.api.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.api.infra.exception.subject.NoAvailableSubjectPlatformSynchronizerException;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.server.core.subject.enums.SubjectSyncAction;
import run.ikaros.server.core.subject.service.SubjectSyncPlatformService;
import run.ikaros.server.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.server.endpoint.CoreEndpoint;

@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import reactor.core.publisher.Mono;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectMeta;
import run.ikaros.api.core.subject.vo.FindSubjectCondition;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.api.wrap.PagingWrap;
import run.ikaros.server.core.subject.vo.FindSubjectCondition;

public interface SubjectService {
Mono<Subject> findById(Long id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package run.ikaros.server.core.subject.service;

import jakarta.annotation.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectSync;
import run.ikaros.api.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.server.core.subject.vo.PostSubjectSyncCondition;

public interface SubjectSyncPlatformService {
Mono<Subject> sync(@Nullable Long subjectId, SubjectSyncPlatform platform, String platformId);

Mono<Subject> sync(PostSubjectSyncCondition condition);

Mono<SubjectSync> save(SubjectSync subjectSync);

Mono<Void> remove(SubjectSync subjectSync);

Flux<SubjectSync> findSubjectSyncsBySubjectId(long subjectId);

Mono<SubjectSync> findSubjectSyncBySubjectIdAndPlatform(long subjectId,
SubjectSyncPlatform platform);

Flux<SubjectSync> findSubjectSyncsByPlatformAndPlatformId(SubjectSyncPlatform platform,
String platformId);

Mono<SubjectSync> findBySubjectIdAndPlatformAndPlatformId(Long subjectId,
SubjectSyncPlatform platform,
String platformId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectMeta;
import run.ikaros.api.core.subject.SubjectSync;
import run.ikaros.api.core.subject.vo.FindSubjectCondition;
import run.ikaros.api.infra.exception.NotFoundException;
import run.ikaros.api.infra.utils.StringUtils;
import run.ikaros.api.store.enums.AttachmentReferenceType;
Expand All @@ -41,7 +42,6 @@
import run.ikaros.server.core.subject.event.SubjectAddEvent;
import run.ikaros.server.core.subject.event.SubjectRemoveEvent;
import run.ikaros.server.core.subject.service.SubjectService;
import run.ikaros.server.core.subject.vo.FindSubjectCondition;
import run.ikaros.server.infra.utils.ReactiveBeanUtils;
import run.ikaros.server.store.entity.AttachmentEntity;
import run.ikaros.server.store.entity.AttachmentReferenceEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.ikaros.server.core.subject.service.impl;

import static run.ikaros.server.infra.utils.ReactiveBeanUtils.copyProperties;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
import java.util.Objects;
Expand All @@ -14,13 +16,14 @@
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import run.ikaros.api.core.subject.Subject;
import run.ikaros.api.core.subject.SubjectSync;
import run.ikaros.api.core.subject.SubjectSyncAction;
import run.ikaros.api.core.subject.SubjectSynchronizer;
import run.ikaros.api.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.api.infra.exception.subject.NoAvailableSubjectPlatformSynchronizerException;
import run.ikaros.api.store.enums.SubjectSyncPlatform;
import run.ikaros.server.core.subject.enums.SubjectSyncAction;
import run.ikaros.server.core.subject.service.SubjectService;
import run.ikaros.server.core.subject.service.SubjectSyncPlatformService;
import run.ikaros.server.core.subject.vo.PostSubjectSyncCondition;
import run.ikaros.server.plugin.ExtensionComponentsFinder;
import run.ikaros.server.store.entity.SubjectSyncEntity;
import run.ikaros.server.store.repository.SubjectSyncRepository;
Expand Down Expand Up @@ -51,7 +54,8 @@ public synchronized Mono<Subject> sync(@Nullable Long subjectId, SubjectSyncPlat
Assert.notNull(platform, "'platform' must not null.");
Assert.hasText(platformId, "'platformId' must has text.");
// 查询是否已经同步过了,如果已经同步过则返回对应的条目信息
return subjectSyncRepository.findByPlatformAndPlatformId(platform, platformId)
return subjectSyncRepository.findBySubjectIdAndPlatformAndPlatformId(
subjectId, platform, platformId)
.map(SubjectSyncEntity::getSubjectId)
.flatMap(subjectService::findById)
.switchIfEmpty(syncBySubjectSynchronizer(subjectId, platform, platformId));
Expand Down Expand Up @@ -101,6 +105,61 @@ public Mono<Subject> sync(PostSubjectSyncCondition condition) {

}

@Override
public Mono<SubjectSync> save(SubjectSync subjectSync) {
return copyProperties(subjectSync, SubjectSyncEntity.builder().build())
.flatMap(subjectSyncRepository::save)
.flatMap(subjectSyncEntity -> copyProperties(subjectSyncEntity, subjectSync));
}

@Override
public Mono<Void> remove(SubjectSync subjectSync) {
return copyProperties(subjectSync, SubjectSyncEntity.builder().build())
.flatMap(subjectSyncRepository::delete);
}

@Override
public Flux<SubjectSync> findSubjectSyncsBySubjectId(long subjectId) {
Assert.isTrue(subjectId > 0, "'subjectId' must gt 0.");
return subjectSyncRepository.findAllBySubjectId(subjectId)
.flatMap(subjectSyncEntity -> copyProperties(subjectSyncEntity,
SubjectSync.builder().build()));
}

@Override
public Mono<SubjectSync> findSubjectSyncBySubjectIdAndPlatform(long subjectId,
SubjectSyncPlatform platform) {
Assert.isTrue(subjectId > 0, "'subjectId' must gt 0.");
Assert.notNull(platform, "'platform' must not null.");
return subjectSyncRepository.findBySubjectIdAndPlatform(subjectId, platform)
.flatMap(subjectSyncEntity -> copyProperties(subjectSyncEntity,
SubjectSync.builder().build()));
}

@Override
public Flux<SubjectSync> findSubjectSyncsByPlatformAndPlatformId(SubjectSyncPlatform platform,
String platformId) {
Assert.notNull(platform, "'platform' must not null.");
Assert.hasText(platformId, "'platformId' must has text.");
return subjectSyncRepository.findByPlatformAndPlatformId(platform, platformId)
.flatMap(subjectSyncEntity -> copyProperties(subjectSyncEntity,
SubjectSync.builder().build()));
}

@Override
public Mono<SubjectSync> findBySubjectIdAndPlatformAndPlatformId(Long subjectId,
SubjectSyncPlatform platform,
String platformId) {
Assert.isTrue(subjectId > 0, "'subjectId' must gt 0.");
Assert.notNull(platform, "'platform' must not null.");
Assert.hasText(platformId, "'platformId' must has text.");
return subjectSyncRepository.findBySubjectIdAndPlatformAndPlatformId(
subjectId, platform, platformId)
.flatMap(subjectSyncEntity -> copyProperties(subjectSyncEntity,
SubjectSync.builder().build()));
}


private Mono<Subject> syncBySubjectSynchronizer(@Nullable Long subjectId,
SubjectSyncPlatform platform,
String platformId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import run.ikaros.server.store.entity.SubjectSyncEntity;

public interface SubjectSyncRepository extends R2dbcRepository<SubjectSyncEntity, Long> {
Mono<SubjectSyncEntity> findByPlatformAndPlatformId(SubjectSyncPlatform platform,
Flux<SubjectSyncEntity> findByPlatformAndPlatformId(SubjectSyncPlatform platform,
String platformId);

Mono<SubjectSyncEntity> findBySubjectIdAndPlatform(Long subjectId,
SubjectSyncPlatform platform);

Mono<SubjectSyncEntity> findBySubjectIdAndPlatformAndPlatformId(Long subjectId,
SubjectSyncPlatform platform,
String platformId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import run.ikaros.api.core.subject.vo.FindSubjectCondition;

class FindSubjectConditionTest {

Expand Down

0 comments on commit 9d60fa2

Please sign in to comment.