Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added remove friend #298

Merged
merged 9 commits into from
Jun 20, 2018
Merged

Added remove friend #298

merged 9 commits into from
Jun 20, 2018

Conversation

ThatAlexanderA
Copy link
Contributor

@ThatAlexanderA ThatAlexanderA commented Jun 4, 2018

  • Added remove friend url to the utils.py
  • Added removeFriend def to the client.py

#294

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made a few comments, and I fully understand if it seems confusing, the current system that _post and _get uses is super confusing, so feel free to ask me anything if you need clarification. Thanks for your contribution ;)

fbchat/client.py Outdated

"""
def removeFriend(self, friend_id=None):
"""param friend_id: The id of the friend that you want to remove"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could add a short description of the method, along with a documentation of the return value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add it soon.

fbchat/client.py Outdated
"confirm": "Confirm",
}
r = self._post(self.req_url.REMOVE_FRIEND,payload)
return r.ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If r.ok is not true, an exception could have been thrown by using fix_request=False in Client._post. Maybe the return value should be used for detecting whether the removal of the person actually succeeded? I tested four scenarios:
If the client isn't logged in, it's redirected to the login page
If the client is logged in, but hasn't been active in a while, it's presented with an error message (No redirect)
If the operation was successful, the client is redirected to /removefriend.php?friend_id=[friend_id]&removed&__req=e&_rdr
And if the friend_id was invalid, it's redirected to /removefriend.php?friend_id=[friend_id]&removed&err=1431010&__req=e&_rdr

So, I think we should update _post to support the parameter allow_redirects, just like _cleanGet does, then use that, look at r['Location'], and determine whether the redirect url starts with /removefriend. If it does, but contains the err parameter, we can either throw an error, or just return False.

Copy link
Contributor Author

@ThatAlexanderA ThatAlexanderA Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I allowed redirects in the _post, but I can't get the location, only with r.url. Can I use that?

Edit: Can we use this?
if "err=1431004" not in r.url: code here else: code here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of disabling redirects and using r['Location'] would be to reduce the number of requests that we need to send, but you can start by implementing it without, and just use r.url.

Considering testing the error code, though your described method would probably work, I would argue we should use two functions from urllib.parse, parse_qs and urlparse, and look at whether the dict parse_qs(urlparse(url).query) contains an error code.

@ThatAlexanderA
Copy link
Contributor Author

I updated the code. I used print for tell if the remove was successful, but we can use something different if you want.

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, now all I have is nitpicky comments. Also, maybe the entire method should be moved to just below the friendConnect method? Seems like it makes more sense in that group

fbchat/client.py Outdated
payload = {
"friend_id": friend_id,
"unref": "none",
"confirm": "Confirm",
}
r = self._post(self.req_url.REMOVE_FRIEND,payload)
return r.ok
dict = parse_qs(urlparse(r.url).query)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful about overwriting dict, since its a builtin. Use a different, perhaps even a more descriptive name, like query

fbchat/client.py Outdated
return r.ok
dict = parse_qs(urlparse(r.url).query)
if "err" not in dict:
print ("Remove was successful!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel like it's needed, log output can be useful, but don't use print, since people can't selectively disable the log output. Use log.info or log.debug here instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that you addressed this in your comment

fbchat/client.py Outdated
Removes a specifed friend from your friend list
:param friend_id: The id of the friend that you want to remove
:returns error when the removing was unsuccessful
:returns True when the removing was successful
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The syntax in the docstrings is quite important, since it gets executed and published on RTFD. :returns is wrong, it should be :return:, and there should only be one of them

fbchat/client.py Outdated
from urllib.parse import parse_qs
except ImportError:
from urlparse import urlparse
from urlparse import parse_qs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When importing two things from the same module you can use commas, like this: from urlparse import urlparse, parse_qs

@ThatAlexanderA
Copy link
Contributor Author

I updated the code. :)

@madsmtm
Copy link
Member

madsmtm commented Jun 7, 2018

Looks good, I'll merge it as soon as I get TravisCI working again

@ThatAlexanderA
Copy link
Contributor Author

Okay, thank you! :)

@madsmtm madsmtm merged commit 51d606a into fbchat-dev:master Jun 20, 2018
madsmtm added a commit that referenced this pull request Jun 20, 2018
* Added `removeFriend` method, #298
* Removed `lxml` from dependencies, #301
* Moved configuration to setup.cfg instead of setup.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants