logs: Added filtering.

This commit is contained in:
2018-10-13 17:17:38 +02:00
parent 8345d1fc91
commit 31251d5c7b
15 changed files with 409 additions and 23 deletions
+41 -8
View File
@@ -27,21 +27,34 @@ class Character {
}
}
class CharacterHeader {
/**
* @param {string} id
* @param {string} author
* @param {string} name
*/
constructor(id, author, name) {
this.id = id
this.author = author
this.name = name
}
/**
* Create a character object from data.
*/
static fromData(data) {
return new CharacterHeader(data.id, data.author, data.name)
}
}
class ChracterAPI {
/**
* Call `characters(filter)` query
*
* @param {{ids:string[], nicks:string[], names:string[], author:string, search:string, logged:boolean}} filter
* @returns {Promise<Story[]>}
* @returns {Promise<Character[]>}
*/
list(filter = {}) {
if (filter.earliestFictionalDate != null && typeof(filter.earliestFictionalDate) !== "string") {
filter.earliestFictionalDate = filter.earliestFictionalDate.toISOString()
}
if (filter.latestFictionalDate != null && typeof(filter.latestFictionalDate) !== "string") {
filter.latestFictionalDate = filter.latestFictionalDate.toISOString()
}
return query(`
query ListCharacters($filter: CharactersFilter) {
characters(filter: $filter) {
@@ -58,6 +71,26 @@ class ChracterAPI {
})
}
/**
* Call `characters(filter)` query
*
* @param {{ids:string[], nicks:string[], names:string[], author:string, search:string, logged:boolean}} filter
* @returns {Promise<CharacterHeader[]>}
*/
listHeaders(filter = {}) {
return query(`
query ListCharacters($filter: CharactersFilter) {
characters(filter: $filter) {
id
name
author
}
}
`, {filter}).then(({characters}) => {
return characters.map(d => CharacterHeader.fromData(d))
})
}
/**
* Call `addCharacter(input)` mutation, returns addable fields.
*
+36 -3
View File
@@ -1,5 +1,7 @@
const {query} = require("../client")
let eventNames = []
class LogHeader {
/**
* Construct a log header. You should probably use the logHeaderApi instead of doing
@@ -50,7 +52,38 @@ class LogHeaderCharacter {
*/
const logHeaderApi = {
/**
* Call `stories(filter)` query
* Call `logs` query, returns event name
*
* @returns {Promise<string[]>}
*/
eventNames() {
return query(`
query LogHeadersEventNames() {
headers: logs(filter:{limit:0}) {
eventName
}
}
`, {}).then(({headers}) => {
const eventNames = []
const seen = {}
for (const header of headers) {
if (!header.eventName) {
continue
}
if (!seen[header.eventName]) {
eventNames.push(header.eventName)
seen[header.eventName] = true
}
}
return eventNames
})
},
/**
* Call `logs(filter)` query
*
* @param {{search:string, channels:string|string[], events:string|string[], open:boolean, characters:string|string[], limit:number}} filter
* @returns {Promise<LogHeader[]>}
@@ -76,9 +109,9 @@ const logHeaderApi = {
}
}
`, {filter}).then(({headers}) => {
return headers.map(h => new LogHeader(h.id, h.shortId, h.date, h.channelName, h.title, h.description, h.eventName, h.open, h.characters))
return headers.map(h => new LogHeader(h.id, h.shortId, h.date, h.channelName, h.title, h.description, h.eventName, h.open, h.characters))
})
},
}
module.exports = {LogHeader, LogHeaderCharacter, logHeaderApi}
module.exports = {LogHeader, LogHeaderCharacter, logHeaderApi, eventNames}