Skip to content

Commit

Permalink
Allow posting without publication name or Id
Browse files Browse the repository at this point in the history
This feature allows you to post under a specific user, instead of under a specific publication.

This resolves issue #16

Signed-off-by: Brend Smits <[email protected]>
  • Loading branch information
Brend-Smits committed Jan 4, 2022
1 parent 3910df0 commit a09b38d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
5 changes: 0 additions & 5 deletions Action/Services/ConfigureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ public void CheckForValidSettings()
throw new ArgumentNullException(nameof(Program.Settings.IntegrationToken),
$"The {nameof(Program.Settings.IntegrationToken)} parameter was not set successfully.");

if (string.IsNullOrEmpty(Program.Settings.PublicationId) &&
string.IsNullOrEmpty(Program.Settings.PublicationName))
throw new ArgumentNullException(nameof(Program.Settings.PublicationId),
"Either the parameter PublicationId or PublicationName should be filled in.");

if (string.IsNullOrEmpty(Program.Settings.Title))
throw new ArgumentNullException(nameof(Program.Settings.Title),
$"The {nameof(Program.Settings.Title)} parameter was not set successfully.");
Expand Down
44 changes: 38 additions & 6 deletions Action/Services/MediumService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ public class MediumService
public async Task SubmitNewContentAsync()
{
User user = await GetCurrentMediumUserAsync();
Publication pub = await FindMatchingPublicationAsync(user.Id, Program.Settings.PublicationName,
Program.Settings.PublicationId);
if (pub == null) throw new Exception("Could not find publication, did you enter the correct name or id?");
Publication pub = null;
if (Program.Settings.PublicationName != null || Program.Settings.PublicationId != null)
{
pub = await FindMatchingPublicationAsync(user.Id, Program.Settings.PublicationName,
Program.Settings.PublicationId);
if (pub == null) throw new Exception("Could not find publication, did you enter the correct name or id?");
}


if (!string.IsNullOrEmpty(Program.Settings.File))
Program.Settings.Content = await ReadFileFromPath(Program.Settings.File);
Expand All @@ -39,8 +44,15 @@ public async Task SubmitNewContentAsync()
Program.Settings.License = Program.Settings.License?.ToLower();
Program.Settings.PublishStatus = Program.Settings.PublishStatus?.ToLower();
Program.Settings.ContentFormat = Program.Settings.ContentFormat?.ToLower();

MediumCreatedPost post = await CreateNewPostUnderPublicationAsync(pub.Id);
MediumCreatedPost post;
if (pub != null)
{
post = await CreateNewPostUnderPublicationAsync(pub.Id);
}
else
{
post = await CreateNewPostWithoutPublicationAsync(user.Id);
}
SetWorkflowOutputs(post);
}

Expand Down Expand Up @@ -144,6 +156,26 @@ await response.Content.ReadAsByteArrayAsync())
/// <param name="publicationId">The id of the publication</param>
/// <returns>Medium Created Post</returns>
public async Task<MediumCreatedPost> CreateNewPostUnderPublicationAsync(string publicationId)
{
return await CreateNewPostAsync($"publications/{publicationId}/posts");
}

/// <summary>
/// Create a new post without a publication.
/// </summary>
/// <param name="authorId">The id of the author</param>
/// <returns>Medium Created Post</returns>
public async Task<MediumCreatedPost> CreateNewPostWithoutPublicationAsync(string authorId)
{
return await CreateNewPostAsync($"users/{authorId}/posts");
}

/// <summary>
/// Create a new post for either a publication or author
/// </summary>
/// <param name="requestUri">The uri of the endpoint</param>
/// <returns>Medium Created Post</returns>
public async Task<MediumCreatedPost> CreateNewPostAsync(string requestUri)
{
Post post = new Post
{
Expand All @@ -155,7 +187,7 @@ public async Task<MediumCreatedPost> CreateNewPostUnderPublicationAsync(string p
CanonicalUrl = Program.Settings.CanonicalUrl,
License = Program.Settings.License
};
HttpResponseMessage response = await Program.Client.PostAsync($"publications/{publicationId}/posts",
HttpResponseMessage response = await Program.Client.PostAsync($"{requestUri}",
new StringContent(JsonSerializer.Serialize(post), Encoding.UTF8, "application/json"))
.ConfigureAwait(false);
try
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<p align="right">(<a href="#top">back to top</a>)</p>

## State

> Functional
## Usage
Expand All @@ -34,7 +35,7 @@ The easiest way to use this action is to add the following into your workflow fi

1. Add the following part in your workflow file:

```
```YAML
jobs:
post-to-medium:
name: Post to Medium
Expand All @@ -47,12 +48,12 @@ The easiest way to use this action is to add the following into your workflow fi
content: "content here"
content_format: "markdown"
notify_followers: "false"
publication_name: "publication"
tags: "test,tag"
title: "title"
license: "all-rights-reserved"
publish_status: "draft"
```
## Inputs
| parameter | description | required | default |
Expand All @@ -64,13 +65,12 @@ The easiest way to use this action is to add the following into your workflow fi
| publish_status | Post's status. Valid values are 'draft', 'public', or 'unlisted'. | `false` | draft |
| notify_followers | Whether to notify followers that the user has published. | `false` | false |
| license | Post's license. Valid values are 'all-rights-reserved', 'cc-40-by', 'cc-40-by-sa', 'cc-40-by-nd', 'cc-40-by-nc', 'cc-40-by-nc-nd', 'cc-40-by-nc-sa', 'cc-40-zero', 'public-domain'. | `false` | all-rights-reserved |
| publication_name | The name of the publication the post is being created under. Either PublicationName of PublicationId should be set. | `false` | |
| publication_id | The id of the publication the post is being created under. If you do not know the Id, use PublicationName. | `false` | |
| publication_name | The name of the publication the post is being created under. If you wish to publish on a publication, either PublicationName of PublicationId should be set. If neither PublicationId or PublicationName is filled in, it will post on the user that is authentication.| `false` | |
| publication_id | The id of the publication the post is being created under. If you do not know the Id, use PublicationName. If you wish to publish on a publication, either PublicationName of PublicationId should be set. If neither PublicationId or PublicationName is filled in, it will post on the user that is authentication. | `false` | |
| canonical_url | The canonical URL of the post. If canonicalUrl was not specified in the creation of the post, this field will not be present. | `false` | |
| tags | The post’s tags. Provide a comma separated string without spaces. | `true` | |
| title | The post's title. | `true` | |
| parse_frontmatter | Should the action read and delete frontmatter in a markdown file. Frontmatter should start with --- and end with ---. Should be on the top of the page. When parsing frontmatter, only markdown is supported and settings will be overwritten if specified in the frontmatter. | `true` | false |

| parse_frontmatter | Should the action read and delete frontmatter in a markdown file. Frontmatter should start with --- and end with ---. Should be on the top of the page. When parsing frontmatter, only markdown is supported and settings will be overwritten if specified in the frontmatter. PascalCasing and under_scored lowercasing naming convention is supported. | `true` | false |

## Outputs

Expand Down

0 comments on commit a09b38d

Please sign in to comment.