+22
-26
@@ -43,6 +43,7 @@ pub struct Blog {
|
||||
pub ap_url: String,
|
||||
pub private_key: Option<String>,
|
||||
pub public_key: String,
|
||||
pub fqn: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
@@ -87,6 +88,15 @@ impl Blog {
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
if inserted.fqn.is_empty() {
|
||||
if instance.local {
|
||||
inserted.fqn = inserted.actor_id.clone();
|
||||
} else {
|
||||
inserted.fqn = format!("{}@{}", inserted.actor_id, instance.public_domain);
|
||||
}
|
||||
}
|
||||
|
||||
inserted.save_changes(conn).map_err(Error::from)
|
||||
});
|
||||
get!(blogs);
|
||||
@@ -129,19 +139,17 @@ impl Blog {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn find_local(conn: &Connection, name: &str) -> Result<Blog> {
|
||||
Blog::find_by_name(conn, name, Instance::get_local(conn)?.id)
|
||||
}
|
||||
|
||||
pub fn find_by_fqn(conn: &Connection, fqn: &str) -> Result<Blog> {
|
||||
let mut split_fqn = fqn.split('@');
|
||||
let actor = split_fqn.next().ok_or(Error::InvalidValue)?;
|
||||
if let Some(domain) = split_fqn.next() { // remote blog
|
||||
Instance::find_by_domain(conn, domain)
|
||||
.and_then(|instance| Blog::find_by_name(conn, actor, instance.id))
|
||||
.or_else(|_| Blog::fetch_from_webfinger(conn, fqn))
|
||||
} else { // local blog
|
||||
Blog::find_local(conn, actor)
|
||||
let from_db = blogs::table
|
||||
.filter(blogs::fqn.eq(fqn))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)?
|
||||
.into_iter()
|
||||
.next();
|
||||
if let Some(from_db) = from_db {
|
||||
Ok(from_db)
|
||||
} else {
|
||||
Blog::fetch_from_webfinger(conn, fqn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,18 +346,6 @@ impl Blog {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_fqn(&self, conn: &Connection) -> String {
|
||||
if self.instance_id == Instance::get_local(conn).ok().expect("Blog::get_fqn: local instance error").id {
|
||||
self.actor_id.clone()
|
||||
} else {
|
||||
format!(
|
||||
"{}@{}",
|
||||
self.actor_id,
|
||||
self.get_instance(conn).ok().expect("Blog::get_fqn: instance error").public_domain
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&self, conn: &Connection, searcher: &Searcher) -> Result<()> {
|
||||
for post in Post::get_for_blog(conn, &self)? {
|
||||
post.delete(&(conn, searcher))?;
|
||||
@@ -649,7 +645,7 @@ pub(crate) mod tests {
|
||||
).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
Blog::find_local(conn, "SomeName").unwrap().id,
|
||||
Blog::find_by_fqn(conn, "SomeName").unwrap().id,
|
||||
blog.id
|
||||
);
|
||||
|
||||
@@ -673,7 +669,7 @@ pub(crate) mod tests {
|
||||
).unwrap(),
|
||||
).unwrap();
|
||||
|
||||
assert_eq!(blog.get_fqn(conn), "SomeName");
|
||||
assert_eq!(blog.fqn, "SomeName");
|
||||
|
||||
Ok(())
|
||||
});
|
||||
|
||||
@@ -71,7 +71,7 @@ impl Mention {
|
||||
.set_href_string(user.ap_url.clone())?;
|
||||
mention
|
||||
.link_props
|
||||
.set_name_string(format!("@{}", user.get_fqn(conn)))?;
|
||||
.set_name_string(format!("@{}", user.fqn))?;
|
||||
Ok(mention)
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ impl Notification {
|
||||
pub fn get_url(&self, conn: &Connection) -> Option<String> {
|
||||
match self.kind.as_ref() {
|
||||
notification_kind::COMMENT => self.get_post(conn).and_then(|p| Some(format!("{}#comment-{}", p.url(conn).ok()?, self.object_id))),
|
||||
notification_kind::FOLLOW => Some(format!("/@/{}/", self.get_actor(conn).ok()?.get_fqn(conn))),
|
||||
notification_kind::FOLLOW => Some(format!("/@/{}/", self.get_actor(conn).ok()?.fqn)),
|
||||
notification_kind::MENTION => Mention::get(conn, self.object_id).and_then(|mention|
|
||||
mention.get_post(conn).and_then(|p| p.url(conn))
|
||||
.or_else(|_| {
|
||||
|
||||
@@ -269,7 +269,7 @@ impl Post {
|
||||
post.ap_url = ap_url(&format!(
|
||||
"{}/~/{}/{}/",
|
||||
*BASE_URL,
|
||||
post.get_blog(conn)?.get_fqn(conn),
|
||||
post.get_blog(conn)?.fqn,
|
||||
post.slug
|
||||
));
|
||||
let _: Post = post.save_changes(conn)?;
|
||||
@@ -850,7 +850,7 @@ impl Post {
|
||||
|
||||
pub fn url(&self, conn: &Connection) -> Result<String> {
|
||||
let blog = self.get_blog(conn)?;
|
||||
Ok(format!("/~/{}/{}", blog.get_fqn(conn), self.slug))
|
||||
Ok(format!("/~/{}/{}", blog.fqn, self.slug))
|
||||
}
|
||||
|
||||
pub fn cover_url(&self, conn: &Connection) -> Option<String> {
|
||||
|
||||
@@ -43,6 +43,7 @@ table! {
|
||||
ap_url -> Text,
|
||||
private_key -> Nullable<Text>,
|
||||
public_key -> Text,
|
||||
fqn -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,6 +202,7 @@ table! {
|
||||
followers_endpoint -> Varchar,
|
||||
avatar_id -> Nullable<Int4>,
|
||||
last_fetched_date -> Timestamp,
|
||||
fqn -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -144,7 +144,7 @@ impl Searcher {
|
||||
let writer = writer.as_mut().unwrap();
|
||||
writer.add_document(doc!(
|
||||
post_id => i64::from(post.id),
|
||||
author => post.get_authors(conn)?.into_iter().map(|u| u.get_fqn(conn)).join(" "),
|
||||
author => post.get_authors(conn)?.into_iter().map(|u| u.fqn).join(" "),
|
||||
creation_date => i64::from(post.creation_date.num_days_from_ce()),
|
||||
instance => Instance::get(conn, post.get_blog(conn)?.instance_id)?.public_domain.clone(),
|
||||
tag => Tag::for_post(conn, post.id)?.into_iter().map(|t| t.tag).join(" "),
|
||||
|
||||
+23
-29
@@ -64,6 +64,7 @@ pub struct User {
|
||||
pub followers_endpoint: String,
|
||||
pub avatar_id: Option<i32>,
|
||||
pub last_fetched_date: NaiveDateTime,
|
||||
pub fqn: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
@@ -119,8 +120,7 @@ impl User {
|
||||
if inserted.shared_inbox_url.is_none() {
|
||||
inserted.shared_inbox_url = Some(ap_url(&format!(
|
||||
"{}/inbox",
|
||||
Instance::get_local(conn)?
|
||||
.public_domain
|
||||
instance.public_domain
|
||||
)));
|
||||
}
|
||||
|
||||
@@ -132,6 +132,14 @@ impl User {
|
||||
);
|
||||
}
|
||||
|
||||
if inserted.fqn.is_empty() {
|
||||
if instance.local {
|
||||
inserted.fqn = inserted.username.clone();
|
||||
} else {
|
||||
inserted.fqn = format!("{}@{}", inserted.username, instance.public_domain);
|
||||
}
|
||||
}
|
||||
|
||||
inserted.save_changes(conn).map_err(Error::from)
|
||||
});
|
||||
get!(users);
|
||||
@@ -218,19 +226,17 @@ impl User {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn find_local(conn: &Connection, username: &str) -> Result<User> {
|
||||
User::find_by_name(conn, username, Instance::get_local(conn)?.id)
|
||||
}
|
||||
|
||||
pub fn find_by_fqn(conn: &Connection, fqn: &str) -> Result<User> {
|
||||
let mut split_fqn = fqn.split('@');
|
||||
let username = split_fqn.next().ok_or(Error::InvalidValue)?;
|
||||
if let Some(domain) = split_fqn.next() { // remote user
|
||||
Instance::find_by_domain(conn, domain)
|
||||
.and_then(|instance| User::find_by_name(conn, username, instance.id))
|
||||
.or_else(|_| User::fetch_from_webfinger(conn, fqn))
|
||||
} else { // local user
|
||||
User::find_local(conn, username)
|
||||
let from_db = users::table
|
||||
.filter(users::fqn.eq(fqn))
|
||||
.limit(1)
|
||||
.load::<User>(conn)?
|
||||
.into_iter()
|
||||
.next();
|
||||
if let Some(from_db) = from_db {
|
||||
Ok(from_db)
|
||||
} else {
|
||||
User::fetch_from_webfinger(conn, fqn)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -518,18 +524,6 @@ impl User {
|
||||
.collect::<Vec<serde_json::Value>>())
|
||||
}
|
||||
|
||||
pub fn get_fqn(&self, conn: &Connection) -> String {
|
||||
if self.instance_id == Instance::get_local(conn).ok().expect("User::get_fqn: instance error").id {
|
||||
self.username.clone()
|
||||
} else {
|
||||
format!(
|
||||
"{}@{}",
|
||||
self.username,
|
||||
self.get_instance(conn).ok().expect("User::get_fqn: instance error").public_domain
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_followers(&self, conn: &Connection) -> Result<Vec<User>> {
|
||||
use schema::follows;
|
||||
let follows = Follow::belonging_to(self).select(follows::follower_id);
|
||||
@@ -805,11 +799,11 @@ impl User {
|
||||
(Utc::now().naive_utc() - self.last_fetched_date).num_days() > 1
|
||||
}
|
||||
|
||||
pub fn name(&self, conn: &Connection) -> String {
|
||||
pub fn name(&self) -> String {
|
||||
if !self.display_name.is_empty() {
|
||||
self.display_name.clone()
|
||||
} else {
|
||||
self.get_fqn(conn)
|
||||
self.fqn.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -987,7 +981,7 @@ pub(crate) mod tests {
|
||||
);
|
||||
assert_eq!(
|
||||
test_user.id,
|
||||
User::find_by_fqn(conn, &test_user.get_fqn(conn)).unwrap().id
|
||||
User::find_by_fqn(conn, &test_user.fqn).unwrap().id
|
||||
);
|
||||
assert_eq!(
|
||||
test_user.id,
|
||||
|
||||
Reference in New Issue
Block a user