This repository has been archived by the owner on May 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
107 lines (91 loc) · 2.38 KB
/
chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-FileCopyrightText: 2021 Anders Rune Jensen <[email protected]>
//
// SPDX-License-Identifier: CC0-1.0
module.exports = function (pull, ssbSingleton) {
let chatFeed = null
function getChatFeed(SSB, cb) {
if (chatFeed !== null) return cb(null, chatFeed)
SSB.metafeeds.findOrCreate((err, metafeed) => {
const details = {
feedpurpose: '8K/chat',
feedformat: 'classic',
}
SSB.metafeeds.findOrCreate(
metafeed,
(f) => f.feedpurpose === details.feedpurpose,
details,
(err, feed) => {
if (err) return cb(err)
chatFeed = feed
cb(null, chatFeed)
}
)
})
}
return {
template: `
<div id="app">
<h2>Chat</h2>
<input type='text' v-model="message" @keyup.enter="post()">
<button v-on:click="post">Send</button>
<div v-for="msg in messages">
<span>{{ msg.user }}:</span>
<span>{{ msg.text }}</span>
</div>
</div>`,
data: function() {
return {
message: '',
messages: [],
componentStillLoaded: false,
}
},
methods: {
post: function() {
if (this.message === '') return
ssbSingleton.getSimpleSSBEventually(
(err, SSB) => {
getChatFeed(SSB, (err, chatFeed) => {
SSB.db.publishAs(chatFeed.keys, {
type: '8K/chat',
message: this.message
}, (err, msg) => {
if (err) console.log(err)
else this.message = ''
})
})
}
)
},
load: function() {
ssbSingleton.getSimpleSSBEventually(
this.render
)
},
render: function(err, SSB) {
const { where, type, live, toPullStream } = SSB.db.operators
pull(
SSB.db.query(
where(type('8K/chat')),
live({ old: true }),
toPullStream()
),
pull.drain((msg) => {
this.messages.push({
user: msg.value.author.substring(0,5),
text: msg.value.content.message
})
})
)
}
},
created: function () {
this.componentStillLoaded = true
document.title = '8K - chat'
this.load()
},
destroyed: function () {
this.componentStillLoaded = false
}
}
}