-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipblog.js
More file actions
429 lines (376 loc) · 11 KB
/
ipblog.js
File metadata and controls
429 lines (376 loc) · 11 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
var ipcommon=require('ipcommon');
var nosql=require('nosql');
var sightmap=require('sightmap');
var Article=require('./article');
var Tag=require('./tag');
var IPBlog=module.exports=function(foo,findUser){
foo.addImportPath(__dirname+'/viewmacros');
this.db={};
this.db.articles=nosql.load('articles');
this.db.polls=nosql.load('polls');
this.db.pages=nosql.load('pages');
this.db.tags=nosql.load('tags');
this.findUser=findUser;
}
IPBlog.prototype.getDatabases=function(){
return [{name:'articles'},{name:'polls'},{name:'pages'},{name:'tags'}];
}
IPBlog.prototype.getCurPoll=function(callback){
this.db.polls.all(function(){return true;},function(err,polls){
var curPoll=null;
if(polls!==null){
for(var p=0;p<polls.length;p++){
var poll=polls[p];
if(poll.published){
curPoll=poll;
break;
}
}
}
callback(curPoll);
});
}
function summarize(article){
var stripped=article.content.replace(/(<([^>]+)>)/ig, '');
var sentences=stripped.match(/[^\.!\?]+[\.!\?]+/g);
var summary='';
var s=0;
while((summary.length<300)&&(s<sentences.length)){
summary+=sentences[s]+' ';
s++;
}
return '<p>'+summary+'... <a class="readmorelink" href="'+article.link+'">Read more</a></p>';
}
IPBlog.prototype.getArticlesPage=function(callback,page,pageSize){
if(page===undefined)page=0;
if(pageSize===undefined)pageSize=10;
var self=this;
var publishedArticles=[];
this.db.articles.all(function(){return true;},function(err,articles){
var numArticles=0;
if(articles!==null){
for(var a=0;a<articles.length;a++){
var article=articles[a];
if(article.published){
article.link='/'+article.url;
article.summary=summarize(article);
var user=self.findUser(article.author);
article.authorName=user.fullName;
article.authorLink=user.authorLink;
article.publishDate=new Date(article.publishDate);
publishedArticles.push(article);
}
}
publishedArticles.sort(function(a,b){
return b.publishDate-a.publishDate;
});
numArticles=publishedArticles.length;
publishedArticles=publishedArticles.filter(function(elem,index){
if((index>=(page*pageSize)) && (index<(page*pageSize+pageSize)))
return true;
else
return false;
});
}
callback(publishedArticles,numArticles,pageSize);
});
}
IPBlog.prototype.getTagsPage=function(callback,tagName,page,pageSize){
if(page===undefined)page=0;
if(pageSize===undefined)pageSize=10;
var self=this;
console.log(arguments);
this.db.tags.one(function(tag){return (tag.kind==='tag') && (tag.name===tagName);},function(err,tag){
if(tag===null){
callback([],0,pageSize);
return;
};
console.log(tag);
var publishedArticles=[];
self.db.articles.all(function(){return true;},function(err,articles){
for(var a=0;a<articles.length;a++){
var article=articles[a];
if(article.published){
for(var a2=0;a2<tag.articles.length;a2++){
if(tag.articles[a2]===article.id){
article.link='/'+article.url;
article.summary=summarize(article);
article.authorName=self.findUser(article.author).fullName;
article.publishDate=new Date(article.publishDate);
publishedArticles.push(article);
break;
}
}
}
}
publishedArticles.sort(function(a,b){
return b.publishDate-a.publishDate;
});
var numArticles=publishedArticles.length;
publishedArticles=publishedArticles.filter(function(elem,index){
if((index>=(page*pageSize)) && (index<(page*pageSize+pageSize)))
return true;
else
return false;
});
callback(publishedArticles,numArticles,pageSize);
});
});
}
IPBlog.prototype.getTags=function(callback){
this.db.tags.all(function(tag){return tag.kind==='tag';},function(err,tags){
if(tags!==null){
for(var t=0;t<tags.length;t++){
tags[t].link=tags[t].name.replace(/\s/g,'-')+'/';
tags[t].name=ipcommon.string.toTitleCase(tags[t].name);
}
}
callback(tags);
});
}
IPBlog.prototype.vote=function(id,voteIndex,callback){
this.db.polls.update(function(doc){
try{
if(doc.id===id){
doc.votes[voteIndex]++;
callback(doc);
}
}
catch(e){}//if we throw exception it will be deleted, we do not want that!
return doc;
});
}
IPBlog.prototype.getVote=function(id,callback){
this.db.polls.one(function(doc){return doc.id===id;},function(err,poll){
callback(poll);
});
}
IPBlog.prototype.getArticles=function(callback){
this.db.articles.all(function(){return true;},function(err,articles){
callback(articles);
});
}
IPBlog.prototype.getPoll=function(id,callback){
this.db.polls.one(function(doc){return doc.id===id;},function(err,poll){
callback(poll);
});
}
IPBlog.prototype.getPolls=function(callback){
this.db.polls.all(function(){return true;},function(err,polls){
callback(polls);
});
}
IPBlog.prototype.getArticle=function(id,callback){
this.db.articles.one(function(doc){return doc.id===id;},function(err,article){
callback(article);
});
}
IPBlog.prototype.getDBPage=function(callback,table,page,pageSize){
if(pageSize===undefined)pageSize=10;
var curIndex=0;
var indexLUT={};
this.db[table].all(function(err,item){
curIndex++;
if((curIndex>=(page*pageSize+1)) && (curIndex<(page*pageSize+pageSize+1))){
indexLUT[item.id]=curIndex-1;
return true;
}
else
return false;
},function(rows){
var items=rows.map(function(item){
return {index:indexLUT[item.id],id:item.id,title:item.title};
});
callback(items,curIndex,pageSize);
});
}
IPBlog.prototype.updateTags=function(category,tags,modifiers,article,callback){
var self=this;
function updateList(kind,list,callback){
function insertNew(index){
if(index>=list.length)callback();
else{
var name=list[index];
var tag=new Tag(kind,name);
self.db.tags.all(function(tag){return (tag.kind===kind)&&(tag.name===name);},function(err,items){
if(items.length===0){
//console.log('inserting',tag);
var t=new Tag(kind,name);
t.articles.push(article);
self.db.tags.insert(t,'creating tag '+name);
insertNew(index+1);
}
else{
//console.log('updating',items);
self.db.tags.update(function(tag){
if((tag!==undefined)&&(tag!==null)&&(tag.kind===kind)&&(tag.name===name)){
var found=false;
for(var a=0;a<tag.articles.length;a++){
if(tag.articles[a]===article){
found=true;
break;
}
}
if(!found)tag.articles.push(article);
}
return tag;
});
insertNew(index+1);
}
});
}
}
insertNew(0);
}
updateList('category',category||[],function(){
updateList('tag',tags||[],function(){
updateList('modifier',modifiers||[],function(){
callback();
});
});
});
}
IPBlog.prototype.getPagination=function(numArticles,pageIndex,pageSize){
var numPages=Math.ceil(numArticles/pageSize);
var maxPages=5;
var pagination=[];
for(var p=pageIndex-1;p<numPages;p++){
if(pagination.length>=maxPages)break;
if(p>=0 && p<numPages){
pagination.push(p+1);
}
}
return pagination;
}
IPBlog.prototype.generateSitemap=function(baseUrl,callback){
var self=this;
var pagesMap=[{loc:baseUrl,changefreq:'daily'}];
self.db.pages.all(function(doc){return true;},function(err,pages){
for(var p=0;p<pages.length;p++){
pagesMap.push({loc:baseUrl+pages[p].url,changefreq:'monthly'});
}
self.db.articles.all(function(doc){return doc.published;},function(err,articles){
for(var a=0;a<articles.length;a++){
pagesMap.push({loc:baseUrl+articles[a].url,changefreq:'weekly'});
}
self.getTags(function(tags){
for(var t=0;t<tags.length;t++){
pagesMap.push({loc:baseUrl+'tag/'+tags[t].link,changefreq:'daily'});
}
sightmap(pagesMap);
callback(sightmap);
});
});
});
}
IPBlog.prototype.getTableItem=function(table,id,callback){
this.db[table].one(function(doc){return doc.id===id;},function(err,item){
callback(item);
});
}
IPBlog.prototype.deleteTableItem=function(table,id,callback){
this.db[table].update(function(doc){
if(doc.id===id)return null;
return doc;
});
callback();
}
IPBlog.prototype.updateTableItem=function(table,id,newItem,callback){
var self=this;
self.db[table].all(function(doc){if(doc.id===undefined)return false;return doc.id===id;},function(err,items){
if(items.length>0){
self.db[table].update(function(doc){
try {// if an exception happens the item will be deleted, we do not want that!
if(doc.id===id){
return newItem;
}
}
catch(e){}
return doc;
});
}
else{
self.db[table].insert(newItem);
}
});
callback();
}
IPBlog.prototype.updateArticle=function(oldId,title,url,content,author,published,images,titleImage,tags,modifiers,callback){
var self=this;
if(oldId){
self.db.articles.all(function(doc){if(doc.id===undefined)return false;return doc.id===oldId;},function(err,items){
if(items.length>0){
var publishDate=undefined;
var doc=items[0];
if((published==='true')&&(doc.published===false))publishDate=new Date();
else publishDate=doc.publishDate;
var article=new Article(title,url,content,author,published==='true',publishDate);
article.images=images;
article.titleImage=titleImage;
article.tags=tags;
article.modifiers=modifiers;
self.updateTags(undefined,article.tags,article.modifiers,article.id,function(){
self.db.articles.update(function(doc){
if(doc.id===oldId){
return article;
}
return doc;
});
callback(article.id)
});
}
});
}
else{
var publishDate=undefined;
if((published==='true')&&(doc.published===false))publishDate=new Date();
var article=new Article(title,url,content,author,published==='true',publishDate);
article.images=images;
article.titleImage=titleImage;
article.tags=tags;
article.modifers=modifiers;
self.updateTags(undefined,article.tags,article.modifiers,article.id,function(){
newId=article.id;
self.db.articles.insert(article,'creating article');
callback(article.id);
});
}
}
IPBlog.prototype.deleteArticle=function(id,callback){
this.deleteTableItem('articles',id,callback);
}
IPBlog.prototype.updatePoll=function(oldId,poll,callback){
if(oldId){
this.db.polls.update(function(doc){
if(doc.id===oldId)return poll;
return doc;
});
}
else{
this.db.polls.insert(poll,'creating poll');
}
}
IPBlog.prototype.deletePoll=function(id,callback){
this.deleteTableItem('polls',id,callback);
}
IPBlog.prototype.findPageByUrl=function(url,callback){
var self=this;
self.db.pages.all(function(doc){if(doc.title===undefined)return false;return (doc.url===url);},function(err,pages){
if(pages && pages.length>0) callback(pages[0]);
else callback();
});
}
IPBlog.prototype.findArticleByUrl=function(url,callback){
var self=this;
self.db.articles.all(function(doc){if(doc.title===undefined)return false;return (doc.url===url);},function(err,articles){
if(articles && articles.length>0){
var article=articles[0];
var user=self.findUser(article.author);
article.authorName=user.fullName;
article.authorLink=user.authorLink;
article.publishDate=new Date(article.publishDate);
callback(article);
}
else callback();
});
}