|
| 1 | +"""MIT License |
| 2 | +
|
| 3 | +Copyright (c) 2023 PythonistaGuild |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +""" |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import logging |
| 26 | +from typing import Any, TYPE_CHECKING |
| 27 | + |
| 28 | +from starlette.authentication import requires |
| 29 | +from starlette.requests import Request |
| 30 | +from starlette.responses import JSONResponse, Response |
| 31 | +from starlette.websockets import WebSocket |
| 32 | + |
| 33 | +import core |
| 34 | + |
| 35 | + |
| 36 | +if TYPE_CHECKING: |
| 37 | + from api.server import Server |
| 38 | + |
| 39 | + |
| 40 | +logger: logging.Logger = logging.getLogger(__name__) |
| 41 | + |
| 42 | + |
| 43 | +class Members(core.View): |
| 44 | + def __init__(self, app: Server) -> None: |
| 45 | + self.app = app |
| 46 | + |
| 47 | + @core.route('/dpy/modlog', methods=['POST']) |
| 48 | + @requires('member') |
| 49 | + async def post_dpy_modlog(self, request: Request) -> Response: |
| 50 | + application: core.ApplicationModel = request.user.model |
| 51 | + |
| 52 | + try: |
| 53 | + data = await request.json() |
| 54 | + except Exception as e: |
| 55 | + logger.debug(f'Received bad JSON in "/members/dpy/modlog": {e}') |
| 56 | + return JSONResponse({'error': 'Bad POST JSON Body.'}, status_code=400) |
| 57 | + |
| 58 | + payload: dict[str, Any] = { |
| 59 | + 'op': core.WebsocketOPCodes.EVENT, |
| 60 | + 'subscription': core.WebsocketSubscriptions.DPY_MOD_LOG, |
| 61 | + 'application': application.uid, |
| 62 | + 'application_name': application.name, |
| 63 | + 'payload': data |
| 64 | + } |
| 65 | + |
| 66 | + count = 0 |
| 67 | + for subscriber in self.app.subscription_sockets[core.WebsocketSubscriptions.DPY_MOD_LOG]: |
| 68 | + websocket: WebSocket = self.app.sockets[subscriber] |
| 69 | + |
| 70 | + payload['user_id'] = subscriber |
| 71 | + |
| 72 | + try: |
| 73 | + await websocket.send_json(data=payload) |
| 74 | + except Exception as e: |
| 75 | + logger.debug(f'Failed to send payload to websocket "{subscriber}": {e}') |
| 76 | + else: |
| 77 | + count += 1 |
| 78 | + |
| 79 | + to_send: dict[str, int] = { |
| 80 | + 'subscribers': len(self.app.subscription_sockets[core.WebsocketSubscriptions.DPY_MOD_LOG]), |
| 81 | + 'successful': count |
| 82 | + } |
| 83 | + return JSONResponse(to_send, status_code=200) |
0 commit comments