Skip to content

Commit

Permalink
Improve news feed handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Jan 11, 2025
1 parent f826f5c commit e4904b0
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: ascii-8bit

# Copyright 2024 OpenC3, Inc.
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
# Copyright 2024 OpenC3, Inc.
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
# Copyright 2024 OpenC3, Inc.
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
# Copyright 2024 OpenC3, Inc.
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
Expand Down Expand Up @@ -156,10 +156,12 @@ export default {
// Whenever we show the user menu, read the news and refresh the list of active users
showUserMenu: function (newValue, oldValue) {
if (newValue === true) {
this.news.forEach((news) => {
news.read = true
})
localStorage.newsRead = this.news.length
if (this.news.length > 0) {
this.news.forEach((news) => {
news.read = true
})
localStorage.lastNewsRead = this.news[0].date
}

if (this.name !== 'Anonymous') {
Api.get('/openc3-api/users/active').then((response) => {
Expand Down Expand Up @@ -205,19 +207,18 @@ export default {
},
fetchNews: function () {
Api.get('/openc3-api/news').then((response) => {
response.data.forEach((news) => {
news.read = false
this.news.some((oldNews) => oldNews.date === news.date)
? null
: this.news.push(news)
})
this.news = this.news.sort(
// We always get the full list of news we want to display
// At some point we may delete old news items so we don't
// want to persist news items in the frontend
this.news = response.data.sort(
(a, b) => Date.parse(b.date) - Date.parse(a.date),
)
if (localStorage.newsRead) {
for (let i = this.news.length - 1; i >= 0; i--) {
this.news[i].read = true
}
// If we've previously read the news then mark anything older than that as read
if (localStorage.lastNewsRead) {
this.news.forEach((news) => {
news.read =
Date.parse(news.date) <= Date.parse(localStorage.lastNewsRead)
})
}
})
},
Expand Down
7 changes: 7 additions & 0 deletions openc3/lib/openc3/api/settings_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ def update_news(manual: false, scope: $openc3_scope, token: $openc3_token)
else
NewsModel.news_error(response)
end

# Test code to update the news feed with a dummy message
# data = NewsModel.all()
# json = JSON.parse(data)
# json.unshift( { date: Time.now.utc.iso8601, title: "News at #{Time.now}", body: "The news feed has been updated at #{Time.now}." })
# json.pop if json.length > 5
# NewsModel.set(json.to_json)
end
end
end
8 changes: 3 additions & 5 deletions openc3/lib/openc3/microservices/periodic_microservice.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: ascii-8bit

# Copyright 2022 OpenC3, Inc.
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
Expand All @@ -23,11 +23,9 @@
require 'openc3/api/settings_api'

module OpenC3
class LocalApi
class PeriodicMicroservice < Microservice
include Api
end

class PeriodicMicroservice < Microservice
STARTUP_DELAY_SECONDS = 2 * 60 # Two Minutes
SLEEP_PERIOD_SECONDS = 24 * 60 * 60 # Run once per day

Expand All @@ -39,7 +37,7 @@ def initialize(*args)
end

def get_news
if LocalApi.new().get_setting('news_feed', scope: @scope)
if get_setting('news_feed', scope: @scope)
unless @conn
@conn = Faraday.new(
url: 'https://news.openc3.com',
Expand Down
3 changes: 1 addition & 2 deletions openc3/lib/openc3/migrations/20250108060000_news_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
module OpenC3
class NewsFeed < Migration
def self.run
setting = SettingModel.get(name: 'news_feed')
SettingModel.set({ name: 'news_feed', data: true }, scope: 'DEFAULT') unless setting
SettingModel.set({ name: 'news_feed', data: true }, scope: 'DEFAULT')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion openc3/lib/openc3/models/scope_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
Expand Down

0 comments on commit e4904b0

Please sign in to comment.