Skip to content

Commit e308a94

Browse files
committed
Add support for long posts
1 parent 4d512a3 commit e308a94

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

devblog/devblog/ClientApp/package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

devblog/devblog/ClientApp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"react-router-bootstrap": "^0.26.2",
2626
"react-router-dom": "^6.8.1",
2727
"react-scripts": "^5.0.1",
28+
"react-spinners": "^0.13.8",
2829
"reactstrap": "^9.1.3",
2930
"rimraf": "^3.0.2",
3031
"web-vitals": "^2.1.4",

devblog/devblog/ClientApp/src/pages/AddPost.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FormEvent, useEffect, useState } from "react";
22
import ReactMarkdown from "react-markdown";
33
import { GetIsAdmin } from "../components/AuthenticationService";
4+
import { BarLoader, BeatLoader, BounceLoader, ClimbingBoxLoader, ClockLoader, GridLoader, FadeLoader, HashLoader, PuffLoader, RingLoader, RiseLoader, CircleLoader, PulseLoader, ScaleLoader } from "react-spinners";
45
import "./styles/AddPost.css";
56

67
const AddPost = () => {
@@ -17,9 +18,11 @@ const AddPost = () => {
1718
const [postToDiscord, setPostToDiscord] = useState(false);
1819
const [postToMastodon, setPostToMastodon] = useState(false);
1920
const [postToDevBlog, setPostToDevBlog] = useState(false);
21+
const [isLoading, setIsLoading] = useState(false);
2022

2123
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
2224
e.preventDefault();
25+
setIsLoading(true);
2326

2427
const formData = new FormData();
2528
if (files != null) {
@@ -42,7 +45,6 @@ const AddPost = () => {
4245
if (postToDevBlog) {
4346
setDevBlogUploadStatus(data.devBlogStatus.statusCode);
4447
setDevBlogErrMessage(data.devBlogStatus.reasonPhrase);
45-
console.log(data);
4648
}
4749

4850
if (postToDiscord) {
@@ -57,6 +59,8 @@ const AddPost = () => {
5759
}
5860
})
5961
.catch(e => console.log("Error uploading file: ", e));
62+
63+
setIsLoading(false);
6064
}
6165

6266
useEffect(() => {
@@ -105,6 +109,8 @@ const AddPost = () => {
105109
</li>
106110
</ul>
107111

112+
<PulseLoader className="loader" color="WHITE" loading={isLoading} />
113+
108114
{/* Upload Statuses */}
109115
<div className="upload-status">
110116
{/* discord */}
@@ -148,6 +154,7 @@ const AddPost = () => {
148154

149155
<label>Description
150156
<p>Mastodon character limit: {charCount}/500</p>
157+
<p>Discord character limit: {charCount}/2000</p>
151158
<div className="addpost-description">
152159
<textarea placeholder="Write description here..." onChange={(e) => setDescription(e.currentTarget.value)} />
153160
</div>

devblog/devblog/ClientApp/src/pages/styles/AddPost.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
list-style: none;
2222
}
2323

24+
.loader {
25+
padding: 10px;
26+
}
27+
2428
.create-post {
2529
align-items: center;
2630
justify-content: center;
@@ -51,14 +55,14 @@ a {
5155
border: .15em solid rgb(70, 68, 60);
5256
border-radius: .5em;
5357
padding: 10px 20px;
58+
margin-bottom: 100px;
5459
}
5560

5661
.addpost-description {
5762
display: flex;
5863
}
5964

6065
.addpost-description>textarea {
61-
/* background-color: rgb(26, 32, 41); */
6266
width: 100%;
6367
min-height: 100px;
6468
resize: none;
@@ -71,7 +75,6 @@ a {
7175
padding: 20px;
7276
margin-bottom: 20px;
7377
min-height: 100px;
74-
/* background-color: rgb(26, 32, 41); */
7578
}
7679

7780
.create-post {

devblog/devblog/Services/PostService.cs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Mastonet;
88
using System.Net;
99
using devblog.Controllers;
10+
using Microsoft.Extensions.Hosting;
1011

1112
namespace devblog.Services
1213
{
@@ -166,14 +167,40 @@ private async Task<HttpResponseMessage> PostToDiscord(string description, IFormF
166167
var channel = await _discordClient.GetChannelAsync(_config.GetValue<ulong>("DiscordChannelId")) as IMessageChannel;
167168
var res = new HttpResponseMessage();
168169

170+
List<string> descriptions = new List<string>();
171+
int posts = (int)Math.Ceiling(description.Length / 1988.0);
172+
169173
// add imgs to request
170174
List<FileAttachment> attachments = new List<FileAttachment>();
171175
foreach (var file in files)
172176
attachments.Add(new FileAttachment(file.OpenReadStream(), file.FileName));
173177

174178
try
175179
{
176-
await channel.SendFilesAsync(attachments, description);
180+
// because discords post chararacter limit is 2000, the description needs to broken into incriments
181+
// of 2000 (including the 'part' string variable below)
182+
for (int i = 1; i < posts; i++)
183+
{
184+
string postDescription = description.Substring(0, 1988);
185+
description = description.Remove(0, 1988);
186+
descriptions.Add(postDescription);
187+
}
188+
189+
descriptions.Add(description);
190+
191+
for (int i = 1; i <= descriptions.Count; i++)
192+
{
193+
string part = $"(Part {i}/{descriptions.Count}) ";
194+
195+
if (i == 1)
196+
{
197+
await channel.SendFilesAsync(attachments, part + descriptions[i - 1]);
198+
} else
199+
{
200+
await channel.SendMessageAsync(part + descriptions[i - 1]);
201+
}
202+
}
203+
177204
await _discordClient.StopAsync();
178205
}
179206
catch (Exception e)
@@ -199,16 +226,40 @@ private async Task<HttpResponseMessage> PostToMastodon(string description, IForm
199226

200227
// add imgs to request
201228
List<string> attachments = new List<string>();
229+
List<string> descriptions = new List<string>();
230+
int posts = (int)Math.Ceiling(description.Length / 488.0);
202231

203232
try
204233
{
205-
foreach (var file in files)
234+
// because mastodons post chararacter limit is 500, the description needs to broken into incriments
235+
// of 500 (including the 'part' string variable below)
236+
for (int i = 1; i < posts; i++)
206237
{
207-
var media = new MediaDefinition(file.OpenReadStream(), file.FileName);
208-
var mediaId = await client.UploadMedia(media);
209-
attachments.Add(mediaId.Id);
238+
string postDescription = description.Substring(0, 488);
239+
description = description.Remove(0, 488);
240+
descriptions.Add(postDescription);
241+
}
242+
243+
descriptions.Add(description);
244+
245+
for (int i = 1; i <= descriptions.Count; i++)
246+
{
247+
string part = $"(Part {i}/{descriptions.Count}) ";
248+
249+
if(i == 1)
250+
{
251+
foreach (var file in files)
252+
{
253+
var media = new MediaDefinition(file.OpenReadStream(), file.FileName);
254+
var mediaId = await client.UploadMedia(media);
255+
attachments.Add(mediaId.Id);
256+
}
257+
await client.PublishStatus(part + descriptions[i - 1], mediaIds: attachments);
258+
} else
259+
{
260+
await client.PublishStatus(part + descriptions[i - 1]);
261+
}
210262
}
211-
await client.PublishStatus(description, mediaIds: attachments);
212263
}
213264
catch (Exception e)
214265
{

0 commit comments

Comments
 (0)