Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.bean.FastBeanCopier;
import org.jetlinks.community.notify.manager.enums.NotificationState;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProvider;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProviders;
import org.jetlinks.community.utils.ObjectMappers;

import javax.persistence.Column;
Expand All @@ -37,9 +39,10 @@
@Getter
@Setter
@Table(name = "notify_notifications",
indexes = @Index(
name = "idx_ntfc_subscribe", columnList = "subscriber_type,subscriber"
))
indexes = {
@Index(name = "idx_ntfc_subscribe", columnList = "subscriber_type,subscriber"),
@Index(name = "idx_ntfc_tp", columnList = "topic_provider")
})
@Comment("消息通知信息表")
public class NotificationEntity extends GenericEntity<String> {
private static final long serialVersionUID = -1L;
Expand Down Expand Up @@ -67,6 +70,13 @@ public class NotificationEntity extends GenericEntity<String> {
@Schema(description = "主题名称")
private String topicName;

public String getI18nTopicName() {
return SubscriberProviders
.getProvider(topicProvider)
.map(SubscriberProvider::getName)
.orElse(topicName);
}

@Column
@ColumnType(jdbcType = JDBCType.CLOB, javaType = String.class)
@Schema(description = "通知消息")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.api.crud.entity.RecordCreationEntity;
import org.hswebframework.web.crud.annotation.EnableEntityEvent;
import org.hswebframework.web.i18n.I18nSupportUtils;
import org.hswebframework.web.i18n.LocaleUtils;
import org.hswebframework.web.i18n.MultipleI18nSupportEntity;
import org.hswebframework.web.validator.CreateGroup;
import org.jetlinks.community.authorize.AuthenticationSpec;
Expand All @@ -33,8 +35,12 @@

import javax.persistence.Column;
import javax.persistence.Table;

import jakarta.validation.constraints.NotBlank;

import java.sql.JDBCType;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -114,4 +120,16 @@ public class NotifySubscriberChannelEntity extends GenericEntity<String> impleme
public String getI18nName() {
return getI18nMessage("name", name);
}

public void putI18nName(String i18nKey) {
putI18nName(i18nKey, LocaleUtils.getSupportLocales());
}

public void putI18nName(String i18nKey,
Collection<Locale> locales) {
this.i18nMessages = I18nSupportUtils
.putI18nMessages(
i18nKey, "name", locales, null, this.i18nMessages
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.crud.annotation.EnableEntityEvent;
import org.jetlinks.community.notify.manager.enums.SubscribeState;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProvider;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProviders;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
Expand All @@ -40,7 +42,7 @@
* @since 1.3
*/
@Table(name = "notify_subscribers",
indexes = @Index(name = "idx_nfy_subs_subscriber", columnList = "subscriber")
indexes = @Index(name = "idx_nfy_subs_subscriber", columnList = "subscriber")
)
@Getter
@Setter
Expand Down Expand Up @@ -74,10 +76,24 @@ public class NotifySubscriberEntity extends GenericEntity<String> {
@Schema(description = "订阅名称")
private String subscribeName;

public String getI18nSubscribeName() {
return SubscriberProviders
.getProvider(subscriber)
.map(SubscriberProvider::getName)
.orElse(subscribeName);
}

@Column(length = 64, nullable = false)
@Schema(description = "主题名称")
private String topicName;

public String getI18nTopicName() {
return SubscriberProviders
.getProvider(topicProvider)
.map(SubscriberProvider::getName)
.orElse(topicName);
}

@Column(length = 3000)
@JsonCodec
@ColumnType(javaType = String.class)
Expand Down Expand Up @@ -111,9 +127,9 @@ public class NotifySubscriberEntity extends GenericEntity<String> {

public String generateId() {
if (super.getId() == null
&& StringUtils.hasText(subscriberType)
&& StringUtils.hasText(subscriber)
&& StringUtils.hasText(topicProvider)) {
&& StringUtils.hasText(subscriberType)
&& StringUtils.hasText(subscriber)
&& StringUtils.hasText(topicProvider)) {
return DigestUtils.md5Hex(String.join(":", subscriberType, subscriber, topicProvider));
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.jetlinks.community.notify.manager.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
Expand All @@ -29,10 +30,11 @@
import org.hswebframework.web.validator.CreateGroup;
import org.jetlinks.community.authorize.AuthenticationSpec;
import org.jetlinks.community.notify.manager.enums.NotifyChannelState;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProvider;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProviders;

import javax.persistence.Column;
import javax.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import java.sql.JDBCType;
import java.util.Map;

Expand All @@ -55,6 +57,13 @@ public class NotifySubscriberProviderEntity extends GenericEntity<String> implem
@Schema(description = "名称")
private String name;

public String getI18nName() {
return SubscriberProviders
.getProvider(provider)
.map(SubscriberProvider::getName)
.orElse(name);
}

/**
* @see org.jetlinks.pro.notify.subscription.SubscriberProvider#getId()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.jetlinks.community.notify.manager.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
Expand All @@ -26,15 +28,14 @@
import org.hswebframework.web.api.crud.entity.RecordCreationEntity;
import org.hswebframework.web.crud.annotation.EnableEntityEvent;
import org.hswebframework.web.crud.generator.Generators;
import org.hswebframework.web.i18n.MultipleI18nSupportEntity;
import org.hswebframework.web.validator.CreateGroup;
import org.jetlinks.community.notify.template.TemplateProperties;
import org.jetlinks.community.notify.template.VariableDefinition;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.sql.JDBCType;
import java.util.List;
import java.util.Map;
Expand All @@ -49,7 +50,7 @@
@Table(name = "notify_template")
@Comment("消息通知模板表")
@EnableEntityEvent
public class NotifyTemplateEntity extends GenericEntity<String> implements RecordCreationEntity {
public class NotifyTemplateEntity extends GenericEntity<String> implements RecordCreationEntity, MultipleI18nSupportEntity {
private static final long serialVersionUID = -6849794470754667710L;

@Override
Expand All @@ -75,23 +76,23 @@ public String getId() {
private String name;

@Column
@ColumnType(jdbcType = JDBCType.LONGVARCHAR,javaType = String.class)
@ColumnType(jdbcType = JDBCType.LONGVARCHAR, javaType = String.class)
@JsonCodec
@Schema(description = "模版内容(根据服务商不同而不同)")
private Map<String,Object> template;
private Map<String, Object> template;

@Column(updatable = false)
@Schema(
description = "创建者ID(只读)"
, accessMode = Schema.AccessMode.READ_ONLY
description = "创建者ID(只读)"
, accessMode = Schema.AccessMode.READ_ONLY
)
private String creatorId;

@Column(updatable = false)
@DefaultValue(generator = Generators.CURRENT_TIME)
@Schema(
description = "创建时间(只读)"
, accessMode = Schema.AccessMode.READ_ONLY
description = "创建时间(只读)"
, accessMode = Schema.AccessMode.READ_ONLY
)
private Long createTime;

Expand Down Expand Up @@ -122,4 +123,14 @@ public TemplateProperties toTemplateProperties() {
properties.setDescription(description);
return properties;
}

@Schema(title = "国际化信息定义")
@Column
@JsonCodec
@ColumnType(jdbcType = JDBCType.LONGVARCHAR, javaType = String.class)
private Map<String, Map<String, String>> i18nMessages;

public String getI18nName() {
return getI18nMessage("name", name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.commons.collections4.MapUtils;
import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
import org.hswebframework.web.crud.service.GenericReactiveCacheSupportCrudService;
import org.hswebframework.web.i18n.LocaleUtils;
import org.jetlinks.community.notify.manager.entity.NotifySubscriberChannelEntity;
import org.jetlinks.community.notify.manager.entity.NotifySubscriberProviderEntity;
import org.jetlinks.community.notify.manager.subscriber.SubscriberProvider;
Expand All @@ -39,78 +40,83 @@
@Service
@AllArgsConstructor
public class NotifySubscriberProviderService
extends GenericReactiveCacheSupportCrudService<NotifySubscriberProviderEntity, String> {
extends GenericReactiveCacheSupportCrudService<NotifySubscriberProviderEntity, String> {

private final ReactiveRepository<NotifySubscriberChannelEntity, String> repository;
public Mono<Void> saveInfo(Flux<NotifyChannelController.SubscriberProviderInfo> infoFlux) {
Flux<Tuple2<NotifySubscriberProviderEntity, List<NotifySubscriberChannelEntity>>>
cache = infoFlux
.map(pro -> Tuples.of(pro.toProviderEntity(), pro.toChannelEntities()))
.cache();
cache = infoFlux
.map(pro -> Tuples.of(pro.toProviderEntity(), pro.toChannelEntities()))
.cache();

return this
.save(cache.map(Tuple2::getT1))
.then(repository
.save(cache.flatMapIterable(tp2 -> {
//provider保存后再回填ID
for (NotifySubscriberChannelEntity entity : tp2.getT2()) {
entity.setProviderId(tp2.getT1().getId());
}
return tp2.getT2();
})))
.then();
.save(cache.map(Tuple2::getT1))
.then(repository
.save(cache.flatMapIterable(tp2 -> {
//provider保存后再回填ID
for (NotifySubscriberChannelEntity entity : tp2.getT2()) {
entity.setProviderId(tp2.getT1().getId());
}
return tp2.getT2();
})))
.then();
}


//获取所有通道配置
/**
* 获取所有订阅提供商(在此过程中自动更新数据库数据)
*
* @return 订阅提供商
*/
public Flux<NotifyChannelController.SubscriberProviderInfo> channels() {

Map<String, NotifyChannelController.SubscriberProviderInfo> info = SubscriberProviders
.getProviders()
.stream()
.collect(Collectors.toMap(
SubscriberProvider::getId,
NotifyChannelController.SubscriberProviderInfo::of));
.getProviders()
.stream()
.collect(Collectors.toMap(
SubscriberProvider::getId,
NotifyChannelController.SubscriberProviderInfo::of));

Map<String, NotifyChannelController.SubscriberProviderInfo> notSaveInfoMap = new HashMap<>(info);
return createQuery()
.fetch()
.collectList()
.flatMap(providers -> {
Map<String, NotifyChannelController.SubscriberProviderInfo> providerInfoMap = new HashMap<>();
for (NotifySubscriberProviderEntity provider : providers) {
NotifyChannelController.SubscriberProviderInfo channelInfo = info.get(provider.getProvider());
if (channelInfo != null) {
channelInfo.with(provider);
providerInfoMap.put(channelInfo.getId(), channelInfo);
}
if (info.get(provider.getProvider()) != null) {
notSaveInfoMap.remove(provider.getProvider());
}
}
if (!MapUtils.isEmpty(notSaveInfoMap)) {
List<NotifySubscriberProviderEntity> providerList = notSaveInfoMap
.values()
.stream()
.map(NotifyChannelController.SubscriberProviderInfo::toProviderEntity)
.collect(Collectors.toList());
return save(providerList)
.thenReturn(providerInfoMap);
}
return Mono.just(providerInfoMap);
})
.filter(MapUtils::isNotEmpty)
.flatMapMany(mapping -> repository
.createQuery()
.in(NotifySubscriberChannelEntity::getProviderId, mapping.keySet())
.fetch()
.doOnNext(channel -> {
NotifyChannelController.SubscriberProviderInfo channelInfo = mapping.get(channel.getProviderId());
if (channelInfo != null) {
channelInfo.with(channel);
.as(LocaleUtils::transform)
.collectList()
.flatMap(providers -> {
Map<String, NotifyChannelController.SubscriberProviderInfo> providerInfoMap = new HashMap<>();
for (NotifySubscriberProviderEntity provider : providers) {
NotifyChannelController.SubscriberProviderInfo channelInfo = info.get(provider.getProvider());
if (channelInfo != null) {
channelInfo.with(provider);
providerInfoMap.put(channelInfo.getId(), channelInfo);
}
if (info.get(provider.getProvider()) != null) {
notSaveInfoMap.remove(provider.getProvider());
}
}
if (!MapUtils.isEmpty(notSaveInfoMap)) {
List<NotifySubscriberProviderEntity> providerList = notSaveInfoMap
.values()
.stream()
.map(NotifyChannelController.SubscriberProviderInfo::toProviderEntity)
.collect(Collectors.toList());
return save(providerList)
.thenReturn(providerInfoMap);
}
}))
.thenMany(Flux.fromIterable(info.values()))
.sort(Comparator.comparing(NotifyChannelController.SubscriberProviderInfo::getOrder));
return Mono.just(providerInfoMap);
})
.filter(MapUtils::isNotEmpty)
.flatMapMany(mapping -> repository
.createQuery()
.in(NotifySubscriberChannelEntity::getProviderId, mapping.keySet())
.fetch()
.doOnNext(channel -> {
channel.putI18nName("message.subscriber.provider." + channel.getChannelProvider());
NotifyChannelController.SubscriberProviderInfo channelInfo = mapping.get(channel.getProviderId());
if (channelInfo != null) {
channelInfo.with(channel);
}
}))
.thenMany(Flux.fromIterable(info.values()))
.sort(Comparator.comparing(NotifyChannelController.SubscriberProviderInfo::getOrder));
}
}
Loading
Loading