From bdf4cc268c076f6aaa4fccb937ffbc56bb69fb9e Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Wed, 11 Nov 2026 22:14:23 +0530 Subject: [PATCH] OpenConceptLab/ocl_online#488 | Skip follows with no object, and load a user's pins only for that user or staff --- src/common/__tests__/following.test.js | 17 +++++++++++++++++ src/common/__tests__/pins.test.js | 12 +++++++++++- src/common/following.js | 4 ++++ src/common/pins.js | 9 +++++++++ src/components/app/LeftMenu.jsx | 4 ++-- src/components/users/Following.jsx | 4 +++- src/hooks/usePins.js | 5 ++++- 7 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/common/__tests__/following.test.js create mode 100644 src/common/following.js diff --git a/src/common/__tests__/following.test.js b/src/common/__tests__/following.test.js new file mode 100644 index 000000000..14170fb6f --- /dev/null +++ b/src/common/__tests__/following.test.js @@ -0,0 +1,17 @@ +import test from 'node:test' +import assert from 'node:assert/strict' + +const { getFollowedObjects, getFollowsWithObject } = await import('../following.js') + +const org = {url: '/orgs/CIEL/', type: 'Organization'} +const following = [{id: 1, object: org}, {id: 2, object: null}, {id: 3}] + +test('follows of objects the user can no longer view are skipped', () => { + assert.deepEqual(getFollowedObjects(following), [org]) + assert.deepEqual(getFollowsWithObject(following), [{id: 1, object: org}]) +}) + +test('no following list gives no follows', () => { + assert.deepEqual(getFollowedObjects(undefined), []) + assert.deepEqual(getFollowsWithObject(null), []) +}) diff --git a/src/common/__tests__/pins.test.js b/src/common/__tests__/pins.test.js index 88fd025b7..9187bd76d 100644 --- a/src/common/__tests__/pins.test.js +++ b/src/common/__tests__/pins.test.js @@ -1,7 +1,7 @@ import test from 'node:test' import assert from 'node:assert/strict' -const { getRepoPinOwner, getRepoPinTarget, canUserPinFor } = await import('../pins.js') +const { getRepoPinOwner, getRepoPinTarget, canUserPinFor, canUserViewPinsOf } = await import('../pins.js') const member = {username: 'tester', subscribed_orgs: [{id: 'CIEL'}]} @@ -40,3 +40,13 @@ test('nobody pins without a signed-in user or an owner', () => { assert.equal(canUserPinFor({is_staff: true}, {type: 'org', id: 'CIEL'}), false) assert.equal(canUserPinFor(member, false), false) }) + +test("a user's pins are listed to that user and staff only, an org's to anyone", () => { + const owner = {type: 'user', id: 'tester'} + assert.equal(canUserViewPinsOf(member, owner), true) + assert.equal(canUserViewPinsOf({username: 'staff', is_staff: true}, owner), true) + assert.equal(canUserViewPinsOf({username: 'bob'}, owner), false) + assert.equal(canUserViewPinsOf(undefined, owner), false) + assert.equal(canUserViewPinsOf(undefined, {type: 'org', id: 'CIEL'}), true) + assert.equal(canUserViewPinsOf(member, false), false) +}) diff --git a/src/common/following.js b/src/common/following.js new file mode 100644 index 000000000..86a93eeef --- /dev/null +++ b/src/common/following.js @@ -0,0 +1,4 @@ +// A user's own following list keeps follows of objects they can no longer view, with no object. +export const getFollowedObjects = following => (following || []).map(follow => follow?.object).filter(Boolean) + +export const getFollowsWithObject = following => (following || []).filter(follow => follow?.object) diff --git a/src/common/pins.js b/src/common/pins.js index d8c70613f..70c656eff 100644 --- a/src/common/pins.js +++ b/src/common/pins.js @@ -24,3 +24,12 @@ export const canUserPinFor = (user, owner) => { (user.subscribed_orgs || []).some(org => org?.id === owner.id) : user.username === owner.id } + +// An org's pins are listed to anyone who can view it; a user's only to that user and staff. +export const canUserViewPinsOf = (user, owner) => { + if(!owner?.id) + return false + if(owner.type === 'org') + return true + return Boolean(user?.username && (user.is_staff || user.is_superuser || user.username === owner.id)) +} diff --git a/src/components/app/LeftMenu.jsx b/src/components/app/LeftMenu.jsx index 4c09a208c..210eade63 100644 --- a/src/components/app/LeftMenu.jsx +++ b/src/components/app/LeftMenu.jsx @@ -14,9 +14,9 @@ import DashboardIcon from '@mui/icons-material/Dashboard'; import FolderOpenIcon from '@mui/icons-material/FolderOutlined'; import CommunityIcon from '@mui/icons-material/GroupsOutlined'; import Divider from '@mui/material/Divider'; -import map from 'lodash/map' import { getCurrentUser, refreshCurrentUserCache, getCurrentUserOrgs, hasMapper, isInWaitlist} from '../../common/utils'; import { trayItems, isTrayItemSelected } from '../../common/navigation'; +import { getFollowedObjects } from '../../common/following'; import { PRIMARY_COLORS } from '../../common/colors'; import { COMMUNITY_SITE_URL } from '../../common/constants'; import useMapProjectCount from '../../hooks/useMapProjectCount'; @@ -45,7 +45,7 @@ const LeftMenu = ({ isOpen, onClose }) => { const location = useLocation() const user = getCurrentUser() - const formatFollowing = following => map(following, 'object') || [] + const formatFollowing = following => getFollowedObjects(following) const mapProjectCount = useMapProjectCount(user?.username, isOpen && hasMapper()) const [orgs, setOrgs] = React.useState(getCurrentUserOrgs()) diff --git a/src/components/users/Following.jsx b/src/components/users/Following.jsx index bef3e679a..774980675 100644 --- a/src/components/users/Following.jsx +++ b/src/components/users/Following.jsx @@ -5,6 +5,7 @@ import Typography from '@mui/material/Typography' import IconButton from '@mui/material/IconButton'; import EntityIcon from '../common/EntityIcon'; import Link from '../common/Link'; +import { getFollowsWithObject } from '../../common/following'; const DEFAULT_MEMBERS_TO_SHOW = 12 @@ -16,8 +17,9 @@ const Followed = ({ object }) => { ) } -const Following = ({ following, title }) => { +const Following = ({ following: allFollowing, title }) => { const { t } = useTranslation() + const following = getFollowsWithObject(allFollowing) const [more, setMore] = React.useState(false) const ordered = [ ...orderBy(filter(following, 'object.logo_url'), 'object.name'), ...orderBy(reject(following, 'object.logo_url'), 'object.name'), diff --git a/src/hooks/usePins.js b/src/hooks/usePins.js index 7302ecedf..78382d170 100644 --- a/src/hooks/usePins.js +++ b/src/hooks/usePins.js @@ -2,7 +2,7 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import APIService from '../services/APIService'; import { getCurrentUser } from '../common/utils'; -import { canUserPinFor } from '../common/pins'; +import { canUserPinFor, canUserViewPinsOf } from '../common/pins'; import { OperationsContext } from '../components/app/LayoutContext'; // Pins (a.k.a. bookmarks) are owned either by a user or by an org. @@ -57,6 +57,9 @@ const usePins = owner => { const service = getService({type: ownerType, id: ownerId}) if(!service) return setPins(false) + // the API refuses others' user pins, and a 401 would send an anonymous visitor to sign in + if(!canUserViewPinsOf(getCurrentUser(), {type: ownerType, id: ownerId})) + return setPins([]) service.get().then(response => setPins(response?.data?.length ? response.data : [])) }, [ownerType, ownerId])