Filter for blockedUsers/content, fix cypress

This commit is contained in:
mattwr18 2019-12-18 20:42:06 +01:00
parent d74d2072ba
commit 46c4602db1
4 changed files with 43 additions and 38 deletions

View File

@ -0,0 +1,25 @@
import { getBlockedUsers, getBlockedByUsers } from '../users.js'
import { mergeWith, isArray } from 'lodash'
export const filterForBlockedUsers = async (params, context) => {
if (!context.user) return params
const [blockedUsers, blockedByUsers] = await Promise.all([
getBlockedUsers(context),
getBlockedByUsers(context),
])
const blockedUsersIds = [...blockedByUsers.map(b => b.id), ...blockedUsers.map(b => b.id)]
if (!blockedUsersIds.length) return params
params.filter = mergeWith(
params.filter,
{
author_not: { id_in: blockedUsersIds },
},
(objValue, srcValue) => {
if (isArray(objValue)) {
return objValue.concat(srcValue)
}
},
)
return params
}

View File

@ -1,33 +1,10 @@
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
import { neo4jgraphql } from 'neo4j-graphql-js' import { neo4jgraphql } from 'neo4j-graphql-js'
import { isEmpty } from 'lodash'
import fileUpload from './fileUpload' import fileUpload from './fileUpload'
import { getBlockedUsers, getBlockedByUsers } from './users.js'
import { mergeWith, isArray, isEmpty } from 'lodash'
import { UserInputError } from 'apollo-server' import { UserInputError } from 'apollo-server'
import Resolver from './helpers/Resolver' import Resolver from './helpers/Resolver'
import { filterForBlockedUsers } from './helpers/filterForBlockedUsers'
const filterForBlockedUsers = async (params, context) => {
if (!context.user) return params
const [blockedUsers, blockedByUsers] = await Promise.all([
getBlockedUsers(context),
getBlockedByUsers(context),
])
const badIds = [...blockedByUsers.map(b => b.id), ...blockedUsers.map(b => b.id)]
if (!badIds.length) return params
params.filter = mergeWith(
params.filter,
{
author_not: { id_in: badIds },
},
(objValue, srcValue) => {
if (isArray(objValue)) {
return objValue.concat(srcValue)
}
},
)
return params
}
const maintainPinnedPosts = params => { const maintainPinnedPosts = params => {
const pinnedPostFilter = { pinned: true } const pinnedPostFilter = { pinned: true }

View File

@ -1,3 +1,5 @@
import { filterForBlockedUsers } from './helpers/filterForBlockedUsers'
const transformReturnType = record => { const transformReturnType = record => {
return { return {
__typename: record.get('type'), __typename: record.get('type'),
@ -6,9 +8,10 @@ const transformReturnType = record => {
} }
export default { export default {
Query: { Query: {
findResources: async (_parent, args, context, _resolveInfo) => { findResources: async (_parent, params, context, _resolveInfo) => {
const { query, limit } = args params = await filterForBlockedUsers(params, context)
const filter = {} const { query, limit } = params
const filter = { ...params.filter }
const { id: thisUserId } = context.user const { id: thisUserId } = context.user
// see http://lucene.apache.org/core/8_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package.description // see http://lucene.apache.org/core/8_3_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#package.description
const myQuery = query.replace(/\s/g, '* ') + '*' const myQuery = query.replace(/\s/g, '* ') + '*'
@ -17,10 +20,10 @@ export default {
YIELD node as resource, score YIELD node as resource, score
MATCH (resource)<-[:WROTE]-(user:User) MATCH (resource)<-[:WROTE]-(user:User)
WHERE score >= 0.5 WHERE score >= 0.5
AND NOT user.deleted = true AND NOT user.disabled = true AND NOT (user.deleted = true AND NOT user.disabled = true
AND NOT resource.deleted = true AND NOT resource.disabled = true OR resource.deleted = true AND NOT resource.disabled = true
AND NOT user.id in COALESCE($filter.author_not.id_in, []) OR user.id in COALESCE($filter.author_not.id_in, [])
AND NOT (:User { id: $thisUserId })-[:BLOCKED]-(user) OR (:User { id: $thisUserId })-[:BLOCKED]-(user))
RETURN resource, labels(resource)[0] AS type RETURN resource, labels(resource)[0] AS type
LIMIT $limit LIMIT $limit
` `
@ -46,8 +49,8 @@ export default {
YIELD node as resource, score YIELD node as resource, score
MATCH (resource) MATCH (resource)
WHERE score >= 0.5 WHERE score >= 0.5
AND NOT resource.deleted = true AND NOT resource.disabled = true AND NOT (resource.deleted = true AND NOT resource.disabled = true
AND NOT (:User { id: $thisUserId })-[:BLOCKED]-(resource) OR (:User { id: $thisUserId })-[:BLOCKED]-(resource))
RETURN resource, labels(resource)[0] AS type RETURN resource, labels(resource)[0] AS type
LIMIT $limit LIMIT $limit
` `

View File

@ -1,6 +1,6 @@
import { When, Then } from "cypress-cucumber-preprocessor/steps"; import { When, Then } from "cypress-cucumber-preprocessor/steps";
When("I search for {string}", value => { When("I search for {string}", value => {
cy.get("#nav-search") cy.get("#search-resources")
.focus() .focus()
.type(value); .type(value);
}); });
@ -25,21 +25,21 @@ Then("I should see the following posts in the select dropdown:", table => {
}); });
When("I type {string} and press Enter", value => { When("I type {string} and press Enter", value => {
cy.get("#nav-search") cy.get("#search-resources")
.focus() .focus()
.type(value) .type(value)
.type("{enter}", { force: true }); .type("{enter}", { force: true });
}); });
When("I type {string} and press escape", value => { When("I type {string} and press escape", value => {
cy.get("#nav-search") cy.get("#search-resources")
.focus() .focus()
.type(value) .type(value)
.type("{esc}"); .type("{esc}");
}); });
Then("the search field should clear", () => { Then("the search field should clear", () => {
cy.get("#nav-search").should("have.text", ""); cy.get("#search-resources").should("have.text", "");
}); });
When("I select an entry", () => { When("I select an entry", () => {