1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mpyw\HardBotter\Traits; |
4
|
|
|
|
5
|
|
|
use mpyw\Co\Co; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method mixed post($endpoint, array $params = []) |
9
|
|
|
* @method \Generator postAsync($endpoint, array $params = []) |
10
|
|
|
*/ |
11
|
|
|
trait FollowManagerTrait |
12
|
|
|
{ |
13
|
|
|
abstract protected static function out($msg); |
14
|
|
|
abstract public function __call($method, array $args); |
15
|
|
|
abstract public function collect($endpoint, $followable_page_count, array $params = []); |
16
|
|
|
abstract public function collectAsync($endpoint, $followable_page_count, array $params = []); |
17
|
|
|
|
18
|
1 |
|
public function forceMutuals($followable_page_count = INF) |
19
|
|
|
{ |
20
|
|
|
list($friends, $followers) = [ |
21
|
1 |
|
$this->collect('friends/ids', $followable_page_count, ['stringify_ids' => true]), |
22
|
1 |
|
$this->collect('followers/ids', $followable_page_count, ['stringify_ids' => true]), |
23
|
|
|
]; |
24
|
1 |
|
list($friends_only, $followers_only) = static::getFriendsOnlyAndFollowersOnly($friends, $followers); |
25
|
1 |
|
$results = array_merge( |
26
|
1 |
|
array_map([$this, 'unfollow'], $friends_only), |
27
|
1 |
|
array_map([$this, 'follow'], $followers_only) |
28
|
|
|
); |
29
|
1 |
|
return !in_array(false, $results, true); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
public function forceMutualsAsync($followable_page_count = INF) |
33
|
|
|
{ |
34
|
|
|
list($friends, $followers) = (yield [ |
35
|
1 |
|
$this->collectAsync('friends/ids', $followable_page_count, ['stringify_ids' => true]), |
36
|
1 |
|
$this->collectAsync('followers/ids', $followable_page_count, ['stringify_ids' => true]), |
37
|
|
|
]); |
38
|
1 |
|
list($friends_only, $followers_only) = static::getFriendsOnlyAndFollowersOnly($friends, $followers); |
39
|
1 |
|
$results = (yield array_merge( |
40
|
1 |
|
array_map([$this, 'unfollowAsync'], $friends_only), |
41
|
1 |
|
array_map([$this, 'followAsync'], $followers_only) |
42
|
|
|
)); |
43
|
1 |
|
yield Co::RETURN_WITH => !in_array(false, $results, true); |
44
|
|
|
// @codeCoverageIgnoreStart |
45
|
|
|
} |
46
|
|
|
// @codeCoverageIgnoreEnd |
47
|
|
|
|
48
|
2 |
|
protected static function getFriendsOnlyAndFollowersOnly(array $friends, array $followers) |
49
|
|
|
{ |
50
|
2 |
|
$fr_flip = array_flip($friends); |
51
|
2 |
|
$fo_flip = array_flip($followers); |
52
|
2 |
|
$fr_only_flip = array_diff_key($fr_flip, $fo_flip); |
53
|
2 |
|
$fo_only_flip = array_diff_key($fo_flip, $fr_flip); |
54
|
2 |
|
$fr_only = array_keys($fr_only_flip); |
55
|
2 |
|
$fo_only = array_keys($fo_only_flip); |
56
|
2 |
|
return [$fr_only, $fo_only]; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function follow($user_id) |
60
|
|
|
{ |
61
|
1 |
|
$result = $this->post('friendships/create', ['user_id' => $user_id]); |
62
|
1 |
|
if ($result !== false) { |
63
|
1 |
|
static::out('FOLLOWED: @' . $result->screen_name); |
64
|
|
|
} |
65
|
1 |
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function followAsync($user_id) |
69
|
|
|
{ |
70
|
1 |
|
$result = (yield $this->postAsync('friendships/create', ['user_id' => $user_id])); |
71
|
1 |
|
if ($result !== false) { |
72
|
1 |
|
static::out('FOLLOWED: @' . $result->screen_name); |
73
|
|
|
} |
74
|
1 |
|
yield Co::RETURN_WITH => $result; |
75
|
|
|
// @codeCoverageIgnoreStart |
76
|
|
|
} |
77
|
|
|
// @codeCoverageIgnoreEnd |
78
|
|
|
|
79
|
1 |
|
public function unfollow($user_id) |
80
|
|
|
{ |
81
|
1 |
|
$result = $this->post('friendships/destroy', ['user_id' => $user_id]); |
82
|
1 |
|
if ($result !== false) { |
83
|
1 |
|
static::out('UNFOLLOWED: @' . $result->screen_name); |
84
|
|
|
} |
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function unfollowAsync($user_id) |
89
|
|
|
{ |
90
|
1 |
|
$result = (yield $this->postAsync('friendships/destroy', ['user_id' => $user_id])); |
91
|
1 |
|
if ($result !== false) { |
92
|
1 |
|
static::out('UNFOLLOWED: @' . $result->screen_name); |
93
|
|
|
} |
94
|
1 |
|
yield Co::RETURN_WITH => $result; |
95
|
|
|
// @codeCoverageIgnoreStart |
96
|
|
|
} |
97
|
|
|
// @codeCoverageIgnoreEnd |
98
|
|
|
} |
99
|
|
|
|