1 | import fetchJSON from './fetchJSON.js'
|
2 | import MapWithFilter from './MapWithFilter.js'
|
3 |
|
4 | class GuildArray extends Array {
|
5 | static from (iter) {
|
6 | return super.from(iter).map(data => new Guild(data))
|
7 | }
|
8 |
|
9 | getChannel (id) {
|
10 | const guild = this.find(guild => guild.channels.has(id))
|
11 | if (!guild) return null
|
12 | return guild.channels.get(id)
|
13 | }
|
14 | }
|
15 |
|
16 | class Guild {
|
17 | constructor (data) {
|
18 | this.name = data.name
|
19 | this.id = data.id
|
20 | this.channels = new MapWithFilter(data.channels.map(channel => [ channel.id, new Channel(channel, this) ]))
|
21 | }
|
22 | }
|
23 |
|
24 | class Channel {
|
25 | constructor (data, guild) {
|
26 | this.guild = guild
|
27 | this.type = data.type
|
28 | this.id = data.id
|
29 | this.name = data.name
|
30 | this.category = data.category
|
31 | }
|
32 |
|
33 | get link () {
|
34 | return `https://discordapp.com/channels/${this.guild.id}/${this.id}`
|
35 | }
|
36 | }
|
37 |
|
38 | class Thread {
|
39 | constructor (data) {
|
40 | this.id = data.id
|
41 | this.name = data.name
|
42 | this.isGroup = data.isGroup
|
43 | }
|
44 |
|
45 | get link () {
|
46 | return `https://messenger.com/t/${this.id}/`
|
47 | }
|
48 | }
|
49 |
|
50 | export default {
|
51 | async getGuilds () {
|
52 | const guilds = await fetchJSON('/discord/channels')
|
53 | return GuildArray.from(guilds)
|
54 | },
|
55 | async getThreads () {
|
56 | const threads = await fetchJSON('/messenger/threads')
|
57 | return new MapWithFilter(threads.map(thread => [ thread.id, new Thread(thread) ]))
|
58 | },
|
59 | getConfig (prefix) {
|
60 | return fetchJSON(`/config/${prefix || ''}`)
|
61 | }
|
62 | }
|