Merge branch 'mastoapi/followers' into 'develop'

MastoAPI support for followers/friends(following)

See merge request pleroma/pleroma-fe!667
This commit is contained in:
HJ 2019-03-27 20:18:44 +00:00
commit 4d66b9cf45
3 changed files with 38 additions and 30 deletions

View file

@ -1,5 +1,5 @@
import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js' import backendInteractorService from '../services/backend_interactor_service/backend_interactor_service.js'
import { compact, map, each, merge, find } from 'lodash' import { compact, map, each, merge, find, last } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js' import { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js'
import oauthApi from '../services/new_api/oauth' import oauthApi from '../services/new_api/oauth'
@ -52,23 +52,23 @@ export const mutations = {
state.loggingIn = false state.loggingIn = false
}, },
// TODO Clean after ourselves? // TODO Clean after ourselves?
addFriends (state, { id, friends, page }) { addFriends (state, { id, friends }) {
const user = state.usersObject[id] const user = state.usersObject[id]
each(friends, friend => { each(friends, friend => {
if (!find(user.friends, { id: friend.id })) { if (!find(user.friends, { id: friend.id })) {
user.friends.push(friend) user.friends.push(friend)
} }
}) })
user.friendsPage = page + 1 user.lastFriendId = last(friends).id
}, },
addFollowers (state, { id, followers, page }) { addFollowers (state, { id, followers }) {
const user = state.usersObject[id] const user = state.usersObject[id]
each(followers, follower => { each(followers, follower => {
if (!find(user.followers, { id: follower.id })) { if (!find(user.followers, { id: follower.id })) {
user.followers.push(follower) user.followers.push(follower)
} }
}) })
user.followersPage = page + 1 user.lastFollowerId = last(followers).id
}, },
// Because frontend doesn't have a reason to keep these stuff in memory // Because frontend doesn't have a reason to keep these stuff in memory
// outside of viewing someones user profile. // outside of viewing someones user profile.
@ -78,7 +78,7 @@ export const mutations = {
return return
} }
user.friends = [] user.friends = []
user.friendsPage = 0 user.lastFriendId = null
}, },
clearFollowers (state, userId) { clearFollowers (state, userId) {
const user = state.usersObject[userId] const user = state.usersObject[userId]
@ -86,7 +86,7 @@ export const mutations = {
return return
} }
user.followers = [] user.followers = []
user.followersPage = 0 user.lastFollowerId = null
}, },
addNewUsers (state, users) { addNewUsers (state, users) {
each(users, (user) => mergeOrAdd(state.users, state.usersObject, user)) each(users, (user) => mergeOrAdd(state.users, state.usersObject, user))
@ -219,10 +219,10 @@ const users = {
addFriends ({ rootState, commit }, fetchBy) { addFriends ({ rootState, commit }, fetchBy) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const user = rootState.users.usersObject[fetchBy] const user = rootState.users.usersObject[fetchBy]
const page = user.friendsPage || 1 const maxId = user.lastFriendId
rootState.api.backendInteractor.fetchFriends({ id: user.id, page }) rootState.api.backendInteractor.fetchFriends({ id: user.id, maxId })
.then((friends) => { .then((friends) => {
commit('addFriends', { id: user.id, friends, page }) commit('addFriends', { id: user.id, friends })
resolve(friends) resolve(friends)
}).catch(() => { }).catch(() => {
reject() reject()
@ -231,10 +231,10 @@ const users = {
}, },
addFollowers ({ rootState, commit }, fetchBy) { addFollowers ({ rootState, commit }, fetchBy) {
const user = rootState.users.usersObject[fetchBy] const user = rootState.users.usersObject[fetchBy]
const page = user.followersPage || 1 const maxId = user.lastFollowerId
return rootState.api.backendInteractor.fetchFollowers({ id: user.id, page }) return rootState.api.backendInteractor.fetchFollowers({ id: user.id, maxId })
.then((followers) => { .then((followers) => {
commit('addFollowers', { id: user.id, followers, page }) commit('addFollowers', { id: user.id, followers })
return followers return followers
}) })
}, },

View file

@ -8,8 +8,6 @@ const RETWEET_URL = '/api/statuses/retweet'
const UNRETWEET_URL = '/api/statuses/unretweet' const UNRETWEET_URL = '/api/statuses/unretweet'
const STATUS_DELETE_URL = '/api/statuses/destroy' const STATUS_DELETE_URL = '/api/statuses/destroy'
const MENTIONS_URL = '/api/statuses/mentions.json' const MENTIONS_URL = '/api/statuses/mentions.json'
const FOLLOWERS_URL = '/api/statuses/followers.json'
const FRIENDS_URL = '/api/statuses/friends.json'
const FOLLOWING_URL = '/api/friendships/create.json' const FOLLOWING_URL = '/api/friendships/create.json'
const UNFOLLOWING_URL = '/api/friendships/destroy.json' const UNFOLLOWING_URL = '/api/friendships/destroy.json'
const REGISTRATION_URL = '/api/account/register.json' const REGISTRATION_URL = '/api/account/register.json'
@ -29,6 +27,8 @@ const DENY_USER_URL = '/api/pleroma/friendships/deny'
const SUGGESTIONS_URL = '/api/v1/suggestions' const SUGGESTIONS_URL = '/api/v1/suggestions'
const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites' const MASTODON_USER_FAVORITES_TIMELINE_URL = '/api/v1/favourites'
const MASTODON_FOLLOWING_URL = id => `/api/v1/accounts/${id}/following`
const MASTODON_FOLLOWERS_URL = id => `/api/v1/accounts/${id}/followers`
const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = '/api/v1/timelines/direct' const MASTODON_DIRECT_MESSAGES_TIMELINE_URL = '/api/v1/timelines/direct'
const MASTODON_PUBLIC_TIMELINE = '/api/v1/timelines/public' const MASTODON_PUBLIC_TIMELINE = '/api/v1/timelines/public'
const MASTODON_USER_HOME_TIMELINE_URL = '/api/v1/timelines/home' const MASTODON_USER_HOME_TIMELINE_URL = '/api/v1/timelines/home'
@ -275,28 +275,36 @@ const fetchUserRelationship = ({id, credentials}) => {
}) })
} }
const fetchFriends = ({id, page, credentials}) => { const fetchFriends = ({id, maxId, sinceId, limit = 20, credentials}) => {
let url = `${FRIENDS_URL}?user_id=${id}` let url = MASTODON_FOLLOWING_URL(id)
if (page) { const args = [
url = url + `&page=${page}` maxId && `max_id=${maxId}`,
} sinceId && `since_id=${sinceId}`,
limit && `limit=${limit}`
].filter(_ => _).join('&')
url = url + (args ? '?' + args : '')
return fetch(url, { headers: authHeaders(credentials) }) return fetch(url, { headers: authHeaders(credentials) })
.then((data) => data.json()) .then((data) => data.json())
.then((data) => data.map(parseUser)) .then((data) => data.map(parseUser))
} }
const exportFriends = ({id, credentials}) => { const exportFriends = ({id, credentials}) => {
let url = `${FRIENDS_URL}?user_id=${id}&all=true` let url = MASTODON_FOLLOWING_URL(id) + `?all=true`
return fetch(url, { headers: authHeaders(credentials) }) return fetch(url, { headers: authHeaders(credentials) })
.then((data) => data.json()) .then((data) => data.json())
.then((data) => data.map(parseUser)) .then((data) => data.map(parseUser))
} }
const fetchFollowers = ({id, page, credentials}) => { const fetchFollowers = ({id, maxId, sinceId, limit = 20, credentials}) => {
let url = `${FOLLOWERS_URL}?user_id=${id}` let url = MASTODON_FOLLOWERS_URL(id)
if (page) { const args = [
url = url + `&page=${page}` maxId && `max_id=${maxId}`,
} sinceId && `since_id=${sinceId}`,
limit && `limit=${limit}`
].filter(_ => _).join('&')
url += args ? '?' + args : ''
return fetch(url, { headers: authHeaders(credentials) }) return fetch(url, { headers: authHeaders(credentials) })
.then((data) => data.json()) .then((data) => data.json())
.then((data) => data.map(parseUser)) .then((data) => data.map(parseUser))

View file

@ -10,16 +10,16 @@ const backendInteractorService = (credentials) => {
return apiService.fetchConversation({id, credentials}) return apiService.fetchConversation({id, credentials})
} }
const fetchFriends = ({id, page}) => { const fetchFriends = ({id, maxId, sinceId, limit}) => {
return apiService.fetchFriends({id, page, credentials}) return apiService.fetchFriends({id, maxId, sinceId, limit, credentials})
} }
const exportFriends = ({id}) => { const exportFriends = ({id}) => {
return apiService.exportFriends({id, credentials}) return apiService.exportFriends({id, credentials})
} }
const fetchFollowers = ({id, page}) => { const fetchFollowers = ({id, maxId, sinceId, limit}) => {
return apiService.fetchFollowers({id, page, credentials}) return apiService.fetchFollowers({id, maxId, sinceId, limit, credentials})
} }
const fetchAllFollowing = ({username}) => { const fetchAllFollowing = ({username}) => {