Transition over to use db instead of fetching discord REST

This commit is contained in:
Jeremy Zhang 2017-05-06 18:19:20 -07:00
parent 9c24093901
commit fc169c2063
4 changed files with 47 additions and 24 deletions

View File

@ -145,6 +145,7 @@ class DatabaseInterface(object):
"color": role.color.value,
"hoist": role.hoist,
"position": role.position,
"permissions": role.permissions.value
})
return roles

View File

@ -1,4 +1,4 @@
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, get_channel_messages
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, GuildMembers, get_channel_messages, list_all_guild_members
from titanembeds.decorators import valid_session_required, discord_users_only
from titanembeds.utils import check_guild_existance, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
@ -27,9 +27,8 @@ def checkUserRevoke(guild_id, user_key=None):
banned = checkUserBanned(guild_id)
if banned:
return revoked
member = discord_api.get_guild_member_nocache(guild_id, session['user_id'])
if member['code'] == 200:
revoked = False
dbUser = GuildMembers.query.filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == session["user_id"]).first()
revoked = not dbUser.active
return revoked
def checkUserBanned(guild_id, ip_address=None):
@ -44,10 +43,8 @@ def checkUserBanned(guild_id, ip_address=None):
banned = False
else:
banned = False
bans = discord_api.get_guild_bans(guild_id)['content']
for user in bans:
if session['user_id'] == user['user']['id']:
return True
dbUser = GuildMembers.query.filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == session["user_id"]).first()
banned = dbUser.banned
return banned
def update_user_status(guild_id, username, user_key=None):
@ -97,7 +94,7 @@ def check_user_in_guild(guild_id):
return guild_id in session['user_keys']
else:
dbUser = db.session.query(AuthenticatedUsers).filter(and_(AuthenticatedUsers.guild_id == guild_id, AuthenticatedUsers.client_id == session['user_id'])).first()
return 200 == discord_api.get_guild_member_nocache(guild_id, session['user_id'])['code'] and dbUser is not None
return not checkUserRevoke(guild_id) and dbUser is not None
def format_post_content(message):
message = message.replace("<", "\<")
@ -114,20 +111,28 @@ def format_post_content(message):
message = "**<{}#{}>** {}".format(session['username'], session['discriminator'], message) # I would like to do a @ mention, but i am worried about notif spam
return message
def get_member_roles(guild_id, user_id):
q = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == user_id).first()
return json.loads(q.roles)
def get_dbguild_channels(guild_id):
q = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
return json.loads(q.channels)
def get_guild_channels(guild_id):
if user_unauthenticated():
member_roles = [guild_id] #equivilant to @everyone role
else:
member = discord_api.get_guild_member(guild_id, session['user_id'])['content']
member_roles = member['roles']
member_roles = get_member_roles(guild_id, session['user_id'])
if guild_id not in member_roles:
member_roles.append(guild_id)
guild_channels = discord_api.get_guild_channels(guild_id)['content']
guild_roles = discord_api.get_guild_roles(guild_id)["content"]
guild_owner = discord_api.get_guild(guild_id)['content']['owner_id']
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
guild_channels = json.loads(dbguild.channels)
guild_roles = json.loads(dbguild.roles)
guild_owner = json.loads(dbguild.owner_id)
result_channels = []
for channel in guild_channels:
if channel['type'] == 0:
if channel['type'] == "text":
result = {"channel": channel, "read": False, "write": False}
if guild_owner == session['user_id']:
result["read"] = True
@ -192,17 +197,17 @@ def get_guild_channels(guild_id):
def filter_guild_channel(guild_id, channel_id):
channels = get_guild_channels(guild_id)
for chan in channels:
if chan["channel"]["id"] == guild_id:
if chan["channel"]["id"] == channel_id:
return chan
return None
def get_online_discord_users(guild_id):
embed = discord_api.get_widget(guild_id)
apimembers = discord_api.list_all_guild_members(guild_id)
apimembers = list_all_guild_members(guild_id)
apimembers_filtered = {}
for member in apimembers:
apimembers_filtered[member["user"]["id"]] = member
guild_roles = discord_api.get_guild_roles(guild_id)["content"]
guild_roles = json.loads(db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().roles)
guildroles_filtered = {}
for role in guild_roles:
guildroles_filtered[role["id"]] = role
@ -235,12 +240,12 @@ def get_online_embed_users(guild_id):
users['unauthenticated'].append(meta)
for user in auths:
client_id = user.client_id
u = discord_api.get_guild_member(guild_id, client_id)['content']['user']
usrdb = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id).filter(GuildMembers.user_id == client_id).first()
meta = {
'id': u['id'],
'username': u['username'],
'discriminator': u['discriminator'],
'avatar_url': generate_avatar_url(u['id'], u['avatar']),
'id': usrdb.user_id,
'username': usrdb.username,
'discriminator': usrdb.discriminator,
'avatar_url': generate_avatar_url(usrdb.user_id, usrdb.avatar),
}
users['authenticated'].append(meta)
return users

View File

@ -6,6 +6,6 @@ from guilds import Guilds
from unauthenticated_users import UnauthenticatedUsers
from unauthenticated_bans import UnauthenticatedBans
from authenticated_users import AuthenticatedUsers
from guild_members import GuildMembers
from guild_members import GuildMembers, list_all_guild_members
from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty
from messages import Messages, get_channel_messages

View File

@ -1,4 +1,5 @@
from titanembeds.database import db
import json
class GuildMembers(db.Model):
__tablename__ = "guild_members"
@ -26,3 +27,19 @@ class GuildMembers(db.Model):
def __repr__(self):
return '<GuildMembers {0} {1} {2} {3} {4}>'.format(self.id, self.guild_id, self.user_id, self.username, self.discriminator)
def list_all_guild_members(guild_id):
memlist = []
members = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id).all()
for member in members:
memlist.append({
"user": {
"username": member.username,
"discriminator": member.discriminator,
"id": member.user_id,
"avatar": member.avatar
},
"roles": json.loads(member.roles),
"nickname": member.nickname,
})
return memlist