From 6d6f779d26f3d114a5db10e5506f2979c79cd520 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 9 Jan 2020 15:17:51 +0100 Subject: [PATCH] Move plan actions into Plan --- fbchat/_client.py | 68 +---------------------------------------- fbchat/_plan.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++- fbchat/_thread.py | 18 ++--------- 3 files changed, 79 insertions(+), 84 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 2d98e98..581d762 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -24,7 +24,7 @@ from ._quick_reply import ( QuickReplyEmail, ) from ._poll import Poll, PollOption -from ._plan import ACONTEXT, PlanData +from ._plan import PlanData class Client: @@ -632,22 +632,6 @@ class Client: j = self._payload_post("/ajax/mercury/get_poll_options", data) return [PollOption._from_graphql(m) for m in j] - def fetch_plan_info(self, plan_id): - """Fetch `Plan` object from the plan id. - - Args: - plan_id: Plan ID to fetch from - - Returns: - Plan: `Plan` object - - Raises: - FBchatException: If request failed - """ - data = {"event_reminder_id": plan_id} - j = self._payload_post("/ajax/eventreminder", data) - return PlanData._from_fetch(self.session, j) - def _get_private_data(self): (j,) = self.graphql_requests(_graphql.from_doc_id("1868889766468115", {})) return j["viewer"] @@ -696,56 +680,6 @@ class Client: SEND METHODS """ - def edit_plan(self, plan, new_plan): - """Edit a plan. - - Args: - plan (Plan): Plan to edit - new_plan: New plan - - Raises: - FBchatException: If request failed - """ - data = { - "event_reminder_id": plan.id, - "delete": "false", - "date": _util.datetime_to_seconds(new_plan.time), - "location_name": new_plan.location or "", - "location_id": new_plan.location_id or "", - "title": new_plan.title, - "acontext": ACONTEXT, - } - j = self._payload_post("/ajax/eventreminder/submit", data) - - def delete_plan(self, plan): - """Delete a plan. - - Args: - plan: Plan to delete - - Raises: - FBchatException: If request failed - """ - data = {"event_reminder_id": plan.id, "delete": "true", "acontext": ACONTEXT} - j = self._payload_post("/ajax/eventreminder/submit", data) - - def change_plan_participation(self, plan, take_part=True): - """Change participation in a plan. - - Args: - plan: Plan to take part in or not - take_part: Whether to take part in the plan - - Raises: - FBchatException: If request failed - """ - data = { - "event_reminder_id": plan.id, - "guest_state": "GOING" if take_part else "DECLINED", - "acontext": ACONTEXT, - } - j = self._payload_post("/ajax/eventreminder/rsvp", data) - def update_poll_vote(self, poll_id, option_ids=[], new_options=[]): """Update a poll vote. diff --git a/fbchat/_plan.py b/fbchat/_plan.py index 5304ebd..2b71a89 100644 --- a/fbchat/_plan.py +++ b/fbchat/_plan.py @@ -1,7 +1,8 @@ import attr +import datetime import json from ._core import attrs_default, Enum -from . import _util, _session +from . import _exception, _util, _session class GuestStatus(Enum): @@ -26,6 +27,80 @@ class Plan: #: The plan's unique identifier. id = attr.ib(converter=str) + def fetch(self) -> "PlanData": + """Fetch fresh `PlanData` object.""" + data = {"event_reminder_id": self.id} + j = self.session._payload_post("/ajax/eventreminder", data) + return PlanData._from_fetch(self.session, j) + + @classmethod + def _create( + cls, + thread, + name: str, + at: datetime.datetime, + location_name: str = None, + location_id: str = None, + ): + data = { + "event_type": "EVENT", + "event_time": _util.datetime_to_seconds(at), + "title": name, + "thread_id": thread.id, + "location_id": location_id or "", + "location_name": location_name or "", + "acontext": ACONTEXT, + } + j = thread.session._payload_post("/ajax/eventreminder/create", data) + if "error" in j: + raise _exception.FBchatFacebookError( + "Failed creating plan: {}".format(j["error"]), + fb_error_message=j["error"], + ) + + def edit( + self, + name: str, + at: datetime.datetime, + location_name: str = None, + location_id: str = None, + ): + """Edit the plan. + + # TODO: Arguments + """ + data = { + "event_reminder_id": self.id, + "delete": "false", + "date": _util.datetime_to_seconds(at), + "location_name": location_name or "", + "location_id": location_id or "", + "title": name, + "acontext": ACONTEXT, + } + j = self.session._payload_post("/ajax/eventreminder/submit", data) + + def delete(self): + """Delete the plan.""" + data = {"event_reminder_id": self.id, "delete": "true", "acontext": ACONTEXT} + j = self.session._payload_post("/ajax/eventreminder/submit", data) + + def _change_participation(self): + data = { + "event_reminder_id": self.id, + "guest_state": "GOING" if take_part else "DECLINED", + "acontext": ACONTEXT, + } + j = self.session._payload_post("/ajax/eventreminder/rsvp", data) + + def participate(self): + """Set yourself as GOING/participating to the plan.""" + self._change_participation(True) + + def decline(self): + """Set yourself as having DECLINED the plan.""" + self._change_participation(False) + @attrs_default class PlanData(Plan): diff --git a/fbchat/_thread.py b/fbchat/_thread.py index d8911d8..dd11ab6 100644 --- a/fbchat/_thread.py +++ b/fbchat/_thread.py @@ -381,24 +381,10 @@ class ThreadABC(metaclass=abc.ABCMeta): # TODO: Arguments Args: - title: Name of the new plan + name: Name of the new plan at: When the plan is for """ - data = { - "event_type": "EVENT", - "event_time": _util.datetime_to_seconds(at), - "title": name, - "thread_id": self.id, - "location_id": location_id or "", - "location_name": location_name or "", - "acontext": _plan.ACONTEXT, - } - j = self.session._payload_post("/ajax/eventreminder/create", data) - if "error" in j: - raise _exception.FBchatFacebookError( - "Failed creating plan: {}".format(j["error"]), - fb_error_message=j["error"], - ) + return _plan.Plan._create(self, name, at, location_name, location_id) def create_poll(self, question: str, options=Iterable[Tuple[str, bool]]): """Create poll in a thread.