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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use super::apply_observation;
use crate::db::PlacesDb;
use crate::error::*;
use crate::observation::VisitObservation;
use crate::types::*;
use types::Timestamp;
use url::Url;
pub fn can_add_url(_url: &Url) -> Result<bool> {
Ok(true)
}
#[derive(Debug)]
pub struct AddablePlaceInfo {
pub url: Url,
pub title: Option<String>,
pub visits: Vec<AddableVisit>,
}
#[derive(Debug)]
pub struct AddableVisit {
pub date: Timestamp,
pub transition: VisitTransition,
pub referrer: Option<Url>,
pub is_local: bool,
}
pub fn insert(conn: &mut PlacesDb, place: AddablePlaceInfo) -> Result<()> {
for v in place.visits {
let obs = VisitObservation::new(place.url.clone())
.with_visit_type(v.transition)
.with_at(v.date)
.with_title(place.title.clone())
.with_is_remote(!v.is_local);
apply_observation(conn, obs)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::places_api::test::new_mem_connection;
use rusqlite::NO_PARAMS;
#[test]
fn test_insert() {
let mut c = new_mem_connection();
let url = Url::parse("http://example.com").expect("it's a valid url");
let date = Timestamp::now();
let visits = vec![AddableVisit {
date,
transition: VisitTransition::Link,
referrer: None,
is_local: true,
}];
let a = AddablePlaceInfo {
url,
title: None,
visits,
};
insert(&mut c, a).expect("should insert");
let sql = "SELECT p.id, p.url, p.title,
p.visit_count_local, p.visit_count_remote,
p.hidden, p.typed, p.frecency,
p.last_visit_date_local, p.last_visit_date_remote,
p.guid, p.foreign_count, p.url_hash, p.description,
p.preview_image_url, p.origin_id,
v.is_local, v.from_visit, v.place_id,
v.visit_date, v.visit_type
FROM moz_places p, moz_historyvisits v
WHERE v.place_id = p.id";
let mut stmt = c.db.prepare(sql).expect("valid sql");
let mut rows = stmt.query(NO_PARAMS).expect("should execute");
let result = rows.next().expect("should get a row");
let row = result.expect("expect anything");
assert_eq!(
row.get::<_, String>("url").expect("should work"),
"http://example.com/"
);
assert_eq!(
row.get::<_, Timestamp>("visit_date").expect("should work"),
date
);
assert_ne!(row.get::<_, i32>("frecency").expect("should work"), 0);
}
}
fn is_recently_visited(_url: &Url) -> Result<bool> {
Ok(false)
}
fn add_recently_visited(_url: &Url) -> Result<()> {
Ok(())
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub enum RedirectSourceType {
Temporary,
Permanent,
}
pub fn visit_uri(
conn: &mut PlacesDb,
url: &Url,
last_url: Option<Url>,
transition: VisitTransition,
redirect_source: Option<RedirectSourceType>,
is_error_page: bool,
) -> Result<()> {
if !can_add_url(&url)? {
return Ok(());
};
if let Some(ref last) = last_url {
if url == last && is_recently_visited(url)? {
add_recently_visited(url)?;
return Ok(());
};
}
if transition == VisitTransition::Embed {
log::warn!("Embed visit, but in-memory storage of these isn't done yet");
return Ok(());
}
let obs = VisitObservation::new(url.clone())
.with_is_error(is_error_page)
.with_visit_type(transition)
.with_is_redirect_source(redirect_source.map(|_r| true))
.with_is_permanent_redirect_source(
redirect_source.map(|r| r == RedirectSourceType::Permanent),
);
apply_observation(conn, obs)
}