DEV Community

Thyrst'
Thyrst'

Posted on

How to Unsubscribe from All Users on DEV.to

Funny title for my first post here.

I created my user profile on DEV.to 4 years ago and didn't really use it back then. Now that I've returned, I find myself following 50 people in whom I'm no longer interested.

There is no option to unsubscribe all, nor is there a comfortable way to do it manually (I would have to open profile page of everyone). Naturally this led me to explore the DevTools' network tab to see if there is an easy way to do this in the console. And there is, here is how:

  1. Go to https://dev.to/dashboard/following_users
  2. Run following script in the DevTools console:
async function fetchFollowedList(page) {
    const signature = parseInt(Date.now() / 4e5, 10);
    const response = await fetch(`https://dev.to/followings/users?page=${page}&controller_action=following_users&signature=${signature}`);
    if (!response.ok) {
        throw new Error(`Failed to fetch followed list: ${response.statusText}`);
    }
    return response.json();
}

async function unfollowUser(userId, csrfToken) {
    const response = await fetch('https://dev.to/follows', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-CSRF-Token': csrfToken,
        },
        body: `followable_type=User&followable_id=${userId}&verb=unfollow&authenticity_token=${encodeURIComponent(csrfToken)}`,
        credentials: 'include',
    });

    if (!response.ok) {
        console.error(`Failed to unfollow user ID ${userId}: ${response.statusText}`);
    }
}

async function unfollowAllUsers() {
    const csrfToken = window.csrfToken; // Retrieve the CSRF token from the window object
    let page = 0;
    let hasMore = true;

    while (hasMore) {
        try {
            const followedList = await fetchFollowedList(page);
            if (followedList.length === 0) {
                hasMore = false;
                break;
            }

            for (const user of followedList) {
                await unfollowUser(user.user_id, csrfToken);
                console.log(`Unfollowed user ID ${user.user_id}`);
            }

            page++;
        } catch (error) {
            console.error(error);
            break;
        }
    }
}

unfollowAllUsers();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)