Polyjuice Client v0.4.2 Polyjuice.Client.Attachment View Source

Attachment related functions.

Use the new function to create an attachment. The attachment type can be set by using the as_* functions. For example:

Polyjuice.Client.Room.send_message(
  client,
  "!roomid",
  Polyjuice.Client.Attachment.new(
    client,
    {:file, "file.png"},
    mimetype: "image/png"
  )
  # mark it as image, and set the size
  |> Polyjuice.Client.Attachment.as_image(width: 250, height: 250)
  |> Polyjuice.Client.Attachment.add_thumbnail(
    client,
    {:file, "thumbnail.png"},
    "image/png",
    width: 10,
    height: 10
  )
)

Link to this section Summary

Types

Represents an attachment message.

Functions

Add a thumbnail to an attachment object.

Transform a attachment object object into an attachment audio object.

Transform a attachment object object into an attachment file object.

Transform a attachment object object into an attachment image object

Transform a attachment object object into an attachment video object.

Download the file from an attachment object.

Download the thumbnail from an attachment object.

Create an attachment object.

Link to this section Types

Link to this type

attachment()

View Source
attachment() :: map()

Represents an attachment message.

The map will have the following keys: "msgtype" (a string), "body" (a string), and "info" (a map).

Link to this section Functions

Link to this function

add_thumbnail(msg, client_api, data, mimetype, opts \\ [])

View Source
add_thumbnail(
  msg :: attachment(),
  client_api :: Polyjuice.Client.API.t() | nil,
  data ::
    {:data, binary(), String.t()}
    | {:file, String.t()}
    | {:url, String.t() | URI.t()},
  mimetype :: String.t(),
  opts :: Keyword.t()
) :: attachment()

Add a thumbnail to an attachment object.

Supported options are:

  • height: the thumbnail height
  • width: the thumbnail width
  • size: the size in bytes of the thumbnail
Link to this function

as_audio(msg, duration \\ nil)

View Source
as_audio(msg :: attachment(), duration :: integer() | nil) :: attachment()

Transform a attachment object object into an attachment audio object.

The duration is given in milliseconds.

Link to this function

as_file(msg, filename)

View Source
as_file(msg :: attachment(), filename :: String.t()) :: attachment()

Transform a attachment object object into an attachment file object.

Link to this function

as_image(msg, opts \\ [])

View Source
as_image(msg :: attachment(), opts :: Keyword.t()) :: attachment()

Transform a attachment object object into an attachment image object

Supported options are:

  • width: the intended display width of the image
  • height: the intended display height of the image
Link to this function

as_video(msg, opts \\ [])

View Source
as_video(msg :: attachment(), opts :: Keyword.t()) :: attachment()

Transform a attachment object object into an attachment video object.

Supported options:

  • width: the display width of the video
  • height: the display height of the video
  • duration: the duration of the video in milliseconds
Link to this function

download(client, msg)

View Source
download(client_api :: Polyjuice.Client.API.t(), msg :: attachment()) ::
  {:ok, String.t(), String.t(), Enumerable.t()} | any()

Download the file from an attachment object.

Returns the same format as Polyjuice.Client.Media.download: if successful, returns a tuple of the form {:ok, filename, content_type, body}, where body is a Stream such that Enum.join(body) is the file contents.

Link to this function

download_thumbnail(client, msg)

View Source
download_thumbnail(client_api :: Polyjuice.Client.API.t(), msg :: attachment()) ::
  {:ok, String.t(), String.t(), Enumerable.t()} | any()

Download the thumbnail from an attachment object.

Returns the same format as Polyjuice.Client.Media.download: if successful, returns a tuple of the form {:ok, filename, content_type, body}, where body is a Stream such that Enum.join(body) is the file contents.

Link to this function

new(client_api, data, opts \\ [])

View Source
new(
  client_api :: Polyjuice.Client.API.t(),
  data ::
    {:data, binary(), String.t()}
    | {:file, String.t()}
    | {:url, String.t() | URI.t()},
  opts :: Keyword.t()
) :: attachment()

Create an attachment object.

The attachment data can be provided as:

  • a tuple of the form {:data, file_contents, filename}, which will use file_contents as the file;
  • a tuple of the form {:file, filename}, which will upload a local file; or
  • a tuple of the form {:url, mxc_url}, which uses an already-uploaded file.

Supported options are:

  • body: the message body. Defaults to the filename, if provided, or "Attachment" if the data is of the form {:url, mxc_url}.
  • mimetype: the mimetype for the attachment. Defaults to application/octet-stream.
  • size: the size in bytes of the file. If not specified, it will attempt to determine the size automatically.
  • info: metadata about the image, in the format specified by the info property of the m.file (or similar) msgtype
  • msgtype: the msgtype to use for the message. Defaults to m.file. This can also be set by using one of the as_* functions (e.g. as_image will change the message to an m.image msgtype) rather than setting the msgtype here.