-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatform.ts
173 lines (152 loc) · 4.46 KB
/
Platform.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as fs from "fs";
import Logger from "./Logger";
import Folder from "./Folder";
import Post from "./Post";
import { PostStatus } from "./Post";
import { PlatformId } from "../platforms";
/**
* Platform base class to extend all platforms on
*
* When extending, implement at least
* preparePost() and publishPost()
*/
export default class Platform {
active: boolean = false;
id: PlatformId = PlatformId.UNKNOWN;
defaultBody: string = "Fairpost feed";
/**
* Return a small report for this feed
* @returns the report in text
*/
report(): string {
Logger.trace("Platform", "report");
let report = "";
report += "\nPlatform: " + this.id;
report += "\n - active: " + this.active;
return report;
}
/**
* getPostFileName
* @returns the intended name for a post of this
* platform to be saved in this folder.
*/
getPostFileName(): string {
return "_" + this.id + ".json";
}
/**
* getPost
* @param folder - the folder to get the post for this platform from
* @returns {Post} the post for this platform for the given folder, if it exists.
*/
getPost(folder: Folder): Post | undefined {
Logger.trace("Platform", "getPost");
if (fs.existsSync(folder.path + "/" + this.getPostFileName())) {
const data = JSON.parse(
fs.readFileSync(folder.path + "/" + this.getPostFileName(), "utf8"),
);
if (data) {
return new Post(folder, this, data);
}
}
return;
}
/**
* preparePost
*
* Prepare the post for this platform for the
* given folder, and save it. Optionally create
* derivates of media and save those, too.
*
* If the post exists and is published, ignore.
* If the post exists and is failed, set it back to
* unscheduled.
* @param folder - the folder for which to prepare a post for this platform
* @returns the prepared post
*/
async preparePost(folder: Folder): Promise<Post> {
Logger.trace("Platform", "preparePost");
const post = this.getPost(folder) ?? new Post(folder, this);
if (post.status === PostStatus.PUBLISHED) {
return post;
}
if (post.status === PostStatus.FAILED) {
post.status = PostStatus.UNSCHEDULED;
}
// some default logic. override this
// in your own platform if you need.
post.files = folder.getFiles();
if (post.files.text?.includes("body.txt")) {
post.body = fs.readFileSync(post.folder.path + "/body.txt", "utf8");
} else if (post.files.text.length === 1) {
const bodyFile = post.files.text[0];
post.body = fs.readFileSync(post.folder.path + "/" + bodyFile, "utf8");
} else {
post.body = this.defaultBody;
}
if (post.files.text?.includes("title.txt")) {
post.title = fs.readFileSync(post.folder.path + "/title.txt", "utf8");
} else {
post.title = post.body.split("\n", 1)[0];
}
if (post.files.text?.includes("tags.txt")) {
post.tags = fs.readFileSync(post.folder.path + "/tags.txt", "utf8");
}
if (post.title) {
post.valid = true;
}
if (post.status === PostStatus.UNKNOWN) {
post.status = PostStatus.UNSCHEDULED;
}
post.save();
return post;
}
/**
* publishPost
*
* publish the post for this platform, sync.
* set the posted date to now.
* add the result to post.results
* on success, set the status to published and return true,
* else set the status to failed and return false
* @returns {Promise} succes status
*/
async publishPost(post: Post, dryrun: boolean = false): Promise<boolean> {
Logger.trace("Platform", "publishPost", post.id, dryrun);
post.results.push({
date: new Date(),
success: false,
error: new Error("publishing not implemented for " + this.id),
response: {},
});
post.published = undefined;
post.status = PostStatus.FAILED;
post.save();
return false;
}
/**
* setup
*
* Set the platform up. Get the required keys and tokens.
* This may involve starting a webserver and/or communicating
* via the CLI.
* @returns - any object
*/
async setup() {
throw new Error(
"No setup implemented for " +
this.id +
". Read the docs in the docs folder.",
);
}
/**
* test
*
* Test the platform installation. This should not post
* anything, but test access tokens et al. It can return
* anything.
* @returns - any object
*/
async test(): Promise<unknown> {
return "No tests implemented for " + this.id;
}
}