forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscourse.ts
More file actions
28 lines (27 loc) · 950 Bytes
/
discourse.ts
File metadata and controls
28 lines (27 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
interface DiscoursePostResponse {
featured_link?: string;
post_stream?: DiscoursePostStream;
}
interface DiscoursePostStream {
posts?: DiscoursePost[];
}
interface DiscoursePost {
link_counts?: DiscourseLinkCount[];
}
interface DiscourseLinkCount {
url?: string;
}
export function extractSharedIdFromPostUrl(url: string): Promise<string> {
// https://docs.discourse.org/#tag/Posts%2Fpaths%2F~1posts~1%7Bid%7D.json%2Fget
return pxt.Util.httpGetJsonAsync(url + ".json")
.then((json: DiscoursePostResponse) => {
// extract from post_stream
let projectId = json.post_stream
&& json.post_stream.posts
&& json.post_stream.posts[0]
&& json.post_stream.posts[0].link_counts
.map(link => pxt.Cloud.parseScriptId(link.url))
.filter(id => !!id)[0];
return projectId;
});
}