Skip to content

Commit

Permalink
Remove old for merged activities
Browse files Browse the repository at this point in the history
  • Loading branch information
0x16c3 committed Oct 21, 2021
1 parent abdbce9 commit f9df27e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
45 changes: 42 additions & 3 deletions cogs/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ async def send_embed(
class CListActivity(ListActivity):

username = None
sent_message = None

@staticmethod
def create(obj: ListActivity, username: str = None) -> "CListActivity":
Expand All @@ -330,6 +331,8 @@ def create(obj: ListActivity, username: str = None) -> "CListActivity":
if username:
obj.username = username

obj.sent_message = None

return obj

@staticmethod
Expand Down Expand Up @@ -367,16 +370,20 @@ async def send_embed(
anilist: AsyncClient,
channel: discord.TextChannel = None,
filter_adult: bool = True,
activity=None,
) -> Optional[discord.Embed]:

item_idx = -1
if activity:
item_idx = activity.feed.entries.index(item)

user, listitem = await item.get_list(anilist)
if not user or not listitem:
return None

user = CUser.create(user)
listitem: MediaList


if channel:
channels = await database.channel_get()
matching = [ch for ch in channels if int(ch["channel"]) == channel.id]
Expand All @@ -401,13 +408,17 @@ async def send_embed(

is_manga = isinstance(item.media, Manga)

merged = False
if item.status and item.status.progress:
progress = "{}. {}".format(
item.status,
"\n Score ⭐: {}".format(listitem.score)
if hasattr(listitem, "score") > 0 and listitem.status == "COMPLETED"
else "",
)

if "-" in str(item.status):
merged = True
else:
if is_manga:
progress = "Total {} chapters. {}{}".format(
Expand Down Expand Up @@ -543,9 +554,37 @@ async def send_embed(

if channel:
try:
await channel.send(embed=embed)
if activity and merged:
for idx, feed in enumerate(activity.feed.entries_processed):
feed: "CListActivity"

if (
not feed.sent_message
or feed.media.id != item.media.id
or feed.status.string
not in [
"WATCHED EPISODE",
"REWATCHED EPISODE",
"READ CHAPTER",
"REREAD CHAPTER",
]
):
continue
else:
try:
await feed.sent_message.delete()
except Exception as e:
logger.debug(
f"Cannot remove message -> {str(channel.id)} : {item.username} {e}"
)

item.sent_message = await channel.send(embed=embed)
activity.feed.entries[item_idx] = item

return "List[CListActivity]", activity.feed.entries, item
return None
except Exception as e:
logger.info(
logger.debug(
f"Cannot send message -> {str(channel.id)} : {item.username} {e}"
)
else:
Expand Down
32 changes: 28 additions & 4 deletions cogs/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .api.database import database
from .api.types import CCharacter, CUser, CAnime, CManga, CListActivity, CTextActivity
from typing import Union
import sys


class Feed:
Expand Down Expand Up @@ -111,7 +112,13 @@ async def retrieve(
self.errors = 0

except Exception as e:
logger.print("Error on {}: {}".format(self.username, e))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.print(
"Error on {}: {}\n[STACK] {} {} {}".format(
self.username, e, exc_type, fname, exc_tb.tb_lineno
)
)
self.errors += 1
return [], []

Expand Down Expand Up @@ -164,15 +171,28 @@ async def move_item(self, item, func=None, **kwargs) -> bool:
"""

processed = False
item_old = None

if item not in self.entries_processed:

if func:
try:
await func(item=item, **kwargs)
res = await func(item=item, **kwargs)

if isinstance(res, tuple):
if res[0] == "List[CListActivity]":
self.entries = res[1]
item_old = item
item = res[2]

except Exception as e:

exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.print(
"Error running function on entry {}: {}".format(item.id, e)
"Error running function on entry {}: {}\n[STACK] {} {} {}".format(
item.id, e, exc_type, fname, exc_tb.tb_lineno
)
)

if self.type == CListActivity:
Expand Down Expand Up @@ -418,6 +438,7 @@ async def on_ready(self):
channel=user.channel,
anilist=anilist,
filter_adult=enable_filter,
activity=user,
)

logger.info(f"Loaded {len(self.feeds)}.")
Expand All @@ -441,6 +462,7 @@ async def process(self):
channel=user.channel,
anilist=anilist,
filter_adult=enable_filter,
activity=user,
)

@cog_ext.cog_slash(
Expand Down Expand Up @@ -843,7 +865,9 @@ def check_author(cctx: ComponentContext):
for i in [
x
for x in self.feeds
if x.username == username and x.type not in selected
if x.username == username
and x.channel == ctx.channel
and x.type not in selected
]:
self.feeds.remove(i)
await database.feed_remove([i.JSON()])
Expand Down

0 comments on commit f9df27e

Please sign in to comment.