const {query, Subscription} = require("../client") const {Character} = require("./Character") const {Channel} = require("./Channel") const {Post} = require("./Post") class Log { /** * @param {string} id * @param {string} shortId * @param {Date | string | number} date * @param {string} title * @param {string} eventName * @param {string} description * @param {string} open * @param {any} channel * @param {any[]} characters * @param {any[]} posts */ constructor(id, shortId, date, title, eventName, description, open, channel, characters, posts) { this.id = id this.shortId = shortId this.date = date this.title = title this.eventName = eventName this.description = description this.open = open this.channel = Channel.fromData(channel) this.characters = characters.map(c => Character.fromData(c)) this.posts = posts.map(p => Post.fromData(p)) } static fromData(data) { return new Log( data.id, data.shortId, data.date, data.title, data.eventName, data.description, data.open, data.channel, data.characters, data.posts ) } } class LogAPI { find(id) { return query(` query FindLog($id: String!) { log(id: $id) { id shortId date title eventName description open channel { name logged hub eventName locationName } characters { id nicks author name shortName description } posts { id position time kind nick text } nextLogs { log {id title date channelName} hasEvent hasChannel characters {name} } prevLogs { log {id title date channelName} hasEvent hasChannel characters {name} } } } `, {id}).then(({log}) => { return log }) } /** * Call `addLog(input)` mutation, returns a Log * * @param {{time:Date, channel:string, title:string, eventName:string, description:string, open:boolean}} input * @returns {Promise} */ add(input) { return query(` mutation FindLog($input: LogAddInput!) { addLog(input: $input) { id shortId date title eventName description open channel { name logged hub eventName locationName } characters { id nicks author name shortName description } posts { id position time kind nick text } } } `, {input}, {permissions: ["log.add "]}).then(({addLog}) => { return Log.fromData(addLog) }) } /** * Call `importLog(input)` mutation, returns multpile logs. * * @param {{importer:"MircLike"|"ForumLog", date?:Date|string|number, channel:string, timezone:string, data:string}} input * @returns {Promise} */ import(input) { return query(` mutation ImportLog($input: LogImportInput!) { importLog(input: $input) { id shortId date title eventName description open channel { name logged hub eventName locationName } characters { id nicks author name shortName description } posts { id position time kind nick text } } } `, {input}, {permissions: ["log.add"]}).then(({importLog}) => { return importLog.map(d => Log.fromData(d)) }) } /** * Call `editLog(input)` mutation, returns the id and all editable fields. * * @param {{id:string, title:string, eventName:string, description:string, open:boolean}} input * @returns {Promise<{id:string, title:string, eventName:string, description:string, open:boolean}>} */ edit(input) { return query(` mutation EditLog($input: LogEditInput!) { editLog(input: $input) { id title eventName description open } } `, {input}, {permissions: ["log.edit"]}).then(({editLog}) => { return editLog }) } /** * Call `removeLog(input)` mutation, returns the id * * @param {{id:string}} input * @returns {Promise<{id:string}>} */ remove(input) { return query(` mutation RemoveLog($input: LogRemoveInput!) { removeLog(input: $input) { id } } `, {input}, {permissions: ["log.remove"]}).then(({editLog}) => { return editLog }) } /** * Subscribe to the changes. * * @param {string} logId The long logId, the short one won't do. */ subscribeChanges(logId) { return new Subscription(` subscription Changes($keys: [ChangeKeyInput!]!) { changes(filter: {keys: $keys}) { id model op author listed keys { model id } date objects { type: __typename ...on Log { id title eventName description open } ...on Post { id time kind nick text position } } } } `, {keys: [{model: "Log", id: logId}]}) } } module.exports = { Log, logsApi: new LogAPI }