Skip to content

Commit

Permalink
Merge pull request #364 from Aymanhki/ayman
Browse files Browse the repository at this point in the history
Break Lines in posts
  • Loading branch information
Aymanhki authored Mar 30, 2024
2 parents 13889b6 + 7f4c286 commit abe5010
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
5 changes: 2 additions & 3 deletions epoch_backend/business/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,15 @@ def get_posts_users_info(posts):
return posts_users


def get_post_dict(current_post, posts_media, username, profile_picture_url, profile_picture_type, profile_picture_name,
i):
def get_post_dict(current_post, posts_media, username, profile_picture_url, profile_picture_type, profile_picture_name, i):
post_dict = {}
post_dict["post_id"] = current_post[0]
post_dict["user_id"] = current_post[1]
post_dict["profile_picture"] = profile_picture_url
post_dict["profile_picture_type"] = profile_picture_type
post_dict["profile_picture_name"] = profile_picture_name
post_dict["username"] = username
post_dict["caption"] = current_post[3]
post_dict["caption"] = current_post[3].replace("\\n", "\n")
post_dict["created_at"] = current_post[4].isoformat()
post_dict["release"] = current_post[5].isoformat()
post_dict["time_zone"] = current_post[8]
Expand Down
11 changes: 11 additions & 0 deletions epoch_backend/persistence/epoch/epoch_post_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def __init__(self):
def add_post(self, new_post: post):
connection = get_db_connection()
cursor = connection.cursor()

if not new_post.caption:
new_post.caption = ''
else:
new_post.caption = new_post.caption.replace('\n', '<br/>')

if new_post.media_id is not None:
cursor.execute(
"INSERT INTO posts (user_id, media_id, caption, created_at, release, time_zone) VALUES (%s, %s, %s, %s, %s, %s) RETURNING post_id",
Expand Down Expand Up @@ -159,6 +165,11 @@ def update_post(self, post_id: int, new_post: post):
old_post_media_id = old_post[2]
old_post_caption = old_post[3]

if not new_post.caption:
new_post.caption = ''
else:
new_post.caption = new_post.caption.replace('\n', '\\n')

if new_post.media_id is not None and new_post.media_id != -1:
if (old_post_media_id is not None and new_post.media_id != old_post_media_id) or old_post_media_id is None:
cursor.execute("UPDATE posts SET media_id=%s, caption=%s, release=%s WHERE post_id=%s",
Expand Down
10 changes: 6 additions & 4 deletions epoch_frontend/src/modules/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost
return null;
}

const replacedNewlines = toRender.replace(/\n/g, '<br>'); // Replace \n with <br> tags

let regex = /(@[a-zA-Z0-9_]+)|(#\w+)|(https?:\/\/[^\s]+)/g;
let parts = toRender.split(regex);
let parts = replacedNewlines.split(regex);

let elements = parts.map((part, index) => {

Expand All @@ -161,7 +163,7 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost
} else if (part.startsWith('http')) {
return <a key={index} href={part} className="hashtag">{part}</a>;
} else {
return part;
return <span key={index} dangerouslySetInnerHTML={{__html: part}}></span>;
}
}
else
Expand Down Expand Up @@ -622,9 +624,9 @@ export default function Post({post, postViewer, isInFavorites, setShowDeletePost

<div className="post-body">
{post.caption && post.caption.length > 0 && (
<p className={"post-caption"}>
<p className={"post-caption"} >
{(showFullCaption && post.caption) ? renderCaptionWithHighlights(post.caption) : (
<>
< >
{renderCaptionWithHighlights(truncatedCaption)}
<span className="see-more" onClick={toggleCaptionVisibility}>
See more
Expand Down

0 comments on commit abe5010

Please sign in to comment.