enhanced the discord notifications #3611

pull/3632/head
tidusjar 4 years ago
parent 51a886cef9
commit 31060fff61

@ -1,5 +1,6 @@
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json;
using Ombi.Api.Discord.Models; using Ombi.Api.Discord.Models;
namespace Ombi.Api.Discord namespace Ombi.Api.Discord
@ -17,7 +18,7 @@ namespace Ombi.Api.Discord
public async Task SendMessage(DiscordWebhookBody body, string webhookId, string webhookToken) public async Task SendMessage(DiscordWebhookBody body, string webhookId, string webhookToken)
{ {
var request = new Request($"webhooks/{webhookId}/{webhookToken}", BaseUrl, HttpMethod.Post); var request = new Request($"webhooks/{webhookId}/{webhookToken}", BaseUrl, HttpMethod.Post);
request.AddJsonBody(body); request.AddJsonBody(body);
request.ApplicationJsonContentType(); request.ApplicationJsonContentType();

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Ombi.Api.Discord.Models namespace Ombi.Api.Discord.Models
{ {
@ -13,8 +14,32 @@ namespace Ombi.Api.Discord.Models
{ {
public string title { get; set; } public string title { get; set; }
public string type => "rich"; // Always rich or embedded content public string type => "rich"; // Always rich or embedded content
public string description { get; set; } // Don't really need to set this public string description { get; set; }
public DiscordImage image { get; set; } public DateTime timestamp => DateTime.Now;
public string color { get; set; }
public DiscordFooter footer { get; set; }
public DiscordImage thumbnail { get; set; }
public DiscordAuthor author { get; set; }
public List<DiscordField> fields { get; set; }
}
public class DiscordFooter
{
public string text { get; set; }
}
public class DiscordAuthor
{
public string name { get; set; }
public string url { get; set; }
public string iconurl { get; set; }
}
public class DiscordField
{
public string name { get; set; }
public string value { get; set; }
public bool inline { get; set; }
} }
public class DiscordImage public class DiscordImage

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ombi.Api.Discord; using Ombi.Api.Discord;
@ -104,21 +105,65 @@ namespace Ombi.Notifications.Agents
username = settings.Username, username = settings.Username,
}; };
var fields = new List<DiscordField>();
if (model.Data.TryGetValue("RequestedUser", out var requestedUser))
{
if (requestedUser.HasValue())
{
fields.Add(new DiscordField { name = "Requsted By", value = requestedUser, inline = true });
}
}
if (model.Data.TryGetValue("DenyReason", out var denyReason))
{
if (denyReason.HasValue())
{
fields.Add(new DiscordField { name = "Denied Reason", value = denyReason, inline = true });
}
}
if (model.Data.TryGetValue("RequestStatus", out var status))
{
if (status.HasValue())
{
fields.Add(new DiscordField { name = "Status", value = status, inline = true });
}
}
var author = new DiscordAuthor
{
};
if (model.Data.TryGetValue("ApplicationUrl", out var appUrl))
{
author.url = appUrl;
}
if (model.Data.TryGetValue("ApplicationName", out var appName))
{
author.name = appName;
}
var embed = new DiscordEmbeds
{
fields = fields,
author = author
};
if (model.Data.TryGetValue("Title", out var title))
{
embed.title = title;
}
if (model.Data.TryGetValue("Overview", out var overview))
{
embed.description = overview;
}
string image; string image;
if (model.Other.TryGetValue("image", out image)) if (model.Other.TryGetValue("image", out image))
{ {
discordBody.embeds = new List<DiscordEmbeds> embed.thumbnail = new DiscordImage { url = image };
{
new DiscordEmbeds
{
image = new DiscordImage
{
url = image
}
}
};
} }
discordBody.embeds = new List<DiscordEmbeds> { embed };
await Api.SendMessage(discordBody, settings.WebHookId, settings.Token); await Api.SendMessage(discordBody, settings.WebHookId, settings.Token);
} }
catch (Exception e) catch (Exception e)
@ -148,6 +193,7 @@ namespace Ombi.Notifications.Agents
var notification = new NotificationMessage var notification = new NotificationMessage
{ {
Message = parsed.Message, Message = parsed.Message,
Data = parsed.Data.ToDictionary(x => x.Key, x => x.Value)
}; };
notification.Other.Add("image", parsed.Image); notification.Other.Add("image", parsed.Image);
await Send(notification, settings); await Send(notification, settings);

@ -64,6 +64,8 @@ namespace Ombi.Notifications
} }
AdditionalInformation = opts?.AdditionalInformation ?? string.Empty; AdditionalInformation = opts?.AdditionalInformation ?? string.Empty;
CalculateRequestStatus(req);
} }
public void Setup(NotificationOptions opts, AlbumRequest req, CustomizationSettings s, UserNotificationPreferences pref) public void Setup(NotificationOptions opts, AlbumRequest req, CustomizationSettings s, UserNotificationPreferences pref)
@ -107,6 +109,7 @@ namespace Ombi.Notifications
PosterImage = (req?.Cover.HasValue() ?? false) ? req.Cover : req?.Disk ?? string.Empty; PosterImage = (req?.Cover.HasValue() ?? false) ? req.Cover : req?.Disk ?? string.Empty;
AdditionalInformation = opts?.AdditionalInformation ?? string.Empty; AdditionalInformation = opts?.AdditionalInformation ?? string.Empty;
CalculateRequestStatus(req);
} }
public void SetupNewsletter(CustomizationSettings s) public void SetupNewsletter(CustomizationSettings s)
@ -197,6 +200,7 @@ namespace Ombi.Notifications
EpisodesList = epSb.ToString(); EpisodesList = epSb.ToString();
SeasonsList = seasonSb.ToString(); SeasonsList = seasonSb.ToString();
CalculateRequestStatus(req);
} }
public void Setup(OmbiUser user, CustomizationSettings s) public void Setup(OmbiUser user, CustomizationSettings s)
@ -220,6 +224,30 @@ namespace Ombi.Notifications
Type = opts.Substitutes.TryGetValue("RequestType", out val) ? val.Humanize() : string.Empty; Type = opts.Substitutes.TryGetValue("RequestType", out val) ? val.Humanize() : string.Empty;
} }
private void CalculateRequestStatus(BaseRequest req)
{
RequestStatus = string.Empty;
if (req != null)
{
if (req.Available)
{
RequestStatus = "Available";
return;
}
if (req.Denied ?? false)
{
RequestStatus = "Denied";
return;
}
if (!req.Available && req.Approved)
{
RequestStatus = "Processing Request";
return;
}
RequestStatus = "Pending Approval";
}
}
// User Defined // User Defined
public string RequestId { get; set; } public string RequestId { get; set; }
public string RequestedUser { get; set; } public string RequestedUser { get; set; }
@ -245,6 +273,7 @@ namespace Ombi.Notifications
public string UserPreference { get; set; } public string UserPreference { get; set; }
public string DenyReason { get; set; } public string DenyReason { get; set; }
public string AvailableDate { get; set; } public string AvailableDate { get; set; }
public string RequestStatus { get; set; }
// System Defined // System Defined
private string LongDate => DateTime.Now.ToString("D"); private string LongDate => DateTime.Now.ToString("D");
@ -282,6 +311,7 @@ namespace Ombi.Notifications
{nameof(UserPreference),UserPreference}, {nameof(UserPreference),UserPreference},
{nameof(DenyReason),DenyReason}, {nameof(DenyReason),DenyReason},
{nameof(AvailableDate),AvailableDate}, {nameof(AvailableDate),AvailableDate},
{nameof(RequestStatus),RequestStatus},
}; };
} }
} }

@ -1,56 +1,55 @@
 <settings-menu></settings-menu>
<settings-menu></settings-menu>
<div *ngIf="form" class="small-middle-container"> <div *ngIf="form" class="small-middle-container">
<fieldset> <fieldset class="top-space">
<legend>Discord Notifications</legend> <legend>Discord Notifications</legend>
<div class="col-md-6"> <div class="row">
<form novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)"> <div class="col-md-8">
<form novalidate [formGroup]="form" (ngSubmit)="onSubmit(form)">
<div class="form-group"> <div class="row">
<div class="checkbox"> <div class="col-md-12 col-12 col-sm-12">
<input type="checkbox" id="enable" formControlName="enabled"> <div>
<label for="enable">Enabled</label> <div class="md-form-field">
<mat-slide-toggle formControlName="enabled" id="enable">Enable</mat-slide-toggle>
</div>
</div>
</div>
</div> </div>
</div> <div class="row">
<div class="md-form-field">
<mat-form-field appearance="outline">
<div class="form-group"> <mat-label>Webhook Url</mat-label>
<label for="webhookUrl" class="control-label">Webhook Url</label> <input matInput formControlName="webhookUrl">
<input type="text" class="form-control form-control-custom " id="webhookUrl" name="webhookUrl" formControlName="webhookUrl" [ngClass]="{'form-error': form.get('webhookUrl').hasError('required')}"> </mat-form-field>
<small *ngIf="form.get('webhookUrl').hasError('required')" class="error-text">The Webhook Url is required</small> <mat-form-field appearance="outline">
</div> <mat-label>Username</mat-label>
<input matInput formControlName="username">
<div class="form-group"> </mat-form-field>
<label for="username" class="control-label">Username</label> </div>
<div>
<input type="text" class="form-control form-control-custom " id="username" name="username" formControlName="username" pTooltip="Optional, this will override the username you used for the Webhook">
</div> </div>
</div> <div class="row">
<div class="form-group">
<div>
<div class="form-group"> <button mat-raised-button type="button" class="btn-spacing" color="accent"
<div> (click)="test(form)" [disabled]="form.invalid">Test <div id="spinner"></div>
<button [disabled]="form.invalid" type="button" (click)="test(form)" class="btn btn-primary-outline"> </button>
Test </div>
<div id="spinner"></div> </div>
</button>
<div class="form-group">
<div>
<button mat-raised-button type="submit" class="btn-spacing" color="primary"
[disabled]="form.invalid">Submit</button>
</div>
</div>
</div> </div>
</div> </form>
</div>
<div class="form-group">
<div>
<button [disabled]="form.invalid" type="submit" id="save" class="btn btn-primary-outline">Submit</button>
</div>
</div>
</form>
</div>
<div class="col-md-6"> <div class="col-md-4">
<notification-templates [templates]="templates" [showSubject]="false"></notification-templates> <notification-templates [templates]="templates" [showSubject]="false"></notification-templates>
</div>
</div> </div>
</fieldset> </fieldset>
</div> </div>
Loading…
Cancel
Save