thelounge-plugin-ntfy 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -41,6 +41,14 @@ To start/stop sending push notifications in the desired network, enter:
41
41
  /ntfy start/stop
42
42
  ```
43
43
 
44
+ ## Private Messages
45
+
46
+ By default, you will only be notified when you are mentioned, **this includes messages sent privately to you**. If you want to be notified of all private messages, enter this command and start the notifier like usual:
47
+
48
+ ```
49
+ /ntfy config set config.notify_on_private_messages true
50
+ ```
51
+
44
52
  ## License
45
53
 
46
54
  This plugin is licensed under [MIT](https://opensource.org/license/mit)
@@ -8,3 +8,7 @@ services:
8
8
  restart: unless-stopped
9
9
  volumes:
10
10
  - $PWD/tl:/var/opt/thelounge
11
+ - $PWD/src:/var/opt/thelounge/packages/thelounge-plugin-ntfy/src
12
+ - $PWD/index.js:/var/opt/thelounge/packages/thelounge-plugin-ntfy/index.js
13
+ - $PWD/package.json:/var/opt/thelounge/packages/thelounge-plugin-ntfy/package.json
14
+ - $PWD/package-lock.json:/var/opt/thelounge/packages/thelounge-plugin-ntfy/package-lock.json
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thelounge-plugin-ntfy",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A plugin for The Lounge that sends push notifications via ntfy when highlighted",
5
5
  "keywords": [
6
6
  "thelounge",
package/src/config.js CHANGED
@@ -14,6 +14,9 @@ const DEFAULT_CONFIG = {
14
14
  password: null,
15
15
  token: null,
16
16
  },
17
+ config: {
18
+ notify_on_private_messages: false,
19
+ },
17
20
  };
18
21
 
19
22
  const ALLOWED_KEYS = new Set([
@@ -22,12 +25,15 @@ const ALLOWED_KEYS = new Set([
22
25
  "ntfy.username",
23
26
  "ntfy.password",
24
27
  "ntfy.token",
28
+ "config.notify_on_private_messages",
25
29
  ]);
26
30
 
31
+ const BOOLEAN_KEYS = new Set(["config.notify_on_private_messages"]);
32
+
27
33
  const userConfigSchema = {
28
34
  type: "object",
29
35
  additionalProperties: false,
30
- required: ["ntfy"],
36
+ required: ["ntfy", "config"],
31
37
  properties: {
32
38
  ntfy: {
33
39
  type: "object",
@@ -90,6 +96,17 @@ const userConfigSchema = {
90
96
  password: ["username"],
91
97
  },
92
98
  },
99
+ config: {
100
+ type: "object",
101
+ additionalProperties: false,
102
+ required: ["notify_on_private_messages"],
103
+ properties: {
104
+ notify_on_private_messages: {
105
+ type: "boolean",
106
+ default: false,
107
+ },
108
+ },
109
+ },
93
110
  },
94
111
  };
95
112
 
@@ -155,6 +172,24 @@ function saveUserSetting(username, settingKey, settingValue) {
155
172
  }
156
173
  }
157
174
 
175
+ if (settingValue && typeof settingValue !== "string") {
176
+ return `Error: expected value to be a string`;
177
+ }
178
+
179
+ if (BOOLEAN_KEYS.has(settingKey)) {
180
+ try {
181
+ settingValue = settingValue
182
+ ? JSON.parse(settingValue.toLowerCase())
183
+ : false;
184
+
185
+ if (typeof settingValue !== "boolean") {
186
+ return `Invalid value for ${settingKey}, expected a boolean`;
187
+ }
188
+ } catch {
189
+ return `Invalid value for ${settingKey}, expected a boolean`;
190
+ }
191
+ }
192
+
158
193
  curr[keys[keys.length - 1]] = settingValue;
159
194
 
160
195
  const userConfigPath = path.join(rootDir, "config", `${username}.json`);
package/src/handler.js CHANGED
@@ -1,45 +1,88 @@
1
1
  "use strict";
2
2
 
3
3
  const { loadUserConfig } = require("./config.js");
4
+ const { PluginLogger } = require("./logger.js");
4
5
 
5
6
  function createHandler(client, network) {
6
7
  return async (data) => {
8
+ // Ignore own messages
9
+ if (network.nick === data.nick) {
10
+ return;
11
+ }
12
+
7
13
  const highlightRegex = new RegExp(network.highlightRegex, "i");
8
14
  const message = data.message || "";
9
15
 
10
- if (highlightRegex.test(message)) {
11
- // Load config after each message to get latest settings
12
- const [userConfig, errors] = loadUserConfig(client.client.name);
16
+ const mentioned = highlightRegex.test(message);
17
+ const isPM = data.target === network.nick;
18
+
19
+ let notify = false;
20
+ let userConfig;
21
+
22
+ if (mentioned) {
23
+ // Mentions always notify
24
+ notify = true;
25
+ } else if (isPM) {
26
+ // PMs notify only if enabled in config
27
+ const [uc, errors] = loadUserConfig(client.client.name);
13
28
 
14
29
  if (errors.length > 0) {
15
30
  return;
16
31
  }
17
32
 
18
- let ntfyAuth;
33
+ userConfig = uc;
19
34
 
20
- if (userConfig.ntfy.token) {
21
- ntfyAuth = userConfig.ntfy.token;
22
- } else if (userConfig.ntfy.username && userConfig.ntfy.password) {
23
- ntfyAuth = {
24
- username: userConfig.ntfy.username,
25
- password: userConfig.ntfy.password,
26
- };
35
+ if (userConfig.config.notify_on_private_messages) {
36
+ notify = true;
27
37
  }
38
+ }
39
+
40
+ if (notify) {
41
+ try {
42
+ // Avoid needlessly loading user config multiple times
43
+ if (!userConfig) {
44
+ const [uc, errors] = loadUserConfig(client.client.name);
28
45
 
29
- const { NtfyClient, MessagePriority } = await import("ntfy");
46
+ if (errors.length > 0) {
47
+ return;
48
+ }
30
49
 
31
- const ntfyClient = new NtfyClient({
32
- server: userConfig.ntfy.server,
33
- topic: userConfig.ntfy.topic,
34
- priority: MessagePriority.HIGH,
35
- tags: ["speech_balloon"],
36
- authorization: ntfyAuth,
37
- });
50
+ userConfig = uc;
51
+ }
38
52
 
39
- ntfyClient.publish({
40
- title: `${network.name} ${data.target}: ${data.nick}`,
41
- message: message,
42
- });
53
+ let ntfyAuth;
54
+
55
+ if (userConfig.ntfy.token) {
56
+ ntfyAuth = {
57
+ username: "",
58
+ password: userConfig.ntfy.token,
59
+ };
60
+ } else if (userConfig.ntfy.username && userConfig.ntfy.password) {
61
+ ntfyAuth = {
62
+ username: userConfig.ntfy.username,
63
+ password: userConfig.ntfy.password,
64
+ };
65
+ }
66
+
67
+ const { NtfyClient, MessagePriority } = await import("ntfy");
68
+
69
+ const ntfyClient = new NtfyClient({
70
+ server: userConfig.ntfy.server,
71
+ topic: userConfig.ntfy.topic,
72
+ priority: MessagePriority.HIGH,
73
+ tags: ["speech_balloon"],
74
+ authorization: ntfyAuth,
75
+ });
76
+
77
+ ntfyClient.publish({
78
+ title: isPM
79
+ ? `${network.name}: ${data.nick}`
80
+ : `${network.name} ${data.target}: ${data.nick}`,
81
+ message: message,
82
+ });
83
+ } catch (e) {
84
+ PluginLogger.error("Failed to send ntfy notification", e);
85
+ }
43
86
  }
44
87
  };
45
88
  }