@xuda.io/account_module 1.2.1026 → 1.2.1028
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/index.mjs +247 -5
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -2406,18 +2406,18 @@ export const ts_contact = async function (contact_id) {
|
|
|
2406
2406
|
};
|
|
2407
2407
|
|
|
2408
2408
|
export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
2409
|
-
const { uid } = req;
|
|
2409
|
+
const { uid, year, month, day } = req;
|
|
2410
2410
|
try {
|
|
2411
2411
|
const app_id = await get_account_default_project_id(uid);
|
|
2412
2412
|
const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
|
|
2413
2413
|
|
|
2414
2414
|
const d = new Date();
|
|
2415
|
-
const
|
|
2416
|
-
const
|
|
2415
|
+
const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
|
|
2416
|
+
const curr_year = d.getFullYear();
|
|
2417
2417
|
|
|
2418
2418
|
const ai_usage = await db_module.get_app_couch_view(app_id, 'ai_chat_usage', {
|
|
2419
|
-
startkey: [year, month, 0],
|
|
2420
|
-
endkey: [year, month, 99],
|
|
2419
|
+
startkey: [year || curr_year, month || curr_month, day || 0],
|
|
2420
|
+
endkey: [year || curr_year, month || curr_month, day || 99],
|
|
2421
2421
|
reduce: true,
|
|
2422
2422
|
group_level: 999,
|
|
2423
2423
|
});
|
|
@@ -2486,3 +2486,245 @@ setTimeout(async () => {
|
|
|
2486
2486
|
|
|
2487
2487
|
console.log(ret);
|
|
2488
2488
|
}, 1000);
|
|
2489
|
+
|
|
2490
|
+
export const get_profiles = async function (req) {
|
|
2491
|
+
const { _id, uid, name, limit, skip, bookmark, search, filter_type = 'all', contact_id } = req;
|
|
2492
|
+
|
|
2493
|
+
if (_id) {
|
|
2494
|
+
let data = await db_module.get_couch_doc('xuda_contacts', _id);
|
|
2495
|
+
return data;
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
let { with_requests } = req;
|
|
2499
|
+
var opt = {
|
|
2500
|
+
selector: {
|
|
2501
|
+
docType: 'contact',
|
|
2502
|
+
uid,
|
|
2503
|
+
stat: { $lt: 4 },
|
|
2504
|
+
},
|
|
2505
|
+
limit: limit || 9999,
|
|
2506
|
+
skip: skip || 0,
|
|
2507
|
+
sort: [{ ts: 'desc' }],
|
|
2508
|
+
};
|
|
2509
|
+
|
|
2510
|
+
switch (filter_type) {
|
|
2511
|
+
case 'archived': {
|
|
2512
|
+
opt.selector.stat = 5;
|
|
2513
|
+
break;
|
|
2514
|
+
}
|
|
2515
|
+
case 'shared': {
|
|
2516
|
+
opt.selector.shared_from_uid = { $gt: null };
|
|
2517
|
+
break;
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
case 'pinned': {
|
|
2521
|
+
opt.selector.pinned = true;
|
|
2522
|
+
break;
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
case 'online': {
|
|
2526
|
+
opt.limit = 9999;
|
|
2527
|
+
break;
|
|
2528
|
+
}
|
|
2529
|
+
|
|
2530
|
+
default:
|
|
2531
|
+
with_requests = true;
|
|
2532
|
+
break;
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
if (_id) {
|
|
2536
|
+
opt.selector._id = _id;
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
if (contact_id) {
|
|
2540
|
+
opt.selector._id = contact_id;
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
if (typeof name !== 'undefined') {
|
|
2544
|
+
opt.selector.name = { $regex: `(?i)${name}` };
|
|
2545
|
+
}
|
|
2546
|
+
|
|
2547
|
+
if (typeof search !== 'undefined') {
|
|
2548
|
+
opt.selector['$or'] = [
|
|
2549
|
+
{
|
|
2550
|
+
name: {
|
|
2551
|
+
$regex: `(?i)${search}`,
|
|
2552
|
+
},
|
|
2553
|
+
},
|
|
2554
|
+
{
|
|
2555
|
+
email: {
|
|
2556
|
+
$regex: `(?i)${search}`,
|
|
2557
|
+
},
|
|
2558
|
+
},
|
|
2559
|
+
];
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
if (bookmark && bookmark !== 'nil') {
|
|
2563
|
+
opt.bookmark = bookmark;
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
let contacts = { docs: [], total_docs: 0 };
|
|
2567
|
+
|
|
2568
|
+
if (filter_type !== 'pending') {
|
|
2569
|
+
contacts = await db_module.find_couch_query('xuda_contacts', opt);
|
|
2570
|
+
if (!limit || contact_id) {
|
|
2571
|
+
contacts.total_docs = contacts.docs.length;
|
|
2572
|
+
} else {
|
|
2573
|
+
delete opt.sort;
|
|
2574
|
+
delete opt.skip;
|
|
2575
|
+
opt.limit = 9999;
|
|
2576
|
+
opt.fields = ['_id'];
|
|
2577
|
+
|
|
2578
|
+
const counter = await db_module.find_couch_query('xuda_contacts', opt);
|
|
2579
|
+
contacts.total_docs = counter.docs.length;
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
let requests_to = { docs: [], total_docs: 0 };
|
|
2584
|
+
let requests_from = { docs: [], total_docs: 0 };
|
|
2585
|
+
let shared_from = { docs: [], total_docs: 0 };
|
|
2586
|
+
let my_contact = { docs: [], total_docs: 0 };
|
|
2587
|
+
let contact_docs = { docs: [], total_docs: contacts.total_docs };
|
|
2588
|
+
for await (let doc of contacts.docs) {
|
|
2589
|
+
const ret_to = requests_to.docs.find((e) => {
|
|
2590
|
+
return e.contact_uid === doc.contact_uid;
|
|
2591
|
+
});
|
|
2592
|
+
if (ret_to) continue;
|
|
2593
|
+
|
|
2594
|
+
const ret_from = requests_from.docs.find((e) => {
|
|
2595
|
+
return e.contact_uid === doc.contact_uid;
|
|
2596
|
+
});
|
|
2597
|
+
if (ret_from) continue;
|
|
2598
|
+
|
|
2599
|
+
doc = await get_contact_info(uid, doc);
|
|
2600
|
+
|
|
2601
|
+
if (filter_type === 'online') {
|
|
2602
|
+
if (!doc.online) continue;
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
contact_docs.docs.push(doc);
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
if (!contact_id && with_requests) {
|
|
2609
|
+
if (filter_type === 'pending') {
|
|
2610
|
+
////// TO - OUT
|
|
2611
|
+
requests_to.docs = await get_pending_contact_out(uid);
|
|
2612
|
+
requests_to.total_docs = requests_to.docs.length;
|
|
2613
|
+
|
|
2614
|
+
////// FROM - IN
|
|
2615
|
+
requests_from.docs = await get_pending_contact_in(uid);
|
|
2616
|
+
requests_from.total_docs = requests_from.docs.length;
|
|
2617
|
+
shared_from.docs = await get_pending_share_contact_in(uid);
|
|
2618
|
+
shared_from.total_docs = shared_from.docs.length;
|
|
2619
|
+
}
|
|
2620
|
+
// my contact
|
|
2621
|
+
if (filter_type === 'all' && !search && contact_docs.docs.length) {
|
|
2622
|
+
const my_contact_doc = await get_user_contact(uid, uid);
|
|
2623
|
+
my_contact.docs = [my_contact_doc];
|
|
2624
|
+
my_contact.total_docs = 1;
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
return {
|
|
2629
|
+
code: 1,
|
|
2630
|
+
data: {
|
|
2631
|
+
docs: [...my_contact.docs, ...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
|
|
2632
|
+
total_docs: my_contact.total_docs + shared_from.total_docs + requests_from.total_docs + requests_to.total_docs + contact_docs.total_docs,
|
|
2633
|
+
},
|
|
2634
|
+
};
|
|
2635
|
+
};
|
|
2636
|
+
|
|
2637
|
+
export const archive_profile = async function (req) {
|
|
2638
|
+
const { contact_id } = req;
|
|
2639
|
+
try {
|
|
2640
|
+
var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
|
|
2641
|
+
contact_doc.stat = 5;
|
|
2642
|
+
contact_doc.stat_ts = Date.now();
|
|
2643
|
+
contact_doc.stat_reason = 'archived by the user';
|
|
2644
|
+
|
|
2645
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2646
|
+
|
|
2647
|
+
return contact_save_ret;
|
|
2648
|
+
} catch (err) {
|
|
2649
|
+
return {
|
|
2650
|
+
code: -22,
|
|
2651
|
+
data: err.message,
|
|
2652
|
+
};
|
|
2653
|
+
}
|
|
2654
|
+
};
|
|
2655
|
+
|
|
2656
|
+
export const delete_profile = async function (req, job_id, headers) {
|
|
2657
|
+
const { contact_id } = req;
|
|
2658
|
+
try {
|
|
2659
|
+
var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
|
|
2660
|
+
|
|
2661
|
+
contact_doc.stat = 4;
|
|
2662
|
+
contact_doc.stat_ts = Date.now();
|
|
2663
|
+
contact_doc.stat_reason = 'deleted by the user';
|
|
2664
|
+
|
|
2665
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2666
|
+
|
|
2667
|
+
ai_module.delete_depended_chats(uid, contact_id);
|
|
2668
|
+
return contact_save_ret;
|
|
2669
|
+
} catch (err) {
|
|
2670
|
+
return {
|
|
2671
|
+
code: -22,
|
|
2672
|
+
data: err.message,
|
|
2673
|
+
};
|
|
2674
|
+
}
|
|
2675
|
+
};
|
|
2676
|
+
|
|
2677
|
+
export const unarchive_profile = async function (req) {
|
|
2678
|
+
const { contact_id } = req;
|
|
2679
|
+
try {
|
|
2680
|
+
var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
|
|
2681
|
+
|
|
2682
|
+
if (contact_doc.stat !== 5) {
|
|
2683
|
+
throw new Error('contact is not archived');
|
|
2684
|
+
}
|
|
2685
|
+
|
|
2686
|
+
contact_doc.stat = 3;
|
|
2687
|
+
contact_doc.stat_ts = Date.now();
|
|
2688
|
+
contact_doc.stat_reason = 'archived by the user';
|
|
2689
|
+
|
|
2690
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2691
|
+
|
|
2692
|
+
return contact_save_ret;
|
|
2693
|
+
} catch (err) {
|
|
2694
|
+
return {
|
|
2695
|
+
code: -22,
|
|
2696
|
+
data: err.message,
|
|
2697
|
+
};
|
|
2698
|
+
}
|
|
2699
|
+
};
|
|
2700
|
+
|
|
2701
|
+
export const unfriend_profile = async function (req) {
|
|
2702
|
+
const { contact_id } = req;
|
|
2703
|
+
try {
|
|
2704
|
+
const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
|
|
2705
|
+
if (contact_ret.code < 0) {
|
|
2706
|
+
throw new Error(contact_ret.data);
|
|
2707
|
+
}
|
|
2708
|
+
var contact_doc = contact_ret.data;
|
|
2709
|
+
|
|
2710
|
+
if (!contact_doc.team_req_id) {
|
|
2711
|
+
throw new Error('no friend relationship found');
|
|
2712
|
+
}
|
|
2713
|
+
let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
|
|
2714
|
+
req_doc.team_req_stat = 4;
|
|
2715
|
+
req_doc.team_req_stat_desc = 'unfriend by the user';
|
|
2716
|
+
await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
|
|
2717
|
+
|
|
2718
|
+
contact_doc.team_req_id = null;
|
|
2719
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2720
|
+
|
|
2721
|
+
const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
|
|
2722
|
+
|
|
2723
|
+
return req_save_ret;
|
|
2724
|
+
} catch (err) {
|
|
2725
|
+
return {
|
|
2726
|
+
code: -21,
|
|
2727
|
+
data: err.message,
|
|
2728
|
+
};
|
|
2729
|
+
}
|
|
2730
|
+
};
|