Skip to content

Commit

Permalink
filename search filter now suggests only attachment names
Browse files Browse the repository at this point in the history
  • Loading branch information
slatinsky committed Oct 28, 2023
1 parent c8e20b4 commit e1e7cfe
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions backend/fastapi/Autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ def autocomplete_filenames(guild_id: str, partial_filename: str, limit: int):
collection_assets = get_guild_collection(guild_id, "assets")

query = {
"searchable": True,
"filenameWithoutHash": {
"$regex": partial_filename,
"$options": "i"
}
}

cursor = collection_assets.aggregate([
{"$match": query},
{
Expand Down
3 changes: 2 additions & 1 deletion backend/preprocess/AssetProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def get_local_path(self, filename_with_hash: str) -> str:
print(" Warning: Could not find local path of " + filename_with_hash)
return None

def process(self, original_filepath: str):
def process(self, original_filepath: str, is_searchable: bool):
"""
provide filepath without base directory
original_filepath is the path from json file, but is not necessarily a valid path
Expand Down Expand Up @@ -231,6 +231,7 @@ def process(self, original_filepath: str):
"filenameWithoutHash": filename_without_hash,
"colorDominant": dominant_color,
"colorPalette": palette,
"searchable": is_searchable
}

self.insert_asset(asset)
Expand Down
22 changes: 11 additions & 11 deletions backend/preprocess/JsonProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, database: MongoDatabase, file_finder: FileFinder, json_path:s
def process_guild(self, guild):
guild["_id"] = pad_id(guild.pop("id"))
self.asset_processor.set_guild_id(guild["_id"])
guild["icon"] = self.asset_processor.process(guild.pop("iconUrl"))
guild["icon"] = self.asset_processor.process(guild.pop("iconUrl"), is_searchable=False)
return guild

def process_channel(self, channel, guild_id):
Expand Down Expand Up @@ -116,7 +116,7 @@ def process_messages(self, messages, guild_id, channel_id, channel_name):
if "width" in embed["thumbnail"] and "height" in embed["thumbnail"]:
original_width = embed["thumbnail"]["width"]
original_height = embed["thumbnail"]["height"]
embed["thumbnail"] = self.asset_processor.process(embed["thumbnail"]["url"])
embed["thumbnail"] = self.asset_processor.process(embed["thumbnail"]["url"], is_searchable=False)

# restore some fields, because we are losing them in the asset preprocess if url is remote
if "originalWidth" in locals():
Expand Down Expand Up @@ -147,7 +147,7 @@ def process_messages(self, messages, guild_id, channel_id, channel_name):
if "width" in image and "height" in image:
original_width = image["width"]
original_height = image["height"]
image = self.asset_processor.process(image["url"]) # does this work?
image = self.asset_processor.process(image["url"], is_searchable=False) # does this work?

# restore some fields, because we are losing them in the asset preprocess if url is remote
if "originalWidth" in locals():
Expand All @@ -159,10 +159,10 @@ def process_messages(self, messages, guild_id, channel_id, channel_name):
embed["images"] = new_images

if "footer" in embed and "iconUrl" in embed["footer"]:
embed["footer"]["icon"] = self.asset_processor.process(embed["footer"].pop("iconUrl"))
embed["footer"]["icon"] = self.asset_processor.process(embed["footer"].pop("iconUrl"), is_searchable=False)

if "author" in embed and "iconUrl" in embed["author"]:
embed["author"]["icon"] = self.asset_processor.process(embed["author"].pop("iconUrl"))
embed["author"]["icon"] = self.asset_processor.process(embed["author"].pop("iconUrl"), is_searchable=False)

if "reactions" in message:
for reaction in message["reactions"]:
Expand All @@ -175,17 +175,17 @@ def process_messages(self, messages, guild_id, channel_id, channel_name):
reaction["emoji"]["_id"] = reaction["emoji"]["name"]
reaction["emoji"]["source"] = "default"

reaction["emoji"]["image"] = self.asset_processor.process(reaction["emoji"].pop("imageUrl"))
reaction["emoji"]["image"] = self.asset_processor.process(reaction["emoji"].pop("imageUrl"), is_searchable=False)

if "users" in reaction:
for user in reaction["users"]:
user["_id"] = pad_id(user.pop("id"))
user["avatar"] = self.asset_processor.process(user.pop("avatarUrl"))
user["avatar"] = self.asset_processor.process(user.pop("avatarUrl"), is_searchable=False)

new_attachments = []
if "attachments" in message:
for attachment in message["attachments"]:
new_attachment = self.asset_processor.process(attachment.pop("url"))
new_attachment = self.asset_processor.process(attachment.pop("url"), is_searchable=True)

# restore some fields, because we are losing them in the asset preprocess if url is remote
if "fileSizeBytes" in attachment:
Expand All @@ -200,7 +200,7 @@ def process_messages(self, messages, guild_id, channel_id, channel_name):

if "stickers" in message:
for sticker in message["stickers"]:
sticker["source"] = self.asset_processor.process(sticker.pop("sourceUrl"))
sticker["source"] = self.asset_processor.process(sticker.pop("sourceUrl"), is_searchable=False)

return messages

Expand All @@ -218,7 +218,7 @@ def process_authors(self, messages: list, guild_id: str) -> list:

# process avatar in message
author = message["author"]
author["avatar"] = self.asset_processor.process(author.pop("avatarUrl"))
author["avatar"] = self.asset_processor.process(author.pop("avatarUrl"), is_searchable=False)
message["author"] = author


Expand All @@ -230,7 +230,7 @@ def process_authors(self, messages: list, guild_id: str) -> list:
continue

author_copy["guildIds"] = [guild_id]
author_copy["avatar"] = self.asset_processor.process(author_copy.pop("avatarUrl"))
author_copy["avatar"] = self.asset_processor.process(author_copy.pop("avatarUrl"), is_searchable=False)
author_copy["names"] = [author_copy.pop("name") + "#" + author_copy.pop("discriminator")]
authors[author_copy["_id"]] = author_copy # new author

Expand Down
2 changes: 1 addition & 1 deletion backend/preprocess/main_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def wipe_database(database: MongoDatabase):
Deletes all collections on version bump (on program update)
Change EXPECTED_VERSION to force wipe on incompatible schema changes
"""
EXPECTED_VERSION = 13 # <---- change this to wipe database
EXPECTED_VERSION = 14 # <---- change this to wipe database
config = database.get_collection("config")

# add empty whitelisted_guild_ids config if it does not exist
Expand Down

0 comments on commit e1e7cfe

Please sign in to comment.