From 0b1506a4c00c1482dd31ee43b17ee673e9f57bde Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 10 Mar 2019 18:05:51 +0200 Subject: [PATCH 1/4] wip support for follower/following, a bit broken and with regression --- src/services/api/api.service.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 2de87026..9cee75b9 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -16,8 +16,6 @@ const MEDIA_UPLOAD_URL = '/api/statusnet/media/upload' const CONVERSATION_URL = '/api/statusnet/conversation' const MENTIONS_URL = '/api/statuses/mentions.json' const DM_TIMELINE_URL = '/api/statuses/dm_timeline.json' -const FOLLOWERS_URL = '/api/statuses/followers.json' -const FRIENDS_URL = '/api/statuses/friends.json' const BLOCKS_URL = '/api/statuses/blocks.json' const FOLLOWING_URL = '/api/friendships/create.json' const UNFOLLOWING_URL = '/api/friendships/destroy.json' @@ -43,6 +41,8 @@ const DENY_USER_URL = '/api/pleroma/friendships/deny' const SUGGESTIONS_URL = '/api/v1/suggestions' 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` import { each, map } from 'lodash' import { parseStatus, parseUser, parseNotification } from '../entity_normalizer/entity_normalizer.service.js' @@ -258,9 +258,9 @@ const fetchUser = ({id, credentials}) => { } const fetchFriends = ({id, page, credentials}) => { - let url = `${FRIENDS_URL}?user_id=${id}` + let url = MASTODON_FOLLOWING_URL(id) if (page) { - url = url + `&page=${page}` + url = url + `?page=${page}` } return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) @@ -268,16 +268,16 @@ const fetchFriends = ({id, page, 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) }) .then((data) => data.json()) .then((data) => data.map(parseUser)) } const fetchFollowers = ({id, page, credentials}) => { - let url = `${FOLLOWERS_URL}?user_id=${id}` + let url = MASTODON_FOLLOWERS_URL(id) if (page) { - url = url + `&page=${page}` + url = url + `?page=${page}` } return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) From 968e6c7fe8392970ddb93751cd8df551ac99c5f1 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 25 Mar 2019 21:04:52 +0200 Subject: [PATCH 2/4] correctly paginate on MastoAPI --- src/modules/users.js | 24 +++++++++---------- src/services/api/api.service.js | 24 ++++++++++++------- .../backend_interactor_service.js | 8 +++---- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/modules/users.js b/src/modules/users.js index 5cfa128e..61d05462 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -52,23 +52,23 @@ export const mutations = { state.loggingIn = false }, // TODO Clean after ourselves? - addFriends (state, { id, friends, page }) { + addFriends (state, { id, friends }) { const user = state.usersObject[id] each(friends, friend => { if (!find(user.friends, { id: friend.id })) { user.friends.push(friend) } }) - user.friendsPage = page + 1 + user.lastFriendId = friends.slice(-1)[0].id }, - addFollowers (state, { id, followers, page }) { + addFollowers (state, { id, followers }) { const user = state.usersObject[id] each(followers, follower => { if (!find(user.followers, { id: follower.id })) { user.followers.push(follower) } }) - user.followersPage = page + 1 + user.lastFollowerId = followers.slice(-1)[0].id }, // Because frontend doesn't have a reason to keep these stuff in memory // outside of viewing someones user profile. @@ -78,7 +78,7 @@ export const mutations = { return } user.friends = [] - user.friendsPage = 0 + user.lastFriendId = null }, clearFollowers (state, userId) { const user = state.usersObject[userId] @@ -86,7 +86,7 @@ export const mutations = { return } user.followers = [] - user.followersPage = 0 + user.lastFollowerId = null }, addNewUsers (state, users) { each(users, (user) => mergeOrAdd(state.users, state.usersObject, user)) @@ -219,10 +219,10 @@ const users = { addFriends ({ rootState, commit }, fetchBy) { return new Promise((resolve, reject) => { const user = rootState.users.usersObject[fetchBy] - const page = user.friendsPage || 1 - rootState.api.backendInteractor.fetchFriends({ id: user.id, page }) + const sinceId = user.lastFriendId + rootState.api.backendInteractor.fetchFriends({ id: user.id, sinceId }) .then((friends) => { - commit('addFriends', { id: user.id, friends, page }) + commit('addFriends', { id: user.id, friends }) resolve(friends) }).catch(() => { reject() @@ -231,10 +231,10 @@ const users = { }, addFollowers ({ rootState, commit }, fetchBy) { const user = rootState.users.usersObject[fetchBy] - const page = user.followersPage || 1 - return rootState.api.backendInteractor.fetchFollowers({ id: user.id, page }) + const sinceId = user.lastFollowerId + return rootState.api.backendInteractor.fetchFollowers({ id: user.id, sinceId }) .then((followers) => { - commit('addFollowers', { id: user.id, followers, page }) + commit('addFollowers', { id: user.id, followers }) return followers }) }, diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 2c5e9d60..a8de736c 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -276,11 +276,15 @@ const fetchUserRelationship = ({id, credentials}) => { }) } -const fetchFriends = ({id, page, credentials}) => { +const fetchFriends = ({id, maxId, sinceId, limit = 20, credentials}) => { let url = MASTODON_FOLLOWING_URL(id) - if (page) { - url = url + `?page=${page}` - } + const args = [ + maxId && `max_id=${maxId}`, + sinceId && `max_id=${sinceId}`, + limit && `limit=${limit}` + ].filter(_ => _).join('&') + + url = url + (args ? '?' + args : '') return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) .then((data) => data.map(parseUser)) @@ -293,11 +297,15 @@ const exportFriends = ({id, credentials}) => { .then((data) => data.map(parseUser)) } -const fetchFollowers = ({id, page, credentials}) => { +const fetchFollowers = ({id, maxId, sinceId, limit = 20, credentials}) => { let url = MASTODON_FOLLOWERS_URL(id) - if (page) { - url = url + `?page=${page}` - } + const args = [ + maxId && `max_id=${maxId}`, + sinceId && `max_id=${sinceId}`, + limit && `limit=${limit}` + ].filter(_ => _).join('&') + + url = url + (args ? '?' + args : '') return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) .then((data) => data.map(parseUser)) diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js index 0f0bcddc..71e78d2f 100644 --- a/src/services/backend_interactor_service/backend_interactor_service.js +++ b/src/services/backend_interactor_service/backend_interactor_service.js @@ -10,16 +10,16 @@ const backendInteractorService = (credentials) => { return apiService.fetchConversation({id, credentials}) } - const fetchFriends = ({id, page}) => { - return apiService.fetchFriends({id, page, credentials}) + const fetchFriends = ({id, maxId, sinceId, limit}) => { + return apiService.fetchFriends({id, maxId, sinceId, limit, credentials}) } const exportFriends = ({id}) => { return apiService.exportFriends({id, credentials}) } - const fetchFollowers = ({id, page}) => { - return apiService.fetchFollowers({id, page, credentials}) + const fetchFollowers = ({id, maxId, sinceId, limit}) => { + return apiService.fetchFollowers({id, maxId, sinceId, limit, credentials}) } const fetchAllFollowing = ({username}) => { From fb2aca06de230a1d52ab5ac50e8916c9d4a8decb Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 27 Mar 2019 22:02:46 +0200 Subject: [PATCH 3/4] review --- src/modules/users.js | 6 +++--- src/services/api/api.service.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/users.js b/src/modules/users.js index 61d05462..cc10b89d 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -1,5 +1,5 @@ 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 { registerPushNotifications, unregisterPushNotifications } from '../services/push/push.js' import oauthApi from '../services/new_api/oauth' @@ -59,7 +59,7 @@ export const mutations = { user.friends.push(friend) } }) - user.lastFriendId = friends.slice(-1)[0].id + user.lastFriendId = last(friends).id }, addFollowers (state, { id, followers }) { const user = state.usersObject[id] @@ -68,7 +68,7 @@ export const mutations = { user.followers.push(follower) } }) - user.lastFollowerId = followers.slice(-1)[0].id + user.lastFollowerId = last(followers).id }, // Because frontend doesn't have a reason to keep these stuff in memory // outside of viewing someones user profile. diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index a8de736c..cf4acdf7 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -280,7 +280,7 @@ const fetchFriends = ({id, maxId, sinceId, limit = 20, credentials}) => { let url = MASTODON_FOLLOWING_URL(id) const args = [ maxId && `max_id=${maxId}`, - sinceId && `max_id=${sinceId}`, + sinceId && `since_id=${sinceId}`, limit && `limit=${limit}` ].filter(_ => _).join('&') @@ -301,11 +301,11 @@ const fetchFollowers = ({id, maxId, sinceId, limit = 20, credentials}) => { let url = MASTODON_FOLLOWERS_URL(id) const args = [ maxId && `max_id=${maxId}`, - sinceId && `max_id=${sinceId}`, + sinceId && `since_id=${sinceId}`, limit && `limit=${limit}` ].filter(_ => _).join('&') - url = url + (args ? '?' + args : '') + url += args ? '?' + args : '' return fetch(url, { headers: authHeaders(credentials) }) .then((data) => data.json()) .then((data) => data.map(parseUser)) From 0a031aae20bf1dc3bc0b15af0b794798d179aafa Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Wed, 27 Mar 2019 22:04:59 +0200 Subject: [PATCH 4/4] errata --- src/modules/users.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modules/users.js b/src/modules/users.js index cc10b89d..1a507d31 100644 --- a/src/modules/users.js +++ b/src/modules/users.js @@ -219,8 +219,8 @@ const users = { addFriends ({ rootState, commit }, fetchBy) { return new Promise((resolve, reject) => { const user = rootState.users.usersObject[fetchBy] - const sinceId = user.lastFriendId - rootState.api.backendInteractor.fetchFriends({ id: user.id, sinceId }) + const maxId = user.lastFriendId + rootState.api.backendInteractor.fetchFriends({ id: user.id, maxId }) .then((friends) => { commit('addFriends', { id: user.id, friends }) resolve(friends) @@ -231,8 +231,8 @@ const users = { }, addFollowers ({ rootState, commit }, fetchBy) { const user = rootState.users.usersObject[fetchBy] - const sinceId = user.lastFollowerId - return rootState.api.backendInteractor.fetchFollowers({ id: user.id, sinceId }) + const maxId = user.lastFollowerId + return rootState.api.backendInteractor.fetchFollowers({ id: user.id, maxId }) .then((followers) => { commit('addFollowers', { id: user.id, followers }) return followers