Add basic guild available updates

- add litecord.guild_memory_store
 - storage: add fetch of unavailable field
This commit is contained in:
Luna 2019-04-21 22:38:41 -03:00
parent 85749f2c8e
commit 4a50ceeb67
4 changed files with 33 additions and 5 deletions

View File

@ -17,11 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
from quart import Blueprint, jsonify, current_app as app from quart import Blueprint, jsonify, current_app as app, request
from litecord.auth import admin_check from litecord.auth import admin_check
# from litecord.schemas import validate from litecord.schemas import validate
# from litecord.admin_schemas import GUILD_UPDATE from litecord.admin_schemas import GUILD_UPDATE
bp = Blueprint('guilds_admin', __name__) bp = Blueprint('guilds_admin', __name__)
@ -34,16 +34,19 @@ async def get_guild(guild_id: int):
await app.storage.get_guild(guild_id) await app.storage.get_guild(guild_id)
) )
@bp.route('/<int:guild_id>', methods=['PATCH']) @bp.route('/<int:guild_id>', methods=['PATCH'])
async def update_guild(guild_id: int): async def update_guild(guild_id: int):
await admin_check() await admin_check()
# j = validate(await request.get_json(), GUILD_UPDATE) j = validate(await request.get_json(), GUILD_UPDATE)
# TODO: add guild availability update, we don't store it, should we?
# TODO: what happens to the other guild attributes when its # TODO: what happens to the other guild attributes when its
# unavailable? do they vanish? # unavailable? do they vanish?
if 'unavailable' in j:
app.guild_store.set(guild_id, 'unavailable', True)
guild = await app.storage.get_guild(guild_id) guild = await app.storage.get_guild(guild_id)
await app.dispatcher.dispatch_guild(guild_id, 'GUILD_UPDATE', guild) await app.dispatcher.dispatch_guild(guild_id, 'GUILD_UPDATE', guild)
return jsonify(guild) return jsonify(guild)

View File

@ -0,0 +1,17 @@
class GuildMemoryStore:
"""Store in-memory properties about guilds.
I could have just used Redis... probably too overkill to add
aioredis to the already long depedency list, plus, I don't need
"""
def __init__(self):
self._store = {}
def get(self, guild_id: int, attribute: str, default=None):
"""get a key"""
return self._store.get(f'{guild_id}:{attribute}', default)
def set(self, guild_id: int, attribute: str, value):
"""set a key"""
self._store[f'{guild_id}:{attribute}'] = value

View File

@ -192,6 +192,12 @@ class Storage:
drow['max_presences'] = 1000 drow['max_presences'] = 1000
drow['max_members'] = 1000 drow['max_members'] = 1000
# this is kept in memory
drow['unavailable'] = self.app.guild_store.get(
guild_id, 'unavailable', False)
# TODO: strip everything when unavailable
return drow return drow
async def _member_basic(self, guild_id: int, member_id: int): async def _member_basic(self, guild_id: int, member_id: int):

2
run.py
View File

@ -75,6 +75,7 @@ from litecord.presence import PresenceManager
from litecord.images import IconManager from litecord.images import IconManager
from litecord.jobs import JobManager from litecord.jobs import JobManager
from litecord.voice.manager import VoiceManager from litecord.voice.manager import VoiceManager
from litecord.guild_memory_store import GuildMemoryStore
from litecord.gateway.gateway import websocket_handler from litecord.gateway.gateway import websocket_handler
@ -246,6 +247,7 @@ def init_app_managers(app_):
app_.storage.presence = app_.presence app_.storage.presence = app_.presence
app_.voice = VoiceManager(app_) app_.voice = VoiceManager(app_)
app_.guild_store = GuildMemoryStore()
async def api_index(app_): async def api_index(app_):