2019-02-10 04:05:23 +00:00
|
|
|
const fetchUser = (attempt, user, store) => new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
store.state.api.backendInteractor.fetchUser({ id: user.id })
|
|
|
|
.then((user) => store.commit('addNewUsers', [user]))
|
2019-08-09 11:18:46 +00:00
|
|
|
.then(() => resolve([user.following, user.requested, user.locked, attempt]))
|
2019-02-10 04:05:23 +00:00
|
|
|
.catch((e) => reject(e))
|
|
|
|
}, 500)
|
2019-08-09 11:18:46 +00:00
|
|
|
}).then(([following, sent, locked, attempt]) => {
|
2019-08-09 12:26:58 +00:00
|
|
|
if (!following && !(locked && sent) && attempt <= 3) {
|
2019-02-10 04:05:23 +00:00
|
|
|
// If we BE reports that we still not following that user - retry,
|
|
|
|
// increment attempts by one
|
|
|
|
return fetchUser(++attempt, user, store)
|
|
|
|
} else {
|
|
|
|
// If we run out of attempts, just return whatever status is.
|
2019-08-09 11:18:46 +00:00
|
|
|
return sent
|
2019-02-10 04:05:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
export const requestFollow = (user, store) => new Promise((resolve, reject) => {
|
|
|
|
store.state.api.backendInteractor.followUser(user.id)
|
|
|
|
.then((updated) => {
|
2019-03-25 19:19:24 +00:00
|
|
|
store.commit('updateUserRelationship', [updated])
|
2019-02-10 04:05:23 +00:00
|
|
|
|
2019-08-09 12:25:58 +00:00
|
|
|
if (updated.following || (user.locked && user.requested)) {
|
2019-08-09 11:18:46 +00:00
|
|
|
// If we get result immediately or the account is locked, just stop.
|
|
|
|
resolve({ sent: updated.requested })
|
|
|
|
return
|
2019-02-10 04:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// But usually we don't get result immediately, so we ask server
|
|
|
|
// for updated user profile to confirm if we are following them
|
|
|
|
// Sometimes it takes several tries. Sometimes we end up not following
|
|
|
|
// user anyway, probably because they locked themselves and we
|
|
|
|
// don't know that yet.
|
|
|
|
// Recursive Promise, it will call itself up to 3 times.
|
|
|
|
|
|
|
|
return fetchUser(1, user, store)
|
2019-08-09 11:18:46 +00:00
|
|
|
.then((sent) => {
|
2019-08-09 12:01:57 +00:00
|
|
|
resolve({ sent })
|
2019-02-10 04:05:23 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
export const requestUnfollow = (user, store) => new Promise((resolve, reject) => {
|
|
|
|
store.state.api.backendInteractor.unfollowUser(user.id)
|
|
|
|
.then((updated) => {
|
2019-03-25 19:19:24 +00:00
|
|
|
store.commit('updateUserRelationship', [updated])
|
2019-02-10 04:05:23 +00:00
|
|
|
resolve({
|
|
|
|
updated
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|