Skip to content

Commit

Permalink
#13 Discord Mentions System Integration, Cleanup
Browse files Browse the repository at this point in the history
- Created functionality for Discord Mentions & laid the foundation for other platforms.

- Added build_mentions_list to the Account class. Returns a list of mentions using the defined settings.

- Added replace_mentions to the DiscordAccount class. Returns a list of [Updated Post Content, Updated Mentions List]. Automatically replaces global ids with their associated platform id & adds propper formatting in post content for Discord.

- Added mention tag settings

- Fixed missing "encrypted_" prefix in config.py for processing_delay_in_seconds.

- Ensured all settings were using the "get_setting_value" function.
  • Loading branch information
VenomStyx committed Apr 26, 2022
1 parent 8289fe7 commit 0dd7814
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 30 deletions.
120 changes: 116 additions & 4 deletions resources/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def __init__(self, display_name=None, name=None, key=None, secret=None, access_k

self.posting_locations = None



########## LOAD DATA
#####
def load_data(self):
Expand All @@ -77,11 +79,11 @@ def load_data(self):
break

self.posting_locations = self.data['posting_locations'].split("|_|")



return data_loaded



########## DATA TO LIST
#####
def data_to_list(self,data):
Expand All @@ -97,6 +99,8 @@ def data_to_list(self,data):

return f"{display_name}|-|{name}|-|{key}|-|{secret}|-|{access_key}|-|{access_secret}|-|{media_platform}|-|{posting_locations}"



########## POSTING LOCATION TO STRING
#####
def posting_location_to_string(self, posting_locations):
Expand All @@ -115,6 +119,7 @@ def posting_location_to_string(self, posting_locations):
return posting_locations_string



########## REGISTER
#####
def register(self, display_name, key, secret, access_key=None, access_secret=None, media_platform=None, posting_locations=None):
Expand Down Expand Up @@ -159,6 +164,7 @@ def update(self, display_name=None, key=None, secret=None, access_key=None, acce
settings.set_setting_value("accounts","encrypted_media_accounts",str(accounts))



########## REMOVE
#####
def remove(self):
Expand Down Expand Up @@ -187,6 +193,7 @@ def remove(self):
return False



########## CONNECT TO SOCIAL MEDIA PROVIDER
#####
def connect(self):
Expand All @@ -196,6 +203,7 @@ def connect(self):
raise NotImplementedError



########## DISCONNECT FROM SOCIAL MEDIA PROVIDER
#####
def disconnect(self):
Expand All @@ -205,6 +213,7 @@ def disconnect(self):
raise NotImplementedError



########## FORMAT POST DATA
#####
def format_post_data(self, post_object):
Expand All @@ -213,6 +222,32 @@ def format_post_data(self, post_object):
"""
raise NotImplementedError



########## BUILD MENTIONS LIST
#####
def build_mentions_list(self, post_content):
"""
Used to build a list of mentions.
"""

working_post_content = post_content.split(settings.mention_tag_end)

mentions_list = []

## Find & extract mentions
for entry in working_post_content:

## Find mention tag
if settings.mention_tag_start in entry:

## Extract mention
mention_name = entry.split(settings.mention_tag_start)[1]
mentions_list.append(mention_name)

return mentions_list



########## PUBLISH POST
#####
Expand All @@ -223,6 +258,7 @@ def publish_post(self, post_object):
raise NotImplementedError



########## PUBLISH POSTS
#####
def publish_posts(self, post_object):
Expand All @@ -232,6 +268,7 @@ def publish_posts(self, post_object):
raise NotImplementedError



########## DELETE POST
#####
def delete_posts(self, post_id):
Expand All @@ -241,6 +278,8 @@ def delete_posts(self, post_id):
## Parent function here will clean up the database & associated files
pass



########## IS READY
#####
def is_ready(self):
Expand Down Expand Up @@ -324,7 +363,70 @@ def format_post_data(self, post_object):
########## BUILD MENTIONS LIST
#####
def build_mentions_list(self, post_content):
return {"users": []}
"""
Used to build a list of mentions.
"""

mentions_list = Account.build_mentions_list(self, post_content)

## Return mentions list
return {"users": mentions_list}



def replace_mentions(self, post_content, mentions_list):
"""
Replaces mentions in a post with the actual mention string.
"""

## Initialize mentions list
mentions_dict = mentions_list
mentions_list = mentions_list['users']

## Replace all mentions with the proper formatting.
for postmention in mentions_list:

## Look for mention in global mentions
mention_index = settings.global_mentions.find_global_mention_index(postmention)

## If global mention found
if mention_index or mention_index==0:
if mention_index >=0:

## Get Global Mention object
global_mention = settings.global_mentions.entries[mention_index]

## Get all Platform Mentions
global_mention_platform_mentions = global_mention.platform_mentions

## For each platform mention
for mention in global_mention_platform_mentions:

## Look for platform mention that matches this platform
if mention[0] == "discord":

## Update Post Content
post_content = post_content.replace(f"{settings.mention_tag_start}{postmention}{settings.mention_tag_end}", f"<@&{mention[1]}>")

## Update Mentions Dictionary
mentions_dict['users'].remove(postmention)
mentions_dict['users'].append(mention[1])

break

## No global mention found, assume it's a local ID
else:
## Replace
post_content = post_content.replace(f"{settings.mention_tag_start}{postmention}{settings.mention_tag_end}", f"<@&{postmention}>")
continue
else:

## Replace
post_content = post_content.replace(f"{settings.mention_tag_start}{postmention}{settings.mention_tag_end}", f"<@&{postmention}>")
continue

## Return updated post content, updated mentions list
return [post_content, mentions_dict]


########## PUBLISH POST
Expand Down Expand Up @@ -354,9 +456,19 @@ def publish_post(self, post_object):

## Create proper Mentions
mentions = self.build_mentions_list(loc_data['content'])

if mentions:
loc_data['allowed_mentions'] = self.build_mentions_list(loc_data['content'])

## Update Post Content
updated_mentions_data = self.replace_mentions(content, mentions)
updated_post_content = updated_mentions_data[0]
updated_mentions = updated_mentions_data[1]

## Log Updated Content
loc_data['content'] = updated_post_content
loc_data['allowed_mentions'] = updated_mentions


## Intialize the Webhook
webhook = DiscordWebhook(**loc_data)

Expand Down
46 changes: 24 additions & 22 deletions resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ def __init__(self):
self.saved_path = str(self.current_path) + "saved/"

##### STORAGE
self.published_posts_file = cfg.get("storage","encrypted_posts_file")
self.published_posts_file = self.get_setting_value("storage","encrypted_posts_file")
self.published_posts_file_location = "saved/" + self.published_posts_file
self.published_posts_file_location_full = current_path + self.published_posts_file
self.scheduled_posts_file = cfg.get("storage","encrypted_scheduled_posts_file")
self.scheduled_posts_file = self.get_setting_value("storage","encrypted_scheduled_posts_file")
self.scheduled_posts_file_location = "saved/" + self.scheduled_posts_file
self.scheduled_posts_file_location_full = current_path + self.scheduled_posts_file # This needs to be changed for S3 support
self.uploaded_media_dir = cfg.get("storage","encrypted_uploaded_media_dir")
self.uploaded_media_dir = self.get_setting_value("storage","encrypted_uploaded_media_dir")
self.full_uploaded_media_dir = self.saved_path + self.uploaded_media_dir

##### ENCRYPTION
self.key_location = cfg.get("encryption","hidden_key_location")
self.key_location = self.get_setting_value("encryption","hidden_key_location")
self.crypt = None

## Has encryption been setup?
Expand Down Expand Up @@ -101,13 +101,13 @@ def __init__(self):
if self.crypt_setup:

##### ACCOUNTS
media_accounts_temp = cfg.get("accounts","encrypted_media_accounts")
media_accounts_temp = self.get_setting_value("accounts","encrypted_media_accounts")
if media_accounts_temp != "None":
self.media_accounts = string_to_list_of_dictionaries(self.get_setting_value(category="accounts", setting="encrypted_media_accounts"))
else:
self.media_accounts = None

self.supported_media_platforms = cfg.get("accounts","encrypted_supported_media_platforms").split(",")
self.supported_media_platforms = self.get_setting_value("accounts","encrypted_supported_media_platforms").split(",")

##### S3 CREDENTIALS
self.s3_access = self.get_setting_value(category="accounts", setting="encrypted_s3_access", deny_plaintext_setting=True)
Expand All @@ -121,6 +121,8 @@ def __init__(self):

##### GLOBAL MENTIONS
local_global_mentions = self.get_setting_value(category="posting", setting="encrypted_global_mentions", deny_plaintext_setting=True)
self.mention_tag_start = self.get_setting_value(category="posting", setting="encrypted_mention_tag_start", deny_plaintext_setting=True)
self.mention_tag_end = self.get_setting_value(category="posting", setting="encrypted_mention_tag_end", deny_plaintext_setting=True)

## No Current Global Manager
if local_global_mentions == "None": # No current setting,
Expand Down Expand Up @@ -149,27 +151,27 @@ def __init__(self):


##### APPLICATION
self.no_posts_title = cfg.get("app","no_posts_title")
self.no_posts_description = cfg.get("app","no_posts_description")
self.post_not_scheduled_for_reason_time_in_past = cfg.get("app","post_not_scheduled_for_reason_time_in_past")
self.value_redaction_message = cfg.get("app", "value_redaction_message")
self.new_gid_mention_platform_message = cfg.get("app", "new_gid_mention_platform_message")
self.new_gid_mention_platform_id_message = cfg.get("app", "new_gid_mention_platform_id_message")
self.new_global_id_message = cfg.get("app", "new_global_id_message")
self.global_mentions_updated_message = cfg.get("app", "global_mentions_updated_message")
self.no_posts_title = self.get_setting_value("app","no_posts_title")
self.no_posts_description = self.get_setting_value("app","no_posts_description")
self.post_not_scheduled_for_reason_time_in_past = self.get_setting_value("app","post_not_scheduled_for_reason_time_in_past")
self.value_redaction_message = self.get_setting_value("app", "value_redaction_message")
self.new_gid_mention_platform_message = self.get_setting_value("app", "new_gid_mention_platform_message")
self.new_gid_mention_platform_id_message = self.get_setting_value("app", "new_gid_mention_platform_id_message")
self.new_global_id_message = self.get_setting_value("app", "new_global_id_message")
self.global_mentions_updated_message = self.get_setting_value("app", "global_mentions_updated_message")

##### PERFORMANCE
self.posts_cache_time = float(cfg.get("performance","encrypted_posts_cache_time"))
self.page_cache_time = float(cfg.get("performance","encrypted_page_cache_time"))
self.posts_cache_time = float(self.get_setting_value("performance","encrypted_posts_cache_time"))
self.page_cache_time = float(self.get_setting_value("performance","encrypted_page_cache_time"))

##### MEDIA
self.supported_image_types = cfg.get("media","encrypted_supported_image_types").split(",")
self.supported_video_types = cfg.get("media","encrypted_supported_video_types").split(",")
self.supported_audio_types = cfg.get("media","encrypted_supported_audio_types").split(",")
self.supported_image_types = self.get_setting_value("media","encrypted_supported_image_types").split(",")
self.supported_video_types = self.get_setting_value("media","encrypted_supported_video_types").split(",")
self.supported_audio_types = self.get_setting_value("media","encrypted_supported_audio_types").split(",")

##### POSTING
self.utc_timezones = cfg.get("posting","encrypted_utc_timezones").split(",")
self.default_timezone = cfg.get("posting","encrypted_default_timezone")
self.utc_timezones = self.get_setting_value("posting","encrypted_utc_timezones").split(",")
self.default_timezone = self.get_setting_value("posting","encrypted_default_timezone")



Expand Down Expand Up @@ -366,6 +368,6 @@ class server_settings:

def __init__(self):

self.processing_delay_in_seconds = int(cfg.get("server","processing_delay_in_seconds"))
self.processing_delay_in_seconds = int(cfg.get("server","encrypted_processing_delay_in_seconds"))


8 changes: 4 additions & 4 deletions settings_template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ encrypted_s3_bucket = None
#####
[posting]
## Display Names
display_name_encrypted_local_mention_tag_start = Local Mention Tag Start
display_name_encrypted_local_mention_tag_end = Local Mention Tag End
display_name_encrypted_mention_tag_start = Local Mention Tag Start
display_name_encrypted_mention_tag_end = Local Mention Tag End
display_name_encrypted_global_mentions = Global Mention IDs
display_name_encrypted_utc_timezones = UTC Timezones
display_name_encrypted_default_timezone = Default Timezone

## Values
encrypted_global_mentions = None
encrypted_local_mention_tag_start = None
encrypted_local_mention_tag_end = None
encrypted_mention_tag_start = None
encrypted_mention_tag_end = None
encrypted_utc_timezones = +1400,+1300,+1200,+1100,+1000,+0900,+0800,+0700,+0600,+0500,+0400,+0300,+0200,+0100,+0000,-0100,-0200,-0300,-0400,-0500,-0600,-0700,-0800,-0900,-1000,-1100,-1200
encrypted_default_timezone = +0000

Expand Down

1 comment on commit 0dd7814

@VenomStyx
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#60 was also addressed in this commit, forgot to mention

Please sign in to comment.