Skip to content

Commit

Permalink
moved alias sql methods to appropriate file
Browse files Browse the repository at this point in the history
  • Loading branch information
MujyKun committed Aug 13, 2021
1 parent 2514f3d commit 5131d50
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
53 changes: 53 additions & 0 deletions IreneUtility/s_sql/s_groupmembers.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,56 @@ async def fetch_data_mods() -> List[int]:
else:
return [record[0] for record in user_ids]


async def set_global_alias(object_id, alias, is_group):
"""
Set a global alias
:param object_id: The idol/group ID
:param alias: The alias to add
:param is_group: Whether we have a group as the object.
"""
await self.ex.conn.execute("INSERT INTO groupmembers.aliases(objectid, alias, isgroup) VALUES($1, $2, $3)",
object_id, alias.lower(), is_group)


async def set_local_alias(object_id, alias, is_group, server_id):
"""
Set a global alias
:param object_id: The idol/group ID
:param alias: The alias to add
:param is_group: Whether we have a group as the object.
:param server_id: The server ID for the local alias.
"""
await self.ex.conn.execute(
"INSERT INTO groupmembers.aliases(objectid, alias, isgroup, serverid) VALUES($1, $2, $3, $4)", object_id,
alias.lower(), is_group, server_id)


async def remove_global_alias(object_id, alias, is_group):
"""
Remove a global idol/group alias.
:param object_id: The idol/group ID
:param alias: The alias to add
:param is_group: Whether we have a group as the object.
"""
await self.ex.conn.execute(
"DELETE FROM groupmembers.aliases WHERE alias = $1 AND isgroup = $2 AND objectid = $3 AND serverid IS NULL",
alias, is_group, object_id)


async def remove_local_alias(object_id, alias, is_group, server_id):
"""
Remove a server idol/group alias.
:param object_id: The idol/group ID
:param alias: The alias to add
:param is_group: Whether we have a group as the object.
:param server_id: The server ID for the local alias.
"""
await self.ex.conn.execute(
"DELETE FROM groupmembers.aliases WHERE alias = $1 AND isgroup = $2 AND serverid = $3 AND objectid = $4",
alias, is_group, server_id, object_id)
28 changes: 16 additions & 12 deletions IreneUtility/util/u_groupmembers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def __init__(self, *args):
self.successful_codes = [200, 301, 415]

async def get_if_user_voted(self, user_id):
"""
Check if a user voted:
:param user_id: The user's ID.
:returns: (bool) True if they voted in the past 24 hours.
"""
time_stamp = self.ex.first_result(
await self.ex.conn.fetchrow("SELECT votetimestamp FROM general.lastvoted WHERE userid = $1", user_id))
if time_stamp:
Expand All @@ -32,6 +38,12 @@ async def get_if_user_voted(self, user_id):
log.console(f"{user_id} has not voted in the past 24 hours.", method=self.get_if_user_voted)

def check_idol_object(self, obj):
"""
Check if we are dealing with an Idol object
:param obj: Object to check.
:returns: (bool) Whether the object is an Idol.
"""
return isinstance(obj, self.ex.u_objects.Idol)

async def send_vote_message(self, message):
Expand All @@ -47,9 +59,7 @@ async def set_global_alias(self, obj, alias):
alias = alias.lower()
obj.aliases.append(alias)
is_group = int(not self.check_idol_object(obj))
await self.ex.conn.execute("INSERT INTO groupmembers.aliases(objectid, alias, isgroup) VALUES($1, $2, $3)",
obj.id,
alias, is_group)
await self.ex.sql.s_groupmembers.set_global_alias(obj.id, alias, is_group)

async def set_local_alias(self, obj, alias, server_id):
"""Set an idol/group alias for a server"""
Expand All @@ -60,27 +70,21 @@ async def set_local_alias(self, obj, alias, server_id):
else:
obj.local_aliases[server_id] = [alias]
is_group = int(not self.check_idol_object(obj))
await self.ex.conn.execute(
"INSERT INTO groupmembers.aliases(objectid, alias, isgroup, serverid) VALUES($1, $2, $3, $4)", obj.id,
alias, is_group, server_id)
await self.ex.sql.s_groupmembers.set_local_alias(obj.id, alias, is_group, server_id)

async def remove_global_alias(self, obj, alias):
"""Remove a global idol/group alias """
obj.aliases.remove(alias)
is_group = int(not self.check_idol_object(obj))
await self.ex.conn.execute(
"DELETE FROM groupmembers.aliases WHERE alias = $1 AND isgroup = $2 AND objectid = $3 AND serverid IS NULL",
alias, is_group, obj.id)
await self.ex.sql.s_groupmembers.remove_global_alias(obj.id, alias, is_group)

async def remove_local_alias(self, obj, alias, server_id):
"""Remove a server idol/group alias"""
is_group = int(not self.check_idol_object(obj))
local_aliases = obj.local_aliases.get(server_id)
if local_aliases:
local_aliases.remove(alias)
await self.ex.conn.execute(
"DELETE FROM groupmembers.aliases WHERE alias = $1 AND isgroup = $2 AND serverid = $3 AND objectid = $4",
alias, is_group, server_id, obj.id)
await self.ex.sql.s_groupmembers.remove_local_alias(obj.id, alias, is_group, server_id)

async def get_member(self, idol_id) -> Optional[models.Idol]:
"""Get a member by the idol id."""
Expand Down

0 comments on commit 5131d50

Please sign in to comment.