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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// Copyright (C) 2018-2021 Vincent Ambo <tazjin@tvl.su>
//
// This file is part of Converse.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/>.
//! This module implements the database executor, which holds the
//! database connection and performs queries on it.
use crate::errors::{ConverseError, Result};
use crate::models::*;
use actix::prelude::*;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sql_types::Text;
use diesel::{self, sql_query};
/// Raw PostgreSQL query used to perform full-text search on posts
/// with a supplied phrase. For now, the query language is hardcoded
/// to English and only "plain" queries (i.e. no searches for exact
/// matches or more advanced query syntax) are supported.
const SEARCH_QUERY: &'static str = r#"
WITH search_query (query) AS (VALUES (plainto_tsquery('english', $1)))
SELECT post_id,
thread_id,
author,
title,
ts_headline('english', body, query) AS headline
FROM search_index, search_query
WHERE document @@ query
ORDER BY ts_rank(document, query) DESC
LIMIT 50
"#;
const REFRESH_QUERY: &'static str = "REFRESH MATERIALIZED VIEW search_index";
pub struct DbExecutor(pub Pool<ConnectionManager<PgConnection>>);
impl DbExecutor {
/// Request a list of threads.
// TODO(tazjin): This should support pagination.
pub fn list_threads(&self) -> Result<Vec<ThreadIndex>> {
use crate::schema::thread_index::dsl::*;
let conn = self.0.get()?;
let results = thread_index.load::<ThreadIndex>(&conn)?;
Ok(results)
}
/// Look up a user based on their email-address. If the user does
/// not exist, it is created.
pub fn lookup_or_create_user(&self, user_email: &str, user_name: &str) -> Result<User> {
use crate::schema::users;
use crate::schema::users::dsl::*;
let conn = self.0.get()?;
let opt_user = users.filter(email.eq(email)).first(&conn).optional()?;
if let Some(user) = opt_user {
Ok(user)
} else {
let new_user = NewUser {
email: user_email.to_string(),
name: user_name.to_string(),
};
let user: User = diesel::insert_into(users::table)
.values(&new_user)
.get_result(&conn)?;
info!("Created new user {} with ID {}", new_user.email, user.id);
Ok(user)
}
}
/// Fetch a specific thread and return it with its posts.
pub fn get_thread(&self, thread_id: i32) -> Result<(Thread, Vec<SimplePost>)> {
use crate::schema::simple_posts::dsl::id;
use crate::schema::threads::dsl::*;
let conn = self.0.get()?;
let thread_result: Thread = threads.find(thread_id).first(&conn)?;
let post_list = SimplePost::belonging_to(&thread_result)
.order_by(id.asc())
.load::<SimplePost>(&conn)?;
Ok((thread_result, post_list))
}
/// Fetch a specific post.
pub fn get_post(&self, post_id: i32) -> Result<SimplePost> {
use crate::schema::simple_posts::dsl::*;
let conn = self.0.get()?;
Ok(simple_posts.find(post_id).first(&conn)?)
}
/// Update the content of a post.
pub fn update_post(&self, post_id: i32, post_text: String) -> Result<Post> {
use crate::schema::posts::dsl::*;
let conn = self.0.get()?;
let updated = diesel::update(posts.find(post_id))
.set(body.eq(post_text))
.get_result(&conn)?;
Ok(updated)
}
/// Create a new thread.
pub fn create_thread(&self, new_thread: NewThread, post_text: String) -> Result<Thread> {
use crate::schema::{posts, threads};
let conn = self.0.get()?;
conn.transaction::<Thread, ConverseError, _>(|| {
// First insert the thread structure itself
let thread: Thread = diesel::insert_into(threads::table)
.values(&new_thread)
.get_result(&conn)?;
// ... then create the first post in the thread.
let new_post = NewPost {
thread_id: thread.id,
body: post_text,
user_id: new_thread.user_id,
};
diesel::insert_into(posts::table)
.values(&new_post)
.execute(&conn)?;
Ok(thread)
})
}
/// Create a new post.
pub fn create_post(&self, new_post: NewPost) -> Result<Post> {
use crate::schema::posts;
let conn = self.0.get()?;
let closed: bool = {
use crate::schema::threads::dsl::*;
threads
.select(closed)
.find(new_post.thread_id)
.first(&conn)?
};
if closed {
return Err(ConverseError::ThreadClosed {
id: new_post.thread_id,
});
}
Ok(diesel::insert_into(posts::table)
.values(&new_post)
.get_result(&conn)?)
}
/// Search for posts.
pub fn search_posts(&self, query: String) -> Result<Vec<SearchResult>> {
let conn = self.0.get()?;
let search_results = sql_query(SEARCH_QUERY)
.bind::<Text, _>(query)
.get_results::<SearchResult>(&conn)?;
Ok(search_results)
}
/// Trigger a refresh of the view used for full-text searching.
pub fn refresh_search_view(&self) -> Result<()> {
let conn = self.0.get()?;
debug!("Refreshing search_index view in DB");
sql_query(REFRESH_QUERY).execute(&conn)?;
Ok(())
}
}
// Old actor implementation:
impl Actor for DbExecutor {
type Context = SyncContext<Self>;
}
/// Message used to look up a user based on their email-address. If
/// the user does not exist, it is created.
pub struct LookupOrCreateUser {
pub email: String,
pub name: String,
}
message!(LookupOrCreateUser, Result<User>);
impl Handler<LookupOrCreateUser> for DbExecutor {
type Result = <LookupOrCreateUser as Message>::Result;
fn handle(&mut self, _: LookupOrCreateUser, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to fetch a specific thread. Returns the thread and
/// its posts.
pub struct GetThread(pub i32);
message!(GetThread, Result<(Thread, Vec<SimplePost>)>);
impl Handler<GetThread> for DbExecutor {
type Result = <GetThread as Message>::Result;
fn handle(&mut self, _: GetThread, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to fetch a specific post.
#[derive(Deserialize, Debug)]
pub struct GetPost {
pub id: i32,
}
message!(GetPost, Result<SimplePost>);
impl Handler<GetPost> for DbExecutor {
type Result = <GetPost as Message>::Result;
fn handle(&mut self, _: GetPost, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to update the content of a post.
#[derive(Deserialize)]
pub struct UpdatePost {
pub post_id: i32,
pub post: String,
}
message!(UpdatePost, Result<Post>);
impl Handler<UpdatePost> for DbExecutor {
type Result = Result<Post>;
fn handle(&mut self, _: UpdatePost, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to create a new thread
pub struct CreateThread {
pub new_thread: NewThread,
pub post: String,
}
message!(CreateThread, Result<Thread>);
impl Handler<CreateThread> for DbExecutor {
type Result = <CreateThread as Message>::Result;
fn handle(&mut self, _: CreateThread, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to create a new reply
pub struct CreatePost(pub NewPost);
message!(CreatePost, Result<Post>);
impl Handler<CreatePost> for DbExecutor {
type Result = <CreatePost as Message>::Result;
fn handle(&mut self, _: CreatePost, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message used to search for posts
#[derive(Deserialize)]
pub struct SearchPosts {
pub query: String,
}
message!(SearchPosts, Result<Vec<SearchResult>>);
impl Handler<SearchPosts> for DbExecutor {
type Result = <SearchPosts as Message>::Result;
fn handle(&mut self, _: SearchPosts, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
/// Message that triggers a refresh of the view used for full-text
/// searching.
pub struct RefreshSearchView;
message!(RefreshSearchView, Result<()>);
impl Handler<RefreshSearchView> for DbExecutor {
type Result = Result<()>;
fn handle(&mut self, _: RefreshSearchView, _: &mut Self::Context) -> Self::Result {
unimplemented!()
}
}
|