Skip to content

armgit / ente

Public Imported from GitHub
0

๐Ÿ’š End-to-end encrypted cloud for everything.

Pad Space comment reactions and refine comment layout

Anand Baburajan8636871cc9af
diff --git a/rust/crates/space/src/client/comments.rs b/rust/crates/space/src/client/comments.rs
index 4949a5d1b8..5600d8f332 100644
--- a/rust/crates/space/src/client/comments.rs
+++ b/rust/crates/space/src/client/comments.rs
@@ -6,6 +6,8 @@ use crate::transport::CommentPageResponse;
use ente_core::{b64, crypto::SecretVec};
use serde::{Deserialize, Serialize};
+const REACTION_PAYLOAD_BYTES: usize = 256;
+
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DiscussionPayload {
@@ -18,13 +20,22 @@ struct DiscussionPayload {
}
fn encrypt_contribution(key: &[u8], payload: &DiscussionPayload) -> Result<String> {
- let bytes =
+ let mut bytes =
serde_json::to_vec(payload).map_err(|_| Error::InvalidInput("invalid comment".into()))?;
+ if payload.kind == "reaction" {
+ if bytes.len() > REACTION_PAYLOAD_BYTES {
+ return Err(Error::InvalidInput("reaction is too large".into()));
+ }
+ bytes.resize(REACTION_PAYLOAD_BYTES, b' ');
+ }
Ok(b64::encode(&encrypt_secretbox_payload(key, &bytes)?))
}
fn decrypt_contribution(key: &[u8], cipher: &str, expected: &DiscussionPayload) -> Result<String> {
let plaintext = decrypt_secretbox_payload(key, &b64::decode(cipher)?)?;
+ if expected.kind == "reaction" && plaintext.len() != REACTION_PAYLOAD_BYTES {
+ return Err(Error::InvalidInput("invalid reaction size".into()));
+ }
let payload: DiscussionPayload = serde_json::from_slice(&plaintext)
.map_err(|_| Error::InvalidInput("invalid comment".into()))?;
if payload.kind != expected.kind
@@ -461,6 +472,10 @@ mod tests {
text: "Hello Z".into(),
};
let cipher = encrypt_contribution(&post_key, &payload).unwrap();
+ assert_eq!(
+ decrypt_secretbox_payload(&post_key, &b64::decode(&cipher).unwrap()).unwrap(),
+ serde_json::to_vec(&payload).unwrap()
+ );
assert_eq!(
decrypt_contribution(&post_key, &cipher, &payload).unwrap(),
"Hello Z"
@@ -471,6 +486,63 @@ mod tests {
assert!(decrypt_secretbox_payload(&post_key, &private_profile).is_err());
}
+ #[test]
+ fn comment_reactions_are_padded_and_round_trip() {
+ let key = generate_key();
+ for emoji in ["๐Ÿ‘", "โค๏ธ", "๐Ÿ‘๐Ÿฝ", "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ", "๐Ÿ‡ฎ๐Ÿ‡ณ", "1๏ธโƒฃ", "๐Ÿณ๏ธโ€โšง๏ธ", "๐Ÿง‘๐Ÿฟโ€๐Ÿ’ป"]
+ {
+ let payload = DiscussionPayload {
+ kind: "reaction".into(),
+ post_id: i64::MAX,
+ comment_id: "a".repeat(64),
+ author_space_id: format!("space_{}", "a".repeat(22)),
+ reply_to_id: String::new(),
+ text: emoji.into(),
+ };
+ let cipher = encrypt_contribution(&key, &payload).unwrap();
+ assert_eq!(b64::decode(&cipher).unwrap().len(), 296, "{emoji}");
+ assert_eq!(
+ decrypt_contribution(&key, &cipher, &payload).unwrap(),
+ emoji
+ );
+ }
+ }
+
+ #[test]
+ fn comment_reactions_reject_invalid_payload_sizes() {
+ let key = generate_key();
+ let mut payload = DiscussionPayload {
+ kind: "reaction".into(),
+ post_id: i64::MAX,
+ comment_id: "a".repeat(64),
+ author_space_id: format!("space_{}", "a".repeat(22)),
+ reply_to_id: String::new(),
+ text: "โค๏ธ".into(),
+ };
+ let bytes = serde_json::to_vec(&payload).unwrap();
+ for size in [bytes.len(), 255, 257] {
+ let mut plaintext = bytes.clone();
+ plaintext.resize(size, b' ');
+ let cipher = b64::encode(&encrypt_secretbox_payload(&key, &plaintext).unwrap());
+ assert!(matches!(
+ decrypt_contribution(&key, &cipher, &payload),
+ Err(Error::InvalidInput(message)) if message == "invalid reaction size"
+ ));
+ }
+ payload.text = "๐Ÿ‘จ\u{200d}".repeat(8) + "๐Ÿ‘จ";
+ validate_contribution("reaction", &payload.text).unwrap();
+ assert!(matches!(
+ encrypt_contribution(&key, &payload),
+ Err(Error::InvalidInput(message)) if message == "reaction is too large"
+ ));
+ payload.kind = "comment".into();
+ let cipher = encrypt_contribution(&key, &payload).unwrap();
+ assert_eq!(
+ decrypt_contribution(&key, &cipher, &payload).unwrap(),
+ payload.text
+ );
+ }
+
#[test]
fn encrypted_contributions_bind_post_author_target_and_kind() {
let key = generate_key();
diff --git a/web/apps/space/src/components/PostComments.tsx b/web/apps/space/src/components/PostComments.tsx
index 09afde0b86..2a666db029 100644
--- a/web/apps/space/src/components/PostComments.tsx
+++ b/web/apps/space/src/components/PostComments.tsx
@@ -1,4 +1,4 @@
-import { ArrowLeft02Icon, SmilePlusIcon } from "@hugeicons/core-free-icons";
+import { ArrowLeft02Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import {
Box,
@@ -173,9 +173,7 @@ const CommentRow: React.FC<{
disabled: boolean;
ownReaction: string | undefined;
onReact: (emoji: string) => void;
- onAddReaction: () => void;
onOpenReactions: () => void;
- onReply: () => void;
onOpenActions: (
anchor: HTMLElement,
source: "touch" | "contextmenu",
@@ -188,9 +186,7 @@ const CommentRow: React.FC<{
disabled,
ownReaction,
onReact,
- onAddReaction,
onOpenReactions,
- onReply,
onOpenActions,
}) => {
const timer = React.useRef<number | undefined>(undefined);
@@ -200,7 +196,7 @@ const CommentRow: React.FC<{
const didOpen = React.useRef(false);
const canOpenActions = !disabled && !comment.deleted;
const reactions = commentReactionGroups(comment.reactions, ownReaction);
- const reactionColumns = Math.min(reactions.length, 4);
+ const reactionColumns = Math.min(reactions.length, 6);
const largeEmoji = isEmojiOnlyComment(comment.text ?? "");
const previousReaction = React.useRef(ownReaction);
const [poppingReaction, setPoppingReaction] = React.useState<string>();
@@ -333,7 +329,7 @@ const CommentRow: React.FC<{
? `${commentEnter} 240ms ease-out`
: undefined,
position: "relative",
- py: 0.5,
+ py: 1,
px: 2,
flexShrink: 0,
cursor: canOpenActions ? "context-menu" : "default",
@@ -367,7 +363,6 @@ const CommentRow: React.FC<{
columnGap: 1,
rowGap: 0.25,
lineHeight: "18px",
- mt: "-2px",
}}
>
{comment.author ? (
@@ -424,20 +419,23 @@ const CommentRow: React.FC<{
)}
{comment.text ?? "Comment unavailable"}
</Box>
- {!comment.deleted && (
+ {!comment.deleted && reactions.length > 0 && (
<Box
sx={{
alignItems: "center",
display: "grid",
- gridTemplateColumns: `repeat(${reactionColumns + 2}, max-content)`,
+ gridTemplateColumns: `repeat(${reactionColumns}, max-content)`,
columnGap: 0.5,
mt: 0.25,
- "@container (max-width: 240px)": {
+ "@container (max-width: 260px)": {
columnGap: 0.25,
},
+ "@container (max-width: 224px)": {
+ gridTemplateColumns: `repeat(${Math.min(reactionColumns, 5)}, max-content)`,
+ },
}}
>
- {reactions.map(({ emoji, count, isOwn }, index) => (
+ {reactions.map(({ emoji, count, isOwn }) => (
<Box
component="button"
type="button"
@@ -457,8 +455,6 @@ const CommentRow: React.FC<{
alignItems: "center",
borderRadius: "999px",
display: "flex",
- gridColumn: (index % 4) + 1,
- gridRow: Math.floor(index / 4) + 1,
justifyContent: "center",
minHeight: 28,
"&:hover > span": {
@@ -487,7 +483,7 @@ const CommentRow: React.FC<{
height: 24,
px: 0.75,
transition: "transform 180ms ease-out",
- "@container (max-width: 240px)": {
+ "@container (max-width: 260px)": {
px: 0.5,
},
animation:
@@ -515,52 +511,6 @@ const CommentRow: React.FC<{
</Box>
</Box>
))}
- {comment.text !== undefined && (
- <Box
- component="button"
- type="button"
- aria-label="Add reaction"
- disabled={disabled}
- onClick={onAddReaction}
- sx={{
- ...textActionSx,
- alignItems: "center",
- display: "flex",
- gridColumn: reactionColumns + 1,
- gridRow: 1,
- justifyContent:
- reactions.length > 0
- ? "center"
- : "flex-start",
- minHeight: 28,
- width: 24,
- "@container (max-width: 240px)": {
- width: 20,
- },
- }}
- >
- <HugeiconsIcon
- icon={SmilePlusIcon}
- size={16}
- strokeWidth={1.8}
- />
- </Box>
- )}
- <Box
- component="button"
- type="button"
- disabled={disabled}
- onClick={onReply}
- sx={{
- ...textActionSx,
- gridColumn: reactionColumns + 2,
- gridRow: 1,
- minHeight: 28,
- px: 0.25,
- }}
- >
- Reply
- </Box>
</Box>
)}
</Box>
@@ -927,9 +877,6 @@ export const PostComments: React.FC<{
?.emoji
}
onReact={(emoji) => react(comment, emoji)}
- onAddReaction={() =>
- setAction({ type: "react", commentId: comment.commentId })
- }
onOpenReactions={() =>
setAction({
type: "reactions",
@@ -937,7 +884,6 @@ export const PostComments: React.FC<{
open: true,
})
}
- onReply={() => replyTo(comment)}
onOpenActions={(anchor, source) => {
menuPointerTarget.current = null;
ignoreMouseAwayUntil.current =