-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmpreview_iota.js
118 lines (109 loc) · 3.88 KB
/
smpreview_iota.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
'use strict';
const Iota=require('iota')();
const MongoModels=require('mongo-models');
var smpreview_iota = {_ids: {}};
var smpreview_subject = 'Simple preview image for socal network query, facebook, tweet';
var smpreview_description = 'Snapshot png image filelink saved in Cloudinary';
async function path(parentId) {
var data = [];
return new Promise(async (ok, ko) => {
try {
data=await Iota.findOne({_id:Iota.ObjectID(parentId)});
return ok(data);
}
catch (error) {
console.error("sIota.path() caught error", error);
return ko(error)
}
})
}
function init(){
return new Promise(async (ok,ko)=>{
try{
await Iota.connectInit();
return ok();
}
catch(err){
ko(err);
}
})
}
function disconnect(){
MongoModels.disconnect();
}
function update_smpreview(parent, iUrl, site){
var data = [];
return new Promise(async (ok,ko)=>{
try{
// Per the discusion, all socialpreview images are kept. When generating social preview, be sure to select the lastest one.
// in the future, need one housekeeper to archive all Cloudinary old image and delete DB entries.
// data=await Iota.findOneAndDelete(
// {
// $and: [
// {parentId: pId},
// {"component.component": "socialpreview"}
// ]
// }
// )
const result = await Iota.insertOne(
{
parentId: parent._id.toString(),
subject: 'Social Media Preview: '+parent.subject,
description: `a social media preivew for taken from ${site}`,
component: {component: 'socialpreview', imgUrl: iUrl}
}
);
if(result && result.length===1)
ok(result[0]);
else {
const msg=`unexpected number of results received ${results.length}`
logger.error(msg)
ko(new Error(msg));
}
return ok();
}
catch(err){
ko(err);
}
})
}
function get_parentId4smpreview() {
var data = [];
return new Promise(async (ok,ko)=>{
try{
var lastSocialPreviews = await Iota.aggregate([
{ $match: { 'component.component': 'socialpreview' } },
{ $sort: { _id: -1 } },
{$limit: 1}
])
if(lastSocialPreviews.length) {
let lastSocialPreview=lastSocialPreviews[0]._id;
data = await Iota.aggregate([
{ $match: {'component.component': 'MergeParticipants', parentId: {$exists: true}, _id: {$gt: lastSocialPreview} } },
{ $sort: { _id: -1 } },
{ $group: { _id: '$userId', latest: { $first: '$$ROOT' } } },
// { $limit: maxParticipants },
{ $replaceRoot: { newRoot: '$latest' } },
]);
} else {
data = await Iota.aggregate([
{ $match: {'component.component': 'MergeParticipants', parentId: {$exists: true}}},
{ $sort: { _id: -1 } },
{ $group: { _id: '$parentId', latest: { $first: '$$ROOT' } } },
// { $limit: maxParticipants },
{ $replaceRoot: { newRoot: '$latest' } },
]);
}
return ok(data);
}
catch(err){
ko(err);
}
})
}
smpreview_iota.connectInit=init;
smpreview_iota.disconnect=disconnect;
smpreview_iota.path=path;
smpreview_iota.update_smpreview=update_smpreview;
smpreview_iota.Get_parentId4simprview=get_parentId4smpreview;
module.exports=smpreview_iota;