Skip to content

Commit

Permalink
Make news feed a setting
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Jan 8, 2025
1 parent bd3ea81 commit 73ad8c7
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,10 @@ export default class OpenC3Api {
return this.exec('set_setting', [name, data])
}

update_news() {
return this.exec('update_news', [])
}

// DEPRECATED for set_setting
save_setting(name, data) {
return this.exec('set_setting', [name, data])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<v-divider />
<time-zone-settings />
<v-divider />
<news-feed-settings />
<v-divider />
<classification-banner-settings />
<v-divider />
<subtitle-settings />
Expand All @@ -44,6 +46,7 @@ import DefaultConfigSettings from './settings/DefaultConfigSettings.vue'
import AstroSettings from './settings/AstroSettings.vue'
import ClassificationBannerSettings from './settings/ClassificationBannerSettings.vue'
import TimeZoneSettings from './settings/TimeZoneSettings.vue'
import NewsFeedSettings from './settings/NewsFeedSettings.vue'
import SubtitleSettings from './settings/SubtitleSettings.vue'
import SourceCodeSettings from './settings/SourceCodeSettings.vue'
import RubyGemsSettings from './settings/RubyGemsSettings.vue'
Expand All @@ -56,6 +59,7 @@ export default {
AstroSettings,
ClassificationBannerSettings,
TimeZoneSettings,
NewsFeedSettings,
SubtitleSettings,
SourceCodeSettings,
RubyGemsSettings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
-->

<template>
<v-card>
<v-card-title>News Feed Settings</v-card-title>
<v-alert v-model="errorLoading" type="error" closable density="compact">
Error loading previous configuration due to {{ errorText }}
</v-alert>
<v-alert v-model="errorSaving" type="error" closable density="compact">
Error saving due to {{ errorText }}
</v-alert>
<v-alert v-model="successSaving" type="success" closable density="compact">
Saved! (Refresh the page to see changes)
</v-alert>
<v-card-text class="pb-0">
<v-switch
label="Allow COSMOS backend to pull the news feed from the COSMOS external news site.
This is a low bandwidth poll which only happens every 12 hrs. To immediately update the news feed, click the 'Refresh' in the User Menu."
v-model="newsFeed"
color="primary"
/>
</v-card-text>
<v-card-actions>
<v-btn
@click="save"
color="success"
variant="text"
data-test="save-news-feed"
>
Save
</v-btn>
</v-card-actions>
</v-card>
</template>

<script>
import Settings from './settings.js'

const settingName = 'news_feed'
export default {
mixins: [Settings],
data() {
return {
newsFeed: true,
}
},
created() {
this.loadSetting(settingName)
},
methods: {
save() {
console.log('save news feed', this.newsFeed)
this.saveSetting(settingName, this.newsFeed)
if (this.newsFeed) {
this.api.update_news()
}
},
parseSetting: function (response) {
console.log('parseSetting response', response)
this.newsFeed = response
},
},
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<v-checkbox
v-model="selectAllSuppressedWarnings"
label="Select All Warnings"
desity="compact"
density="compact"
/>
<v-checkbox
v-for="warning in suppressedWarnings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default {
}
},
methods: {
loadSetting: function (setting, variable, defaultValue) {
loadSetting: function (setting) {
this.api
.get_setting(setting)
.then((response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,39 @@
COSMOS Enterprise Edition
</div>
</v-card-text>
<div class="news-header">
COSMOS News
<v-switch
v-model="displayNews"
color="primary"
data-test="display-news"
class="news-switch"
hide-details
/>
<div v-if="newsFeed">
<v-row no-gutters class="news-header">
<v-col cols="auto" class="me-auto">COSMOS News</v-col>
<v-col cols="auto">
<v-btn
@click="refreshNews"
color="primary"
density="compact"
block
>
Refresh
</v-btn>
</v-col>
</v-row>
<v-list
lines="two"
width="420"
max-height="75vh"
class="overflow-y-auto"
data-test="news-list"
>
<template v-for="(news, index) in news" :key="`news-${index}`">
<hr />
<v-list-item class="pl-2">
<v-list-item-title>
<span class="news-title">{{ news.title }}</span
><span class="news-date">{{ formatDate(news.date) }}</span>
</v-list-item-title>
<div v-html="news.body"></div>
</v-list-item>
</template>
</v-list>
</div>
<v-list
v-if="displayNews"
lines="two"
width="420"
max-height="80vh"
class="overflow-y-auto"
data-test="news-list"
>
<template v-for="(news, index) in news" :key="`news-${index}`">
<hr />
<v-list-item class="pl-2">
<v-list-item-title>
<span class="news-title">{{ news.title }}</span
><span class="news-date">{{ formatDate(news.date) }}</span>
</v-list-item-title>
<div v-html="news.body"></div>
</v-list-item>
</template>
</v-list>
</v-card>
</v-menu>
<upgrade-to-enterprise-dialog
Expand All @@ -115,6 +119,7 @@

<script>
import { Api } from '@openc3/js-common/services'
import { OpenC3Api } from '@openc3/js-common/services'
import { UpgradeToEnterpriseDialog } from '@/components'

export default {
Expand All @@ -130,14 +135,15 @@ export default {
data: function () {
let user = OpenC3Auth.user()
return {
api: new OpenC3Api(),
showUserMenu: false,
authenticated: !!localStorage.openc3Token,
name: user['name'],
// preferred_username is returned by the token (see authorization.rb)
username: user['preferred_username'],
showUpgradeToEnterpriseDialog: false,
activeUsers: ['None'],
displayNews: true,
newsFeed: false,
news: [],
}
},
Expand All @@ -147,9 +153,6 @@ export default {
},
},
watch: {
displayNews: function (newValue, oldValue) {
localStorage.displayNews = newValue
},
// Whenever we show the user menu, read the news and refresh the list of active users
showUserMenu: function (newValue, oldValue) {
if (newValue === true) {
Expand All @@ -172,15 +175,30 @@ export default {
},
},
created: function () {
if (localStorage.displayNews) {
this.displayNews = localStorage.displayNews === 'true'
}
this.fetchNews()
// Every hour fetch news from the backend
// Note: the backend updates from news.openc3.org every 12 hours
setInterval(this.fetchNews, 60 * 60 * 1000)
this.api
.get_setting('news_feed')
.then((response) => {
if (response) {
this.newsFeed = response
if (this.newsFeed) {
this.fetchNews()
// Every hour fetch news from the backend
// Note: the backend updates from news.openc3.org every 12 hours
setInterval(this.fetchNews, 60 * 60 * 1000)
}
}
})
.catch((error) => {
// Do nothing
})
},
methods: {
refreshNews() {
// Force the backend to update the news feed
this.api.update_news().then(() => {
this.fetchNews()
})
},
formatDate(date) {
// Just show the YYYY-MM-DD part of the date
return date.split('T')[0]
Expand Down Expand Up @@ -227,14 +245,6 @@ export default {
}
</script>

<style>
.v-switch .v-selection-control {
min-height: 20px;
}
.v-switch .v-selection-control--density-default {
--v-selection-control-size: 20px;
}
</style>
<style scoped>
.news-header {
padding: 10px;
Expand All @@ -249,10 +259,6 @@ export default {
color: grey;
float: right;
}
.news-switch {
float: right;
padding-right: 10px;
}
.link {
cursor: pointer;
}
Expand Down
30 changes: 28 additions & 2 deletions openc3/lib/openc3/api/settings_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
# GNU Affero General Public License for more details.

# Modified by OpenC3, Inc.
# All changes Copyright 2022, OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

begin
require 'openc3-enterprise/version'
VERSION = OPENC3_ENTERPRISE_VERSION
ENTERPRISE = true
rescue LoadError
require 'openc3/version'
VERSION = OPENC3_VERSION
ENTERPRISE = false
end
require 'openc3/models/setting_model'
require 'openc3/models/news_model'

module OpenC3
module Api
Expand All @@ -31,7 +41,8 @@ module Api
'get_setting',
'get_settings',
'set_setting',
'save_setting' # DEPRECATED
'save_setting', # DEPRECATED
'update_news',
])

def list_settings(manual: false, scope: $openc3_scope, token: $openc3_token)
Expand Down Expand Up @@ -68,5 +79,20 @@ def set_setting(name, data, manual: false, scope: $openc3_scope, token: $openc3_
end
# save_setting is DEPRECATED
alias save_setting set_setting

# Update the news feed on demand to respond to frontend setting changes
def update_news(manual: false, scope: $openc3_scope, token: $openc3_token)
authorize(permission: 'admin', manual: manual, scope: scope, token: token)
conn = Faraday.new(
url: 'https://news.openc3.com',
params: {version: VERSION, enterprise: ENTERPRISE},
)
response = conn.get('/news')
if response.success?
NewsModel.set(response.body)
else
NewsModel.news_error(response)
end
end
end
end
Loading

0 comments on commit 73ad8c7

Please sign in to comment.