-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDeviceController.cs
More file actions
153 lines (135 loc) · 5.02 KB
/
Copy pathDeviceController.cs
File metadata and controls
153 lines (135 loc) · 5.02 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using iGotify_Notification_Assist.Models;
using iGotify_Notification_Assist.Services;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SecNtfyNuGet;
using Environments = iGotify_Notification_Assist.Services.Environments;
namespace iGotify_Notification_Assist.Controller;
[ApiController]
[Route("[controller]")]
public class DeviceController : ControllerBase
{
/// <summary>
/// Add device token to the TXT for sending the push notificcation
/// </summary>
/// <param name="deviceModel"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> PostDeviceModel(DeviceModel deviceModel)
{
string result;
bool resultBool;
Console.WriteLine($"ClientToken: {deviceModel.ClientToken}");
Console.WriteLine($"DeviceToken: {deviceModel.DeviceToken}");
Console.WriteLine($"GotifyUrl: {deviceModel.GotifyUrl}");
if (
deviceModel.ClientToken.Length == 0 || deviceModel.ClientToken == "string" ||
deviceModel.DeviceToken.Length == 0 || deviceModel.DeviceToken.Length < 60 ||
deviceModel.DeviceToken == "string" ||
deviceModel.GotifyUrl.Length == 0 || deviceModel.GotifyUrl == "string"
)
{
result = "Fehler beim hinzugefügen des Gerätes!";
resultBool = false;
return Ok(new { Message = result, Successful = resultBool });
}
if (await deviceModel.Insert())
{
var gss = GotifySocketService.getInstance();
GotifySocketService.KillAllWsThread();
gss.Start();
result = "Gerät erfolgreich hinzugefügt";
resultBool = true;
}
else
{
result = "Fehler beim hinzugefügen des Gerätes!";
resultBool = false;
}
return Ok(new { Message = result, Successful = resultBool });
}
/// <summary>
/// Delete device from TXT when loggin out from iGotify
/// </summary>
/// <param name="token">Clienttoken for verify the correct entry</param>
/// <returns></returns>
[HttpDelete]
public async Task<IActionResult> DeleteDevcice(string token)
{
string result;
bool resultBool;
Console.WriteLine($"Delete Token: {token}");
if (token.Length == 0 || token == "string")
{
result = "Error deleting device!";
resultBool = false;
return Ok(new { Message = result, Successful = resultBool });
}
var deviceModel = new DeviceModel { ClientToken = token };
var usr = await DatabaseService.GetUser(token);
if (await deviceModel.Delete())
{
var gss = GotifySocketService.getInstance();
GotifySocketService.KillAllWsThread();
gss.Start();
result = "Device deleted successfully!";
resultBool = true;
}
else
{
result = "Error deleting device!";
resultBool = false;
}
return Ok(new { Message = result, Successful = resultBool });
}
/// <summary>
/// Add Custom Headers e.g. Cloudflare, Pangolin authentication
/// </summary>
/// <param name="customHeaders">Custome Header items</param>
/// <param name="token">Clienttoken for verify the correct entry and instance</param>
/// <returns></returns>
[HttpPost("CustomHeaders/{token}")]
public async Task<IActionResult> CustomHeaders([FromBody] List<CustomHeaders> customHeaders, string token)
{
string result = "";
bool resultBool = false;
if (token == null || token.Length == 0)
{
resultBool = false;
result = "Token not set!";
return BadRequest(new { Message = result, Successful = resultBool });
}
if (customHeaders.Count == 0)
{
resultBool = false;
result = "CustomHeaders were not set!";
return BadRequest(new { Message = result, Successful = resultBool });
}
var usr = await DatabaseService.GetUser(token);
usr.Headers = JsonConvert.SerializeObject(customHeaders);
resultBool = await usr.Update();
if (resultBool)
{
var gss = GotifySocketService.getInstance();
GotifySocketService.KillAllWsThread();
gss.Start();
result = "CustomHeaders successfully added!";
}
return Ok(new { Message = result, Successful = resultBool });
}
/// <summary>
/// Send a Test message if remote notification work
/// </summary>
/// <param name="deviceToken">SecNtfy Token</param>
/// <returns></returns>
[HttpGet("Test/{deviceToken}")]
public async Task<IActionResult> Test(string deviceToken)
{
var ntfy = new SecNtfy(Environments.secNtfyUrl);
if (deviceToken.Length > 0)
_ = await ntfy.SendNotification(deviceToken, "Test", "Test Notification");
if (Environments.isLogEnabled)
Console.WriteLine(ntfy.encTitle);
return Ok();
}
}