aboutsummaryrefslogtreecommitdiff
path: root/src/services/follow_manipulate/follow_manipulate.js
blob: b2486e7c17a888a608d90ad110b586768a254463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fetchUser = (attempt, user, store) => new Promise((resolve, reject) => {
  setTimeout(() => {
    store.state.api.backendInteractor.fetchUser({ id: user.id })
      .then((user) => store.commit('addNewUsers', [user]))
      .then(() => resolve([user.following, attempt]))
      .catch((e) => reject(e))
  }, 500)
}).then(([following, attempt]) => {
  if (!following && attempt <= 3) {
    // 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.
    return following
  }
})

export const requestFollow = (user, store) => new Promise((resolve, reject) => {
  store.state.api.backendInteractor.followUser(user.id)
    .then((updated) => {
      store.commit('updateUserRelationship', [updated])

      // For locked users we just mark it that we sent the follow request
      if (updated.locked) {
        resolve({ sent: true })
      }

      if (updated.following) {
        // If we get result immediately, just stop.
        resolve({ sent: false })
      }

      // 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)
        .then((following) => {
          if (following) {
            // We confirmed and everything's good.
            resolve({ sent: false })
          } else {
            // If after all the tries, just treat it as if user is locked
            resolve({ sent: false })
          }
        })
    })
})

export const requestUnfollow = (user, store) => new Promise((resolve, reject) => {
  store.state.api.backendInteractor.unfollowUser(user.id)
    .then((updated) => {
      store.commit('updateUserRelationship', [updated])
      resolve({
        updated
      })
    })
})