From 03fc7d4c19969adee0ce5c1f5ccd1bd47779aa36 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Wed, 13 May 2020 22:14:28 +0100 Subject: [PATCH] Added the new notification provider to the settings page --- .../{Class1.cs => CloudMobileNotification.cs} | 9 +- src/Ombi.Helpers/ApplicationSettings.cs | 1 + .../ClientApp/src/app/interfaces/IUser.ts | 12 +++ .../src/app/services/cloudmobile.service.ts | 22 +++++ .../notifications/cloudmobile.component.html | 55 +++++++++++++ .../notifications/cloudmobile.coponent.ts | 82 +++++++++++++++++++ .../notifications/mobile.component.html | 4 +- .../src/app/settings/settings.module.ts | 5 ++ .../app/settings/settingsmenu.component.html | 3 +- src/Ombi/Config/UserSettings.cs | 8 -- src/Ombi/Controllers/V2/MobileController.cs | 49 ++++++++++- src/Ombi/Extensions/StartupExtensions.cs | 1 - src/Ombi/Models/V2/SendMobileNotification.cs | 13 +++ src/Ombi/appsettings.json | 9 +- 14 files changed, 250 insertions(+), 23 deletions(-) rename src/Ombi.Api.CloudService/{Class1.cs => CloudMobileNotification.cs} (81%) create mode 100644 src/Ombi/ClientApp/src/app/services/cloudmobile.service.ts create mode 100644 src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.component.html create mode 100644 src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.coponent.ts delete mode 100644 src/Ombi/Config/UserSettings.cs create mode 100644 src/Ombi/Models/V2/SendMobileNotification.cs diff --git a/src/Ombi.Api.CloudService/Class1.cs b/src/Ombi.Api.CloudService/CloudMobileNotification.cs similarity index 81% rename from src/Ombi.Api.CloudService/Class1.cs rename to src/Ombi.Api.CloudService/CloudMobileNotification.cs index 704883f07..aba2d90d3 100644 --- a/src/Ombi.Api.CloudService/Class1.cs +++ b/src/Ombi.Api.CloudService/CloudMobileNotification.cs @@ -1,4 +1,6 @@ using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Ombi.Helpers; using System; using System.Collections.Generic; using System.Net.Http; @@ -14,17 +16,18 @@ namespace Ombi.Api.CloudService { private readonly IApi _api; private readonly ILogger _logger; - private const string BaseUrl = "https://ombinotifications.azurewebsites.net/api/"; + private readonly string _baseUrl; - public CloudMobileNotification(IApi api, ILogger logger) + public CloudMobileNotification(IApi api, ILogger logger, IOptions settings) { _api = api; + _baseUrl = settings.Value.NotificationService; _logger = logger; } public async Task SendMessage(MobileNotificationRequest notification) { - var request = new Request("MobileNotification", BaseUrl, HttpMethod.Post); + var request = new Request("MobileNotification", _baseUrl, HttpMethod.Post); request.AddJsonBody(notification); var response = await _api.Request(request); diff --git a/src/Ombi.Helpers/ApplicationSettings.cs b/src/Ombi.Helpers/ApplicationSettings.cs index 12be2087c..753b5a4d4 100644 --- a/src/Ombi.Helpers/ApplicationSettings.cs +++ b/src/Ombi.Helpers/ApplicationSettings.cs @@ -3,5 +3,6 @@ public class ApplicationSettings { public string OmbiService { get; set; } + public string NotificationService { get; set; } } } \ No newline at end of file diff --git a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts index 9b0500891..d3b6c783a 100644 --- a/src/Ombi/ClientApp/src/app/interfaces/IUser.ts +++ b/src/Ombi/ClientApp/src/app/interfaces/IUser.ts @@ -71,6 +71,18 @@ export interface IMobileUsersViewModel { devices: number; } +export interface ICloudMobileModel { + userId: string; + username: string; + devices: ICloudMobileDevices[]; +} +export interface ICloudMobileDevices { + token: string; + userId: string; + addedAt: Date; + user: IUser; +} + export interface IMassEmailUserModel { user: IUser; selected: boolean; diff --git a/src/Ombi/ClientApp/src/app/services/cloudmobile.service.ts b/src/Ombi/ClientApp/src/app/services/cloudmobile.service.ts new file mode 100644 index 000000000..d1675ae75 --- /dev/null +++ b/src/Ombi/ClientApp/src/app/services/cloudmobile.service.ts @@ -0,0 +1,22 @@ +import { APP_BASE_HREF } from "@angular/common"; +import { Injectable, Inject } from "@angular/core"; + +import { HttpClient } from "@angular/common/http"; +import { Observable } from "rxjs"; + +import { ICloudMobileDevices, ICloudMobileModel } from "../interfaces"; +import { ServiceHelpers } from "./service.helpers"; + +@Injectable() +export class CloudMobileService extends ServiceHelpers { + constructor(http: HttpClient, @Inject(APP_BASE_HREF) href:string) { + super(http, "/api/v2/mobile/", href); + } + public getDevices(): Observable { + return this.http.get(`${this.url}users/`, {headers: this.headers}); + } + + public send(userId: string, message: string): Promise { + return this.http.post(`${this.url}send/`, { userId, message }, {headers: this.headers}).toPromise(); + } +} diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.component.html b/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.component.html new file mode 100644 index 000000000..c34f61687 --- /dev/null +++ b/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.component.html @@ -0,0 +1,55 @@ + + +
+
+ Mobile Notifications + +
+
+
+ + + + + + + + + + + + + + +
+ + + + + Username {{element.username}}
+ + +
+ + Message + + +
+
+ +
+
+
+ +
+ +
+ +
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.coponent.ts b/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.coponent.ts new file mode 100644 index 000000000..c10ab97b6 --- /dev/null +++ b/src/Ombi/ClientApp/src/app/settings/notifications/cloudmobile.coponent.ts @@ -0,0 +1,82 @@ +import { Component, OnInit } from "@angular/core"; +import { FormBuilder, FormGroup } from "@angular/forms"; + +import { IMobileNotifcationSettings, IMobileUsersViewModel, INotificationTemplates, NotificationType, ICloudMobileDevices, ICloudMobileModel } from "../../interfaces"; +import { TesterService } from "../../services"; +import { NotificationService } from "../../services"; +import { MobileService, SettingsService } from "../../services"; +import { CloudMobileService } from "../../services/cloudmobile.service"; +import { SelectionModel } from "@angular/cdk/collections"; +import { MatTableDataSource } from "@angular/material"; + +@Component({ + templateUrl: "./cloudmobile.component.html", +}) +export class CloudMobileComponent implements OnInit { + + public NotificationType = NotificationType; + public templates: INotificationTemplates[]; + public form: FormGroup; + public devices: MatTableDataSource; + public selection = new SelectionModel(true, []); + displayedColumns: string[] = ['select', 'username']; + public message: string; + + constructor(private settingsService: SettingsService, + private notificationService: NotificationService, + private fb: FormBuilder, + private mobileService: CloudMobileService) { } + + public async ngOnInit() { + this.settingsService.getMobileNotificationSettings().subscribe(x => { + this.templates = x.notificationTemplates; + + this.form = this.fb.group({ + }); + }); + + var result = await this.mobileService.getDevices().toPromise(); + if (result.length > 0) { + this.devices = new MatTableDataSource(result); + } + } + + public onSubmit(form: FormGroup) { + if (form.invalid) { + this.notificationService.error("Please check your entered values"); + return; + } + + const settings = form.value; + settings.notificationTemplates = this.templates; + + this.settingsService.saveMobileNotificationSettings(settings).subscribe(x => { + if (x) { + this.notificationService.success("Successfully saved the Mobile settings"); + } else { + this.notificationService.success("There was an error when saving the Mobile settings"); + } + }); + + } + + public async sendMessage(form: FormGroup) { + if (form.invalid) { + this.notificationService.error("Please check your entered values"); + return; + } + if (this.selection.selected.length <= 0) { + this.notificationService.warning("Warning", "Please select a user to send the test notification"); + return; + } + + await this.selection.selected.forEach(async (u) => { + await this.mobileService.send(u.userId, this.message); + + this.notificationService.success( + "Successfully sent a Mobile message"); + + + }); + } +} diff --git a/src/Ombi/ClientApp/src/app/settings/notifications/mobile.component.html b/src/Ombi/ClientApp/src/app/settings/notifications/mobile.component.html index 2b7ea9b2c..829133665 100644 --- a/src/Ombi/ClientApp/src/app/settings/notifications/mobile.component.html +++ b/src/Ombi/ClientApp/src/app/settings/notifications/mobile.component.html @@ -2,7 +2,7 @@
- Mobile Notifications + Legacy Mobile Notifications
@@ -35,7 +35,7 @@
- +