-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters