message method

Future<void> message(
  1. String subject,
  2. String message, {
  3. SubredditRef? fromSubreddit,
})

Send a message.

subject is the subject of the message, message is the content of the message, and fromSubreddit is a Subreddit that the message should be sent from. fromSubreddit must be a subreddit that the current user is a moderator of and has permissions to send mail on behalf of the subreddit.

Implementation

// TODO(bkonyi): error handling
Future<void> message(String subject, String message,
    {SubredditRef? fromSubreddit}) async {
  var messagePrefix = '';
  if (this is Subreddit) {
    messagePrefix = '#';
  }
  final data = {
    'subject': subject,
    'text': message,
    'to': messagePrefix + displayName,
    'api_type': 'json',
  };

  if (fromSubreddit != null) {
    data['from_sr'] = fromSubreddit.displayName;
  }

  try {
    await reddit.post(apiPath['compose'], data);
  } on DRAWInvalidSubredditException catch (e) {
    String name;
    if (e.subredditName == 'from_sr') {
      name = fromSubreddit!.displayName;
    } else {
      name = displayName;
    }
    throw DRAWInvalidSubredditException(name);
    // ignore: unused_catch_clause
  } on DRAWInvalidRedditorException catch (e) {
    throw DRAWInvalidRedditorException(displayName);
  }
}