A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
1
import {state} from '../main.js';2
import {$lang, http} from '../utils.js';3
import VButton from '../element/button.js';4
import VIcon from '../element/icon.js';
13
$lang,14
async setLike(is_favorite) {15
try {15
await http`post::/api/favorite`(this.station.uuid);16
await http`${is_favorite ? 'post' : 'delete'}::/api/like`(this.station.uuid);17
this.station.is_favorite = is_favorite;18
state.app.$refs.favorites.doFetch();19
}20
catch(error) {21
console.log(error);22
}23
},24
setStation(station) {25
state.station = station;26
}27
},28
template: `
94
icon="player-play"95
class="bg-zinc-900 hover:bg-white"96
:title="$lang('global.play')"97
@click="() => setStation(station)"98
></v-button>99
</div>100
</div>
1
export default {2
props: ['id', 'tab'],3
watch: {4
tab: function(value) {5
this.$emit(value === this.id ? 'show' : 'hide');6
}7
},8
template: `9
<div v-if="id === tab" class="flex flex-col gap-6 sm:gap-12">10
<slot></slot>
3
computed: {4
styles() {5
return {6
'-webkit-mask': `url(https://cdn.typable.dev/tabler/${this.id})`,6
'mask-image': `url(https://cdn.typable.dev/tabler/${this.id})`,7
'mask-repeat': 'no-repeat',8
'mask-position': 'center',9
'mask-size': this.size ?? ''
5
import VAccount from './page/account.js';7
export const state = {8
app: null,9
tab: 'search',10
account: {11
username: 'typable',
34
},35
mounted() {36
document.body.style.display = '';37
state.app = this;38
},39
template: `40
<div class="px-4 sm:px-10 pb-8 sm:pb-[100px] max-w-[800px] mx-auto flex flex-col">41
<v-menu :state="state"></v-menu>42
<v-search :state="state"></v-search>41
<v-favorites :state="state"></v-favorites>43
<v-favorites ref="favorites" :state="state"></v-favorites>44
<v-account :state="state"></v-account>45
</div>46
`
21
methods: {22
$lang,23
async doFetch() {24
this.results = [];25
this.loading = true;25
const result = await http`get::/api/favorite`();26
const result = await http`get::/api/like`();27
this.loading = false;28
this.results = result;29
}
26
methods: {27
$lang,28
async doSearch() {29
this.results = [];30
this.loading = true;31
const result = await http`get::/api/search`(this.query);32
this.loading = false;
37
this.doSearch();38
},39
template: `39
<v-tab id="search" :tab="state.tab">40
<v-tab id="search" :tab="state.tab" @show="doSearch">41
<v-player :station="state.station"></v-player>42
<input43
v-model="query.name"
4
use crate::utils::upgrade_to_https;6
#[derive(Debug, Serialize)]7
pub struct Account {8
pub username: Option<String>,9
pub language: String,10
pub is_playback_history: bool,11
}13
impl Into<Account> for model::Account {14
fn into(self) -> Account {15
Account {16
username: self.username,17
language: self.language,18
is_playback_history: self.is_playback_history,19
}20
}21
}23
#[derive(Debug, Serialize)]24
pub struct Station {25
pub uuid: String,26
pub name: String,
67
}68
});70
app.at("/api/favorite")70
app.at("/api/like")71
.get(|req: tide::Request<()>| async move {72
match Service::get_favorites(req).await {72
match Service::get_likes(req).await {73
Ok(data) => Response::with(data),74
Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),75
}76
});78
app.at("/api/favorite")78
app.at("/api/like")79
.post(|req: tide::Request<()>| async move {80
match Service::add_favorite(req).await {80
match Service::add_like(req).await {81
Ok(data) => Response::with(data),82
Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),83
}84
});86
app.at("/api/like")87
.delete(|req: tide::Request<()>| async move {88
match Service::remove_like(req).await {89
Ok(data) => Response::with(data),90
Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),91
}
41
Ok(data)42
}44
pub async fn get_favorites(req: tide::Request<()>) -> Result<Vec<data::Station>> {44
pub async fn get_likes(req: tide::Request<()>) -> Result<Vec<data::Station>> {45
let account = req.ext::<model::Account>().unwrap();46
let query = format!(47
"48
SELECT *49
FROM \"like\"50
WHERE \"like\".account_id = '{}'50
WHERE account_id = '{}'51
ORDER BY created_at DESC52
",53
&account.id,54
);
62
.map(|like| &like.station_id)63
.cloned()64
.collect::<Vec<String>>();64
let rest = match RadioBrowserApi::by_uuids(uuids).await {65
let rests = match RadioBrowserApi::by_uuids(uuids).await {66
Ok(rest) => rest,67
Err(err) => return Err(err),68
};68
let data = rest69
.iter()70
.cloned()71
.map(|item| {72
let mut data: data::Station = item.clone().into();73
data.is_favorite = true;74
data75
})76
.collect::<Vec<data::Station>>();69
let mut data = vec![];70
for like in likes {71
for rest in &rests {72
if like.station_id.eq(&rest.stationuuid) {73
let mut item: data::Station = rest.clone().into();74
item.is_favorite = true;75
data.push(item);76
break;77
}78
}79
}80
Ok(data)81
}80
pub async fn add_favorite(mut req: tide::Request<()>) -> Result<()> {83
pub async fn add_like(mut req: tide::Request<()>) -> Result<()> {84
let uuid = match req.body_json::<String>().await {85
Ok(body) => body,86
Err(_) => {
109
Err(err) => Err(Error::new(ErrorKind::Query, &err.to_string())),110
}111
}113
pub async fn remove_like(mut req: tide::Request<()>) -> Result<()> {114
let uuid = match req.body_json::<String>().await {115
Ok(body) => body,116
Err(_) => {117
return Err(Error::new(118
ErrorKind::Arguments,119
"No station uuid provided!",120
))121
}122
};123
let account = req.ext::<model::Account>().unwrap();124
let query = format!(125
"126
DELETE FROM \"like\"127
WHERE account_id = {}128
AND station_id = '{}'129
",130
&account.id, &uuid,131
);132
let mut conn = req.sqlx_conn::<Postgres>().await;133
match sqlx::query(&query)134
.execute(conn.acquire().await.unwrap())135
.await136
{137
Ok(_) => Ok(()),138
Err(err) => Err(Error::new(ErrorKind::Query, &err.to_string())),139
}140
}141
}