Commit fa053026 authored by Nastassia Danilova's avatar Nastassia Danilova

following/followers api and actions, tests

parent 22f0dde1
......@@ -23,6 +23,34 @@ const Users = {
queries
})
},
async followers ({ config, fullUrl, params, queries }) {
if (fullUrl) {
return utils.request({
config,
params,
fullUrl
})
}
return utils.request({
config,
url: `${ACCOUNTS_URL}/${params.id}/followers`,
queries
})
},
async following ({ config, fullUrl, params, queries }) {
if (fullUrl) {
return utils.request({
config,
params,
fullUrl
})
}
return utils.request({
config,
url: `${ACCOUNTS_URL}/${params.id}/following`,
queries
})
},
async register ({ config, params }) {
return utils.request({
config,
......
......@@ -14,6 +14,8 @@ const initialState = {
notifications: {},
conversations: {},
userStatuses: {},
userFollowers: {},
userFollowing: {},
conversation: {},
tagTimeline: {},
polls: {},
......
import { reduce } from 'lodash'
import { emojify, emojifyStatus } from '../utils/parse_utils'
import { addStatuses } from '../utils/status_utils'
import { addIdsToList } from '../utils/common_utils'
const initialState = {
usersByIds: {},
......@@ -63,6 +64,40 @@ const addUserStatuses = (state, { userId, statuses }) => {
}
}
const addUserFollowers = (state, { userId, followers }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
...oldUser,
followers: addIdsToList(oldUser.followers || [],
followers ? followers.map(account => account.id) : [])
}
return {
...state,
usersByIds: {
...state.usersByIds,
[userId]: user
}
}
}
const addUserFollowing = (state, { userId, following }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
...oldUser,
following: addIdsToList(oldUser.following || [],
following ? following.map(account => account.id) : [])
}
return {
...state,
usersByIds: {
...state.usersByIds,
[userId]: user
}
}
}
const deleteUserStatus = (state, { userId, statusId }) => {
const oldUser = state.usersByIds[userId] || {}
const user = {
......@@ -98,6 +133,8 @@ const reducers = {
setCurrentUser,
updateCurrentUser,
addUserStatuses,
addUserFollowers,
addUserFollowing,
deleteUserStatus,
updateUnreadNotificationsCount
}
......@@ -133,6 +170,18 @@ const actions = {
payload: { userId, statuses }
}
},
addUserFollowers: ({ userId, followers }) => {
return {
type: 'addUserFollowers',
payload: { userId, followers }
}
},
addUserFollowing: ({ userId, following }) => {
return {
type: 'addUserFollowing',
payload: { userId, following }
}
},
deleteUserStatus: ({ userId, statusId }) => {
return {
type: 'deleteUserStatus',
......
......@@ -93,6 +93,47 @@ describe('User api', () => {
})
})
describe('/api/v1/accounts/:id/followers', () => {
it('returns the followers and the links', async () => {
const id = 1
fetch.mockImplementationOnce(fetchMocker(
[{ id: 1 }, { id: 2 }],
{
expectedUrl: `https://pleroma.soykaf.com/api/v1/accounts/${id}/followers?max_id=1`,
headers: {
link: '<https://pleroma.soykaf.com/api/v1/accounts/1/statuses?max_id=9gZ5VYhDG8GeCL8Vay>; rel="next", <https://pleroma.soykaf.com/api/v1/accounts/1/statuses?since_id=9gZ5g5Q6RlaAaN9Z5M>; rel="prev"'
}
}))
const res = await api.users.followers({ config, params: { id }, queries: { max_id: 1 } })
expect(res.state).toBe('ok')
expect(res.data).toEqual([{ id: 1 }, { id: 2 }])
expect(res.links).not.toBe(null)
})
})
describe('/api/v1/accounts/:id/following', () => {
it('returns the following and the links', async () => {
const id = 1
fetch.mockImplementationOnce(fetchMocker(
[{ id: 1 }, { id: 2 }],
{
expectedUrl: `https://pleroma.soykaf.com/api/v1/accounts/${id}/following?max_id=1`,
headers: {
link: '<https://pleroma.soykaf.com/api/v1/accounts/1/following?max_id=9gZ5VYhDG8GeCL8Vay>; rel="next", <https://pleroma.soykaf.com/api/v1/accounts/1/statuses?since_id=9gZ5g5Q6RlaAaN9Z5M>; rel="prev"'
}
}))
const res = await api.users.following({ config, params: { id }, queries: { max_id: 1 } })
expect(res.state).toBe('ok')
expect(res.data).toEqual([{ id: 1 }, { id: 2 }])
expect(res.links).not.toBe(null)
})
})
describe('POST /api/v1/accounts', () => {
it('returns a new account', async () => {
const id = 1
......
......@@ -133,15 +133,63 @@ describe('User reducers', () => {
expect(resultState.usersByIds['1']).toEqual({ ...user, statuses: [{ id: 2, content: 'b' }] })
})
it('update unread notifications count', () => {
const currentUser = { id: '1', pleroma: { unread_notifications_count: 5 } }
})
describe(`adding a user's followers`, () => {
it('add followersIds no a new user', () => {
const followers = [{ id: 2, acct: 'b' }, { id: 1, acct: 'a' }]
const resultState = Users.reducer(
undefined,
Users.actions.addUserFollowers({ userId: '1', followers })
)
expect(resultState.usersByIds['1']).toEqual({ followers: [2, 1] })
})
it('update user followersIds', () => {
const user = { id: 1 }
const followers = [{ id: 2, acct: 'b' }, { id: 1, acct: 'a' }]
const resultState = Users.reducer(
{ usersByIds: { 1: user } },
Users.actions.addUserFollowers({ userId: user.id, followers })
)
expect(resultState.usersByIds['1']).toEqual({ ...user, followers: [2, 1] })
})
})
describe(`adding a user's following`, () => {
it('add followingIds no a new user', () => {
const following = [{ id: 2, acct: 'b' }, { id: 1, acct: 'a' }]
const resultState = Users.reducer(
{ currentUser },
Users.actions.updateUnreadNotificationsCount({ unreadNotificationsCount: 1 })
undefined,
Users.actions.addUserFollowing({ userId: '1', following })
)
expect(resultState.currentUser).toEqual({ id: '1', pleroma: { unread_notifications_count: 1 } })
expect(resultState.usersByIds['1']).toEqual({ following: [2, 1] })
})
it('update user followingIds', () => {
const user = { id: 1 }
const following = [{ id: 2, acct: 'b' }, { id: 1, acct: 'a' }]
const resultState = Users.reducer(
{ usersByIds: { 1: user } },
Users.actions.addUserFollowing({ userId: user.id, following })
)
expect(resultState.usersByIds['1']).toEqual({ ...user, following: [2, 1] })
})
})
it('update unread notifications count', () => {
const currentUser = { id: '1', pleroma: { unread_notifications_count: 5 } }
const resultState = Users.reducer(
{ currentUser },
Users.actions.updateUnreadNotificationsCount({ unreadNotificationsCount: 1 })
)
expect(resultState.currentUser).toEqual({ id: '1', pleroma: { unread_notifications_count: 1 } })
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment