so://phie

verbal

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

blob: 8ca25bd202f8f2487220dd10021920d008c42cf0 (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::Utils;

9

10use crate::adapter::Adapter;

11

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

13pub struct Account {

14 pub username: Option<String>,

15 pub language: String,

16 pub is_playback_history: bool,

17}

18

19impl Default for Account {

20 fn default() -> Self {

21 Self {

22 username: None,

23 language: "en".into(),

24 is_playback_history: true,

25 }

26 }

27}

28

29impl Query for Account {

30 fn into_keys(&self) -> String {

31 format!("

32 username,

33 language,

34 is_playback_history,

35 ")

36 }

37 fn into_values(&self) -> String {

38 format!(

39 "

40 {},

41 '{}',

42 {},

43 ",

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

45 &self.language,

46 &self.is_playback_history,

47 )

48 }

49 fn into_update(&self) -> String {

50 format!(

51 "

52 username = {},

53 language = '{}',

54 is_playback_history = {},

55 ",

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

57 &self.language,

58 &self.is_playback_history,

59 )

60 }

61}

62

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

64pub struct Favorite {

65 pub station_id: String,

66 pub account_id: i32,

67}

68

69impl Favorite {

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

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

72 }

73}

74

75impl Query for Favorite {

76 fn into_keys(&self) -> String {

77 format!("

78 station_id,

79 account_id,

80 ")

81 }

82 fn into_values(&self) -> String {

83 format!(

84 "

85 '{}',

86 {},

87 ",

88 &self.station_id,

89 &self.account_id,

90 )

91 }

92 fn into_update(&self) -> String {

93 format!(

94 "

95 station_id = '{}',

96 account_id = {},

97 ",

98 &self.station_id,

99 &self.account_id,

100 )

101 }

102}

103

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

105pub struct Search {

106 pub name: String,

107 pub page: Option<i64>,

108}

109

110impl Search {

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

112 format!(

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

114 &self.name,

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

116 )

117 }

118}

119

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

121pub struct Station {

122 pub id: String,

123 pub name: String,

124 pub stream_url: String,

125 pub votes: i64,

126 pub favicon: String,

127 pub homepage: String,

128 pub tags: Vec<String>,

129 pub click_trend: i64,

130 pub click_count: i64,

131 pub country: String,

132 pub languages: Vec<String>,

133 pub state: String,

134 pub is_favorite: bool,

135}

136

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

138pub struct StationAdapter {

139 pub stationuuid: String,

140 pub name: String,

141 pub url: String,

142 pub votes: i64,

143 pub favicon: String,

144 pub homepage: String,

145 pub tags: String,

146 pub clicktrend: i64,

147 pub clickcount: i64,

148 pub countrycode: String,

149 pub languagecodes: String,

150 pub state: String,

151}

152

153impl Adapter<Station> for StationAdapter {

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

155 target.id = self.stationuuid;

156 target.name = self.name;

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

158 target.votes = self.votes;

159 target.favicon = self.favicon;

160 target.homepage = self.homepage;

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

162 target.click_trend = self.clicktrend;

163 target.click_count = self.clickcount;

164 target.country = self.countrycode;

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

166 target.state = self.state;

167 target

168 }

169}

170

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

172 value.split(",")

173 .map(str::to_string)

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

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

176}

177

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

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

180 .unwrap()

181 .is_match(&url)

182 {

183 /* TODO: include in config struct */

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

185 }

186 url

187}

188

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

190pub struct Code {

191 pub code: String,

192 pub used: bool,

193 pub expired: bool,

194}

195

196impl Code {

197 pub fn new() -> Self {

198 let code = rand::thread_rng()

199 .sample_iter(&Alphanumeric)

200 .take(6)

201 .map(char::from)

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

203 .collect::<String>();

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

205 }

206}