so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: b61744f78fb7c3d3f7e3f44fd1e3c487be79af65 (4825 bytes)[doc][blame][raw]

1

2use serde::{Deserialize, Serialize};

3use sqlx::FromRow;

4use regex::Regex;

5use rand::{distributions::Alphanumeric, Rng};

6

7use verbal::model::Query;

8use verbal::adapter::Adapter;

9use verbal::Utils;

10

11#[derive(Debug, Deserialize, Serialize, FromRow)]

12pub struct Account {

13 pub username: Option<String>,

14 pub language: String,

15 pub is_playback_history: bool,

16}

17

18impl Default for Account {

19 fn default() -> Self {

20 Self {

21 username: None,

22 language: "en".into(),

23 is_playback_history: true,

24 }

25 }

26}

27

28impl Query for Account {

29 fn into_keys(&self) -> String {

30 format!("

31 username,

32 language,

33 is_playback_history,

34 ")

35 }

36 fn into_values(&self) -> String {

37 format!(

38 "

39 {},

40 '{}',

41 {},

42 ",

43 Utils::null(&self.username),

44 &self.language,

45 &self.is_playback_history,

46 )

47 }

48 fn into_update(&self) -> String {

49 format!(

50 "

51 username = {},

52 language = '{}',

53 is_playback_history = {},

54 ",

55 Utils::null(&self.username),

56 &self.language,

57 &self.is_playback_history,

58 )

59 }

60}

61

62#[derive(Debug, Deserialize, Serialize, FromRow)]

63pub struct Favorite {

64 pub station_id: String,

65 pub account_id: i32,

66}

67

68impl Favorite {

69 pub fn new(station_id: &str, account_id: i32) -> Self {

70 Self { station_id: station_id.into(), account_id }

71 }

72}

73

74impl Query for Favorite {

75 fn into_keys(&self) -> String {

76 format!("

77 station_id,

78 account_id,

79 ")

80 }

81 fn into_values(&self) -> String {

82 format!(

83 "

84 '{}',

85 {},

86 ",

87 &self.station_id,

88 &self.account_id,

89 )

90 }

91 fn into_update(&self) -> String {

92 format!(

93 "

94 station_id = '{}',

95 account_id = {},

96 ",

97 &self.station_id,

98 &self.account_id,

99 )

100 }

101}

102

103#[derive(Debug, Deserialize, Serialize)]

104pub struct Search {

105 pub name: String,

106 pub page: Option<i64>,

107}

108

109impl Search {

110 pub fn into_query(&self) -> String {

111 format!(

112 "name={}&offset={}&limit=10&order=votes&reverse=true&hidebroken=true",

113 &self.name,

114 &self.page.unwrap_or(0) * 10,

115 )

116 }

117}

118

119#[derive(Debug, Default, Deserialize, Serialize)]

120pub struct Station {

121 pub id: String,

122 pub name: String,

123 pub stream_url: String,

124 pub votes: i64,

125 pub favicon: String,

126 pub homepage: String,

127 pub tags: Vec<String>,

128 pub click_trend: i64,

129 pub click_count: i64,

130 pub country: String,

131 pub languages: Vec<String>,

132 pub state: String,

133 pub is_favorite: bool,

134}

135

136#[derive(Debug, Deserialize, Clone)]

137pub struct StationAdapter {

138 pub stationuuid: String,

139 pub name: String,

140 pub url: String,

141 pub votes: i64,

142 pub favicon: String,

143 pub homepage: String,

144 pub tags: String,

145 pub clicktrend: i64,

146 pub clickcount: i64,

147 pub countrycode: String,

148 pub languagecodes: String,

149 pub state: String,

150}

151

152impl Adapter<Station> for StationAdapter {

153 fn populate(self, mut target: Station) -> Station {

154 target.id = self.stationuuid;

155 target.name = self.name;

156 target.stream_url = upgrade_to_https(self.url);

157 target.votes = self.votes;

158 target.favicon = self.favicon;

159 target.homepage = self.homepage;

160 target.tags = to_array(self.tags);

161 target.click_trend = self.clicktrend;

162 target.click_count = self.clickcount;

163 target.country = self.countrycode;

164 target.languages = to_array(self.languagecodes);

165 target.state = self.state;

166 target

167 }

168}

169

170fn to_array(value: String) -> Vec<String> {

171 value.split(",")

172 .map(str::to_string)

173 .filter(|tag| tag.trim().len() > 0)

174 .collect::<Vec<String>>()

175}

176

177fn upgrade_to_https(url: String) -> String {

178 if Regex::new("^http://")

179 .unwrap()

180 .is_match(&url)

181 {

182 /* TODO: include in config struct */

183 return format!("https://api.radio.typable.dev/pass?redirect={}", &url);

184 }

185 url

186}

187

188#[derive(Debug, Deserialize, Serialize, FromRow)]

189pub struct Code {

190 pub code: String,

191 pub used: bool,

192 pub expired: bool,

193}

194

195impl Code {

196 pub fn new() -> Self {

197 let code = rand::thread_rng()

198 .sample_iter(&Alphanumeric)

199 .take(6)

200 .map(char::from)

201 .map(|c| c.to_uppercase().to_string())

202 .collect::<String>();

203 Self { code, used: false, expired: false }

204 }

205}