Add a page to list articles by tag
This commit is contained in:
@@ -59,6 +59,27 @@ impl Post {
|
||||
find_by!(posts, find_by_slug, slug as String, blog_id as i32);
|
||||
find_by!(posts, find_by_ap_url, ap_url as String);
|
||||
|
||||
pub fn list_by_tag(conn: &PgConnection, tag: String, (min, max): (i32, i32)) -> Vec<Post> {
|
||||
use schema::tags;
|
||||
|
||||
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
|
||||
posts::table.filter(posts::id.eq(any(ids)))
|
||||
.order(posts::creation_date.desc())
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.get_results::<Post>(conn)
|
||||
.expect("Error loading posts by tag")
|
||||
}
|
||||
|
||||
pub fn count_for_tag(conn: &PgConnection, tag: String) -> i64 {
|
||||
use schema::tags;
|
||||
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
|
||||
posts::table.filter(posts::id.eq(any(ids)))
|
||||
.count()
|
||||
.get_result(conn)
|
||||
.expect("Error counting posts by tag")
|
||||
}
|
||||
|
||||
pub fn count_local(conn: &PgConnection) -> usize {
|
||||
use schema::post_authors;
|
||||
use schema::users;
|
||||
@@ -220,7 +241,7 @@ impl Post {
|
||||
article.object_props.set_content_string(self.content.get().clone()).expect("Article::into_activity: content error");
|
||||
article.object_props.set_published_utctime(Utc.from_utc_datetime(&self.creation_date)).expect("Article::into_activity: published error");
|
||||
article.object_props.set_summary_string(self.subtitle.clone()).expect("Article::into_activity: summary error");
|
||||
article.object_props.tag = json!(mentions_json.append(&mut tags_json));
|
||||
article.object_props.tag = Some(json!(mentions_json.append(&mut tags_json)));
|
||||
article.object_props.set_url_string(self.ap_url.clone()).expect("Article::into_activity: url error");
|
||||
article.object_props.set_to_link_vec::<Id>(to.into_iter().map(Id::new).collect()).expect("Article::into_activity: to error");
|
||||
article.object_props.set_cc_link_vec::<Id>(vec![]).expect("Article::into_activity: cc error");
|
||||
@@ -300,11 +321,11 @@ impl FromActivity<Article, PgConnection> for Post {
|
||||
// save mentions and tags
|
||||
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() {
|
||||
for tag in tags.into_iter() {
|
||||
serde_json::from_value::<link::Mention>(tag)
|
||||
serde_json::from_value::<link::Mention>(tag.clone())
|
||||
.map(|m| Mention::from_activity(conn, m, post.id, true))
|
||||
.ok();
|
||||
|
||||
serde_json::from_value::<Hashtag>(tag)
|
||||
serde_json::from_value::<Hashtag>(tag.clone())
|
||||
.map(|t| Tag::from_activity(conn, t, post.id))
|
||||
.ok();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use ap_url;
|
||||
use instance::Instance;
|
||||
use schema::tags;
|
||||
|
||||
#[derive(Serialize, Queryable)]
|
||||
#[derive(Serialize, Queryable, Clone)]
|
||||
pub struct Tag {
|
||||
pub id: i32,
|
||||
pub tag: String,
|
||||
@@ -24,12 +24,13 @@ pub struct NewTag {
|
||||
impl Tag {
|
||||
insert!(tags, NewTag);
|
||||
get!(tags);
|
||||
find_by!(tags, find_by_name, tag as String);
|
||||
list_by!(tags, for_post, post_id as i32);
|
||||
|
||||
pub fn into_activity(&self, conn: &PgConnection) -> Hashtag {
|
||||
let ht = Hashtag::default();
|
||||
let mut ht = Hashtag::default();
|
||||
ht.set_href_string(ap_url(format!("{}/tag/{}", Instance::get_local(conn).unwrap().public_domain, self.tag))).expect("Tag::into_activity: href error");
|
||||
ht.set_name_string(self.tag).expect("Tag::into_activity: name error");
|
||||
ht.set_name_string(self.tag.clone()).expect("Tag::into_activity: name error");
|
||||
ht
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user