-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Post news feed to user menu #1807
Changes from 4 commits
ac8c545
bd3ea81
73ad8c7
f826f5c
e4904b0
f5ca53b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# encoding: ascii-8bit | ||
|
||
# Copyright 2024 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. | ||
|
||
require 'openc3/models/news_model' | ||
|
||
class NewsController < ApplicationController | ||
def index | ||
render json: OpenC3::NewsModel.all() | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<!-- | ||
# 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() { | ||
this.saveSetting(settingName, this.newsFeed) | ||
if (this.newsFeed) { | ||
this.api.update_news() | ||
} | ||
}, | ||
parseSetting: function (response) { | ||
this.newsFeed = response | ||
}, | ||
}, | ||
} | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
status="off" | ||
:label="name" | ||
:sublabel="roles()" | ||
:notifications="unreadNews.length" | ||
></rux-monitoring-icon> | ||
</template> | ||
|
||
|
@@ -74,6 +75,39 @@ | |
COSMOS Enterprise Edition | ||
</div> | ||
</v-card-text> | ||
<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-card> | ||
</v-menu> | ||
<upgrade-to-enterprise-dialog | ||
|
@@ -85,6 +119,7 @@ | |
|
||
<script> | ||
import { Api } from '@openc3/js-common/services' | ||
import { OpenC3Api } from '@openc3/js-common/services' | ||
import { UpgradeToEnterpriseDialog } from '@/components' | ||
|
||
export default { | ||
|
@@ -100,19 +135,32 @@ 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'], | ||
newsFeed: false, | ||
news: [], | ||
} | ||
}, | ||
computed: { | ||
unreadNews: function () { | ||
return this.news.filter((news) => !news.read) | ||
}, | ||
}, | ||
watch: { | ||
// Whenever we show the user menu, refresh the list of active users | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will break when we start deleting old news. Maybe use a id / timestamp from the last news item? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I could grab the newest timestamp and then look to see if there is anything newer. |
||
|
||
if (this.name !== 'Anonymous') { | ||
Api.get('/openc3-api/users/active').then((response) => { | ||
this.activeUsers = response.data.filter( | ||
|
@@ -126,7 +174,53 @@ export default { | |
} | ||
}, | ||
}, | ||
created: function () { | ||
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] | ||
}, | ||
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( | ||
(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 | ||
} | ||
} | ||
}) | ||
}, | ||
logout: function () { | ||
OpenC3Auth.logout() | ||
Api.put(`/openc3-api/users/logout/${this.username}`) | ||
|
@@ -152,6 +246,19 @@ export default { | |
</script> | ||
|
||
<style scoped> | ||
.news-header { | ||
padding: 10px; | ||
background-color: var(--color-background-base-default); | ||
text-align: center; | ||
} | ||
.news-title { | ||
font-weight: bold; | ||
} | ||
.news-date { | ||
font-size: 0.8rem; | ||
color: grey; | ||
float: right; | ||
} | ||
.link { | ||
cursor: pointer; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we want to have an HTML version of this page too, and return either json or html based on extension?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a news site (https://www.openc3.com/news) so I can't imagine having another one for the little blurb news that goes in this json feed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine either way. It wouldn't really be used, just a pretty response when people poke around at the domain