Skip to content

Commit

Permalink
Add query info for token. fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunicorn committed May 11, 2024
1 parent 24b8c43 commit f2f6e21
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
8 changes: 6 additions & 2 deletions synapse_super_invites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from twisted.web.static import File

from .config import SynapseSuperInvitesConfig, run_alembic
from .resource import RedeemResource, TokensResource, WebAccessResource
from .resource import RedeemResource, TokenInfoResource, TokensResource, WebAccessResource

__version__ = "0.8.0"
__version__ = "0.8.3"

PKG_DIR = os.path.dirname(os.path.realpath(__file__))

Expand All @@ -26,6 +26,10 @@ def __init__(self, config: SynapseSuperInvitesConfig, api: ModuleApi):
self.setup()

def setup(self) -> None:
self._api.register_web_resource(
"/_synapse/client/super_invites/info",
TokenInfoResource(self._config, self._api, self._sessions),
)
self._api.register_web_resource(
"/_synapse/client/super_invites/tokens",
TokensResource(self._config, self._api, self._sessions),
Expand Down
1 change: 1 addition & 0 deletions synapse_super_invites/resource/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .redeem import RedeemResource
from .info import TokenInfoResource
from .tokens import TokensResource
from .web_access import WebAccessResource
47 changes: 47 additions & 0 deletions synapse_super_invites/resource/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from sqlalchemy import func, select
from synapse.http.servlet import parse_json_object_from_request, parse_string
from synapse.http.site import SynapseRequest
from synapse.types import JsonDict, Tuple, UserID # type: ignore[attr-defined]

from synapse_super_invites.model import Room, Token, Accepted

from .base import SuperInviteResourceBase, can_edit_token, serialize_token, token_query


class TokenInfoResource(SuperInviteResourceBase):

async def _async_render_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
requester = await self.api.get_user_by_req(request, allow_guest=False)
my_id = str(requester.user)
token_id = parse_string(request, "token", required=True)
with self.db.begin() as session:
token = session.scalar(select(Token).where(
Token.token == token_id # noqa: E711
))
if not token:
return 403, {"error": "Token not found", "errcode": "NOT_FOUND"}

if token.deleted_at != None:
return 403, {"error": "Token not longer valid", "errcode": "CANT_REDEEM"}

has_redeemed = session.scalar(
select(Accepted).where(Accepted.user == my_id, Accepted.token == token)
) != None

rooms_count = len(token.rooms)
if token.create_dm:
rooms_count += 1

user_id = token.owner
owner_info = await self.api._store.get_profileinfo(UserID.from_string(user_id))

return 200, {
"rooms_count": rooms_count,
"has_redeemed": has_redeemed,
"create_dm": token.create_dm,
"inviter": {
"user_id": user_id,
"display_name": owner_info.display_name,
"avatar_url": owner_info.avatar_url,
}
}
33 changes: 32 additions & 1 deletion tests/test_integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ def test_simple_invite_token_test(self) -> None:
_f_id = self.register_user("flit", "flit")
f_access_token = self.login("flit", "flit")


channel = self.make_request(
"GET",
"/_synapse/client/super_invites/info?token={token}".format(token=token),
access_token=f_access_token,
)
self.assertEqual(channel.code, 200, msg=channel.result)
# list the rooms we were invited to

self.assertEqual(channel.json_body["rooms_count"], 3)
self.assertEqual(channel.json_body["create_dm"], False)
self.assertEqual(channel.json_body["has_redeemed"], False)
self.assertEqual(channel.json_body["inviter"]["user_id"], '@meeko:test')
self.assertEqual(channel.json_body["inviter"]["display_name"], 'meeko')

channel = self.make_request(
"POST",
"/_synapse/client/super_invites/redeem?token={token}".format(token=token),
Expand All @@ -192,6 +207,14 @@ def test_simple_invite_token_test(self) -> None:
token_data = channel.json_body["token"]
self.assertEquals(token_data["accepted_count"], 1)

channel = self.make_request(
"GET",
"/_synapse/client/super_invites/info?token={token}".format(token=token),
access_token=f_access_token,
)
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertEqual(channel.json_body["has_redeemed"], True)

# and flit was invited to these, too:
channel = self.make_request(
"GET", "/_matrix/client/v3/sync", access_token=f_access_token
Expand Down Expand Up @@ -606,10 +629,18 @@ def test_deletion(self) -> None:
self.assertEqual(channel.code, 200, msg=channel.result)
self.assertEqual(channel.json_body["tokens"], [])

# redeem the new token
# trying to redeem token
_f_id = self.register_user("flit", "flit")
f_access_token = self.login("flit", "flit")

# and it can't be found
channel = self.make_request(
"GET",
"/_synapse/client/super_invites/info?token={token}".format(token=token),
access_token=f_access_token,
)
self.assertEqual(channel.code, 403, msg=channel.result) # access denied

# and it can't be redeemed
channel = self.make_request(
"POST",
Expand Down

0 comments on commit f2f6e21

Please sign in to comment.