rename statuses to notes
This matches the ActivityPub terminology, so why not do it as long as it's still possible to arbitrarily change the database schema.
This commit is contained in:
parent
ef911481a3
commit
c227d09af3
11 changed files with 78 additions and 80 deletions
|
@ -1,4 +1,4 @@
|
|||
CREATE TABLE statuses (
|
||||
CREATE TABLE notes (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
account_id BIGINT REFERENCES accounts(id),
|
||||
uri VARCHAR,
|
||||
|
@ -9,4 +9,4 @@ CREATE TABLE statuses (
|
|||
updated_at TIMESTAMP NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX index_statuses_on_created_at ON statuses (created_at);
|
||||
CREATE INDEX index_notes_on_created_at ON notes (created_at);
|
|
@ -1,9 +1,9 @@
|
|||
pub mod account;
|
||||
pub mod follow;
|
||||
pub mod status;
|
||||
pub mod note;
|
||||
pub mod user;
|
||||
|
||||
pub use account::*;
|
||||
pub use follow::*;
|
||||
pub use status::*;
|
||||
pub use note::*;
|
||||
pub use user::*;
|
||||
|
|
3
src/data/note/mod.rs
Normal file
3
src/data/note/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod pg;
|
||||
|
||||
pub use pg::PgNoteDataSource;
|
|
@ -1,38 +1,37 @@
|
|||
use chrono::prelude::*;
|
||||
use serde;
|
||||
use sqlx::{Executor, PgPool};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::core::*;
|
||||
use crate::model::status::{NewStatus, Status};
|
||||
use crate::model::note::{NewNote, Note};
|
||||
|
||||
pub struct PgStatusDataSource {
|
||||
pub struct PgNoteDataSource {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PgStatusDataSource {
|
||||
pub fn new(pool: PgPool) -> PgStatusDataSource {
|
||||
PgStatusDataSource { pool }
|
||||
impl PgNoteDataSource {
|
||||
pub fn new(pool: PgPool) -> PgNoteDataSource {
|
||||
PgNoteDataSource { pool }
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<Status>
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<Note>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
let id: Id = id.into();
|
||||
let status: Status = sqlx::query_as("SELECT * FROM statuses WHERE id = $1")
|
||||
let note: Note = sqlx::query_as("SELECT * FROM notes WHERE id = $1")
|
||||
.bind(id)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(status)
|
||||
Ok(note)
|
||||
}
|
||||
|
||||
pub async fn by_account<I>(&self, id: I, since: NaiveDateTime) -> Result<Vec<Status>>
|
||||
pub async fn by_account<I>(&self, id: I, since: NaiveDateTime) -> Result<Vec<Note>>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
let id: Id = id.into();
|
||||
let statuses: Vec<Status> = sqlx::query_as(
|
||||
"SELECT * FROM statuses \
|
||||
let notes: Vec<Note> = sqlx::query_as(
|
||||
"SELECT * FROM notes \
|
||||
WHERE account_id = $1 AND created_at < $1 \
|
||||
ORDER BY created_at \
|
||||
LIMIT 20",
|
||||
|
@ -41,16 +40,16 @@ impl PgStatusDataSource {
|
|||
.bind(since)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
Ok(statuses)
|
||||
Ok(notes)
|
||||
}
|
||||
|
||||
pub async fn create<T>(&self, new: T) -> Result<Status>
|
||||
pub async fn create<T>(&self, new: T) -> Result<Note>
|
||||
where
|
||||
T: Into<NewStatus> + Send,
|
||||
T: Into<NewNote> + Send,
|
||||
{
|
||||
let new = new.into();
|
||||
let status: Status = sqlx::query_as(
|
||||
"INSERT INTO statuses ( \
|
||||
let note: Note = sqlx::query_as(
|
||||
"INSERT INTO notes ( \
|
||||
account_id, \
|
||||
uri, \
|
||||
content, \
|
||||
|
@ -65,6 +64,6 @@ impl PgStatusDataSource {
|
|||
.bind(new.sensitive)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(status)
|
||||
Ok(note)
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
mod pg;
|
||||
|
||||
pub use pg::PgStatusDataSource;
|
|
@ -1,9 +1,9 @@
|
|||
pub mod account;
|
||||
pub mod follow;
|
||||
pub mod status;
|
||||
pub mod note;
|
||||
pub mod user;
|
||||
|
||||
pub use account::{Account, NewAccount};
|
||||
pub use follow::{Follow, NewFollow};
|
||||
pub use status::{NewStatus, Status};
|
||||
pub use note::{NewNote, Note};
|
||||
pub use user::{NewUser, User};
|
||||
|
|
|
@ -5,7 +5,7 @@ use sqlx::FromRow;
|
|||
use crate::core::*;
|
||||
|
||||
#[derive(Deserialize, Serialize, FromRow)]
|
||||
pub struct Status {
|
||||
pub struct Note {
|
||||
pub id: Id,
|
||||
pub account_id: Id,
|
||||
pub uri: Option<String>,
|
||||
|
@ -16,7 +16,7 @@ pub struct Status {
|
|||
pub updated_at: NaiveDateTime,
|
||||
}
|
||||
|
||||
pub struct NewStatus {
|
||||
pub struct NewNote {
|
||||
pub account_id: Id,
|
||||
pub uri: String,
|
||||
pub content: String,
|
||||
|
@ -24,8 +24,8 @@ pub struct NewStatus {
|
|||
pub sensitive: bool,
|
||||
}
|
||||
|
||||
impl From<Status> for Id {
|
||||
fn from(status: Status) -> Id {
|
||||
impl From<Note> for Id {
|
||||
fn from(status: Note) -> Id {
|
||||
status.id
|
||||
}
|
||||
}
|
|
@ -2,11 +2,11 @@ use sqlx::PgPool;
|
|||
|
||||
pub mod account;
|
||||
pub mod follow;
|
||||
pub mod status;
|
||||
pub mod note;
|
||||
pub mod user;
|
||||
use account::AccountRepo;
|
||||
use follow::FollowRepo;
|
||||
use status::StatusRepo;
|
||||
use note::NoteRepo;
|
||||
use user::UserRepo;
|
||||
|
||||
/// The central collection of all data accessible to the app.
|
||||
|
@ -15,7 +15,7 @@ use user::UserRepo;
|
|||
pub struct AppRepo {
|
||||
pub accounts: AccountRepo,
|
||||
pub follows: FollowRepo,
|
||||
pub statuses: StatusRepo,
|
||||
pub notes: NoteRepo,
|
||||
pub users: UserRepo,
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ impl AppRepo {
|
|||
AppRepo {
|
||||
accounts: AccountRepo::new(db_pool.clone()),
|
||||
follows: FollowRepo::new(db_pool.clone()),
|
||||
statuses: StatusRepo::new(db_pool.clone()),
|
||||
notes: NoteRepo::new(db_pool.clone()),
|
||||
users: UserRepo::new(db_pool.clone()),
|
||||
}
|
||||
}
|
||||
|
|
39
src/repo/note.rs
Normal file
39
src/repo/note.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use chrono::NaiveDateTime;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::core::*;
|
||||
use crate::data::PgNoteDataSource;
|
||||
use crate::model::{NewNote, Note};
|
||||
|
||||
pub struct NoteRepo {
|
||||
db: PgNoteDataSource,
|
||||
}
|
||||
|
||||
impl NoteRepo {
|
||||
pub fn new(db_pool: PgPool) -> NoteRepo {
|
||||
NoteRepo {
|
||||
db: PgNoteDataSource::new(db_pool),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<Note>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_id(id).await
|
||||
}
|
||||
|
||||
pub async fn by_account<I>(&self, account_id: I, since: NaiveDateTime) -> Result<Vec<Note>>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_account(account_id, since).await
|
||||
}
|
||||
|
||||
pub async fn create<T>(&self, new: T) -> Result<Note>
|
||||
where
|
||||
T: Into<NewNote> + Send,
|
||||
{
|
||||
self.db.create(new).await
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
use chrono::NaiveDateTime;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::core::*;
|
||||
use crate::data::PgStatusDataSource;
|
||||
use crate::model::{NewStatus, Status};
|
||||
|
||||
pub struct StatusRepo {
|
||||
db: PgStatusDataSource,
|
||||
}
|
||||
|
||||
impl StatusRepo {
|
||||
pub fn new(db_pool: PgPool) -> StatusRepo {
|
||||
StatusRepo {
|
||||
db: PgStatusDataSource::new(db_pool),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<Status>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_id(id).await
|
||||
}
|
||||
|
||||
pub async fn by_account<I>(&self, account_id: I, since: NaiveDateTime) -> Result<Vec<Status>>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_account(account_id, since).await
|
||||
}
|
||||
|
||||
pub async fn create<T>(&self, new: T) -> Result<Status>
|
||||
where
|
||||
T: Into<NewStatus> + Send,
|
||||
{
|
||||
self.db.create(new).await
|
||||
}
|
||||
}
|
|
@ -12,14 +12,13 @@ async fn get_by_id(path: web::Path<Id>, state: AppState) -> Result<HttpResponse>
|
|||
}
|
||||
|
||||
#[get("/{id}/statuses")]
|
||||
async fn get_statuses(path: web::Path<Id>, state: AppState) -> Result<HttpResponse> {
|
||||
async fn get_notes(path: web::Path<Id>, state: AppState) -> Result<HttpResponse> {
|
||||
let id = path.into_inner();
|
||||
let account = state.repo.accounts.by_id(id).await?;
|
||||
let statuses = state.repo.statuses.by_account(account, utc_now()).await?;
|
||||
Ok(HttpResponse::Ok().json(statuses))
|
||||
let notes = state.repo.notes.by_account(account, utc_now()).await?;
|
||||
Ok(HttpResponse::Ok().json(notes))
|
||||
}
|
||||
|
||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(get_by_id);
|
||||
cfg.service(get_statuses);
|
||||
cfg.service(get_by_id).service(get_notes);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue