Skip to content

Commit

Permalink
Return text info to user in /authorize, not JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywink committed Aug 8, 2021
1 parent 2f5d1fe commit 100e825
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion maubot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ maubot: 0.1.2
id: me.jasonrobinson.pocket

# A PEP 440 compliant version string.
version: 0.1.16
version: 0.1.17

# The SPDX license identifier for the plugin. https://spdx.org/licenses/
# Optional, assumes all rights reserved if omitted.
Expand Down
18 changes: 11 additions & 7 deletions pocket/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Type, Dict, Optional, Tuple
from uuid import uuid4

from aiohttp.web import Request, Response, json_response
from aiohttp.web import Request, Response
from mautrix.types import UserID, EventType, ReactionEvent
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from maubot import Plugin, MessageEvent
Expand Down Expand Up @@ -61,11 +61,11 @@ async def archive(self, event: ReactionEvent, _: Tuple[str]) -> None:
async def authorize(self, request: Request) -> Response:
request_state = request.match_info.get("request_state")
if not request_state:
return json_response({}, status=400)
return Response(text=f"Failed to connect to Pocket, no request_state found.")

user = self.db.get_user_by_request_state(request_state)
if not user:
return json_response({}, status=400)
return Response(text=f"Failed to connect to Pocket, could not match request state to a request.")

data = await self.pocket_authorize(user.request_token)
if data.get("error_code"):
Expand All @@ -74,7 +74,10 @@ async def authorize(self, request: Request) -> Response:
user.request_room,
f"Failed to connect to Pocket, response code: {data.get('error_code')}",
)
return json_response({}, status=400)
return Response(
text=f"Failed to connect to Pocket, response code: {data.get('error_code')}. "
f"Please try giving the command again.",
)
try:
self.db.set_user_access_token(UserID(user.user_id), data.get("access_token"))
except DBAPIError as ex:
Expand All @@ -83,14 +86,15 @@ async def authorize(self, request: Request) -> Response:
user.request_room,
f"Failed to connect to Pocket due to database error. Please try again.",
)
return json_response({}, status=400)
return Response(
text="Failed to connect to Pocket due to database error. Please try giving the command again.",
)
self.log.info(f"User {user.user_id} successfully connected to Pocket")
await self.client.send_notice(
user.request_room,
f"Successfully connected to Pocket! Use `!pocket` to get a random article.",
)
# TODO make all responses render a web page, not JSON
return json_response({})
return Response(text="Successfully connected to Pocket! You can safely close this window.")

async def get_random_article(self, user: User) -> Optional[Dict]:
response = await self.http.post(
Expand Down

0 comments on commit 100e825

Please sign in to comment.