120 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-08-14 19:00:22 +03:00
import {JSONEncoder} from "../node_modules/assemblyscript-json/assembly/encoder";
2019-08-17 20:15:34 +03:00
import {JSONDecoder, JSONHandler} from "../node_modules/assemblyscript-json/assembly/decoder";
import {string2Bytes} from "./request";
2019-08-14 19:00:22 +03:00
export abstract class Response {
serialize(): string {
unreachable();
return "";
};
}
export class Message {
2019-08-16 19:47:38 +03:00
message: string;
2019-08-15 19:10:13 +03:00
username: string;
2019-08-14 19:00:22 +03:00
2019-08-16 19:47:38 +03:00
constructor(message: string, username: string) {
this.message = message;
2019-08-15 19:10:13 +03:00
this.username = username;
2019-08-14 19:00:22 +03:00
}
}
export class UnknownResponse extends Response {
constructor() {
super();
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
2019-08-16 19:47:38 +03:00
encoder.setString("message", "cannot username request");
2019-08-14 19:00:22 +03:00
encoder.popObject();
return encoder.toString();
}
}
export class PostResponse extends Response {
2019-08-16 19:47:38 +03:00
count: u32;
constructor(count: u32) {
2019-08-14 19:00:22 +03:00
super();
2019-08-16 19:47:38 +03:00
this.count = count;
2019-08-14 19:00:22 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
2019-08-16 19:47:38 +03:00
encoder.setInteger("count", this.count);
2019-08-14 19:00:22 +03:00
encoder.popObject();
return encoder.toString();
}
}
2019-08-17 20:15:34 +03:00
class PostsJSONEventsHandler extends JSONHandler {
public messages: Array<Message> = new Array<Message>();
message: string | null;
username: string | null;
setString(name: string, value: string): void {
if (name == "message") {
this.message = value;
} else if (name == "username") {
this.username = value;
}
}
pushObject(name: string): bool {
this.message = null;
this.username = null;
return true;
}
popObject(): void {
let message = new Message(this.message as string, this.username as string);
this.messages.push(message)
}
}
export function decodePosts(input: string): FetchResponse {
let jsonHandler = new PostsJSONEventsHandler();
let decoder = new JSONDecoder<PostsJSONEventsHandler>(jsonHandler);
let bytes = string2Bytes(input);
decoder.deserialize(bytes);
let messages = jsonHandler.messages;
return new FetchResponse(messages);
}
2019-08-14 19:00:22 +03:00
export class FetchResponse extends Response {
2019-08-15 12:50:41 +03:00
posts: Array<Message>;
2019-08-14 19:00:22 +03:00
2019-08-15 12:50:41 +03:00
constructor(posts: Array<Message>) {
2019-08-14 19:00:22 +03:00
super();
2019-08-15 12:50:41 +03:00
this.posts = posts;
2019-08-14 19:00:22 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
2019-08-15 12:50:41 +03:00
encoder.pushArray("posts");
for (let i = 0; i < this.posts.length; i++) {
let twit = this.posts[i];
2019-08-14 19:00:22 +03:00
encoder.pushObject(null);
2019-08-16 19:47:38 +03:00
encoder.setString("message", twit.message);
2019-08-15 19:10:13 +03:00
encoder.setString("username", twit.username);
2019-08-14 19:00:22 +03:00
encoder.popObject();
}
encoder.popArray();
encoder.popObject();
return encoder.toString();
}
}