-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDeviceModel.cs
More file actions
64 lines (55 loc) · 2.1 KB
/
Copy pathDeviceModel.cs
File metadata and controls
64 lines (55 loc) · 2.1 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
using iGotify_Notification_Assist.Services;
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)
{
var title = iGotifyMessage.title;
var msg = iGotifyMessage.message;
var protocol = webSock.Url.ToString().Contains("ws://") ? "http://" : "https://";
var gotifyServerUrl = webSock.Url.ToString().Replace("ws://", "").Replace("wss://", "").Replace("\"", "")
.Split("/stream");
var imageUrl = gotifyServerUrl.Length > 0
? $"{protocol}{gotifyServerUrl[0]}$$${iGotifyMessage.appid}$$${webSock.Name}"
: "";
var usr = await DatabaseService.GetUser(webSock.Name!);
if (usr.Uid == 0)
{
Console.WriteLine("THERE'S SOMETHING WRONG HERE? NO USER FOUND");
}
var ntfy = new SecNtfy(Environments.secNtfyUrl);
var response = await ntfy.SendNotification(usr.DeviceToken, title, msg, iGotifyMessage.priority == 10, imageUrl,
iGotifyMessage.priority);
Console.WriteLine(response);
}
}