Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/common/__tests__/following.test.js
Original file line number Diff line number Diff line change
@@ -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), [])
})
12 changes: 11 additions & 1 deletion src/common/__tests__/pins.test.js
Original file line number Diff line number Diff line change
@@ -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'}]}

Expand Down Expand Up @@ -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)
})
4 changes: 4 additions & 0 deletions src/common/following.js
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions src/common/pins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions src/components/app/LeftMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 3 additions & 1 deletion src/components/users/Following.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'),
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/usePins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])

Expand Down
Loading