Base Message Receiver
wagtail_live.receivers.base.BaseMessageReceiver
Base Receiver class.
add_message(self, message)
Adds a received message from a messaging app to the live page corresponding to the channel where the message was posted if such a page exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A message received from a messaging app. |
required |
Source code in wagtail_live/receivers/base.py
def add_message(self, message):
"""
Adds a received message from a messaging app to the live page corresponding
to the channel where the message was posted if such a page exists.
Args:
message (dict): A message received from a messaging app.
"""
channel_id = self.get_channel_id_from_message(message=message)
try:
live_page = self.get_live_page_from_channel_id(channel_id=channel_id)
except self.model.DoesNotExist:
return
message_id = self.get_message_id_from_message(message=message)
live_post = construct_live_post_block(message_id=message_id, created=now())
message_text = self.get_message_text(message=message)
self.process_text(live_post=live_post, message_text=message_text)
files = self.get_message_files(message=message)
self.process_files(live_post=live_post, files=files)
live_page.add_live_post(live_post=live_post)
change_message(self, message)
Changes an edited message in a messaging app in the live page corresponding to the channel where the message was posted if such a page exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A message edited from a messaging app. |
required |
Source code in wagtail_live/receivers/base.py
def change_message(self, message):
"""
Changes an edited message in a messaging app in the live page corresponding
to the channel where the message was posted if such a page exists.
Args:
message (dict): A message edited from a messaging app.
"""
channel_id = self.get_channel_id_from_message(message=message)
try:
live_page = self.get_live_page_from_channel_id(channel_id=channel_id)
except self.model.DoesNotExist:
return
message_id = self.get_message_id_from_edited_message(message=message)
live_post = live_page.get_live_post_by_message_id(message_id=message_id)
clear_live_post_content(live_post=live_post)
message_text = self.get_message_text_from_edited_message(message=message)
self.process_text(live_post=live_post.value, message_text=message_text)
files = self.get_message_files_from_edited_message(message=message)
self.process_files(live_post=live_post.value, files=files)
live_page.update_live_post(live_post=live_post)
delete_message(self, message)
Deletes a message in the live page corresponding to the channel where the message was posted if such a page exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A message deleted from a messaging app. |
required |
Source code in wagtail_live/receivers/base.py
def delete_message(self, message):
"""
Deletes a message in the live page corresponding to the channel where
the message was posted if such a page exists.
Args:
message (dict): A message deleted from a messaging app.
"""
channel_id = self.get_channel_id_from_message(message=message)
try:
live_page = self.get_live_page_from_channel_id(channel_id=channel_id)
except self.model.DoesNotExist:
return
message_id = self.get_message_id_from_edited_message(message=message)
try:
live_page.delete_live_post(message_id=message_id)
except KeyError:
logger.warning(
f"Couldn't delete message with id={message_id}.\n"
"This may be due for 2 reasons:\n"
"1- The post hasn't been saved on the live page.\n"
"2- The post has been deleted in the admin interface.\n"
)
dispatch_event(self, event)
Dispatches an event to find the corresponding handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
dict |
New event from a messaging app. |
required |
Source code in wagtail_live/receivers/base.py
def dispatch_event(self, event):
"""
Dispatches an event to find the corresponding handler.
Args:
event (dict): New event from a messaging app.
"""
raise NotImplementedError
get_channel_id_from_message(self, message)
Retrieves a channel ID from a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app. |
required |
Returns:
Type | Description |
---|---|
str |
ID of the channel which the given message belongs to. |
Source code in wagtail_live/receivers/base.py
def get_channel_id_from_message(self, message):
"""
Retrieves a channel ID from a message.
Args:
message (dict): A received message from a messaging app.
Returns:
str: ID of the channel which the given message belongs to.
"""
raise NotImplementedError
get_embed(self, text)
Checks if a text is an embed for this receiver and return embed URL if so.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
Text to check. |
required |
Returns:
Type | Description |
---|---|
str |
URL of the embed if the text contains an embed, else |
Source code in wagtail_live/receivers/base.py
def get_embed(self, text):
"""
Checks if a text is an embed for this receiver and return embed URL if so.
Args:
text (str): Text to check.
Returns:
str: URL of the embed if the text contains an embed, else `""`.
"""
return text if is_embed(text=text) else ""
get_image_content(self, image)
Retrieves the content of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
dict |
Informations about an image |
required |
Returns:
Type | Description |
---|---|
File |
Content of the image. |
Source code in wagtail_live/receivers/base.py
def get_image_content(self, image):
"""
Retrieves the content of an image.
Args:
image (dict): Informations about an image
Returns:
File: Content of the image.
"""
raise NotImplementedError
get_image_dimensions(self, image)
Retrieves the width and height of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
dict |
Informations about an image |
required |
Returns:
Type | Description |
---|---|
(int, int) |
Width and height of the image. |
Exceptions:
Type | Description |
---|---|
ValueError |
if the width and height of the image can't be retrieved. |
Source code in wagtail_live/receivers/base.py
def get_image_dimensions(self, image):
"""
Retrieves the width and height of an image.
Args:
image (dict): Informations about an image
Returns:
(int, int): Width and height of the image.
Raises:
ValueError: if the width and height of the image can't be retrieved.
"""
raise NotImplementedError
get_image_mimetype(self, image)
Retrieves the mimetype of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
dict |
Informations about an image |
required |
Returns:
Type | Description |
---|---|
str |
Mimetype of the image. |
Source code in wagtail_live/receivers/base.py
def get_image_mimetype(self, image):
"""
Retrieves the mimetype of an image.
Args:
image (dict): Informations about an image
Returns:
str: Mimetype of the image.
"""
raise NotImplementedError
get_image_name(self, image)
Retrieves the name of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
dict |
Informations about an image |
required |
Returns:
Type | Description |
---|---|
str |
Name of the image. |
Source code in wagtail_live/receivers/base.py
def get_image_name(self, image):
"""
Retrieves the name of an image.
Args:
image (dict): Informations about an image
Returns:
str: Name of the image.
"""
raise NotImplementedError
get_image_title(self, image)
Retrieves the title of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
dict |
Informations about an image |
required |
Returns:
Type | Description |
---|---|
str |
Title of the image. |
Source code in wagtail_live/receivers/base.py
def get_image_title(self, image):
"""
Retrieves the title of an image.
Args:
image (dict): Informations about an image
Returns:
str: Title of the image.
"""
raise NotImplementedError
get_live_page_from_channel_id(self, channel_id)
Retrieves the live page with a given channel ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel_id |
str |
Channel ID |
required |
Returns:
Type | Description |
---|---|
LivePageMixin |
The livepage corresponding to |
Exceptions:
Type | Description |
---|---|
Http404 |
if a page with the given |
Source code in wagtail_live/receivers/base.py
def get_live_page_from_channel_id(self, channel_id):
"""
Retrieves the live page with a given channel ID.
Args:
channel_id (str): Channel ID
Returns:
LivePageMixin: The livepage corresponding to `channel_id`.
Raises:
Http404: if a page with the given `channel_id` doesn't exist.
"""
return self.model.objects.get(channel_id=channel_id)
get_message_files(self, message)
Retrieves the files of a message.
A message is made of text and files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app |
required |
Returns:
Type | Description |
---|---|
list |
Files included in the given message. |
Source code in wagtail_live/receivers/base.py
def get_message_files(self, message):
"""
Retrieves the files of a message.
A message is made of text and files.
Args:
message (dict): A received message from a messaging app
Returns:
list: Files included in the given message.
"""
raise NotImplementedError
get_message_files_from_edited_message(self, message)
Retrieves the files from an edited message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app. |
required |
Returns:
Type | Description |
---|---|
list |
Files of the edited message. |
Source code in wagtail_live/receivers/base.py
def get_message_files_from_edited_message(self, message):
"""
Retrieves the files from an edited message.
Args:
message (dict): A received message from a messaging app.
Returns:
list: Files of the edited message.
"""
raise NotImplementedError
get_message_id_from_edited_message(self, message)
Retrieves the ID of the original message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app. |
required |
Returns:
Type | Description |
---|---|
str |
ID of the original message that is being edited. |
Source code in wagtail_live/receivers/base.py
def get_message_id_from_edited_message(self, message):
"""
Retrieves the ID of the original message.
Args:
message (dict): A received message from a messaging app.
Returns:
str: ID of the original message that is being edited.
"""
raise NotImplementedError
get_message_id_from_message(self, message)
Retrieves message's ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app |
required |
Returns:
Type | Description |
---|---|
str |
Id of the given message |
Source code in wagtail_live/receivers/base.py
def get_message_id_from_message(self, message):
"""
Retrieves message's ID.
Args:
message (dict): A received message from a messaging app
Returns:
str: Id of the given message
"""
raise NotImplementedError
get_message_text(self, message)
Retrieves the text of a message.
A message is made of text and files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app |
required |
Returns:
Type | Description |
---|---|
str |
Text of the given message. |
Source code in wagtail_live/receivers/base.py
def get_message_text(self, message):
"""
Retrieves the text of a message.
A message is made of text and files.
Args:
message (dict): A received message from a messaging app
Returns:
str: Text of the given message.
"""
raise NotImplementedError
get_message_text_from_edited_message(self, message)
Retrieves the text an edited message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
dict |
A received message from a messaging app. |
required |
Returns:
Type | Description |
---|---|
str |
Text of the edited message. |
Source code in wagtail_live/receivers/base.py
def get_message_text_from_edited_message(self, message):
"""
Retrieves the text an edited message.
Args:
message (dict): A received message from a messaging app.
Returns:
str: Text of the edited message.
"""
raise NotImplementedError
parse_text(self, text)
Parses a raw text content according to the input source formatting rules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str |
a text. |
required |
Returns:
Type | Description |
---|---|
str |
the actual content of the text. (Returns the text itself by default). |
Source code in wagtail_live/receivers/base.py
def parse_text(self, text):
"""
Parses a raw text content according to the input source formatting rules.
Args:
text (str): a text.
Returns:
str: the actual content of the text. (Returns the text itself by default).
"""
return text
process_files(self, live_post, files)
Processes the files of a message.
Creates the corresponding block for any file and adds it to the given live post.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
live_post |
LivePostBlock |
Live post to update. |
required |
files |
list |
Files to add to a live post. |
required |
Source code in wagtail_live/receivers/base.py
def process_files(self, live_post, files):
"""
Processes the files of a message.
Creates the corresponding block for any file and adds it to the given live post.
Args:
live_post (LivePostBlock):
Live post to update.
files (list):
Files to add to a live post.
"""
for item in files:
image_title = self.get_image_title(image=item)
try:
image_width, image_height = self.get_image_dimensions(image=item)
except ValueError:
logger.error(f"Unable to retrieve the dimensions of {image_title}")
continue
mime_type = self.get_image_mimetype(image=item)
if mime_type not in SUPPORTED_MIME_TYPES:
logger.error(
f"Couldn't upload {image_title}. "
+ f"Images of type {mime_type} aren't supported yet."
)
continue
image = get_image_model()(
title=image_title,
width=image_width,
height=image_height,
)
image_content = self.get_image_content(image=item)
image_name = self.get_image_name(image=item)
image.file.save(name=image_name, content=image_content, save=True)
block = construct_image_block(image=image)
add_block_to_live_post(
block_type=IMAGE,
block=block,
live_block=live_post,
)
process_text(self, live_post, message_text)
Processes the text of a message.
Parses the message, constructs corresponding block types i.e Embed or Text and add those blocks to the given live post.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
live_post |
LivePostBlock |
Live post to update. |
required |
message_text |
str |
Text to add to a live post. |
required |
Source code in wagtail_live/receivers/base.py
def process_text(self, live_post, message_text):
"""
Processes the text of a message.
Parses the message, constructs corresponding block types i.e Embed or Text
and add those blocks to the given live post.
Args:
live_post (LivePostBlock):
Live post to update.
message_text (str):
Text to add to a live post.
"""
message_parts = message_text.split("\n")
for text in message_parts:
# Avoid creating a block for empty content
text = text.strip()
if not text:
continue
block_type = ""
url = self.get_embed(text=text)
if url:
block = construct_embed_block(url=url)
block_type = EMBED
else:
text_content = self.parse_text(text=text)
block = construct_text_block(text=text_content)
block_type = TEXT
add_block_to_live_post(
block_type=block_type,
block=block,
live_block=live_post,
)