forked from yossizahn/nodebb-plugin-chat-perms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarningDisplay.js
More file actions
115 lines (100 loc) · 2.63 KB
/
warningDisplay.js
File metadata and controls
115 lines (100 loc) · 2.63 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
/**
* Warning Display Module
* Displays configurable privacy warnings to users in chat interface
*
* Requirements: 2.1, 2.2, 2.3, 2.4
*/
'use strict';
// Load plugin settings from environment
let pluginSettings;
try {
pluginSettings = JSON.parse(process.env.CHAT_PERMS_PLUGIN_SETTINGS || '{}');
} catch {
pluginSettings = {};
}
// Default warning configuration
const DEFAULT_WARNING_CONFIG = {
enabled: false,
message: 'שים לב: ההנהלה יכולה לצפות בהודעות הצ\'אט',
displayType: 'banner'
};
// Valid display types
const VALID_DISPLAY_TYPES = ['banner', 'popup', 'inline'];
/**
* Gets the current warning configuration from plugin settings
* @returns {{enabled: boolean, message: string, displayType: 'banner' | 'popup' | 'inline'}}
*/
function getWarningConfig() {
const enabled = pluginSettings.WARNING_ENABLED === true;
const message = typeof pluginSettings.WARNING_MESSAGE === 'string' && pluginSettings.WARNING_MESSAGE.trim()
? pluginSettings.WARNING_MESSAGE
: DEFAULT_WARNING_CONFIG.message;
let displayType = pluginSettings.WARNING_DISPLAY_TYPE;
if (!VALID_DISPLAY_TYPES.includes(displayType)) {
displayType = DEFAULT_WARNING_CONFIG.displayType;
}
return {
enabled,
message,
displayType
};
}
/**
* Checks if the warning feature is enabled
* @returns {boolean}
*/
function isWarningEnabled() {
return getWarningConfig().enabled;
}
/**
* Injects warning data into chat response
* @param {Object} data - Chat response data
* @returns {Object} Data with warning injected (if enabled)
*/
function injectWarning(data) {
// Handle null/undefined data
if (data === null || data === undefined) {
data = {};
}
// If data is not an object, wrap it
if (typeof data !== 'object' || Array.isArray(data)) {
data = { originalData: data };
}
const config = getWarningConfig();
// Only inject warning if enabled
if (!config.enabled) {
return data;
}
// Inject warning data
return {
...data,
chatPermsWarning: {
message: config.message,
displayType: config.displayType
}
};
}
/**
* Updates the warning configuration (for testing purposes)
* @param {Object} newSettings - New settings to apply
*/
function updateSettings(newSettings) {
if (newSettings && typeof newSettings === 'object') {
pluginSettings = { ...pluginSettings, ...newSettings };
}
}
/**
* Resets settings to defaults (for testing purposes)
*/
function resetSettings() {
pluginSettings = {};
}
module.exports = {
getWarningConfig,
isWarningEnabled,
injectWarning,
updateSettings,
resetSettings,
VALID_DISPLAY_TYPES,
DEFAULT_WARNING_CONFIG
};