committed by
GitHub
69 changed files with 1823 additions and 984 deletions
@ -0,0 +1,103 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.notification.channels; |
||||
|
|
||||
|
import com.google.firebase.messaging.FirebaseMessagingException; |
||||
|
import com.google.firebase.messaging.MessagingErrorCode; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.rule.engine.api.notification.FirebaseService; |
||||
|
import org.thingsboard.server.common.data.User; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
||||
|
import org.thingsboard.server.common.data.notification.info.NotificationInfo; |
||||
|
import org.thingsboard.server.common.data.notification.settings.MobileAppNotificationDeliveryMethodConfig; |
||||
|
import org.thingsboard.server.common.data.notification.settings.NotificationSettings; |
||||
|
import org.thingsboard.server.common.data.notification.template.MobileAppDeliveryMethodNotificationTemplate; |
||||
|
import org.thingsboard.server.dao.notification.NotificationSettingsService; |
||||
|
import org.thingsboard.server.dao.user.UserService; |
||||
|
import org.thingsboard.server.service.notification.NotificationProcessingContext; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.HashSet; |
||||
|
import java.util.Map; |
||||
|
import java.util.Optional; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Component |
||||
|
@RequiredArgsConstructor |
||||
|
@Slf4j |
||||
|
public class MobileAppNotificationChannel implements NotificationChannel<User, MobileAppDeliveryMethodNotificationTemplate> { |
||||
|
|
||||
|
private final FirebaseService firebaseService; |
||||
|
private final UserService userService; |
||||
|
private final NotificationSettingsService notificationSettingsService; |
||||
|
|
||||
|
@Override |
||||
|
public void sendNotification(User recipient, MobileAppDeliveryMethodNotificationTemplate processedTemplate, NotificationProcessingContext ctx) throws Exception { |
||||
|
var mobileSessions = userService.findMobileSessions(recipient.getTenantId(), recipient.getId()); |
||||
|
if (mobileSessions.isEmpty()) { |
||||
|
throw new IllegalArgumentException("User doesn't use the mobile app"); |
||||
|
} |
||||
|
|
||||
|
MobileAppNotificationDeliveryMethodConfig config = ctx.getDeliveryMethodConfig(NotificationDeliveryMethod.MOBILE_APP); |
||||
|
String credentials = config.getFirebaseServiceAccountCredentials(); |
||||
|
Set<String> validTokens = new HashSet<>(mobileSessions.keySet()); |
||||
|
|
||||
|
String subject = processedTemplate.getSubject(); |
||||
|
String body = processedTemplate.getBody(); |
||||
|
Map<String, String> data = Optional.ofNullable(processedTemplate.getAdditionalConfig()) |
||||
|
.map(JacksonUtil::toFlatMap).orElseGet(HashMap::new); |
||||
|
Optional.ofNullable(ctx.getRequest().getInfo()) |
||||
|
.map(NotificationInfo::getStateEntityId) |
||||
|
.ifPresent(stateEntityId -> { |
||||
|
data.put("stateEntityId", stateEntityId.getId().toString()); |
||||
|
data.put("stateEntityType", stateEntityId.getEntityType().name()); |
||||
|
}); |
||||
|
for (String token : mobileSessions.keySet()) { |
||||
|
try { |
||||
|
firebaseService.sendMessage(ctx.getTenantId(), credentials, token, subject, body, data); |
||||
|
} catch (FirebaseMessagingException e) { |
||||
|
MessagingErrorCode errorCode = e.getMessagingErrorCode(); |
||||
|
if (errorCode == MessagingErrorCode.UNREGISTERED || errorCode == MessagingErrorCode.INVALID_ARGUMENT) { |
||||
|
validTokens.remove(token); |
||||
|
userService.removeMobileSession(recipient.getTenantId(), token); |
||||
|
continue; |
||||
|
} |
||||
|
throw new RuntimeException("Failed to send message via FCM: " + e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
if (validTokens.isEmpty()) { |
||||
|
throw new IllegalArgumentException("User doesn't use the mobile app"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void check(TenantId tenantId) throws Exception { |
||||
|
NotificationSettings systemSettings = notificationSettingsService.findNotificationSettings(TenantId.SYS_TENANT_ID); |
||||
|
if (!systemSettings.getDeliveryMethodsConfigs().containsKey(NotificationDeliveryMethod.MOBILE_APP)) { |
||||
|
throw new RuntimeException("Push-notifications to mobile are not configured"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationDeliveryMethod getDeliveryMethod() { |
||||
|
return NotificationDeliveryMethod.MOBILE_APP; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,133 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.service.notification.provider; |
||||
|
|
||||
|
import com.github.benmanes.caffeine.cache.Cache; |
||||
|
import com.github.benmanes.caffeine.cache.Caffeine; |
||||
|
import com.github.benmanes.caffeine.cache.RemovalCause; |
||||
|
import com.google.auth.oauth2.GoogleCredentials; |
||||
|
import com.google.firebase.FirebaseApp; |
||||
|
import com.google.firebase.FirebaseOptions; |
||||
|
import com.google.firebase.messaging.AndroidConfig; |
||||
|
import com.google.firebase.messaging.FirebaseMessaging; |
||||
|
import com.google.firebase.messaging.FirebaseMessagingException; |
||||
|
import com.google.firebase.messaging.Message; |
||||
|
import com.google.firebase.messaging.Notification; |
||||
|
import lombok.Getter; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.io.IOUtils; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.rule.engine.api.notification.FirebaseService; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.util.Map; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
@Service |
||||
|
@Slf4j |
||||
|
public class DefaultFirebaseService implements FirebaseService { |
||||
|
|
||||
|
private final Cache<String, FirebaseContext> contexts = Caffeine.newBuilder() |
||||
|
.expireAfterAccess(1, TimeUnit.DAYS) |
||||
|
.<String, FirebaseContext>removalListener((key, context, cause) -> { |
||||
|
if (cause == RemovalCause.EXPIRED && context != null) { |
||||
|
context.destroy(); |
||||
|
} |
||||
|
}) |
||||
|
.build(); |
||||
|
|
||||
|
@Override |
||||
|
public void sendMessage(TenantId tenantId, String credentials, String fcmToken, String title, String body, Map<String, String> data) throws FirebaseMessagingException { |
||||
|
FirebaseContext firebaseContext = contexts.asMap().compute(tenantId.toString(), (key, context) -> { |
||||
|
if (context == null) { |
||||
|
return new FirebaseContext(key, credentials); |
||||
|
} else { |
||||
|
context.check(credentials); |
||||
|
return context; |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
Message message = Message.builder() |
||||
|
.setToken(fcmToken) |
||||
|
.setNotification(Notification.builder() |
||||
|
.setTitle(title) |
||||
|
.setBody(body) |
||||
|
.build()) |
||||
|
.setAndroidConfig(AndroidConfig.builder() |
||||
|
.setPriority(AndroidConfig.Priority.HIGH) |
||||
|
.build()) |
||||
|
.putAllData(data) |
||||
|
.build(); |
||||
|
firebaseContext.getMessaging().send(message); |
||||
|
log.trace("[{}] Sent message for FCM token {}", tenantId, fcmToken); |
||||
|
} |
||||
|
|
||||
|
public static class FirebaseContext { |
||||
|
private final String key; |
||||
|
private String credentials; |
||||
|
private FirebaseApp app; |
||||
|
@Getter |
||||
|
private FirebaseMessaging messaging; |
||||
|
|
||||
|
public FirebaseContext(String key, String credentials) { |
||||
|
this.key = key; |
||||
|
this.credentials = credentials; |
||||
|
init(); |
||||
|
} |
||||
|
|
||||
|
private void init() { |
||||
|
FirebaseOptions options; |
||||
|
try { |
||||
|
options = FirebaseOptions.builder() |
||||
|
.setCredentials(GoogleCredentials.fromStream(IOUtils.toInputStream(credentials, StandardCharsets.UTF_8))) |
||||
|
.build(); |
||||
|
} catch (IOException e) { |
||||
|
throw new RuntimeException("Failed to process service account credentials: " + e.getMessage(), e); |
||||
|
} |
||||
|
try { |
||||
|
app = FirebaseApp.initializeApp(options, key); |
||||
|
} catch (IllegalStateException alreadyExists) { // should never normally happen
|
||||
|
app = FirebaseApp.getInstance(key); |
||||
|
} |
||||
|
try { |
||||
|
messaging = FirebaseMessaging.getInstance(app); |
||||
|
} catch (IllegalStateException alreadyExists) { // should never normally happen
|
||||
|
messaging = FirebaseMessaging.getInstance(app); |
||||
|
} |
||||
|
log.debug("[{}] Initialized new FirebaseContext", key); |
||||
|
} |
||||
|
|
||||
|
public void check(String credentials) { |
||||
|
if (!this.credentials.equals(credentials)) { |
||||
|
destroy(); |
||||
|
this.credentials = credentials; |
||||
|
init(); |
||||
|
} else if (app == null || messaging == null) { |
||||
|
throw new IllegalStateException("Firebase app couldn't be initialized"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void destroy() { |
||||
|
app.delete(); |
||||
|
app = null; |
||||
|
messaging = null; |
||||
|
log.debug("[{}] Destroyed FirebaseContext", key); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.mobile; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class MobileSessionInfo { |
||||
|
private long fcmTokenTimestamp; |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.mobile; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Data |
||||
|
public class UserMobileInfo { |
||||
|
|
||||
|
private Map<String, MobileSessionInfo> sessions; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.settings; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
||||
|
|
||||
|
import javax.validation.constraints.NotEmpty; |
||||
|
|
||||
|
@Data |
||||
|
public class MobileAppNotificationDeliveryMethodConfig implements NotificationDeliveryMethodConfig { |
||||
|
|
||||
|
private String firebaseServiceAccountCredentialsFileName; |
||||
|
@NotEmpty |
||||
|
private String firebaseServiceAccountCredentials; |
||||
|
|
||||
|
@Override |
||||
|
public NotificationDeliveryMethod getMethod() { |
||||
|
return NotificationDeliveryMethod.MOBILE_APP; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.notification.template; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
import lombok.ToString; |
||||
|
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
||||
|
|
||||
|
import javax.validation.constraints.NotEmpty; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
@NoArgsConstructor |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ToString(callSuper = true) |
||||
|
public class MobileAppDeliveryMethodNotificationTemplate extends DeliveryMethodNotificationTemplate implements HasSubject { |
||||
|
|
||||
|
@NotEmpty |
||||
|
private String subject; |
||||
|
private JsonNode additionalConfig; |
||||
|
|
||||
|
private final List<TemplatableValue> templatableValues = List.of( |
||||
|
TemplatableValue.of(this::getBody, this::setBody), |
||||
|
TemplatableValue.of(this::getSubject, this::setSubject) |
||||
|
); |
||||
|
|
||||
|
public MobileAppDeliveryMethodNotificationTemplate(MobileAppDeliveryMethodNotificationTemplate other) { |
||||
|
super(other); |
||||
|
this.subject = other.subject; |
||||
|
this.additionalConfig = other.additionalConfig; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public NotificationDeliveryMethod getMethod() { |
||||
|
return NotificationDeliveryMethod.MOBILE_APP; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public MobileAppDeliveryMethodNotificationTemplate copy() { |
||||
|
return new MobileAppDeliveryMethodNotificationTemplate(this); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<TemplatableValue> getTemplatableValues() { |
||||
|
return templatableValues; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.rule.engine.api.notification; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
|
||||
|
public interface FirebaseService { |
||||
|
|
||||
|
void sendMessage(TenantId tenantId, String credentials, String fcmToken, String title, String body, Map<String, String> data) throws Exception; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2024 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<div class="tb-form-panel tb-slide-toggle stroked" [formGroup]="actionButtonConfigForm"> |
||||
|
<mat-expansion-panel class="tb-settings" |
||||
|
[expanded]="actionButtonConfigForm.get('enabled').value"> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title fxFlex="60"> |
||||
|
<mat-slide-toggle class="mat-slide" formControlName="enabled" (click)="$event.stopPropagation()" |
||||
|
fxLayoutAlign="center"> |
||||
|
{{ actionTitle }} |
||||
|
</mat-slide-toggle> |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column"> |
||||
|
<mat-form-field class="mat-block" fxFlex *ngIf="!hideButtonText"> |
||||
|
<mat-label translate>notification.button-text</mat-label> |
||||
|
<input matInput formControlName="text" required> |
||||
|
<mat-error |
||||
|
*ngIf="actionButtonConfigForm.get('text').hasError('required')"> |
||||
|
{{ 'notification.button-text-required' | translate }} |
||||
|
</mat-error> |
||||
|
<mat-error |
||||
|
*ngIf="actionButtonConfigForm.get('text').hasError('maxlength')"> |
||||
|
{{ 'notification.button-text-max-length' | translate : |
||||
|
{length: actionButtonConfigForm.get('text').getError('maxlength').requiredLength} |
||||
|
}} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
</div> |
||||
|
<div fxLayout="row" fxLayoutGap.gt-xs="8px" fxLayout.xs="column"> |
||||
|
<mat-form-field fxFlex="30" fxFlex.xs="100"> |
||||
|
<mat-label translate>notification.action-type</mat-label> |
||||
|
<mat-select formControlName="linkType"> |
||||
|
<mat-option *ngFor="let actionButtonLinkType of actionButtonLinkTypes" |
||||
|
[value]="actionButtonLinkType"> |
||||
|
{{ actionButtonLinkTypeTranslateMap.get(actionButtonLinkType) | translate }} |
||||
|
</mat-option> |
||||
|
</mat-select> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field fxFlex |
||||
|
*ngIf="actionButtonConfigForm.get('linkType').value === actionButtonLinkType.LINK; else dashboardSelector"> |
||||
|
<mat-label translate>notification.link</mat-label> |
||||
|
<input matInput formControlName="link" required> |
||||
|
<mat-error |
||||
|
*ngIf="actionButtonConfigForm.get('link').hasError('required')"> |
||||
|
{{ 'notification.link-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<ng-template #dashboardSelector> |
||||
|
<tb-dashboard-autocomplete |
||||
|
fxFlex="35" fxFlex.xs="100" |
||||
|
required |
||||
|
formControlName="dashboardId"> |
||||
|
</tb-dashboard-autocomplete> |
||||
|
<tb-dashboard-state-autocomplete fxFlex="35" fxFlex.xs="100" |
||||
|
[dashboardId]="actionButtonConfigForm.get('dashboardId').value" |
||||
|
formControlName="dashboardState"> |
||||
|
</tb-dashboard-state-autocomplete> |
||||
|
</ng-template> |
||||
|
</div> |
||||
|
<mat-slide-toggle formControlName="setEntityIdInState" class="toggle" |
||||
|
*ngIf="actionButtonConfigForm.get('linkType').value === actionButtonLinkType.DASHBOARD"> |
||||
|
{{ 'notification.set-entity-from-notification' | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</div> |
||||
@ -0,0 +1,168 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
||||
|
import { |
||||
|
ControlValueAccessor, |
||||
|
FormBuilder, |
||||
|
FormGroup, |
||||
|
NG_VALIDATORS, |
||||
|
NG_VALUE_ACCESSOR, |
||||
|
ValidationErrors, |
||||
|
Validator, |
||||
|
Validators |
||||
|
} from '@angular/forms'; |
||||
|
import { ActionButtonLinkType, ActionButtonLinkTypeTranslateMap } from '@shared/models/notification.models'; |
||||
|
import { takeUntil } from 'rxjs/operators'; |
||||
|
import { Subject } from 'rxjs'; |
||||
|
import { isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-notification-action-button-configuration', |
||||
|
templateUrl: './notification-action-button-configuration.component.html', |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => NotificationActionButtonConfigurationComponent), |
||||
|
multi: true |
||||
|
}, |
||||
|
{ |
||||
|
provide: NG_VALIDATORS, |
||||
|
useExisting: forwardRef(() => NotificationActionButtonConfigurationComponent), |
||||
|
multi: true, |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class NotificationActionButtonConfigurationComponent implements ControlValueAccessor, Validator, OnInit, OnDestroy { |
||||
|
|
||||
|
@Input() |
||||
|
actionTitle: string; |
||||
|
|
||||
|
private hideButtonTextValue = false; |
||||
|
|
||||
|
get hideButtonText(): boolean { |
||||
|
return this.hideButtonTextValue; |
||||
|
} |
||||
|
|
||||
|
@Input() |
||||
|
@coerceBoolean() |
||||
|
set hideButtonText(hideButtonText: boolean) { |
||||
|
this.hideButtonTextValue = hideButtonText; |
||||
|
if (this.hideButtonTextValue) { |
||||
|
this.actionButtonConfigForm.removeControl('text'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
actionButtonConfigForm: FormGroup; |
||||
|
|
||||
|
readonly actionButtonLinkType = ActionButtonLinkType; |
||||
|
readonly actionButtonLinkTypes = Object.keys(ActionButtonLinkType) as ActionButtonLinkType[]; |
||||
|
readonly actionButtonLinkTypeTranslateMap = ActionButtonLinkTypeTranslateMap; |
||||
|
|
||||
|
private propagateChange = (v: any) => { }; |
||||
|
private readonly destroy$ = new Subject<void>(); |
||||
|
|
||||
|
constructor(private fb: FormBuilder) { |
||||
|
this.actionButtonConfigForm = this.fb.group({ |
||||
|
enabled: [false], |
||||
|
text: [{value: '', disabled: true}, [Validators.required, Validators.maxLength(50)]], |
||||
|
linkType: [ActionButtonLinkType.LINK], |
||||
|
link: [{value: '', disabled: true}, Validators.required], |
||||
|
dashboardId: [{value: null, disabled: true}, Validators.required], |
||||
|
dashboardState: [{value: null, disabled: true}], |
||||
|
setEntityIdInState: [{value: true, disabled: true}] |
||||
|
}); |
||||
|
|
||||
|
|
||||
|
this.actionButtonConfigForm.get('enabled').valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe((value) => { |
||||
|
if (value) { |
||||
|
if (!this.hideButtonText) { |
||||
|
this.actionButtonConfigForm.get('text').enable({emitEvent: false}); |
||||
|
} |
||||
|
this.actionButtonConfigForm.get('linkType').enable({onlySelf: false}); |
||||
|
} else { |
||||
|
this.actionButtonConfigForm.disable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('enabled').enable({emitEvent: false}); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
this.actionButtonConfigForm.get('linkType').valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe((value) => { |
||||
|
const isEnabled = this.actionButtonConfigForm.get('enabled').value; |
||||
|
if (isEnabled) { |
||||
|
if (value === ActionButtonLinkType.LINK) { |
||||
|
this.actionButtonConfigForm.get('link').enable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('dashboardId').disable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('dashboardState').disable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('setEntityIdInState').disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.actionButtonConfigForm.get('link').disable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('dashboardId').enable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('dashboardState').enable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('setEntityIdInState').enable({emitEvent: false}); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
this.actionButtonConfigForm.valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe(value => this.propagateChange(value)); |
||||
|
} |
||||
|
|
||||
|
ngOnInit() { |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
this.destroy$.next(); |
||||
|
this.destroy$.complete(); |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any) { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any) { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean) { |
||||
|
if (isDisabled) { |
||||
|
this.actionButtonConfigForm.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.actionButtonConfigForm.enable({emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('enabled').updateValueAndValidity({onlySelf: true}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
validate(): ValidationErrors | null { |
||||
|
return this.actionButtonConfigForm.valid ? null : { |
||||
|
actionButtonConfigForm: { |
||||
|
valid: false |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
writeValue(obj) { |
||||
|
if (isDefinedAndNotNull(obj)) { |
||||
|
this.actionButtonConfigForm.patchValue(obj, {emitEvent: false}); |
||||
|
this.actionButtonConfigForm.get('enabled').updateValueAndValidity({onlySelf: true}); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,261 @@ |
|||||
|
<!-- |
||||
|
|
||||
|
Copyright © 2016-2024 The Thingsboard Authors |
||||
|
|
||||
|
Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
you may not use this file except in compliance with the License. |
||||
|
You may obtain a copy of the License at |
||||
|
|
||||
|
http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
|
||||
|
Unless required by applicable law or agreed to in writing, software |
||||
|
distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
See the License for the specific language governing permissions and |
||||
|
limitations under the License. |
||||
|
|
||||
|
--> |
||||
|
<section class="tb-template-header"> |
||||
|
<div class="tb-form-panel-title" translate>notification.customize-messages</div> |
||||
|
<div class="tb-form-panel-hint tb-hint-available-params"> |
||||
|
<span class="content">{{ 'notification.input-fields-support-templatization' | translate}}</span> |
||||
|
<span tb-help-popup="{{ NotificationTemplateTypeTranslateMap.get(notificationType).helpId }}" |
||||
|
tb-help-popup-placement="bottom" |
||||
|
trigger-style="letter-spacing:0.25px;font-size:12px;" |
||||
|
[tb-help-popup-style]="{maxWidth: '800px'}" |
||||
|
trigger-text="{{ 'notification.see-documentation' | translate }}"></span> |
||||
|
</div> |
||||
|
</section> |
||||
|
<section [formGroup]="templateConfigurationForm" class="tb-form-panel no-border no-padding"> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.WEB" |
||||
|
*ngIf="templateConfigurationForm.get('WEB.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.WEB).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.WEB).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.subject</mat-label> |
||||
|
<input matInput formControlName="subject"> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('WEB.subject').hasError('required')"> |
||||
|
{{ 'notification.subject-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.message</mat-label> |
||||
|
<textarea matInput |
||||
|
cdkTextareaAutosize |
||||
|
cols="1" |
||||
|
cdkAutosizeMinRows="1" |
||||
|
formControlName="body"> |
||||
|
</textarea> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('WEB.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<section formGroupName="additionalConfig" class="tb-form-panel no-padding no-border"> |
||||
|
<div class="tb-form-row space-between" formGroupName="icon"> |
||||
|
<mat-slide-toggle formControlName="enabled" class="mat-slide"> |
||||
|
{{ 'icon.icon' | translate }} |
||||
|
</mat-slide-toggle> |
||||
|
<div fxLayout="row" fxLayoutAlign="start center" fxLayoutGap="8px"> |
||||
|
<tb-material-icon-select asBoxInput |
||||
|
[color]="templateConfigurationForm.get('WEB.additionalConfig.icon.color').value" |
||||
|
formControlName="icon"> |
||||
|
</tb-material-icon-select> |
||||
|
<tb-color-input asBoxInput |
||||
|
formControlName="color"> |
||||
|
</tb-color-input> |
||||
|
</div> |
||||
|
</div> |
||||
|
<tb-notification-action-button-configuration |
||||
|
actionTitle="{{ 'notification.action-button' | translate }}" |
||||
|
formControlName="actionButtonConfig"> |
||||
|
</tb-notification-action-button-configuration> |
||||
|
</section> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.MOBILE_APP" |
||||
|
*ngIf="templateConfigurationForm.get('MOBILE_APP.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.MOBILE_APP).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.MOBILE_APP).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.subject</mat-label> |
||||
|
<input matInput formControlName="subject"> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('MOBILE_APP.subject').hasError('required')"> |
||||
|
{{ 'notification.subject-required' | translate }} |
||||
|
</mat-error> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('MOBILE_APP.subject').hasError('maxlength')"> |
||||
|
{{ 'notification.subject-max-length' | translate : |
||||
|
{length: templateConfigurationForm.get('MOBILE_APP.subject').getError('maxlength').requiredLength} |
||||
|
}} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.message</mat-label> |
||||
|
<textarea matInput |
||||
|
cdkTextareaAutosize |
||||
|
cols="1" |
||||
|
cdkAutosizeMinRows="1" |
||||
|
formControlName="body"> |
||||
|
</textarea> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('MOBILE_APP.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('MOBILE_APP.body').hasError('maxlength')"> |
||||
|
{{ 'notification.message-max-length' | translate : |
||||
|
{length: templateConfigurationForm.get('MOBILE_APP.body').getError('maxlength').requiredLength} |
||||
|
}} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<div formGroupName="additionalConfig"> |
||||
|
<tb-notification-action-button-configuration |
||||
|
formControlName="onClick" |
||||
|
hideButtonText |
||||
|
actionTitle="{{ 'notification.notification-tap-action' | translate }}"> |
||||
|
</tb-notification-action-button-configuration> |
||||
|
</div> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.SMS" |
||||
|
*ngIf="templateConfigurationForm.get('SMS.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.SMS).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.SMS).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block" subscriptSizing="dynamic"> |
||||
|
<mat-label translate>notification.message</mat-label> |
||||
|
<textarea matInput |
||||
|
cdkTextareaAutosize |
||||
|
cols="1" |
||||
|
cdkAutosizeMinRows="1" |
||||
|
formControlName="body"> |
||||
|
</textarea> |
||||
|
<mat-hint></mat-hint> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('SMS.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('SMS.body').hasError('maxlength')"> |
||||
|
{{ 'notification.message-max-length' | translate : |
||||
|
{length: templateConfigurationForm.get('SMS.body').getError('maxlength').requiredLength} |
||||
|
}} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.EMAIL" |
||||
|
*ngIf="templateConfigurationForm.get('EMAIL.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.EMAIL).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.EMAIL).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.subject</mat-label> |
||||
|
<input matInput formControlName="subject"> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('EMAIL.subject').hasError('required')"> |
||||
|
{{ 'notification.subject-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<mat-label class="tb-title tb-required" |
||||
|
[class.tb-error]="(interacted || templateConfigurationForm.get('EMAIL.body').touched) && templateConfigurationForm.get('EMAIL.body').hasError('required')" |
||||
|
translate>notification.message |
||||
|
</mat-label> |
||||
|
<editor [init]="tinyMceOptions" formControlName="body"></editor> |
||||
|
<mat-error class="tb-mat-error" |
||||
|
*ngIf="(interacted || templateConfigurationForm.get('EMAIL.body').touched) && templateConfigurationForm.get('EMAIL.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.SLACK" |
||||
|
*ngIf="templateConfigurationForm.get('SLACK.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.SLACK).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.SLACK).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.message</mat-label> |
||||
|
<textarea matInput |
||||
|
cdkTextareaAutosize |
||||
|
cols="1" |
||||
|
cdkAutosizeMinRows="1" |
||||
|
formControlName="body"> |
||||
|
</textarea> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('SLACK.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
<section class="tb-form-panel tb-slide-toggle stroked" |
||||
|
[formGroupName]="NotificationDeliveryMethod.MICROSOFT_TEAMS" |
||||
|
*ngIf="templateConfigurationForm.get('MICROSOFT_TEAMS.enabled').value"> |
||||
|
<mat-expansion-panel class="tb-settings" expanded> |
||||
|
<mat-expansion-panel-header fxLayout="row wrap" class="fill-width"> |
||||
|
<mat-panel-title class="template-tittle"> |
||||
|
<tb-icon>{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.MICROSOFT_TEAMS).icon }}</tb-icon> |
||||
|
{{ NotificationDeliveryMethodInfoMap.get(NotificationDeliveryMethod.MICROSOFT_TEAMS).name | translate }} |
||||
|
</mat-panel-title> |
||||
|
</mat-expansion-panel-header> |
||||
|
<ng-template matExpansionPanelContent class="tb-extension-panel"> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.subject</mat-label> |
||||
|
<input matInput formControlName="subject"> |
||||
|
</mat-form-field> |
||||
|
<mat-form-field class="mat-block"> |
||||
|
<mat-label translate>notification.message</mat-label> |
||||
|
<textarea matInput |
||||
|
cdkTextareaAutosize |
||||
|
cols="1" |
||||
|
cdkAutosizeMinRows="1" |
||||
|
formControlName="body"> |
||||
|
</textarea> |
||||
|
<mat-error *ngIf="templateConfigurationForm.get('MICROSOFT_TEAMS.body').hasError('required')"> |
||||
|
{{ 'notification.message-required' | translate }} |
||||
|
</mat-error> |
||||
|
</mat-form-field> |
||||
|
<div class="tb-form-panel no-padding no-border"> |
||||
|
<div class="tb-form-row space-between"> |
||||
|
<div translate>notification.theme-color</div> |
||||
|
<tb-color-input asBoxInput formControlName="themeColor"></tb-color-input> |
||||
|
</div> |
||||
|
<tb-notification-action-button-configuration |
||||
|
actionTitle="{{ 'notification.action-button' | translate }}" |
||||
|
formControlName="button"> |
||||
|
</tb-notification-action-button-configuration> |
||||
|
</div> |
||||
|
</ng-template> |
||||
|
</mat-expansion-panel> |
||||
|
</section> |
||||
|
</section> |
||||
@ -0,0 +1,72 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
:host { |
||||
|
.tb-template-header { |
||||
|
position: sticky; |
||||
|
top: 0; |
||||
|
z-index: 1; |
||||
|
padding-bottom: 12px; |
||||
|
background-color: var(--mdc-dialog-container-color, white); |
||||
|
|
||||
|
.tb-hint-available-params { |
||||
|
color: rgba(0, 0, 0, 0.54); |
||||
|
letter-spacing: 0.4px; |
||||
|
|
||||
|
.content { |
||||
|
vertical-align: middle; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.template-tittle { |
||||
|
gap: 12px; |
||||
|
font-weight: normal; |
||||
|
|
||||
|
tb-icon { |
||||
|
color: rgba(0, 0, 0, .38); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
.tb-mat-error { |
||||
|
font-size: 13px; |
||||
|
} |
||||
|
|
||||
|
.tb-title { |
||||
|
font-size: 16px; |
||||
|
line-height: 24px; |
||||
|
|
||||
|
&.tb-required::after { |
||||
|
font-size: initial; |
||||
|
content: "*"; |
||||
|
} |
||||
|
|
||||
|
&.tb-error { |
||||
|
color: var(--mdc-theme-error, #f44336); |
||||
|
|
||||
|
&.tb-required::after { |
||||
|
color: var(--mdc-theme-error, #f44336); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
:host ::ng-deep { |
||||
|
.tb-form-panel .mat-expansion-panel.tb-settings { |
||||
|
& > .mat-expansion-panel-content > .mat-expansion-panel-body { |
||||
|
gap: 0; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,235 @@ |
|||||
|
///
|
||||
|
/// Copyright © 2016-2024 The Thingsboard Authors
|
||||
|
///
|
||||
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
/// you may not use this file except in compliance with the License.
|
||||
|
/// You may obtain a copy of the License at
|
||||
|
///
|
||||
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
///
|
||||
|
/// Unless required by applicable law or agreed to in writing, software
|
||||
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
/// See the License for the specific language governing permissions and
|
||||
|
/// limitations under the License.
|
||||
|
///
|
||||
|
|
||||
|
import { Component, forwardRef, Input, OnDestroy } from '@angular/core'; |
||||
|
import { |
||||
|
ControlValueAccessor, |
||||
|
FormBuilder, |
||||
|
FormGroup, |
||||
|
NG_VALIDATORS, |
||||
|
NG_VALUE_ACCESSOR, |
||||
|
ValidationErrors, |
||||
|
Validator, |
||||
|
Validators |
||||
|
} from '@angular/forms'; |
||||
|
import { |
||||
|
DeliveryMethodsTemplates, |
||||
|
NotificationDeliveryMethod, |
||||
|
NotificationDeliveryMethodInfoMap, |
||||
|
NotificationTemplateTypeTranslateMap, |
||||
|
NotificationType |
||||
|
} from '@shared/models/notification.models'; |
||||
|
import { takeUntil } from 'rxjs/operators'; |
||||
|
import { Subject } from 'rxjs'; |
||||
|
import { isDefinedAndNotNull } from '@core/utils'; |
||||
|
import { coerceBoolean } from '@shared/decorators/coercion'; |
||||
|
|
||||
|
@Component({ |
||||
|
selector: 'tb-template-configuration', |
||||
|
templateUrl: './notification-template-configuration.component.html', |
||||
|
styleUrls: ['./notification-template-configuration.component.scss'], |
||||
|
providers: [ |
||||
|
{ |
||||
|
provide: NG_VALUE_ACCESSOR, |
||||
|
useExisting: forwardRef(() => NotificationTemplateConfigurationComponent), |
||||
|
multi: true |
||||
|
}, |
||||
|
{ |
||||
|
provide: NG_VALIDATORS, |
||||
|
useExisting: forwardRef(() => NotificationTemplateConfigurationComponent), |
||||
|
multi: true, |
||||
|
} |
||||
|
] |
||||
|
}) |
||||
|
export class NotificationTemplateConfigurationComponent implements OnDestroy, ControlValueAccessor, Validator { |
||||
|
|
||||
|
templateConfigurationForm: FormGroup; |
||||
|
|
||||
|
NotificationDeliveryMethodInfoMap = NotificationDeliveryMethodInfoMap; |
||||
|
|
||||
|
@Input() |
||||
|
set predefinedDeliveryMethodsTemplate(value: Partial<DeliveryMethodsTemplates>) { |
||||
|
if (isDefinedAndNotNull(value)) { |
||||
|
this.templateConfigurationForm.patchValue(value, {emitEvent: false}); |
||||
|
this.updateDisabledForms(); |
||||
|
this.templateConfigurationForm.updateValueAndValidity(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Input() |
||||
|
notificationType: NotificationType; |
||||
|
|
||||
|
@Input() |
||||
|
@coerceBoolean() |
||||
|
interacted: boolean; |
||||
|
|
||||
|
readonly NotificationDeliveryMethod = NotificationDeliveryMethod; |
||||
|
readonly NotificationTemplateTypeTranslateMap = NotificationTemplateTypeTranslateMap; |
||||
|
|
||||
|
tinyMceOptions: Record<string, any> = { |
||||
|
base_url: '/assets/tinymce', |
||||
|
suffix: '.min', |
||||
|
plugins: ['link table image imagetools code fullscreen'], |
||||
|
menubar: 'edit insert tools view format table', |
||||
|
toolbar: 'fontselect fontsizeselect | formatselect | bold italic strikethrough forecolor backcolor ' + |
||||
|
'| link | table | image | alignleft aligncenter alignright alignjustify ' + |
||||
|
'| numlist bullist outdent indent | removeformat | code | fullscreen', |
||||
|
toolbar_mode: 'sliding', |
||||
|
height: 400, |
||||
|
autofocus: false, |
||||
|
branding: false |
||||
|
}; |
||||
|
|
||||
|
private propagateChange = (v: any) => { }; |
||||
|
private readonly destroy$ = new Subject<void>(); |
||||
|
|
||||
|
constructor(private fb: FormBuilder) { |
||||
|
this.templateConfigurationForm = this.buildForm(); |
||||
|
this.templateConfigurationForm.valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe((value) => { |
||||
|
this.propagateChange(value); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
ngOnDestroy() { |
||||
|
this.destroy$.next(); |
||||
|
this.destroy$.complete(); |
||||
|
} |
||||
|
|
||||
|
writeValue(value: any) { |
||||
|
this.templateConfigurationForm.patchValue(value, {emitEvent: false}); |
||||
|
} |
||||
|
|
||||
|
registerOnChange(fn: any): void { |
||||
|
this.propagateChange = fn; |
||||
|
} |
||||
|
|
||||
|
registerOnTouched(fn: any): void { |
||||
|
} |
||||
|
|
||||
|
setDisabledState(isDisabled: boolean) { |
||||
|
if (isDisabled) { |
||||
|
this.templateConfigurationForm.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
this.updateDisabledForms(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
validate(): ValidationErrors { |
||||
|
return this.templateConfigurationForm.valid ? null : { |
||||
|
templateConfiguration: { |
||||
|
valid: false, |
||||
|
}, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
private updateDisabledForms(){ |
||||
|
Object.values(NotificationDeliveryMethod).forEach((method) => { |
||||
|
const form = this.templateConfigurationForm.get(method); |
||||
|
if (!form.get('enabled').value) { |
||||
|
form.disable({emitEvent: false}); |
||||
|
} else { |
||||
|
form.enable({emitEvent: false}); |
||||
|
switch (method) { |
||||
|
case NotificationDeliveryMethod.WEB: |
||||
|
form.get('additionalConfig.icon.enabled').updateValueAndValidity({onlySelf: true}); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private buildForm(): FormGroup { |
||||
|
const form = this.fb.group({}); |
||||
|
|
||||
|
Object.values(NotificationDeliveryMethod).forEach((method) => { |
||||
|
form.addControl(method, this.buildDeliveryMethodControl(method), {emitEvent: false}); |
||||
|
}); |
||||
|
|
||||
|
return form; |
||||
|
} |
||||
|
|
||||
|
private buildDeliveryMethodControl(deliveryMethod: NotificationDeliveryMethod): FormGroup { |
||||
|
let deliveryMethodForm: FormGroup; |
||||
|
switch (deliveryMethod) { |
||||
|
case NotificationDeliveryMethod.WEB: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
subject: ['', Validators.required], |
||||
|
body: ['', Validators.required], |
||||
|
additionalConfig: this.fb.group({ |
||||
|
icon: this.fb.group({ |
||||
|
enabled: [false], |
||||
|
icon: [{value: 'notifications', disabled: true}, Validators.required], |
||||
|
color: [{value: '#757575', disabled: true}] |
||||
|
}), |
||||
|
actionButtonConfig: [null] |
||||
|
}) |
||||
|
}); |
||||
|
|
||||
|
deliveryMethodForm.get('additionalConfig.icon.enabled').valueChanges.pipe( |
||||
|
takeUntil(this.destroy$) |
||||
|
).subscribe((value) => { |
||||
|
if (value) { |
||||
|
deliveryMethodForm.get('additionalConfig.icon.icon').enable({emitEvent: false}); |
||||
|
deliveryMethodForm.get('additionalConfig.icon.color').enable({emitEvent: false}); |
||||
|
} else { |
||||
|
deliveryMethodForm.get('additionalConfig.icon.icon').disable({emitEvent: false}); |
||||
|
deliveryMethodForm.get('additionalConfig.icon.color').disable({emitEvent: false}); |
||||
|
} |
||||
|
}); |
||||
|
break; |
||||
|
case NotificationDeliveryMethod.EMAIL: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
subject: ['', Validators.required], |
||||
|
body: ['', Validators.required] |
||||
|
}); |
||||
|
break; |
||||
|
case NotificationDeliveryMethod.SMS: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
body: ['', [Validators.required, Validators.maxLength(320)]] |
||||
|
}); |
||||
|
break; |
||||
|
case NotificationDeliveryMethod.SLACK: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
body: ['', Validators.required] |
||||
|
}); |
||||
|
break; |
||||
|
case NotificationDeliveryMethod.MOBILE_APP: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
subject: ['', [Validators.required, Validators.maxLength(50)]], |
||||
|
body: ['', [Validators.required, Validators.maxLength(150)]], |
||||
|
additionalConfig: this.fb.group({ |
||||
|
onClick: [null] |
||||
|
}) |
||||
|
}); |
||||
|
break; |
||||
|
case NotificationDeliveryMethod.MICROSOFT_TEAMS: |
||||
|
deliveryMethodForm = this.fb.group({ |
||||
|
subject: [''], |
||||
|
body: ['', Validators.required], |
||||
|
themeColor: [''], |
||||
|
button: [null] |
||||
|
}); |
||||
|
break; |
||||
|
default: |
||||
|
throw new Error(`Not configured templated for notification delivery method: ${deliveryMethod}`); |
||||
|
} |
||||
|
deliveryMethodForm.addControl('enabled', this.fb.control(false), {emitEvent: false}); |
||||
|
deliveryMethodForm.addControl('method', this.fb.control(deliveryMethod), {emitEvent: false}); |
||||
|
return deliveryMethodForm; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue