-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
83 lines (68 loc) · 1.97 KB
/
news.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
/*************************************************
* The scripts for MedCalc's newsfeed. *
* Created by Pascal Pfiffner, 2012-11-09 *
* Copyright 2012 MedCalc, all rights reserved *
*************************************************/
/**
* Our main module.
*/
var _main = angular.module('MedCalcNews', []);
/**
* The news controller.
*/
function NewsController($scope, $http, $location) {
$scope.newsitems = [];
$scope.didLoad = false;
// the last-time-read date can be passed as URL path component in epoch time (/index.html#/1234567890)
var ref_date = 0;
var path = ($location.path().length > 1) ? $location.path().substr(1) : null;
if (path) {
ref_date = (new Date(1000 * path)).getTime();
}
// this method starts loading the news
$scope.fetchNews = function() {
$http.get('./posts.json')
.then(function(json) {
// filter out @-replies
if (json && json.data && json.data.data && json.data.data.length > 0) {
items = [];
for (var i = 0; i < json.data.data.length; i++) {
item = json.data.data[i];
// was this deleted?
if (item.is_deleted) {
continue;
}
// is this a new item?
if (ref_date > 0) {
var my_date = Date.parse(item.created_at);
item.is_new = (my_date > ref_date);
}
else {
item.is_new = false;
}
// do we have mentions?
if ('entities' in item && 'mentions' in item.entities && item.entities.mentions.length > 0) {
var allowed = true;
// we have mentions, check that none is at the beginning of the post
for (var m = 0; m < item.entities.mentions.length; m++) {
var mention = item.entities.mentions[m];
if (0 == mention.pos) {
allowed = false;
break;
}
}
if (allowed) {
items.push(item);
}
}
else {
items.push(item);
}
}
$scope.newsitems = items;
}
$scope.didLoad = true;
});
}
$scope.fetchNews();
}