2020-04-22 15:06:10 +03:00
|
|
|
const fetchRelationship = (attempt, userId, store) => new Promise((resolve, reject) => {
|
2019-02-09 23:05:23 -05:00
|
|
|
setTimeout(() => {
|
2020-04-22 15:06:10 +03:00
|
|
|
store.state.api.backendInteractor.fetchUserRelationship({ id: userId })
|
2020-04-21 23:27:51 +03:00
|
|
|
.then((relationship) => {
|
|
|
|
store.commit('updateUserRelationship', [relationship])
|
|
|
|
return relationship
|
|
|
|
})
|
2020-04-22 15:06:10 +03:00
|
|
|
.then((relationship) => resolve([relationship.following, relationship.requested, relationship.locked, attempt]))
|
2019-02-09 23:05:23 -05:00
|
|
|
.catch((e) => reject(e))
|
|
|
|
}, 500)
|
2019-08-09 14:18:46 +03:00
|
|
|
}).then(([following, sent, locked, attempt]) => {
|
2019-08-09 12:26:58 +00:00
|
|
|
if (!following && !(locked && sent) && attempt <= 3) {
|
2019-02-09 23:05:23 -05:00
|
|
|
// If we BE reports that we still not following that user - retry,
|
|
|
|
// increment attempts by one
|
2020-04-22 15:06:10 +03:00
|
|
|
fetchRelationship(++attempt, userId, store)
|
2019-02-09 23:05:23 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-22 15:06:10 +03:00
|
|
|
export const requestFollow = (userId, store) => new Promise((resolve, reject) => {
|
|
|
|
store.state.api.backendInteractor.followUser({ id: userId })
|
2019-02-09 23:05:23 -05:00
|
|
|
.then((updated) => {
|
2019-03-25 21:19:24 +02:00
|
|
|
store.commit('updateUserRelationship', [updated])
|
2019-02-09 23:05:23 -05:00
|
|
|
|
2020-04-22 15:06:10 +03:00
|
|
|
if (updated.following || (updated.locked && updated.requested)) {
|
2019-08-09 14:18:46 +03:00
|
|
|
// If we get result immediately or the account is locked, just stop.
|
2019-09-05 11:16:11 +03:00
|
|
|
resolve()
|
2019-08-09 14:18:46 +03:00
|
|
|
return
|
2019-02-09 23:05:23 -05: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.
|
|
|
|
|
2020-04-22 15:06:10 +03:00
|
|
|
return fetchRelationship(1, updated, store)
|
2019-09-05 11:16:11 +03:00
|
|
|
.then(() => {
|
|
|
|
resolve()
|
2019-02-09 23:05:23 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-22 15:06:10 +03:00
|
|
|
export const requestUnfollow = (userId, store) => new Promise((resolve, reject) => {
|
|
|
|
store.state.api.backendInteractor.unfollowUser({ id: userId })
|
2019-02-09 23:05:23 -05:00
|
|
|
.then((updated) => {
|
2019-03-25 21:19:24 +02:00
|
|
|
store.commit('updateUserRelationship', [updated])
|
2019-02-09 23:05:23 -05:00
|
|
|
resolve({
|
|
|
|
updated
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|