-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
240 lines (210 loc) · 7.24 KB
/
main.js
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const isDev = require('electron-is-dev')
const { initDb, getDb } = require('./db/config')
const axios = require('axios')
// WordPress API helper functions
async function initializeWordPressApi(settings, event) {
try {
if (!settings?.url || !settings?.username || !settings?.password) {
throw new Error('WordPress settings are incomplete. Please check your settings.');
}
const apiUrl = settings.url.replace(/\/$/, '');
const authString = Buffer.from(`${settings.username}:${settings.password}`).toString('base64');
// Get the root response
const response = await axios.get(`${apiUrl}`, {
headers: {
'Authorization': `Basic ${authString}`
}
});
console.log('WordPress Site Info:', {
name: response.data?.name,
description: response.data?.description,
url: response.data?.url,
namespaces: response.data?.namespaces,
everything: response.data
});
// Format the response data
const wpData = {
success: true,
namespaces: response.data?.namespaces || [],
name: response.data?.name || '-',
description: response.data?.description || '-',
url: response.data?.url || settings.url
};
// Get counts for posts, pages, and categories
const [posts, pages, categories] = await Promise.all([
axios.get(`${apiUrl}/wp-json/wp/v2/posts`, { headers: { 'Authorization': `Basic ${authString}` } }),
axios.get(`${apiUrl}/wp-json/wp/v2/pages`, { headers: { 'Authorization': `Basic ${authString}` } }),
axios.get(`${apiUrl}/wp-json/wp/v2/categories`, { headers: { 'Authorization': `Basic ${authString}` } })
]);
console.log('WordPress Content Stats:', {
posts: posts.data?.length || 0,
pages: pages.data?.length || 0,
categories: categories.data?.length || 0
});
// Send dashboard stats to renderer if event is provided
if (event?.sender) {
event.sender.send('wp:dashboard-stats', {
site: {
name: wpData.name,
description: wpData.description,
url: wpData.url,
apiUrl: apiUrl
},
content: {
posts: posts.data?.length || 0,
pages: pages.data?.length || 0,
categories: categories.data?.length || 0
}
});
}
return {
success: true,
data: wpData,
namespaces: wpData.namespaces,
name: wpData.name,
description: wpData.description,
url: wpData.url,
apiUrl: apiUrl
};
} catch (error) {
console.error('WordPress Initialization Error:', error.message);
return {
success: false,
error: error.response
? `HTTP error! status: ${error.response.status}`
: error.message
};
}
}
function createWindow() {
const mainWindow = new BrowserWindow({
width: 1200,
height: 650,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
frame: false,
resizable: false
});
mainWindow.loadFile('index.html');
}
ipcMain.on('minimize-window', () => {
BrowserWindow.getFocusedWindow().minimize();
});
ipcMain.on('close-window', () => {
BrowserWindow.getFocusedWindow().close();
});
// Database operations
ipcMain.handle('db:get', async (event, key) => {
const db = getDb();
await db.read();
return key.split('.').reduce((obj, prop) => obj?.[prop], db.data);
});
ipcMain.handle('db:set', async (event, key, value) => {
const db = getDb();
await db.read();
const keys = key.split('.');
const lastKey = keys.pop();
const target = keys.reduce((obj, prop) => obj[prop] = obj[prop] || {}, db.data);
target[lastKey] = value;
await db.write();
return value;
});
ipcMain.handle('db:push', async (event, key, value) => {
const db = getDb();
await db.read();
const target = key.split('.').reduce((obj, prop) => obj[prop] = obj[prop] || [], db.data);
if (Array.isArray(target)) {
target.push(value);
await db.write();
return target;
}
throw new Error('Target is not an array');
});
// Keyword-related handlers
ipcMain.handle('get-saved-keywords', async () => {
const db = getDb();
await db.read();
return db.data.keywords || [];
});
ipcMain.handle('save-keywords', async (event, keywords) => {
const db = getDb();
await db.read();
if (!db.data.keywords) {
db.data.keywords = [];
}
// Add only unique keywords
keywords.forEach(keyword => {
if (!db.data.keywords.includes(keyword)) {
db.data.keywords.push(keyword);
}
});
await db.write();
return db.data.keywords;
});
ipcMain.handle('delete-saved-keyword', async (event, keyword) => {
const db = getDb();
await db.read();
if (db.data.keywords) {
db.data.keywords = db.data.keywords.filter(k => k !== keyword);
await db.write();
}
return db.data.keywords || [];
});
// WordPress API handlers
ipcMain.handle('wp:testConnection', async (event, settings) => {
try {
// Basic validation
if (!settings.siteUrl || !settings.username || !settings.appPassword) {
throw new Error('Please fill in all fields');
}
// Ensure the URL ends with /wp-json/
const apiUrl = settings.siteUrl.replace(/\/?$/, '/wp-json/');
// Create authentication header
const authString = Buffer.from(`${settings.username}:${settings.appPassword}`).toString('base64');
// Test connection by fetching users endpoint
const response = await axios.get(`${apiUrl}wp/v2/users/me`, {
headers: {
'Authorization': `Basic ${authString}`
}
});
return { success: true, data: response.data };
} catch (error) {
const errorMessage = error.response
? `HTTP error! status: ${error.response.status}`
: error.message;
return { success: false, error: errorMessage };
}
});
// Initialize WordPress API handler
ipcMain.handle('wp:initialize', async (event) => {
const db = getDb();
await db.read();
const settings = db.data?.settings?.wordpress;
// Map the old settings structure to the new one
const mappedSettings = {
url: settings?.siteUrl,
username: settings?.username,
password: settings?.appPassword
};
return await initializeWordPressApi(mappedSettings, event);
});
// Initialize database when app is ready
app.whenReady().then(async () => {
await initDb();
createWindow();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});