mirror of
https://github.com/TitanEmbeds/Titan.git
synced 2024-12-24 14:07:03 +01:00
Fetching database for messages instead
This commit is contained in:
parent
b537aacb1c
commit
9c24093901
4
.gitignore
vendored
4
.gitignore
vendored
@ -92,5 +92,5 @@ ENV/
|
|||||||
config.py
|
config.py
|
||||||
redislite.db
|
redislite.db
|
||||||
redislite.db.settings
|
redislite.db.settings
|
||||||
tmp/*
|
webapp/tmp/*
|
||||||
!tmp/.gitinclude
|
!webapp/tmp/.gitinclude
|
||||||
|
@ -54,14 +54,38 @@ class DatabaseInterface(object):
|
|||||||
message.channel.id,
|
message.channel.id,
|
||||||
message.id,
|
message.id,
|
||||||
message.content,
|
message.content,
|
||||||
|
json.dumps(self.get_message_author(message)),
|
||||||
str(message.timestamp),
|
str(message.timestamp),
|
||||||
edit_ts,
|
edit_ts,
|
||||||
json.dumps(message.mentions),
|
json.dumps(self.get_message_mentions(message.mentions)),
|
||||||
json.dumps(message.attachments)
|
json.dumps(message.attachments)
|
||||||
)
|
)
|
||||||
session.add(msg)
|
session.add(msg)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
def get_message_author(self, message):
|
||||||
|
author = message.author
|
||||||
|
obj = {
|
||||||
|
"username": author.name,
|
||||||
|
"discriminator": author.discriminator,
|
||||||
|
"bot": author.bot,
|
||||||
|
"id": author.id,
|
||||||
|
"avatar": author.avatar
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def get_message_mentions(self, mentions):
|
||||||
|
ments = []
|
||||||
|
for author in mentions:
|
||||||
|
ments.append({
|
||||||
|
"username": author.name,
|
||||||
|
"discriminator": author.discriminator,
|
||||||
|
"bot": author.bot,
|
||||||
|
"id": author.id,
|
||||||
|
"avatar": author.avatar
|
||||||
|
})
|
||||||
|
return ments
|
||||||
|
|
||||||
async def update_message(self, message):
|
async def update_message(self, message):
|
||||||
if message.server:
|
if message.server:
|
||||||
async with threadpool():
|
async with threadpool():
|
||||||
@ -73,8 +97,9 @@ class DatabaseInterface(object):
|
|||||||
if msg:
|
if msg:
|
||||||
msg.content = message.content
|
msg.content = message.content
|
||||||
msg.edited_timestamp = message.edited_timestamp
|
msg.edited_timestamp = message.edited_timestamp
|
||||||
msg.mentions = json.dumps(message.mentions)
|
msg.mentions = json.dumps(self.get_message_mentions(message.mentions))
|
||||||
msg.attachments = json.dumps(message.attachments)
|
msg.attachments = json.dumps(message.attachments)
|
||||||
|
msg.author = json.dumps(self.get_message_author(message))
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
async def delete_message(self, message):
|
async def delete_message(self, message):
|
||||||
|
@ -7,16 +7,18 @@ class Messages(Base):
|
|||||||
channel_id = db.Column(db.String(255)) # Channel id
|
channel_id = db.Column(db.String(255)) # Channel id
|
||||||
message_id = db.Column(db.String(255)) # Message snowflake
|
message_id = db.Column(db.String(255)) # Message snowflake
|
||||||
content = db.Column(db.Text()) # Message contents
|
content = db.Column(db.Text()) # Message contents
|
||||||
|
author = db.Column(db.Text()) # Author json
|
||||||
timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is created
|
timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is created
|
||||||
edited_timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is edited
|
edited_timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is edited
|
||||||
mentions = db.Column(db.Text()) # Mentions serialized
|
mentions = db.Column(db.Text()) # Mentions serialized
|
||||||
attachments = db.Column(db.Text()) # serialized attachments
|
attachments = db.Column(db.Text()) # serialized attachments
|
||||||
|
|
||||||
def __init__(self, guild_id, channel_id, message_id, content, timestamp, edited_timestamp, mentions, attachments):
|
def __init__(self, guild_id, channel_id, message_id, content, author, timestamp, edited_timestamp, mentions, attachments):
|
||||||
self.guild_id = guild_id
|
self.guild_id = guild_id
|
||||||
self.channel_id = channel_id
|
self.channel_id = channel_id
|
||||||
self.message_id = message_id
|
self.message_id = message_id
|
||||||
self.content = content
|
self.content = content
|
||||||
|
self.author = author
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
self.edited_timestamp = edited_timestamp
|
self.edited_timestamp = edited_timestamp
|
||||||
self.mentions = mentions
|
self.mentions = mentions
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties
|
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, get_channel_messages
|
||||||
from titanembeds.decorators import valid_session_required, discord_users_only
|
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.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
|
from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild
|
||||||
@ -265,10 +265,9 @@ def fetch():
|
|||||||
if not chan.get("read"):
|
if not chan.get("read"):
|
||||||
status_code = 401
|
status_code = 401
|
||||||
else:
|
else:
|
||||||
messages = discord_api.get_channel_messages(channel_id, after_snowflake)
|
messages = get_channel_messages(channel_id, after_snowflake)
|
||||||
status_code = messages['code']
|
response = jsonify(messages=messages, status=status)
|
||||||
response = jsonify(messages=messages.get('content', messages), status=status)
|
response.status_code = 200
|
||||||
response.status_code = status_code
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@api.route("/post", methods=["POST"])
|
@api.route("/post", methods=["POST"])
|
||||||
@ -382,4 +381,4 @@ def cleanup_keyval_db():
|
|||||||
db.session.delete(m)
|
db.session.delete(m)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return ('', 204)
|
return ('', 204)
|
||||||
abort(401)
|
abort(401)
|
||||||
|
@ -6,4 +6,6 @@ from guilds import Guilds
|
|||||||
from unauthenticated_users import UnauthenticatedUsers
|
from unauthenticated_users import UnauthenticatedUsers
|
||||||
from unauthenticated_bans import UnauthenticatedBans
|
from unauthenticated_bans import UnauthenticatedBans
|
||||||
from authenticated_users import AuthenticatedUsers
|
from authenticated_users import AuthenticatedUsers
|
||||||
|
from guild_members import GuildMembers
|
||||||
from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty
|
from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty
|
||||||
|
from messages import Messages, get_channel_messages
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
import urlparse
|
|
||||||
from limits.storage import Storage
|
|
||||||
from redislite import Redis
|
|
||||||
import time
|
|
||||||
|
|
||||||
class LimitsRedisLite(Storage): # For Python Limits
|
|
||||||
STORAGE_SCHEME = "redislite"
|
|
||||||
def __init__(self, uri, **options):
|
|
||||||
self.redis_instance = Redis(urlparse.urlparse(uri).netloc)
|
|
||||||
|
|
||||||
def check(self):
|
|
||||||
return True
|
|
||||||
|
|
||||||
def get_expiry(self, key):
|
|
||||||
return (self.redis_instance.ttl(key) or 0) + time.time()
|
|
||||||
|
|
||||||
def incr(self, key, expiry, elastic_expiry=False):
|
|
||||||
if not self.redis_instance.exists(key):
|
|
||||||
self.redis_instance.set(key, 1, ex=expiry)
|
|
||||||
else:
|
|
||||||
oldexp = oldexp = self.get_expiry(key) - time.time()
|
|
||||||
if oldexp <= 0:
|
|
||||||
self.redis_instance.delete(key)
|
|
||||||
return self.incr(key, expiry, elastic_expiry)
|
|
||||||
self.redis_instance.set(key, int(self.redis_instance.get(key))+1, ex=int(round(oldexp)))
|
|
||||||
return int(self.get(key))
|
|
||||||
|
|
||||||
def get(self, key):
|
|
||||||
value = self.redis_instance.get(key)
|
|
||||||
if value:
|
|
||||||
return int(value)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
return self.redis_instance.flushdb()
|
|
28
webapp/titanembeds/database/guild_members.py
Normal file
28
webapp/titanembeds/database/guild_members.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from titanembeds.database import db
|
||||||
|
|
||||||
|
class GuildMembers(db.Model):
|
||||||
|
__tablename__ = "guild_members"
|
||||||
|
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
|
||||||
|
guild_id = db.Column(db.String(255)) # Discord guild id
|
||||||
|
user_id = db.Column(db.String(255)) # Discord user id
|
||||||
|
username = db.Column(db.String(255)) # Name
|
||||||
|
discriminator = db.Column(db.Integer) # User discriminator
|
||||||
|
nickname = db.Column(db.String(255)) # User nickname
|
||||||
|
avatar = db.Column(db.String(255)) # The avatar str of the user
|
||||||
|
active = db.Column(db.Boolean()) # If the user is a member of the guild
|
||||||
|
banned = db.Column(db.Boolean()) # If the user is banned in the guild
|
||||||
|
roles = db.Column(db.Text()) # Member roles
|
||||||
|
|
||||||
|
def __init__(self, guild_id, user_id, username, discriminator, nickname, avatar, active, banned, roles):
|
||||||
|
self.guild_id = guild_id
|
||||||
|
self.user_id = user_id
|
||||||
|
self.username = username
|
||||||
|
self.discriminator = discriminator
|
||||||
|
self.nickname = nickname
|
||||||
|
self.avatar = avatar
|
||||||
|
self.active = active
|
||||||
|
self.banned = banned
|
||||||
|
self.roles = roles
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<GuildMembers {0} {1} {2} {3} {4}>'.format(self.id, self.guild_id, self.user_id, self.username, self.discriminator)
|
@ -4,11 +4,21 @@ class Guilds(db.Model):
|
|||||||
__tablename__ = "guilds"
|
__tablename__ = "guilds"
|
||||||
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
|
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
|
||||||
guild_id = db.Column(db.String(255)) # Discord guild id
|
guild_id = db.Column(db.String(255)) # Discord guild id
|
||||||
|
name = db.Column(db.String(255)) # Name
|
||||||
unauth_users = db.Column(db.Boolean()) # If allowed unauth users
|
unauth_users = db.Column(db.Boolean()) # If allowed unauth users
|
||||||
|
roles = db.Column(db.Text()) # Guild Roles
|
||||||
|
channels = db.Column(db.Text()) # Guild channels
|
||||||
|
owner_id = db.Column(db.String(255)) # Snowflake of the owner
|
||||||
|
icon = db.Column(db.String(255)) # The icon string, null if none
|
||||||
|
|
||||||
def __init__(self, guild_id):
|
def __init__(self, guild_id, name, roles, channels, owner_id, icon):
|
||||||
self.guild_id = guild_id
|
self.guild_id = guild_id
|
||||||
|
self.name = name
|
||||||
self.unauth_users = True # defaults to true
|
self.unauth_users = True # defaults to true
|
||||||
|
self.roles = roles
|
||||||
|
self.channels = channels
|
||||||
|
self.owner_id = owner_id
|
||||||
|
self.icon = icon
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Guilds {0} {1}>'.format(self.id, self.guild_id)
|
return '<Guilds {0} {1}>'.format(self.id, self.guild_id)
|
||||||
|
49
webapp/titanembeds/database/messages.py
Normal file
49
webapp/titanembeds/database/messages.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
from titanembeds.database import db
|
||||||
|
from sqlalchemy import cast
|
||||||
|
import json
|
||||||
|
|
||||||
|
class Messages(db.Model):
|
||||||
|
__tablename__ = "messages"
|
||||||
|
id = db.Column(db.Integer, primary_key=True) # Auto incremented id
|
||||||
|
guild_id = db.Column(db.String(255)) # Discord guild id
|
||||||
|
channel_id = db.Column(db.String(255)) # Channel id
|
||||||
|
message_id = db.Column(db.String(255)) # Message snowflake
|
||||||
|
content = db.Column(db.Text()) # Message contents
|
||||||
|
author = db.Column(db.Text()) # Author
|
||||||
|
timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is created
|
||||||
|
edited_timestamp = db.Column(db.TIMESTAMP) # Timestamp of when content is edited
|
||||||
|
mentions = db.Column(db.Text()) # Mentions serialized
|
||||||
|
attachments = db.Column(db.Text()) # serialized attachments
|
||||||
|
|
||||||
|
def __init__(self, guild_id, channel_id, message_id, content, author, timestamp, edited_timestamp, mentions, attachments):
|
||||||
|
self.guild_id = guild_id
|
||||||
|
self.channel_id = channel_id
|
||||||
|
self.message_id = message_id
|
||||||
|
self.content = content
|
||||||
|
self.author = author
|
||||||
|
self.timestamp = timestamp
|
||||||
|
self.edited_timestamp = edited_timestamp
|
||||||
|
self.mentions = mentions
|
||||||
|
self.attachments = attachments
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<Messages {0} {1} {2} {3} {4}>'.format(self.id, self.guild_id, self.guild_id, self.channel_id, self.message_id)
|
||||||
|
|
||||||
|
def get_channel_messages(channel_id, after_snowflake=None):
|
||||||
|
if not after_snowflake:
|
||||||
|
q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.id.desc()).limit(50)
|
||||||
|
else:
|
||||||
|
q = db.session.query(Messages).filter(cast(Messages.channel_id, db.Integer) == int(channel_id)).filter(Messages.message_id > after_snowflake).order_by(Messages.id.desc()).limit(50)
|
||||||
|
msgs = []
|
||||||
|
for x in q:
|
||||||
|
msgs.append({
|
||||||
|
"attachments": json.loads(x.attachments),
|
||||||
|
"timestamp": x.timestamp,
|
||||||
|
"id": x.message_id,
|
||||||
|
"edited_timestamp": x.edited_timestamp,
|
||||||
|
"author": json.loads(x.author),
|
||||||
|
"content": x.content,
|
||||||
|
"channel_id": x.channel_id,
|
||||||
|
"mentions": json.loads(x.mentions)
|
||||||
|
})
|
||||||
|
return msgs
|
Loading…
Reference in New Issue
Block a user