Skip to content

Commit

Permalink
[ModConfig, ws_util] Add support for resetting configuration to default
Browse files Browse the repository at this point in the history
  • Loading branch information
m4heshd committed Jul 9, 2024
1 parent cdbf310 commit 2b3c465
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/src/config_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct ProxyAuth {
// Enums
/// Specifies which fields in the configuration are being updated.
pub enum ConfigUpdate {
Default,
Config(Box<UFCRConfig>),
Region(String),
Auth(String),
Expand Down Expand Up @@ -178,6 +179,7 @@ pub async fn update_config(update: ConfigUpdate) {
let mut new_config = get_config().as_ref().clone();

match update {
ConfigUpdate::Default => new_config = UFCRConfig::default(),
ConfigUpdate::Config(data) => new_config = *data,
ConfigUpdate::Region(data) => new_config.region = data,
ConfigUpdate::Auth(data) => new_config.auth_token = data,
Expand Down
20 changes: 16 additions & 4 deletions backend/src/ws_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use ufcr_libs::{log_err, log_info};
use crate::{
app_util::{check_app_update, get_app_metadata},
bin_util::{cancel_download, get_vod_formats, start_download, validate_bins},
config_util::{ConfigUpdate, get_config, is_debug, UFCRConfig, update_config},
config_util::{get_config, is_debug, update_config, ConfigUpdate, UFCRConfig},
fs_util::open_downloads_dir,
net_util::{
download_media_tools, get_vod_meta, get_vod_stream_url, JSON, JsonTryGet,
login_to_fight_pass, search_vods, update_proxied_client,
download_media_tools, get_vod_meta, get_vod_stream_url, login_to_fight_pass, search_vods,
update_proxied_client, JsonTryGet, JSON,
},
rt_util::QuitUnwrap,
state_util::{clear_inactive_dlq_vods, get_dlq, Vod},
Expand Down Expand Up @@ -60,6 +60,8 @@ fn handle_ws_client(socket: &SocketRef) {

socket.on("save-config", handle_save_config_event);

socket.on("reset-config", handle_reset_config_event);

socket.on("get-dlq", |ack: AckSender| {
ack.send(get_dlq().clone()).ok();
});
Expand Down Expand Up @@ -192,6 +194,16 @@ async fn handle_save_config_event(ack: AckSender, Data(data): Data<JSON>) {
}
}

/// Handles the `reset-config` WS event.
async fn handle_reset_config_event(ack: AckSender) {
update_config(ConfigUpdate::Default).await;
ack.send(get_config().as_ref()).ok();

if let Err(error) = update_proxied_client() {
emit_error(error);
}
}

/// Handles the `get-media-tools` WS event.
async fn handle_get_media_tools_event(socket: SocketRef, ack: AckSender, Data(data): Data<JSON>) {
if let Ok(missing_tools) = serde_json::from_value::<Vec<String>>(data) {
Expand Down Expand Up @@ -285,7 +297,7 @@ async fn handle_get_playable_event(ack: AckSender, Data(data): Data<JSON>) {
Ok(hls) => {
vod.hls = hls;
vod.q_id = create_uuid();

ack.send(vod).ok();
}
Err(error) => send_error(ack, error),
Expand Down
35 changes: 33 additions & 2 deletions frontend/src/components/ModConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,21 @@
<button>
<i>file_save</i>
Import
<input ref="configFileInput" type="file" id="config-import" accept=".json"/>
<input
ref="configFileInput"
type="file"
id="config-import"
accept=".json"
/>
</button>
<button @click="onConfigFileExport">
<i>upload_file</i>
Export
</button>
<button @click="store.showModConfigResetPrompt">
<i>settings_backup_restore</i>
Reset
</button>
</article>

<article class="border round mod-config__content__section">
Expand Down Expand Up @@ -469,6 +478,17 @@
<span>Save</span>
</button>
</nav>

<ModMsgBox
vID="modConfigResetPrompt"
vIcon="settings_backup_restore"
vTitle="Reset configuration to default"
vType="yes-no"
@onYes="onConfigReset"
>
This process will reset all of the configurations to default and log you out of the currently logged in Fight Pass
account. Are you sure you want to continue?
</ModMsgBox>
</div>
</template>

Expand All @@ -481,6 +501,7 @@ import {useAppStore} from '@/store';
import {useWSUtil} from '@/modules/ws-util';
// Components
import VAnchor from '@/components/VAnchor.vue';
import ModMsgBox from '@/components/ModMsgBox.vue';
// Store
const store = useAppStore();
Expand All @@ -495,7 +516,7 @@ const fail = (error) => {
};
// Websocket
const {login, saveConfig} = useWSUtil();
const {login, resetConfig, saveConfig} = useWSUtil();
// Account
const txtEmail = ref('');
Expand Down Expand Up @@ -542,6 +563,16 @@ function onConfigFileExport() {
window.location.href = '/export_config';
}
// Config reset
function onConfigReset() {
resetConfig()
.then(() => {
store.popSuccess('Configuration was successfully reset to default');
window.ui('#modConfig');
})
.catch(fail);
}
// Misc functions
function save(config) {
saveConfig(config || modConfig.data)
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/modules/ws-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export function useWSUtil() {
store.config = await emitPromise('save-config', newConfig);
}

async function resetConfig() {
store.config = await emitPromise('reset-config');
}

async function login(region, email, pass) {
store.config = await emitPromise('login', region, email, pass);
}
Expand Down Expand Up @@ -170,6 +174,7 @@ export function useWSUtil() {
initSocket,
getConfig,
saveConfig,
resetConfig,
login,
verifyURL,
getPlayableVOD,
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export const useAppStore = defineStore('app', {
showModUpdatePrompt() {
window.ui('#modUpdatePrompt');
},
showModConfigResetPrompt() {
window.ui('#modConfigResetPrompt');
},
showModConfig() {
this.modals.modConfig.data = JSON.parse(JSON.stringify(this.config));
window.ui('#modConfig');
Expand Down

0 comments on commit 2b3c465

Please sign in to comment.