-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDeviceModel.cs
More file actions
82 lines (71 loc) · 2.85 KB
/
Copy pathDeviceModel.cs
File metadata and controls
82 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using iGotify_Notification_Assist.Services;
using Newtonsoft.Json;
using SecNtfyNuGet;
using Websocket.Client;
using Environments = iGotify_Notification_Assist.Services.Environments;
namespace iGotify_Notification_Assist.Models;
public class DeviceModel
{
public string ClientToken { get; set; } = "";
public string DeviceToken { get; set; } = "";
public string GotifyUrl { get; set; } = "";
/// <summary>
/// Add device token to txt file
/// </summary>
/// <returns></returns>
public async Task<bool> Insert()
{
if (!await DatabaseService.CheckIfUserExists(this))
return await DatabaseService.InsertUser(this);
else
return false;
}
/// <summary>
/// delete device token
/// </summary>
/// <returns></returns>
public async Task<bool> Delete()
{
return await DatabaseService.DeleteUser(ClientToken);
}
/// <summary>
/// Send the passed notification from the gotify instance that was passed via WebSocket
/// </summary>
/// <param name="iGotifyMessage"></param>
/// <param name="clientToken"></param>
public async Task SendNotifications(GotifyMessage iGotifyMessage, WebsocketClient webSock)
{
await SendNotifications(iGotifyMessage, webSock.Url.ToString(), webSock.Name ?? "");
}
/// <summary>
/// Send the passed notification from a native websocket context
/// </summary>
public async Task SendNotifications(GotifyMessage iGotifyMessage, string wsUrl, string clientToken)
{
if (string.IsNullOrWhiteSpace(clientToken))
{
AppLog.Warn("Notification", "Cannot send notification because client token is empty.");
return;
}
var title = iGotifyMessage.title;
var msg = iGotifyMessage.message;
var protocol = wsUrl.Contains("ws://") ? "http://" : "https://";
var gotifyServerUrl = wsUrl.Replace("ws://", "").Replace("wss://", "").Replace("\"", "")
.Split("/stream");
var imageUrl = gotifyServerUrl.Length > 0
? $"{protocol}{gotifyServerUrl[0]}$$${iGotifyMessage.appid}$$${clientToken}"
: "";
var usr = await DatabaseService.GetUser(clientToken);
if (usr.Uid == 0)
{
AppLog.Warn("Notification", $"No user found for client={AppLog.MaskSecret(clientToken)}");
}
var ntfy = new SecNtfy(Environments.secNtfyUrl);
var response = await ntfy.SendNotification(usr.DeviceToken, title, msg, iGotifyMessage.priority == 10, imageUrl,
iGotifyMessage.priority);
AppLog.Debug("Notification",
response != null
? $"SecNtfy response client={AppLog.MaskSecret(clientToken)} response={JsonConvert.SerializeObject(response)}"
: $"SecNtfy response client={AppLog.MaskSecret(clientToken)} response=<null>");
}
}