mirror of
https://github.com/ntop/ntopng.git
synced 2026-04-29 07:29:32 +00:00
reworked blog notifications
now the notification are stored in redis and not anymore in browser local storage
This commit is contained in:
parent
24b13e240c
commit
633ee3096d
8 changed files with 147 additions and 212 deletions
|
|
@ -6,12 +6,83 @@ local json = require("dkjson")
|
|||
|
||||
local blog_utils = {}
|
||||
|
||||
function intersect_posts(s1, s2)
|
||||
|
||||
local newSet = {}
|
||||
local post1 = s1[1]
|
||||
|
||||
-- if there aren't any old post then return the new ones
|
||||
if (s1[1] == nil) then
|
||||
for _, p in ipairs(s2) do
|
||||
p.users = {}
|
||||
for username, _ in pairs(ntop.getUsers()) do
|
||||
p["users"][username] = {}
|
||||
p["users"][username]["isNew"] = true
|
||||
end
|
||||
end
|
||||
return s2
|
||||
end
|
||||
-- if there aren't any new post then return the old ones
|
||||
if (s2[1] == nil) then
|
||||
return s1
|
||||
end
|
||||
|
||||
for i = 1, 3 do
|
||||
|
||||
local post2 = s2[i]
|
||||
|
||||
if (post1.epoch < post2.epoch) then
|
||||
newSet[i] = post2
|
||||
newSet[i + 1] = s1[i]
|
||||
newSet[i]["users"] = {}
|
||||
for username, _ in pairs(ntop.getUsers()) do
|
||||
newSet[i]["users"][username] = {}
|
||||
newSet[i]["users"][username]["isNew"] = true
|
||||
end
|
||||
elseif (post1.epoch == post2.epoch) then
|
||||
newSet[i + 1] = s1[i]
|
||||
end
|
||||
end
|
||||
|
||||
return newSet
|
||||
end
|
||||
|
||||
function blog_utils.updatePostState(blogNotificationId, username)
|
||||
|
||||
local postsJSON = ntop.getPref("ntopng.prefs.blog_feed")
|
||||
local posts = json.decode(postsJSON)
|
||||
local success = false
|
||||
|
||||
for _, p in ipairs(posts) do
|
||||
if p.id == blogNotificationId then
|
||||
p.users[username].isNew = false
|
||||
success = true
|
||||
end
|
||||
end
|
||||
|
||||
ntop.setPref("ntopng.prefs.blog_feed", json.encode(posts))
|
||||
|
||||
return (success)
|
||||
end
|
||||
|
||||
function blog_utils.updateRedis(newPosts)
|
||||
|
||||
-- decode older posts from updateRedis
|
||||
local oldPostsJSON = ntop.getPref("ntopng.prefs.blog_feed")
|
||||
local oldPosts = {}
|
||||
if (not isEmptyString(oldPostsJSON)) then
|
||||
oldPosts = json.decode(oldPostsJSON)
|
||||
end
|
||||
|
||||
-- intersect two notifications sets and marks the new
|
||||
local intersected = intersect_posts(oldPosts, newPosts)
|
||||
-- save the posts inside redis
|
||||
ntop.setPref("ntopng.notifications.blog_feed", json.encode(newPosts))
|
||||
ntop.setPref("ntopng.prefs.blog_feed", json.encode(intersected))
|
||||
|
||||
end
|
||||
|
||||
function blog_utils.fetchLatestPosts()
|
||||
|
||||
local JSON_FEED = "https://www.ntop.org/blog/feed/json"
|
||||
local response = ntop.httpGet(JSON_FEED)
|
||||
|
||||
|
|
@ -62,8 +133,9 @@ function blog_utils.fetchLatestPosts()
|
|||
return(true)
|
||||
end
|
||||
|
||||
function blog_utils.readPostsFromRedis()
|
||||
local postsJSON = ntop.getPref("ntopng.notifications.blog_feed")
|
||||
function blog_utils.readPostsFromRedis(username)
|
||||
|
||||
local postsJSON = ntop.getPref("ntopng.prefs.blog_feed")
|
||||
local posts = nil
|
||||
|
||||
if not isEmptyString(postsJSON) then
|
||||
|
|
@ -74,6 +146,11 @@ function blog_utils.readPostsFromRedis()
|
|||
posts = {}
|
||||
end
|
||||
|
||||
-- normalize the post data
|
||||
for i, p in pairs(posts) do
|
||||
p.isNew = p.users[username].isNew
|
||||
end
|
||||
|
||||
return posts
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue