Begin support for comment visibility

Add public_visibility column to comment model
Add a table to map comments to possible seers
Only show public comment at the moment
pull/364/head
Trinity Pointard 5 years ago
parent 9e799f2cf2
commit 141931fb94

@ -0,0 +1,4 @@
-- This file should undo anything in `up.sql`
ALTER TABLE comments DROP COLUMN public_visibility;
DROP TABLE comment_seers;

@ -0,0 +1,9 @@
-- Your SQL goes here
ALTER TABLE comments ADD public_visibility BOOLEAN NOT NULL DEFAULT 't';
CREATE TABLE comment_seers (
id SERIAL PRIMARY KEY,
comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
UNIQUE (comment_id, user_id)
)

@ -0,0 +1,18 @@
-- This file should undo anything in `up.sql`
CREATE TABLE comments2 (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL DEFAULT '',
in_response_to_id INTEGER REFERENCES comments(id),
post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE NOT NULL,
author_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
creation_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
ap_url VARCHAR,
sensitive BOOLEAN NOT NULL DEFAULT 'f',
spoiler_text TEXT NOT NULL DEFAULT ''
);
INSERT INTO comments2 SELECT id,content,in_response_to_id,post_id,author_id,creation_date,ap_url,sensitive,spoiler_text FROM comments;
DROP TABLE comments;
ALTER TABLE comments2 RENAME TO comments;
DROP TABLE comment_seers;

@ -0,0 +1,9 @@
-- Your SQL goes here
ALTER TABLE comments ADD public_visibility BOOLEAN NOT NULL DEFAULT 't';
CREATE TABLE comment_seers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE NOT NULL,
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL,
UNIQUE (comment_id, user_id)
)

@ -28,6 +28,7 @@ pub struct Comment {
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String,
pub public_visibility: bool,
}
#[derive(Insertable, Default)]
@ -40,6 +41,7 @@ pub struct NewComment {
pub ap_url: Option<String>,
pub sensitive: bool,
pub spoiler_text: String,
pub public_visibility: bool,
}
impl Comment {
@ -179,12 +181,25 @@ impl FromActivity<Note, Connection> for Comment {
let previous_url = note
.object_props
.in_reply_to
.clone()
.as_ref()
.expect("Comment::from_activity: not an answer error");
let previous_url = previous_url
.as_str()
.expect("Comment::from_activity: in_reply_to parsing error");
let previous_comment = Comment::find_by_ap_url(conn, previous_url);
let public_visibility = note.object_props.to.as_ref().map(|to| to.as_array()
.ok_or_else(|| note.object_props.to.as_ref().unwrap().as_str())
.map(|v| v.iter().any(|to| to.as_str().map(|to| to==PUBLIC_VISIBILTY).unwrap_or(false)))
.unwrap_or_else(|to| to.map(|to| to==PUBLIC_VISIBILTY).unwrap_or(false))
).unwrap_or(false);
let public_visibility = public_visibility || note.object_props.cc.as_ref().map(|to| to.as_array()
.ok_or_else(|| note.object_props.cc.as_ref().unwrap().as_str())
.map(|v| v.iter().any(|to| to.as_str().map(|to| to==PUBLIC_VISIBILTY).unwrap_or(false)))
.unwrap_or_else(|to| to.map(|to| to==PUBLIC_VISIBILTY).unwrap_or(false))
).unwrap_or(false);
let comm = Comment::insert(
conn,
@ -210,6 +225,7 @@ impl FromActivity<Note, Connection> for Comment {
.expect("Comment::from_activity: author error")
.id,
sensitive: false, // "sensitive" is not a standard property, we need to think about how to support it with the activitypub crate
public_visibility
},
);

@ -57,6 +57,15 @@ table! {
ap_url -> Nullable<Varchar>,
sensitive -> Bool,
spoiler_text -> Text,
public_visibility -> Bool,
}
}
table! {
comment_seers (id) {
id -> Int4,
comment_id -> Int4,
user_id -> Int4,
}
}
@ -201,6 +210,8 @@ joinable!(api_tokens -> users (user_id));
joinable!(blog_authors -> blogs (blog_id));
joinable!(blog_authors -> users (author_id));
joinable!(blogs -> instances (instance_id));
joinable!(comment_seers -> comments (comment_id));
joinable!(comment_seers -> users (user_id));
joinable!(comments -> posts (post_id));
joinable!(comments -> users (author_id));
joinable!(likes -> posts (post_id));
@ -224,6 +235,7 @@ allow_tables_to_appear_in_same_query!(
blog_authors,
blogs,
comments,
comment_seers,
follows,
instances,
likes,

@ -43,7 +43,8 @@ pub fn create(blog_name: String, slug: String, form: LenientForm<NewCommentForm>
author_id: user.id,
ap_url: None,
sensitive: !form.warning.is_empty(),
spoiler_text: form.warning.clone()
spoiler_text: form.warning.clone(),
public_visibility: true
}).update_ap_url(&*conn);
let new_comment = comm.create_activity(&*conn);

@ -5,6 +5,7 @@
@(ctx: BaseContext, comm: &Comment, author: User, in_reply_to: Option<&str>)
@if comm.public_visibility {
<div class="comment u-comment h-cite" id="comment-@comm.id">
<a class="author u-author h-card" href="@uri!(user::details: name = author.get_fqn(ctx.0))">
@avatar(ctx.0, &author, Size::Small, true, ctx.1)
@ -28,6 +29,9 @@
}
</div>
<a class="button icon icon-message-circle" href="?responding_to=@comm.id">@i18n!(ctx.1, "Respond")</a>
} else {
<div class="comment">
}
@for res in comm.get_responses(ctx.0) {
@:comment(ctx, &res, res.get_author(ctx.0), comm.ap_url.as_ref().map(|u| &**u))
}

Loading…
Cancel
Save