forked from terhechte/twitvault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.rs
186 lines (166 loc) · 4.97 KB
/
storage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use egg_mode::{list, tweet::Tweet, user::TwitterUser};
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
/// The folder locations for the different data
const FOLDER_MEDIA: &str = "media";
const FILE_ROOT: &str = "_data.json";
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct List {
pub name: String,
pub list: list::List,
pub members: Vec<UserId>,
}
impl PartialEq for List {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.list.id == other.list.id
}
}
impl Eq for List {}
pub type UserId = u64;
pub type TweetId = u64;
pub type UrlString = String;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Data {
/// The profile of the owner
pub profile: TwitterUser,
/// The tweets of the owner
pub tweets: Vec<Tweet>,
/// Mentions of the owner
pub mentions: Vec<Tweet>,
/// Responses to tweets of the owner: FIXME: Not ther eyet
pub responses: HashMap<TweetId, Vec<Tweet>>,
/// Profiles from responses, bookmarks, DMs,
/// followers and follows
pub profiles: HashMap<UserId, TwitterUser>,
/// Followers
pub followers: Vec<UserId>,
/// Follows
pub follows: Vec<UserId>,
/// Lists
pub lists: Vec<List>,
/// Downloaded media with path to local file
/// - Tweet Media: ExtendedUrlString
/// - Profiles: Various Urls
pub media: HashMap<UrlString, String>,
/// The likes the user performed
#[serde(default)]
pub likes: Vec<Tweet>,
}
impl Data {
pub fn any_tweet(&self, id: TweetId) -> Option<&Tweet> {
for tweets in [&self.tweets, &self.mentions, &self.likes] {
for t in tweets {
if t.id == id {
return Some(t);
}
}
}
for tweets in self.responses.values() {
for t in tweets {
if t.id == id {
return Some(t);
}
}
}
None
}
}
#[derive(Debug, Clone)]
pub struct Storage {
pub root_folder: PathBuf,
data_path: PathBuf,
data: Data,
}
impl Storage {
fn storage_for_data(path: impl AsRef<Path>, data: Data) -> Result<Self> {
let root_folder = path.as_ref().to_path_buf();
if !root_folder.exists() {
std::fs::create_dir(&root_folder)?;
}
if !root_folder.join(FOLDER_MEDIA).exists() {
std::fs::create_dir(&root_folder.join(FOLDER_MEDIA))?;
}
let data_path = root_folder.join(FILE_ROOT);
Ok(Storage {
root_folder,
data_path,
data,
})
}
pub fn media_path(&self, filename: &str) -> PathBuf {
self.root_folder.join(FOLDER_MEDIA).join(filename)
}
pub fn new(profile: TwitterUser, path: impl AsRef<Path>) -> Result<Self> {
Self::storage_for_data(
path,
Data {
profile,
tweets: Default::default(),
mentions: Default::default(),
responses: Default::default(),
profiles: Default::default(),
followers: Default::default(),
follows: Default::default(),
lists: Default::default(),
media: Default::default(),
likes: Default::default(),
},
)
}
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
let data_path = path.as_ref().join(FILE_ROOT);
let input = std::fs::read(&data_path)?;
let data: Data = serde_json::from_slice(&input)?;
Self::storage_for_data(path, data)
}
pub fn data(&self) -> &Data {
&self.data
}
pub fn data_mut(&mut self) -> &mut Data {
&mut self.data
}
pub fn with_data(&mut self, action: impl Fn(&mut Data)) {
action(&mut self.data)
}
pub fn resolver(&self) -> MediaResolver {
MediaResolver {
root_folder: self.root_folder.join(FOLDER_MEDIA),
media: &self.data.media,
}
}
// Blocking write
pub fn save(&self) -> Result<()> {
use std::fs::OpenOptions;
let outfile = OpenOptions::new()
.create(true)
.write(true)
.open(&self.data_path)?;
Ok(serde_json::to_writer(outfile, &self.data)?)
}
}
#[allow(unused)]
#[derive(Clone)]
pub struct MediaResolver<'a> {
root_folder: PathBuf,
media: &'a HashMap<UrlString, String>,
}
impl<'a> MediaResolver<'a> {
pub fn resolve(&self, url: &str) -> Option<String> {
// if we're on windows, we just return the URL. Somehow the file locating
// trick we use with Dioxus doesn't work on Windows
#[cfg(target_os = "windows")]
{
Some(url.to_string())
}
#[cfg(not(target_os = "windows"))]
{
let found = self.media.get(url)?;
let path = self.root_folder.join(found);
Some(path.display().to_string())
}
}
}