-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.js
More file actions
437 lines (382 loc) · 18 KB
/
Copy pathlibrary.js
File metadata and controls
437 lines (382 loc) · 18 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
'use strict';
const db = require.main.require('./src/database');
const meta = require.main.require('./src/meta');
const posts = require.main.require('./src/posts');
const topics = require.main.require('./src/topics');
const users = require.main.require('./src/user');
const websockets = require.main.require('./src/socket.io');
const AliExpressService = require('./aliexpress');
const plugin = {};
const ADMIN_UIDS = [1, 2];
const WHITELIST_DB_KEY = 'plugin:cline-links:whitelist';
// מפתחות DB עבור state של עיבוד הפוסט.
// state: 'pending' (עיבוד טרם הסתיים) | 'clean' (אין שינוי) | 'modified' (יש החלפות).
// כשהמצב 'modified', המפתח replacements מכיל JSON של מפת original→final.
const stateKey = (pid) => `plugin:cline-links:post:${pid}:state`;
const replacementsKey = (pid) => `plugin:cline-links:post:${pid}:replacements`;
const PENDING_INDEX_KEY = 'plugin:cline-links:pending'; // set של pids עם state=pending (לניקוי בעלייה)
const POLL_INTERVAL_MS = 200;
const POLL_TIMEOUT_MS = 10000;
const BLACKLISTED_PARAMS = [
'spm', 'aff_id', 'aff_platform', 'aff_trace_key', 'tag', 'ref',
'_x_cid', '_x_ads_channel', '_x_campaign', '_x_vst_scene',
'refer_share_id', 'refer_share_uid', 'invite_code',
'linkCode', 'ref_', 'creative', 'camp', 'collection_id',
'pdp_npi', 'gps-id', 'scm', 'ws_ab_test', 'pdp_ext_f', 'sourceType'
];
const WHITELISTED_PARAMS = [
'productIds', 'bundle_id', 'g_site', 'g_region', 'g_lg', 'g_ccy', 'subj'
];
const CLEANING_RULES = [
{
name: 'Short Links',
regex: /(?:https?:\/\/)?(?:s\.click\.aliexpress\.com|a\.aliexpress\.com|temu\.to|share\.temu\.com|amzn\.to|ebay\.to)\/[^\s)]+/g,
resolve: true,
isAliExpress: true
},
{
name: 'AliExpress Direct',
regex: /(?:https?:\/\/)?(?:\w+\.)?aliexpress\.com\/(?:item\/|ssr\/|store\/|p\/)[^\s)]+/g,
resolve: false,
isAliExpress: true
},
{
name: 'Temu Direct',
regex: /(?:https?:\/\/)?(?:\w+\.)?temu\.com\/[^\s)]+/g,
resolve: false,
isAliExpress: false
},
{
name: 'Amazon Direct',
regex: /(?:https?:\/\/)?(?:\w+\.)?amazon\.(?:com|co\.uk|de|it|fr|es|ca)\/(?:dp|gp\/product)\/[\w\d]+[^\s)]*/g,
resolve: false,
isAliExpress: false
}
];
plugin.init = async function (params) {
// console.log('[cline-links] ✅ Plugin init() called - hooks should be registered');
const { router, middleware } = params;
router.get('/admin/plugins/cline-links', middleware.admin.buildHeader, plugin.renderAdmin);
router.get('/api/admin/plugins/cline-links', plugin.renderAdmin);
// ניקוי pending תקוע מ-crash קודם: כל פוסט שנשאר ב-pending לא יסיים לעולם
// ויחסום את המייל עד timeout. נסיר את הסימון כדי שהמיילים ייצאו (עם תוכן מ-DB,
// שייתכן שלא עובד, אבל זו הברירה הטובה יותר ממילא תקוע).
try {
const stuckPids = await db.getSetMembers(PENDING_INDEX_KEY);
if (stuckPids && stuckPids.length) {
// console.log(`[cline-links] 🧹 cleaning up ${stuckPids.length} stuck pending pids from previous run`);
await Promise.all(stuckPids.map(async (pid) => {
await db.delete(stateKey(pid));
await db.setRemove(PENDING_INDEX_KEY, pid);
}));
}
} catch (err) {
console.error('[cline-links] init cleanup error:', err);
}
};
plugin.renderAdmin = function (req, res) {
res.render('admin/plugins/cline-links', { title: 'Cline Links & Affiliate' });
};
plugin.addAdminNavigation = async function (header) {
header.plugins.push({ route: '/plugins/cline-links', icon: 'fa-shopping-cart', name: 'Cline Links' });
return header;
};
// קיצור-דרך לתהליך הנוכחי: אם העיבוד רץ באותו תהליך, ה-email hook יכול
// לחכות ישירות ל-Promise במקום polling ל-DB. בקונפיגורציית multi-process,
// תהליך אחר יזדקק ל-polling — וזה בסדר.
const inFlight = new Map();
async function markPending(pid) {
await Promise.all([
db.set(stateKey(pid), 'pending'),
db.setAdd(PENDING_INDEX_KEY, String(pid)),
db.delete(replacementsKey(pid)),
]);
}
async function markFinished(pid, replacements) {
const hasChanges = replacements && replacements.size > 0;
if (hasChanges) {
const obj = {};
for (const [orig, final] of replacements) obj[orig] = final;
await db.setObject(replacementsKey(pid), obj);
await db.set(stateKey(pid), 'modified');
} else {
await db.delete(replacementsKey(pid));
await db.set(stateKey(pid), 'clean');
}
await db.setRemove(PENDING_INDEX_KEY, String(pid));
}
async function getState(pid) {
return await db.get(stateKey(pid));
}
async function getReplacements(pid) {
const obj = await db.getObject(replacementsKey(pid));
if (!obj) return null;
return new Map(Object.entries(obj));
}
function startProcessing(pid) {
if (!pid) return null;
const key = String(pid);
if (inFlight.has(key)) return inFlight.get(key);
const p = (async () => {
let result = { replacements: new Map() };
try {
// Safety net: ב-filter:post.create ייתכן שה-pid עוד לא היה זמין
// ולכן markPending לא רץ. נסמן כאן ליתר ביטחון, לפני העיבוד עצמו.
try { await markPending(pid); }
catch (err) { console.error('[cline-links] markPending in startProcessing error:', err); }
result = await processPostContent(pid);
} finally {
try { await markFinished(pid, result.replacements); }
catch (err) { console.error('[cline-links] markFinished error:', err); }
}
return result;
})().finally(() => { if (inFlight.get(key) === p) inFlight.delete(key); });
inFlight.set(key, p);
return p;
}
// Hooks: filter:post.create / filter:post.edit רצים *לפני* שמירת הפוסט.
// אנחנו מסמנים pending כאן כדי שאם ה-email hook נורה לפני ש-post.save הסתיים,
// הוא ימצא את הסימון ויחכה.
plugin.handlePostFilter = async (data) => {
const pid = data?.post?.pid;
if (pid) {
try { await markPending(pid); }
catch (err) { console.error('[cline-links] markPending error:', err); }
}
return data;
};
plugin.handlePostSave = (data) => {
// console.log('[cline-links] 🔔 handlePostSave fired, pid=', data?.post?.pid, 'hasContent=', !!data?.post?.content);
if (data?.post?.pid) setImmediate(() => startProcessing(data.post.pid));
};
plugin.handlePostEdit = (data) => {
// console.log('[cline-links] 🔔 handlePostEdit fired, pid=', data?.post?.pid, 'hasContent=', !!data?.post?.content);
if (data?.post?.pid) setImmediate(() => startProcessing(data.post.pid));
};
async function waitForState(pid) {
// קיצור-דרך לתהליך הנוכחי
const local = inFlight.get(String(pid));
if (local) {
try { await local; } catch (_) { /* state ב-DB יספר את הסיפור */ }
}
const deadline = Date.now() + POLL_TIMEOUT_MS;
while (Date.now() < deadline) {
const state = await getState(pid);
if (state !== 'pending') return state; // 'clean' | 'modified' | null
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
}
console.warn(`[cline-links] ⏱️ waitForState timeout for pid=${pid}`);
return await getState(pid); // נחזיר את מה שיש, גם אם עדיין pending
}
plugin.handleEmailParams = async (data) => {
try {
// ה-hook מקבל מעטפת: { template, email, language, params, caller }.
// הפרמטרים האמיתיים של התבנית נמצאים ב-data.params.
const inner = data?.params || {};
const pid = parseInt(inner.pid ?? inner.notification?.pid, 10);
if (!pid) return data;
const state = await waitForState(pid);
// 'clean' או null/לא-מסומן → אין מה להחליף, יוצאים מיד (קריאת DB אחת).
if (state !== 'modified') return data;
const replacements = await getReplacements(pid);
if (!replacements || replacements.size === 0) return data;
const apply = (text) => {
if (typeof text !== 'string' || !text) return text;
let out = text;
for (const [orig, final] of replacements) {
if (orig === final) continue;
const escaped = orig.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
out = out.replace(new RegExp(escaped, 'g'), final);
}
return out;
};
if (inner.body) inner.body = apply(inner.body);
if (inner.intro) inner.intro = apply(inner.intro);
if (inner.subject) inner.subject = apply(inner.subject);
if (inner.notification) {
if (inner.notification.bodyShort) inner.notification.bodyShort = apply(inner.notification.bodyShort);
if (inner.notification.bodyLong) inner.notification.bodyLong = apply(inner.notification.bodyLong);
if (inner.notification.bodyEmail) inner.notification.bodyEmail = apply(inner.notification.bodyEmail);
if (inner.notification.subject) inner.notification.subject = apply(inner.notification.subject);
}
} catch (err) {
console.error('[cline-links] handleEmailParams error:', err);
}
return data;
};
async function processPostContent(pid) {
// console.log(`[cline-links] ▶️ processPostContent START pid=${pid}`);
const replacements = new Map();
try {
const postData = await posts.getPostData(pid);
if (!postData || !postData.content) {
// console.log(`[cline-links] ⛔ pid=${pid} no postData/content, postData=${!!postData}`);
return { replacements };
}
// console.log(`[cline-links] 📄 pid=${pid} content length=${postData.content.length}, uid=${postData.uid}`);
// console.log(`[cline-links] 📄 pid=${pid} content preview:`, postData.content.substring(0, 300));
const settings = await meta.settings.get('cline-links');
const enabled = settings && settings.enabled === 'on';
const postOwnerUid = parseInt(postData.uid, 10);
// console.log(`[cline-links] ⚙️ settings enabled=${enabled}, raw settings=`, JSON.stringify(settings));
let currentContent = postData.content;
let modified = false;
let matches = [];
for (const rule of CLEANING_RULES) {
const found = currentContent.match(rule.regex);
// console.log(`[cline-links] 🔍 rule "${rule.name}" matches=${found ? found.length : 0}`);
if (found) found.forEach(url => matches.push({ url, rule }));
}
if (matches.length === 0) {
// console.log(`[cline-links] ⛔ pid=${pid} no matching URLs found, returning`);
return { replacements };
}
// console.log(`[cline-links] ✅ pid=${pid} total matches=${matches.length}`);
const uniqueLinks = [...new Set(matches.map(m => m.url))];
const aliService = new AliExpressService(settings);
for (const originalUrl of uniqueLinks) {
const normalizedOriginal = normalizeUrl(originalUrl);
// console.log(`[cline-links] 🔗 processing URL: ${normalizedOriginal}`);
// בדיקה: האם הקישור הזה הוא כבר קישור "מוסכם" (הומר בעבר ע"י הפורום)
if (await db.isSetMember(WHITELIST_DB_KEY, normalizedOriginal)) {
// console.log(`[cline-links] Whitelist hit: skipping ${normalizedOriginal}`);
continue;
}
const match = matches.find(m => m.url === originalUrl);
let workUrl = normalizedOriginal;
let wasConverted = false;
// 1. Resolve (רק אם לא מולבן)
if (match.rule.resolve) {
workUrl = await resolveShortLink(workUrl);
// console.log(`[cline-links] 🔁 resolved to: ${workUrl}`);
}
// 2. Strip Parameters
let finalUrl = stripAffiliateParameters(workUrl);
// console.log(`[cline-links] 🧹 after strip: ${finalUrl}`);
// 3. AliExpress API Conversion
if (enabled && (match.rule.isAliExpress || finalUrl.includes('aliexpress.com'))) {
const subId = postOwnerUid > 0 ? `u${postOwnerUid}` : 'guest';
// console.log(`[cline-links] 💱 calling AliExpress API, subId=${subId}, url=${finalUrl}`);
const affiliateUrl = await aliService.convertToAffiliate(finalUrl, subId);
// console.log(`[cline-links] 💱 API returned: ${affiliateUrl}`);
if (affiliateUrl) {
finalUrl = affiliateUrl;
wasConverted = true;
}
} else {
// console.log(`[cline-links] ⏭️ skipping AliExpress conversion (enabled=${enabled}, isAli=${match.rule.isAliExpress}, containsAli=${finalUrl.includes('aliexpress.com')})`);
}
// 4. הלבנה: אנחנו שומרים את התוצאה הסופית ברשימה הלבנה
// אם זה קישור מקוצר (s.click) שנוצר עכשיו, הוא לא יעובד שוב לעולם.
if (wasConverted || match.rule.resolve) {
await db.setAdd(WHITELIST_DB_KEY, normalizeUrl(finalUrl));
}
// 5. החלפה
if (finalUrl !== originalUrl) {
const escaped = originalUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
currentContent = currentContent.replace(new RegExp(escaped, 'g'), finalUrl);
modified = true;
replacements.set(originalUrl, finalUrl);
}
}
if (modified) {
// console.log(`[cline-links] 💾 saving pid=${pid}, new content preview:`, currentContent.substring(0, 300));
await posts.setPostField(pid, 'content', currentContent);
await posts.clearCachedPost(pid);
const verifyData = await posts.getPostData(pid);
// console.log(`[cline-links] ✅ verify after save pid=${pid}, stored content preview:`, verifyData?.content?.substring(0, 300));
// בנייה מדויקת של אובייקט ה-Socket לפי הדוגמה התקינה
const [topicData, userData, parsedPost] = await Promise.all([
topics.getTopicData(postData.tid),
users.getUserFields(postOwnerUid, ['username', 'userslug', 'displayname']),
posts.parsePost({ ...postData, content: currentContent })
]);
const isMainPost = parseInt(pid, 10) === parseInt(topicData.mainPid, 10);
// אובייקט ה-Topic המצומצם שמופיע פעמיים
const simpleTopic = {
tid: parseInt(topicData.tid, 10),
cid: parseInt(topicData.cid, 10),
title: topicData.title,
isMainPost: isMainPost,
renamed: false,
tagsupdated: false
};
const editResult = {
topic: simpleTopic,
editor: {
username: userData.username,
userslug: userData.userslug,
uid: postOwnerUid,
displayname: userData.displayname,
isLocal: true
},
post: {
content: parsedPost.content, // ה-HTML המעובד
pid: parseInt(pid, 10),
tid: parseInt(postData.tid, 10),
uid: postOwnerUid,
timestamp: postData.timestamp,
timestampISO: new Date(postData.timestamp).toISOString(),
deleted: false,
upvotes: 0,
downvotes: 0,
deleterUid: 0,
replies: 0,
bookmarks: 0,
votes: 0,
cid: parseInt(topicData.cid, 10),
editor: postOwnerUid,
topic: simpleTopic, // חייב להופיע גם פה
changed: true // חייב להופיע בתוך הפוסט
}
};
websockets.in(`topic_${postData.tid}`).emit('event:post_edited', editResult);
// console.log(`[cline-links] Broadcasted exact structure for PID ${pid}`);
}
} catch (err) {
console.error('[cline-links] Error:', err);
}
return { replacements };
}
function normalizeUrl(url) {
if (!url) return '';
let cleaned = url.replace(/[).,;!]+$/, '').trim();
if (!/^https?:\/\//i.test(cleaned)) {
cleaned = 'https://' + cleaned;
}
return cleaned;
}
function stripAffiliateParameters(url) {
try {
const urlObj = new URL(normalizeUrl(url));
const params = urlObj.searchParams;
const keys = Array.from(params.keys());
keys.forEach(key => {
if (BLACKLISTED_PARAMS.includes(key) || key.startsWith('_x_')) {
params.delete(key);
} else if (
(urlObj.pathname.includes('/item/') || urlObj.pathname.includes('/ssr/') || urlObj.pathname.includes('/dp/')) &&
!WHITELISTED_PARAMS.includes(key)
) {
params.delete(key);
}
});
return urlObj.toString();
} catch (e) {
return url;
}
}
async function resolveShortLink(url) {
try {
const response = await fetch(url, {
method: 'GET',
redirect: 'follow',
headers: { 'User-Agent': 'Mozilla/5.0' },
signal: AbortSignal.timeout(5000)
});
return response.url;
} catch (err) {
return url;
}
}
module.exports = plugin;