From 91edd79b67725b5c2f251ab3d37a97e3e9124faa Mon Sep 17 00:00:00 2001 From: Mustafa Buyukcelebi Date: Tue, 5 Mar 2019 12:09:18 +0300 Subject: [PATCH] Integrated filter nsfw feature with posts screens --- .../posts/container/postsContainer.js | 1 + src/components/posts/view/postsView.js | 6 +++--- src/providers/steem/dsteem.js | 8 +++++++- src/utils/filterNsfwPost.js | 17 +++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/utils/filterNsfwPost.js diff --git a/src/components/posts/container/postsContainer.js b/src/components/posts/container/postsContainer.js index 94b79dd7b..d17b68296 100644 --- a/src/components/posts/container/postsContainer.js +++ b/src/components/posts/container/postsContainer.js @@ -59,6 +59,7 @@ const mapStateToProps = state => ({ isLoggedIn: state.application.isLoggedIn, isLoginDone: state.application.isLoginDone, isCollapsePostButtonOpen: state.ui.isCollapsePostButton, + nsfw: state.application.nsfw, }); export default connect(mapStateToProps)(PostsContainer); diff --git a/src/components/posts/view/postsView.js b/src/components/posts/view/postsView.js index fd856a5ae..701c0d713 100644 --- a/src/components/posts/view/postsView.js +++ b/src/components/posts/view/postsView.js @@ -81,7 +81,7 @@ class PostsView extends Component { _loadPosts = () => { const { - getFor, tag, currentAccountUsername, pageType, + getFor, tag, currentAccountUsername, pageType, nsfw, } = this.props; const { posts, startAuthor, startPermlink, refreshing, selectedFilterIndex, @@ -114,7 +114,7 @@ class PostsView extends Component { options.start_permlink = startPermlink; } - getPostsSummary(filter, options, currentAccountUsername) + getPostsSummary(filter, options, currentAccountUsername, nsfw) .then((result) => { if (result.length > 0) { let _posts = result; @@ -160,7 +160,7 @@ class PostsView extends Component { this.setState({ isNoPost: true }); } }) - .catch((err) => { + .catch(() => { this.setState({ refreshing: false, isPostsLoading: false, diff --git a/src/providers/steem/dsteem.js b/src/providers/steem/dsteem.js index 0ce0147f4..195fd1c74 100644 --- a/src/providers/steem/dsteem.js +++ b/src/providers/steem/dsteem.js @@ -11,6 +11,7 @@ import { parsePosts, parsePost, parseComments } from '../../utils/postParser'; import { getName, getAvatar } from '../../utils/user'; import { getReputation } from '../../utils/reputation'; import parseToken from '../../utils/parseToken'; +import filterNsfwPost from '../../utils/filterNsfwPost'; // Constant import AUTH_TYPE from '../../constants/authType'; @@ -274,12 +275,17 @@ export const getPosts = async (by, query, user) => { export const getActiveVotes = (author, permlink) => client.database.call('get_active_votes', [author, permlink]); -export const getPostsSummary = async (by, query, currentUserName) => { +export const getPostsSummary = async (by, query, currentUserName, filterNsfw) => { try { let posts = await client.database.getDiscussions(by, query); if (posts) { posts = await parsePosts(posts, currentUserName, true); + + if (filterNsfw !== '0') { + const updatedPosts = filterNsfwPost(posts, filterNsfw); + return updatedPosts; + } } return posts; } catch (error) { diff --git a/src/utils/filterNsfwPost.js b/src/utils/filterNsfwPost.js new file mode 100644 index 000000000..297ef1985 --- /dev/null +++ b/src/utils/filterNsfwPost.js @@ -0,0 +1,17 @@ +export default (posts, option) => { + const updatedPosts = []; + if (option === '1') { + posts.map((post) => { + if (post.parent_permlink === 'nsfw' || post.json_metadata.tags.includes('nsfw')) { + post.image = 'https://steemitimages.com/p/Zskj9C56UonWToSX7uRkL8H89BqdWqSPnzP84oDpA65G4PfbZk688V2cRYbWszK7zoMMj35CfbJfcXuJSSwTvasK4pPKfHMy2xjGJSkmqs8gLQnoddR8?format=match&mode=fit&width=600&height=600'; + } + }); + return posts; + } + posts.map((post) => { + if (post.parent_permlink !== 'nsfw' && !post.json_metadata.tags.includes('nsfw')) { + updatedPosts.push(post); + } + }); + return updatedPosts; +};