Delete extra messages in each guild endpoint

This commit is contained in:
Jeremy Zhang 2017-05-08 20:44:12 -07:00
parent a621fa8b0a
commit 948ebb8af3

View File

@ -1,4 +1,4 @@
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, GuildMembers, get_channel_messages, list_all_guild_members from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, GuildMembers, Messages, get_channel_messages, list_all_guild_members
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
@ -398,12 +398,24 @@ def create_authenticated_user():
response.status_code = 403 response.status_code = 403
return response return response
@api.route("/cleanup-keyval-db", methods=["DELETE"]) @api.route("/cleanup-db", methods=["DELETE"])
def cleanup_keyval_db(): def cleanup_keyval_db():
if request.form.get("secret", None) == config["app-secret"]: if request.form.get("secret", None) == config["app-secret"]:
q = KeyValueProperties.query.filter(KeyValueProperties.expiration < datetime.datetime.now()).all() q = KeyValueProperties.query.filter(KeyValueProperties.expiration < datetime.datetime.now()).all()
for m in q: for m in q:
db.session.delete(m) db.session.delete(m)
guilds = Guilds.query.all()
for guild in guilds:
channelsjson = json.loads(guild.channels)
for channel in channelsjson:
chanid = channel["id"]
dbmsg = Messages.query.filter(Messages.channel_id == chanid).all()
for idx, val in enumerate(dbmsg):
if len(dbmsg) - idx > 50:
db.session.delete(val)
else:
continue
db.session.commit() db.session.commit()
return ('', 204) return ('', 204)
abort(401) abort(401)