Compare commits

...

4 Commits

7 changed files with 128 additions and 2 deletions

View File

@ -0,0 +1,28 @@
package dev.struchkov.bot.gitlab.context.domain.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDateTime;
@Getter
@Setter
@Entity
@Table(name = "deferred_messages")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class DeferredMessage {
@Id
@Column(name = "deferred_messages_id")
private int id;
@Column(name = "message")
private String message;
@Column (name = "time")
private LocalDateTime time;
@Column (name = "type")
private String type;
}

View File

@ -1,5 +1,6 @@
package dev.struchkov.bot.gitlab.context.domain.notify.mergerequest;
import dev.struchkov.bot.gitlab.context.domain.notify.Notify;
import lombok.Builder;
import lombok.Getter;

View File

@ -90,6 +90,15 @@
<artifactId>jakarta.el</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>dev.struchkov.bot.gitlab</groupId>
<artifactId>bot-data</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,67 @@
package dev.struchkov.bot.gitlab.core.service.impl;
import com.google.gson.Gson;
import dev.struchkov.bot.gitlab.context.domain.entity.DeferredMessage;
import dev.struchkov.bot.gitlab.context.domain.notify.Notify;
import dev.struchkov.bot.gitlab.context.domain.notify.comment.NewCommentNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.mergerequest.*;
import dev.struchkov.bot.gitlab.context.domain.notify.pipeline.PipelineNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.project.NewProjectNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.task.DiscussionNewNotify;
import dev.struchkov.bot.gitlab.context.domain.notify.task.TaskCloseNotify;
import dev.struchkov.bot.gitlab.data.jpa.DelayedNotifyJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
public class DelayedNotifyServiceImpl {
private final DelayedNotifyJpaRepository delayedNotifyJpaRepository;
@Transactional
public <T extends Notify> void save(T notify) {
Gson gson = new Gson();
DeferredMessage deferredMessage = new DeferredMessage();
String notifyForSave = gson.toJson(notify);
deferredMessage.setMessage(notifyForSave);
deferredMessage.setTime(LocalDateTime.now().withNano(0));
deferredMessage.setType(notify.getType());
delayedNotifyJpaRepository.save(deferredMessage);
}
public List<Object> getAllNotify(){
List<DeferredMessage> allDeferredMessageFromDb = delayedNotifyJpaRepository.findAll();
return allDeferredMessageFromDb.stream()
.map(DelayedNotifyServiceImpl::convertToNotifyClass).toList();
}
public static <T extends Notify> T convertToNotifyClass(DeferredMessage deferredMessage) {
Gson gson = new Gson();
Class<? extends Notify> newNotifyClass = switch (deferredMessage.getType()) {
case ConflictMrNotify.TYPE -> ConflictMrNotify.class;
case DiscussionNewNotify.TYPE -> DiscussionNewNotify.class;
case NewCommentNotify.TYPE -> NewCommentNotify.class;
case NewMrForAssignee.TYPE -> NewMrForAssignee.class;
case NewMrForReview.TYPE -> NewMrForReview.class;
case NewProjectNotify.TYPE -> NewProjectNotify.class;
case PipelineNotify.TYPE -> PipelineNotify.class;
case StatusMrNotify.TYPE -> StatusMrNotify.class;
case TaskCloseNotify.TYPE -> TaskCloseNotify.class;
case UpdateMrNotify.TYPE -> UpdateMrNotify.class;
default -> throw new RuntimeException("the " + deferredMessage.getType() + " type class is not found");
};
Object notify = gson.fromJson(deferredMessage.getMessage(), newNotifyClass);
return (T) notify;
}
}

View File

@ -12,18 +12,21 @@ public class NotifyServiceImpl implements NotifyService {
private final MessageSendService messageSendService;
private final AppSettingService settingService;
private final DelayedNotifyServiceImpl delayedNotifyService;
public NotifyServiceImpl(
@Lazy MessageSendService messageSendService,
AppSettingService settingService
) {
AppSettingService settingService,
DelayedNotifyServiceImpl delayedNotifyService) {
this.messageSendService = messageSendService;
this.settingService = settingService;
this.delayedNotifyService = delayedNotifyService;
}
@Override
public <T extends Notify> void send(T notify) {
if (settingService.isEnableAllNotify()) {
delayedNotifyService.save(notify);
messageSendService.send(notify);
}
}

View File

@ -0,0 +1,7 @@
package dev.struchkov.bot.gitlab.data.jpa;
import dev.struchkov.bot.gitlab.context.domain.entity.DeferredMessage;
import org.springframework.data.jpa.repository.JpaRepository;
public interface DelayedNotifyJpaRepository extends JpaRepository<DeferredMessage, Long> {
}

View File

@ -274,4 +274,15 @@
<addPrimaryKey tableName="merge_request_reviewer" columnNames="merge_request_id, person_id"/>
</changeSet>
<changeSet id="2023-02-08-create-table-messages" author="uPagge">
<createTable tableName="deferred_messages">
<column name="deferred_messages_id" type="int">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="message" type="varchar"/>
<column name="time" type="timestamp"/>
<column name="type" type="varchar"/>
</createTable>
</changeSet>
</databaseChangeLog>