Skip to content

Commit

Permalink
Post hiding unhide
Browse files Browse the repository at this point in the history
  • Loading branch information
previnder committed Nov 26, 2024
1 parent efc2a24 commit deaa16c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
5 changes: 5 additions & 0 deletions core/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,11 @@ func (u *User) HidePost(ctx context.Context, postID uid.ID) error {
})
}

func (u *User) UnhidePost(ctx context.Context, postID uid.ID) error {
_, err := u.db.ExecContext(ctx, "DELETE FROM hidden_posts WHERE user_id = ? AND post_id = ?", u.ID, postID)
return err
}

// NewBadgeType creates a new type of user badge. Calling this function more
// than once with the same name will not result in an error.
func NewBadgeType(db *sql.DB, name string) error {
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func New(db *sql.DB, conf *config.Config) (*Server, error) {
r.Handle("/api/users/{username}/badges", s.withHandler(s.addBadge)).Methods("POST")
r.Handle("/api/users/{username}/badges/{badgeId}", s.withHandler(s.deleteBadge)).Methods("DELETE")
r.Handle("/api/hidden_posts", s.withHandler(s.handleHiddenPosts)).Methods("POST")
r.Handle("/api/hidden_posts/{postId}", s.withHandler(s.unhidePost)).Methods("DELETE")

r.Handle("/api/users/{username}/lists", s.withHandler(s.handleLists)).Methods("GET", "POST")
r.Handle("/api/lists/_saved_to", s.withHandler(s.getSaveToLists)).Methods("GET")
Expand Down
22 changes: 22 additions & 0 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,25 @@ func (s *Server) handleHiddenPosts(w *responseWriter, r *request) error {

return w.writeString(`{"success":true}`)
}

func (s *Server) unhidePost(w *responseWriter, r *request) error {
if !r.loggedIn {
return errNotLoggedIn
}

user, err := core.GetUser(r.ctx, s.db, *r.viewer, nil)
if err != nil {
return err
}

postID, err := uid.FromString(r.muxVar("postId"))
if err != nil {
return httperr.NewBadRequest("invalid-post-id", "Invalid post id.")
}

if err := user.UnhidePost(r.ctx, postID); err != nil {
return err
}

return w.writeString(`{"success":true}`)
}
32 changes: 28 additions & 4 deletions ui/src/components/PostCard/PostCard.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import PropTypes from 'prop-types';
import React, { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { omitWWWFromHostname, stringCount } from '../../helper';
import { mfetchjson, omitWWWFromHostname, stringCount } from '../../helper';
import { useIsMobile } from '../../hooks';
import { snackAlertError } from '../../slices/mainSlice';
import { postHidden } from '../../slices/postsSlice';
import { SVGExternalLink } from '../../SVGs';
import Button from '../Button';
import Link from '../Link';
Expand Down Expand Up @@ -64,6 +67,28 @@ const PostCard = ({
}
};

const dispatch = useDispatch();
const handleHidePost = async () => {
try {
await mfetchjson('/api/hidden_posts', {
method: 'POST',
body: JSON.stringify({ postId: post.id }),
});
dispatch(postHidden(post.publicId, true, feedItemKey));
} catch (error) {
dispatch(snackAlertError(error));
}
};

const handleUnHidePost = async () => {
try {
await mfetchjson(`/api/hidden_posts/${post.id}`, { method: 'DELETE' });
dispatch(postHidden(post.publicId, false, feedItemKey));
} catch (error) {
dispatch(snackAlertError(error));
}
};

const [isDomainHovering, setIsDomainHovering] = useState(false);

const isMobile = useIsMobile();
Expand Down Expand Up @@ -132,7 +157,7 @@ const PostCard = ({
if (post.hidden) {
return (
<div className="card post-card-hidden">
<div>Hidden post</div> <Button>Undo</Button>
<div>Hidden post</div> <Button onClick={handleUnHidePost}>Undo</Button>
</div>
);
}
Expand All @@ -159,8 +184,7 @@ const PostCard = ({
target={target}
onRemoveFromList={onRemoveFromList}
compact={compact}
feedItemKey={feedItemKey}
showHideButton={!inModTools}
onHidePost={handleHidePost}
/>
</div>
<div className={'post-card-body' + (isDomainHovering ? ' is-domain-hover' : '')}>
Expand Down
27 changes: 6 additions & 21 deletions ui/src/components/PostCard/PostCardHeadingDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { mfetchjson, toTitleCase, userGroupSingular } from '../../helper';
import { toTitleCase, userGroupSingular } from '../../helper';
import { useIsMobile, useMuteCommunity, useMuteUser } from '../../hooks';
import { userHasSupporterBadge } from '../../pages/User';
import { saveToListModalOpened, snackAlertError } from '../../slices/mainSlice';
import { postHidden } from '../../slices/postsSlice';
import { saveToListModalOpened } from '../../slices/mainSlice';
import { ButtonMore } from '../Button';
import Dropdown from '../Dropdown';
import TimeAgo from '../TimeAgo';
Expand All @@ -18,8 +17,7 @@ const PostCardHeadingDetails = ({
showAuthorProPic = false,
onRemoveFromList = null,
compact = false,
showHideButton = false,
feedItemKey,
onHidePost,
}) => {
// const userURL = `/@${post.username}`;
userGroup = userGroup ?? post.userGroup;
Expand Down Expand Up @@ -58,18 +56,6 @@ const PostCardHeadingDetails = ({
communityName: post.communityName,
});

const handleHidePost = async () => {
try {
await mfetchjson('/api/hidden_posts', {
method: 'POST',
body: JSON.stringify({ postId: post.id }),
});
dispatch(postHidden(post.publicId, true, feedItemKey));
} catch (error) {
dispatch(snackAlertError(error));
}
};

const isAuthorSupporter = userHasSupporterBadge(post.author);
const isUsernameGhost = post.userDeleted && !viewerAdmin;

Expand Down Expand Up @@ -132,8 +118,8 @@ const PostCardHeadingDetails = ({
Remove from list
</button>
)}
{showHideButton && (
<button className="button-clear dropdown-item" onClick={handleHidePost}>
{onHidePost && (
<button className="button-clear dropdown-item" onClick={onHidePost}>
Hide
</button>
)}
Expand All @@ -153,8 +139,7 @@ PostCardHeadingDetails.propTypes = {
showAuthorProPic: PropTypes.bool,
onRemoveFromList: PropTypes.func,
compact: PropTypes.bool,
feedItemKey: PropTypes.string,
showHideButton: PropTypes.bool,
onHidePost: PropTypes.func,
};

export default PostCardHeadingDetails;
1 change: 0 additions & 1 deletion ui/src/slices/postsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export const postHidden =
(dispatch: AppDispatch) => {
dispatch({ type: typePostHidden, payload: { postId, hidden } });
if (feedItemKey) {
console.log('Height changing: ', feedItemKey);
window.setTimeout(() => {
dispatch(feedItemHeightChanged(feedItemKey, null));
}, 10);
Expand Down

0 comments on commit deaa16c

Please sign in to comment.