diff --git a/.gitignore b/.gitignore index 51f1010..ae2b325 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ access-token email pass -vk_auth_token.temp \ No newline at end of file +vk_auth_token.temp +friends-state.json \ No newline at end of file diff --git a/Application/acceptAllFriendRequests.js b/Application/acceptAllFriendRequests.js index 6ef3958..e941eaf 100644 --- a/Application/acceptAllFriendRequests.js +++ b/Application/acceptAllFriendRequests.js @@ -1,8 +1,13 @@ var requests = API.friends.getRequests({ count: 23 }).items; var i = 0; +var accepted = []; +var errors = []; while(i < requests.length) { - API.friends.add({ user_id: requests[i] }); + var result = API.friends.add({ user_id: requests[i] }); + if (result == 1 || result == 2 || result == 4) { + accepted.push(requests[i]); + } i = i + 1; } -return requests; +return { "accepted": accepted, "total_requests": requests.length }; diff --git a/Application/getFriendsCount.js b/Application/getFriendsCount.js new file mode 100644 index 0000000..9978a13 --- /dev/null +++ b/Application/getFriendsCount.js @@ -0,0 +1 @@ +return API.friends.get({ count: 1 }).count; diff --git a/do-all.sh b/do-all.sh index c4cf4ae..6d30d48 100755 --- a/do-all.sh +++ b/do-all.sh @@ -1,7 +1,8 @@ #!/bin/bash set -e -./delete-out-friend-request.sh +# Initial run with friends limit management +./manage-friends-limit.sh sleep 16 @@ -9,19 +10,16 @@ sleep 16 sleep 16 -./accept-all-friend-requests-once.sh - +# Main loop with friends limit checks while : do sleep 600 - ./delete-out-friend-request.sh + ./manage-friends-limit.sh sleep 600 ./delete-first-deactivated-friend.sh sleep 600 - - ./accept-all-friend-requests-once.sh done diff --git a/experiments/test-friends-limit-logic.sh b/experiments/test-friends-limit-logic.sh new file mode 100755 index 0000000..8756d16 --- /dev/null +++ b/experiments/test-friends-limit-logic.sh @@ -0,0 +1,87 @@ +#!/bin/bash +# Test script for friends limit logic +# This script simulates different scenarios to test the friends limit handling + +set -e + +echo "Testing friends limit management logic..." +echo "==========================================" +echo "" + +# Test 1: Check if get-friends-count.sh works +echo "Test 1: Check friends count retrieval" +echo "--------------------------------------" +if [ -f "../get-friends-count.sh" ]; then + echo "✓ get-friends-count.sh exists" +else + echo "✗ get-friends-count.sh not found" + exit 1 +fi + +# Test 2: Check if manage-friends-limit.sh exists +echo "" +echo "Test 2: Check manage-friends-limit.sh exists" +echo "---------------------------------------------" +if [ -f "../manage-friends-limit.sh" ]; then + echo "✓ manage-friends-limit.sh exists" + if [ -x "../manage-friends-limit.sh" ]; then + echo "✓ manage-friends-limit.sh is executable" + else + echo "✗ manage-friends-limit.sh is not executable" + exit 1 + fi +else + echo "✗ manage-friends-limit.sh not found" + exit 1 +fi + +# Test 3: Check if VK API scripts are present +echo "" +echo "Test 3: Check VK API scripts" +echo "-----------------------------" +for script in "Application/getFriendsCount.js" "Application/acceptAllFriendRequests.js" "Application/deleteInactiveFriends.js" "Application/deleteOutFriendRequests.js"; do + if [ -f "../$script" ]; then + echo "✓ $script exists" + else + echo "✗ $script not found" + exit 1 + fi +done + +# Test 4: Check updated acceptAllFriendRequests.js structure +echo "" +echo "Test 4: Validate acceptAllFriendRequests.js changes" +echo "----------------------------------------------------" +if grep -q "accepted" "../Application/acceptAllFriendRequests.js" && grep -q "total_requests" "../Application/acceptAllFriendRequests.js"; then + echo "✓ acceptAllFriendRequests.js has been updated with proper return structure" +else + echo "✗ acceptAllFriendRequests.js missing expected changes" + exit 1 +fi + +# Test 5: Check .gitignore +echo "" +echo "Test 5: Check .gitignore includes friends-state.json" +echo "------------------------------------------------------" +if grep -q "friends-state.json" "../.gitignore"; then + echo "✓ friends-state.json is in .gitignore" +else + echo "✗ friends-state.json not in .gitignore" + exit 1 +fi + +# Test 6: Check do-all.sh integration +echo "" +echo "Test 6: Check do-all.sh uses manage-friends-limit.sh" +echo "-----------------------------------------------------" +if grep -q "manage-friends-limit.sh" "../do-all.sh"; then + echo "✓ do-all.sh integrated with manage-friends-limit.sh" +else + echo "✗ do-all.sh not using manage-friends-limit.sh" + exit 1 +fi + +echo "" +echo "==========================================" +echo "All tests passed! ✓" +echo "==========================================" diff --git a/get-friends-count.sh b/get-friends-count.sh new file mode 100755 index 0000000..cba54a8 --- /dev/null +++ b/get-friends-count.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +ACCESS_TOKEN=`cat access-token` + +./execute.sh "https://api.vk.com/method/execute.getFriendsCount?access_token=${ACCESS_TOKEN}&v=5.131" + +echo diff --git a/manage-friends-limit.sh b/manage-friends-limit.sh new file mode 100755 index 0000000..f920700 --- /dev/null +++ b/manage-friends-limit.sh @@ -0,0 +1,87 @@ +#!/bin/bash +set -e + +# Configuration +FRIENDS_LIMIT=10000 +STATE_FILE="friends-state.json" +SAFETY_MARGIN=50 # Keep some room below the limit + +# Get current friends count from VK API +get_friends_count() { + local result=$(./get-friends-count.sh 2>/dev/null | jq -r '.response // 0') + echo "$result" +} + +# Save state to file +save_state() { + local count=$1 + local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") + echo "{\"friends_count\": $count, \"last_updated\": \"$timestamp\"}" > "$STATE_FILE" +} + +# Load state from file +load_state() { + if [ -f "$STATE_FILE" ]; then + cat "$STATE_FILE" | jq -r '.friends_count // 0' + else + echo "0" + fi +} + +# Check if we're at or near the limit +is_at_limit() { + local count=$1 + if [ "$count" -ge $((FRIENDS_LIMIT - SAFETY_MARGIN)) ]; then + return 0 # true, at limit + else + return 1 # false, not at limit + fi +} + +# Main logic +echo "Checking friends count..." +CURRENT_COUNT=$(get_friends_count) +echo "Current friends count: $CURRENT_COUNT" + +# Save state +save_state "$CURRENT_COUNT" + +if is_at_limit "$CURRENT_COUNT"; then + echo "⚠️ Friends limit reached ($CURRENT_COUNT >= $((FRIENDS_LIMIT - SAFETY_MARGIN)))" + echo "Starting cleanup process..." + + echo "Step 1: Canceling outgoing friend requests..." + ./delete-out-friend-request.sh + + echo "Step 2: Deleting inactive friends..." + # Try to delete inactive friends in batches + for offset in {0..9}; do + echo " Deleting inactive friends batch $offset..." + ./delete-inactive-friends.sh "$offset" || true + sleep 2 + done + + # Update count after cleanup + echo "Updating friends count after cleanup..." + CURRENT_COUNT=$(get_friends_count) + echo "Friends count after cleanup: $CURRENT_COUNT" + save_state "$CURRENT_COUNT" + + if is_at_limit "$CURRENT_COUNT"; then + echo "⚠️ Still at limit after cleanup. Skipping friend request acceptance." + exit 0 + else + echo "✓ Cleanup successful. Below limit now." + fi +else + echo "✓ Friends count is below limit." +fi + +# Only accept friend requests if we're below the limit +echo "Accepting friend requests..." +./accept-all-friend-requests-once.sh + +# Final count update +FINAL_COUNT=$(get_friends_count) +echo "Final friends count: $FINAL_COUNT" +save_state "$FINAL_COUNT"