80 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-08-14 16:35:24 +03:00
import {JSONEncoder} from "../node_modules/assemblyscript-json/assembly/encoder";
export abstract class Response {
serialize(): string {
unreachable();
return "";
};
}
2019-08-16 12:27:30 +03:00
export class Message {
msg: string;
username: string;
constructor(msg: string, username: string) {
this.msg = msg;
this.username = username;
}
}
2019-08-14 16:35:24 +03:00
export class UnknownResponse extends Response {
constructor() {
super();
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
2019-08-15 19:10:13 +03:00
encoder.setString("msg", "cannot username request");
2019-08-14 16:35:24 +03:00
encoder.popObject();
return encoder.toString();
}
}
export class PostResponse extends Response {
2019-08-16 12:27:30 +03:00
msgCount: u32;
constructor(msgCount: u32) {
2019-08-14 16:35:24 +03:00
super();
2019-08-15 12:50:41 +03:00
this.msgCount = msgCount;
2019-08-14 16:35:24 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
2019-08-15 12:50:41 +03:00
encoder.setInteger("count", this.msgCount);
2019-08-14 16:35:24 +03:00
encoder.popObject();
return encoder.toString();
}
}
export class FetchResponse extends Response {
2019-08-16 12:27:30 +03:00
posts: Array<Message>;
2019-08-14 16:35:24 +03:00
2019-08-16 12:27:30 +03:00
constructor(posts: Array<Message>) {
2019-08-14 16:35:24 +03:00
super();
2019-08-15 12:50:41 +03:00
this.posts = posts;
2019-08-14 16:35:24 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Fetch");
2019-08-15 12:50:41 +03:00
encoder.pushArray("posts");
for (let i = 0; i < this.posts.length; i++) {
2019-08-16 12:27:30 +03:00
let twit = this.posts[i];
2019-08-14 16:35:24 +03:00
encoder.pushObject(null);
2019-08-16 12:27:30 +03:00
encoder.setString("msg", twit.msg);
encoder.setString("username", twit.username);
2019-08-14 16:35:24 +03:00
encoder.popObject();
}
encoder.popArray();
encoder.popObject();
return encoder.toString();
}
}