Добавил сервисный ключ
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-02-26 20:21:42 +03:00
parent 9d59624372
commit 80294316de
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
5 changed files with 57 additions and 6 deletions

View File

@ -10,6 +10,8 @@ import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import java.util.UUID;
/**
* Основные настройки приложения.
*
@ -25,6 +27,9 @@ public class AppSetting {
@Column(name = "id")
private Long id;
@Column(name = "service_key")
private UUID serviceKey = UUID.randomUUID();
@Column(name = "first_start")
private boolean firstStart;

View File

@ -2,6 +2,8 @@ package dev.struchkov.bot.gitlab.context.service;
import dev.struchkov.bot.gitlab.context.domain.notify.level.DiscussionLevel;
import java.util.UUID;
/**
* Сервис отвечает за пользовательские настройки приложения.
*
@ -39,4 +41,6 @@ public interface AppSettingService {
void setDiscussionLevel(DiscussionLevel level);
UUID getServiceKey();
}

View File

@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
import java.util.function.Supplier;
import static dev.struchkov.haiti.context.exception.NotFoundException.notFoundException;
@ -92,6 +93,16 @@ public class AppSettingServiceImpl implements AppSettingService {
appSetting.setDiscussionNotifyLevel(level);
}
@Override
@Transactional
public UUID getServiceKey() {
final AppSetting appSetting = getAppSetting();
if (appSetting.getServiceKey() == null) {
appSetting.setServiceKey(UUID.randomUUID());
}
return appSetting.getServiceKey();
}
private AppSetting getAppSetting() {
return appSettingRepository.findById(KEY)
.orElseThrow(NOT_FOUND_SETTINGS);

View File

@ -8,6 +8,7 @@
<column name="id" type="int">
<constraints primaryKey="true"/>
</column>
<column name="service_key" type="uuid"/>
<column name="first_start" type="boolean" defaultValue="true">
<constraints nullable="false"/>
</column>

View File

@ -1,6 +1,7 @@
package dev.struchkov.bot.gitlab.telegram.service;
import dev.struchkov.bot.gitlab.context.service.AppSettingService;
import dev.struchkov.bot.gitlab.context.service.NotifyService;
import dev.struchkov.bot.gitlab.context.utils.Icons;
import dev.struchkov.bot.gitlab.core.config.properties.AppProperty;
import dev.struchkov.bot.gitlab.core.config.properties.PersonProperty;
@ -16,8 +17,8 @@ import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import static dev.struchkov.bot.gitlab.telegram.utils.UnitName.DELETE_MESSAGE;
import static dev.struchkov.godfather.main.domain.keyboard.button.SimpleButton.simpleButton;
@ -38,6 +39,8 @@ public class StartNotify {
private final TelegramSending sending;
private final TelegramService telegramService;
private final NotifyService notifyService;
private final AppProperty appProperty;
private final AppSettingService settingService;
private final PersonProperty personProperty;
@ -66,6 +69,23 @@ public class StartNotify {
sendNotice();
}
registrationForStatistic();
}
/**
* Отправляет service_key для сбора анонимной статистики использования.
*/
private void registrationForStatistic() {
final UUID serviceKey = settingService.getServiceKey();
final boolean firstStart = settingService.isFirstStart();
final String requestUrl = "https://metrika.struchkov.dev/gitlab-notify/registration?key=" + serviceKey + "&initFlow=" + firstStart;
final Request request = new Request.Builder().get().url(requestUrl).build();
try {
client.newCall(request).execute();
} catch (Exception e) {
log.warn(e.getMessage());
}
}
/**
@ -73,10 +93,7 @@ public class StartNotify {
*/
private void sendNotice() {
final String requestUrl = "https://metrika.struchkov.dev/gitlab-notify/start-notice";
final Request request = new Request.Builder()
.get()
.url(requestUrl)
.build();
final Request request = new Request.Builder().get().url(requestUrl).build();
try {
final Response response = client.newCall(request).execute();
if (response.code() == 200) {
@ -95,9 +112,22 @@ public class StartNotify {
}
}
}
} catch (IOException e) {
} catch (Exception e) {
log.warn(e.getMessage());
}
}
// @PostConstruct
// public void demo() {
// notifyService.send(
// DiscussionNewNotify.builder()
// .authorName("Ivan Ivanov")
// .threadId("1")
// .discussionMessage("Кажется здесь можно сделать лучше.")
// .mergeRequestName("Merge Request Name")
// .url("https://ya.ru")
// .build()
// );
// }
}