You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.9 KiB
115 lines
2.9 KiB
const {query} = require("../client")
|
|
|
|
class Post {
|
|
/**
|
|
* @param {string} id
|
|
* @param {number} position
|
|
* @param {Date | string | number} time
|
|
* @param {string} kind
|
|
* @param {string} nick
|
|
* @param {string} text
|
|
*/
|
|
constructor(id, position, time, kind, nick, text) {
|
|
this.id = id
|
|
this.position = position
|
|
this.time = new Date(time)
|
|
this.kind = kind
|
|
this.nick = nick
|
|
this.text = text
|
|
}
|
|
|
|
static fromData(data) {
|
|
return new Post(data.id, data.position, data.time, data.kind, data.nick, data.text)
|
|
}
|
|
}
|
|
|
|
class PostAPI {
|
|
/**
|
|
* Call `addPost(input)` mutation, returns the id and edited fields of the post
|
|
*
|
|
* @param {{logId:string, time:Date, kind:string, nick:string, text:string}} input
|
|
* @returns {Promise<{id:string, time:Date, kind:string, nick:string, text:string}>}
|
|
*/
|
|
add(input) {
|
|
return query(`
|
|
mutation AddPost($input: PostAddInput!) {
|
|
addPost(input: $input) {
|
|
id
|
|
position
|
|
time
|
|
kind
|
|
nick
|
|
text
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["post.add"]}).then(({addPost}) => {
|
|
return new Post(addPost.id, addPost.position, addPost.time, addPost.kind, addPost.nick, addPost.text)
|
|
})
|
|
}
|
|
|
|
|
|
/**
|
|
* Call `movePost(input)` mutation, returns the id and position of all affected posts.
|
|
*
|
|
* @param {{id:string, toPosition: number}} input
|
|
* @returns {Promise<{id:string, position:number}[]>}
|
|
*/
|
|
move(input) {
|
|
return query(`
|
|
mutation MovePost($input: PostMoveInput!) {
|
|
movePost(input: $input) {
|
|
id
|
|
position
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["post.move"]}).then(({movePost}) => {
|
|
return movePost
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Call `editPost(input)` mutation, returns the id and edited fields of the post
|
|
*
|
|
* @param {{id:string, time:Date, kind:string, nick:string, text:string}} input
|
|
* @returns {Promise<{id:string, time:Date, kind:string, nick:string, text:string}>}
|
|
*/
|
|
edit(input) {
|
|
return query(`
|
|
mutation EditPost($input: PostEditInput!) {
|
|
editPost(input: $input) {
|
|
id
|
|
time
|
|
kind
|
|
nick
|
|
text
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["post.edit"]}).then(({editPost}) => {
|
|
if (editPost.time != null) {
|
|
editPost.time = new Date(editPost.time)
|
|
}
|
|
|
|
return editPost
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Call `removePost(input)` mutation, returns the id of the affected post.
|
|
*
|
|
* @param {{id:string, toPosition: number}} input
|
|
* @returns {Promise<{id:string}>}
|
|
*/
|
|
remove(input) {
|
|
return query(`
|
|
mutation RemovePost($input: PostRemoveInput!) {
|
|
removePost(input: $input) {
|
|
id
|
|
}
|
|
}
|
|
`, {input}, {permissions: ["post.remove"]}).then(({removePost}) => {
|
|
return removePost
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = {Post, postApi: new PostAPI}
|