generated from decoded-cipher/node-hbs-templete
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.js
134 lines (109 loc) · 4.96 KB
/
helper.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
var config = require('./config');
var ago = require('s-ago');
module.exports = {
getAllScraps : () => {
return new Promise((resolve, reject) => {
config.ScrapData.orderByChild("status").equalTo(1).once('value', (snapshot) => {
resolve(snapshot.val());
reject(new Error('Error while fetching Scrap Data'));
});
})
},
getScrapById : (id) => {
return new Promise((resolve, reject) => {
config.ScrapData.child(id).once('value', (snapshot) => {
resolve(snapshot.val());
reject(new Error('Scrap Data not found'));
});
})
},
decideAttachmentType : (x) => {
return new Promise((resolve, reject) => {
x.ago = ago(new Date(x.createdAt));
if (x.attachment) {
if (x.attachment.type == 'png' || x.attachment.type == 'jpg' || x.attachment.type == 'jpeg' || x.attachment.type == 'gif') {
x.isImage = true;
} else if (x.attachment.type == 'mp4') {
x.isVideo = true;
}
}
// check if x.content has any url, then return the url
if (x.content.match(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/g)) {
x.url = x.content.match(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/g)[0];
}
// console.log(x.url);
resolve(x);
reject(new Error('Error while deciding attachment type'));
})
},
postScrapLikesCounter : (scrap) => {
return new Promise( async (resolve, reject) => {
await config.ScrapData.child(scrap.id).child('likes').once('value', async (snapshot) => {
var likes = snapshot.val();
if (likes) {
for (var key in likes) {
var likesArray = Object.keys(likes).map((key) => {
return likes[key];
})
}
if (likesArray.includes(scrap.visitorId)) {
totalLikes = likesArray.length - 1;
await config.ScrapData.child(scrap.id).child('likes').child(key).remove();
await config.ScrapData.child(scrap.id).child('likesCount').set(totalLikes);
if (totalLikes == 0) {
await config.ScrapData.child(scrap.id).child('likesCount').remove();
}
} else {
totalLikes = likesArray.length + 1;
await config.ScrapData.child(scrap.id).child('likes').push(scrap.visitorId);
await config.ScrapData.child(scrap.id).child('likesCount').set(totalLikes);
}
} else {
var totalLikes = 1;
await config.ScrapData.child(scrap.id).child('likes').push(scrap.visitorId);
await config.ScrapData.child(scrap.id).child('likesCount').set(totalLikes);
}
resolve(totalLikes);
}).catch((err) => {
reject(err);
})
})
},
generateScrapMetaData : (scrap) => {
return new Promise((resolve, reject) => {
metadata = {
title: "Scrap | Inovus Scrapbook",
description: scrap.content,
image: scrap.attachment.url,
url: process.env.BASE_URL + "/scrap/" + scrap._id,
author: scrap.user.name
}
resolve(metadata);
reject(new Error('Error while generating metadata'));
})
},
replaceURLWithHTMLLinks : (scrap) => {
return new Promise((resolve, reject) => {
scrap.content = scrap.content.replace(/\n/g, '<br>');
var regex = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
var url = regex.exec(scrap.content);
if (url) {
var nonUrlPart = scrap.content.substring(0, scrap.content.indexOf(url[0]));
var urlPart = scrap.content.substring(scrap.content.indexOf(url[0]));
var urlPart = urlPart.replace(url[0], `<a target="_blank" href="${url[0]}">${url[0]}</a>`);
scrap.content = nonUrlPart + urlPart;
}
resolve(scrap);
reject(new Error('Error while replacing URL with HTML links'));
})
},
generateLikeCountMessage : (scrap, visitorId) => {
return new Promise((resolve, reject) => {
if (scrap.likesCount) {
scrap.likeMessage = `${scrap.likesCount} people liked`;
}
resolve(scrap);
reject(new Error('Error while generating relative like count'));
})
}
}