-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchat-notify.js
346 lines (299 loc) · 11.7 KB
/
chat-notify.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* Notify me of solid chat new things
*
* Keeps track of what things you want to track in your solid preferences file
*
*/
import $rdf from 'rdflib'
import {SolidNodeClient} from 'solid-node-client';
const client = new SolidNodeClient({parser:$rdf})
global.solidFetcher = client.fetch.bind(client);
// import DateFolder from '../solid-ui/src/chat/dateFolders.js'
import { DateFolder } from './logic/dateFolder.js'
import fs from 'fs'
import solidNamespace from 'solid-namespace'
const store = new $rdf.Store()
const kb = store // shorthand -- knowledge base
const fetcher = $rdf.fetcher(store);
const updater = new $rdf.UpdateManager(store)
const instructions = `Solid chat export and subscriptions
These (mostly) leverage your solid pod to store your subscriptions
and record where
Run this as node chat-notify <command> <solidChatUri> <filename>
show chat file Makes a local HTML file of the chat
list Lists your subscriptions
subscribe chat Add this chat to the ones you get notified about
notify
`
// const solidNamespace = require('solid-namespace')
const ns = solidNamespace($rdf)
const a = ns.rdf('type')
const command = process.argv[2]
const solidChatURI = process.argv[3] || 'https://timbl.com/timbl/Public/Archive/solid/chat/index.ttl#this'
const outputFileName = process.argv[4] || null
const solidChat = $rdf.sym(solidChatURI)
// const messageBodyStyle = 'white-space: pre-wrap; width: 99%; font-size:100%; border: 0.07em solid #eee; padding: .3em 0.5em; margin: 0.1em;',
// const messageBodyStyle = require('../solid-ui/src/style').messageBodyStyle
function delay (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/* Solid chat stuff
*/
function chatDocumentFromDate (chatChannel, date) {
let isoDate = date.toISOString() // Like "2018-05-07T17:42:46.576Z"
var path = isoDate.split('T')[0].replace(/-/g, '/') // Like "2018/05/07"
path = chatChannel.dir().uri + path + '/chat.ttl'
return $rdf.sym(path)
}
function clone (options) {
return Object.assign({}, options)
}
async function loadIfExists (doc) {
try {
// delete fetcher.requested[doc.uri]
await fetcher.load(doc, clone(normalOptions))
return true
} catch (err) {
if (err.response && err.response.status && err.response.status === 404) {
console.log(' No chat file yet, creating later ' + doc)
return false
} else {
console.log(' #### Error reading file ' + err)
console.log(' error object ' + JSON.stringify(err))
console.log(' err.response ' + err.response)
console.log(' err.response.status ' + err.response.status)
process.exit(4)
}
}
}
function escapeXml (unsafe) {
return unsafe.replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '<'
case '>': return '>'
case '&': return '&'
case '\'': return '''
case '"': return '"'
}
})
}
const normalOptions = {
// headers: {Authorization: 'Bearer ' + SOLID_TOKEN}
}
const forcingOptions = {
// headers: {Authorization: 'Bearer ' + SOLID_TOKEN},
force: true }
function suitable (x) {
let tail = x.uri.slice(0, -1).split('/').slice(-1)[0]
if (!'0123456789'.includes(tail[0])) return false // not numeric
return true
// return kb.anyValue(chatDocument, POSIX('size')) !== 0 // empty file?
}
/* Find the first (or last) message in the time series folders
*/
async function firstMessage (chatChannel, backwards) { // backwards -> last message
var folderStore = $rdf.graph()
var folderFetcher = new $rdf.fetcher(folderStore)
async function earliestSubfolder (parent) {
console.log(' parent ' + parent)
delete folderFetcher.requested[parent.uri]
var resp = await folderFetcher.load(parent, clone(forcingOptions)) // Force fetch as will have changed
var kids = folderStore.each(parent, ns.ldp('contains'))
kids = kids.filter(suitable)
if (kids.length === 0) {
throw new Error(' @@@ No children to parent2 ' + parent)
}
kids.sort()
if (backwards) kids.reverse()
return kids[0]
}
let y = await earliestSubfolder(chatChannel.dir())
let month = await earliestSubfolder(y)
let d = await earliestSubfolder(month)
let chatDocument = $rdf.sym(d.uri + 'chat.ttl')
await folderFetcher.load(chatDocument, clone(normalOptions))
let messages = folderStore.each(chatChannel, ns.wf('message'), null, chatDocument)
if (messages.length === 0) {
let msg = ' INCONSISTENCY -- no chat message in file ' + chatDocument
console.trace(msg)
throw new Error(msg)
}
let sortMe = messages.map(message => [folderStore.any(message, ns.dct('created')), message])
sortMe.sort()
if (backwards) sortMe.reverse()
console.log((backwards ? 'Latest' : 'Earliest') + ' message in solid chat is ' + sortMe[0][1])
return sortMe[0][1]
} // firstMessage
function hashSymbol (x, doc) {
var hash = function (x) { return x.split('').reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a }, 0) }
return $rdf.sym(doc.uri + '#X' + (hash(x.uri) & 0x7fffffff).toString(16))
}
/* Convert messages to HTML
*/
async function htmlFromMessages (chatChannel, messages, startTime) {
console.log(`Messages from ${startTime}`)
var sortMe = messages.map(m => [ kb.the(m, ns.dct('created')).value, m])
sortMe.sort()
sortMe = sortMe.filter(x => x[0] >= startTime)
var todo = sortMe.map(x => x[1])
console.log('to do ' + todo.length)
var t = kb.anyValue(chatChannel, ns.dct('title')) || ''
var title = `Messages from solid chat ${t}`
var htmlText = `<html>
<head>
<title>${escapeXml(title)}</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<table>
`
var lastMaker = null
for (var message of todo) {
let created = kb.the(message, ns.dct('created'))
let when = created.value.slice(11, 16) // hhmm
let maker = kb.the(message, ns.foaf('maker'))
await fetcher.load(maker.doc())
let nick = kb.any(maker, ns.foaf('nick')).value
let photo = kb.any(maker, ns.vcard('photo')).uri
let content = kb.anyValue(message, ns.sioc('richContent')) || escapeXml(kb.anyValue(message, ns.sioc('content')))
// @@ todo: sanitize html content
console.log()
console.log('-' + nick + ': ' + when)
console.log(' ---> ' + content)
let dup = lastMaker && lastMaker.sameTerm(maker)
let picHTML = dup ? '' : `<img src="${photo}" style="width:3em; height:3em;">`
let nameHTML = dup ? '' : `${nick} ${when}<br/>`
htmlText += `\n<tr><td>${picHTML}</td><td>${nameHTML}\n${content}</td></tr>`
lastMaker = maker
}
htmlText += `</table>
</body>
</html>
`
return htmlText
}
async function logInGetSubscriptions () {
console.log('Log into solid')
var session = await client.login()
if (!session) throw new Error('Wot no solid session?')
// console.log('sesssion ' + JSON.stringify(session))
var me = session.webId
if (!me) throw new Error('Wot no solid session web ID?')
console.log('me ' + me)
me = $rdf.sym(me)
var profile = me.doc()
var context = { me }
try {
await kb.fetcher.load(profile)
} catch (err) {
console.error(err)
}
console.log('loaded ' + profile)
var prefs = kb.any(me, ns.space('preferencesFile'), null, profile)
if (!prefs) {
console.log('hmmm ' + kb.connectedStatements(me))
throw new Error('Cant find preferences file in ' + me.doc())
}
console.log('Preferences file: ' + prefs)
try {
await kb.fetcher.load(prefs)
} catch (err) {
throw new Error('cant load preferences file' + err)
}
console.log('statements: ' + kb.statementsMatching(null, null, null, prefs).length)
const actions = kb.each(null, ns.schema('agent'), me, prefs) // is subscriber of?
const subscriptions = actions.filter(action => kb.holds(action, a, ns.schema('SubscribeAction'), prefs))
console.log('Actions: ' + actions.length)
console.log('Subscriptions: ' + subscriptions.length)
return { me, subscriptions, prefs}
}
async function loadMessages (chatChannel, lastNotified) {
console.log('loadMessages ' + chatChannel)
const dateFolder = new DateFolder(chatChannel, 'chat.ttl', ns.wf('message'), kb)
const finalMessage = await dateFolder.firstLeaf(true)
const initialMessage = lastNotified || await dateFolder.firstLeaf(false)
console.log(` Notifying messages between ${initialMessage} and ${finalMessage}`)
if (initialMessage.sameTerm(finalMessage)) {
console.log(' No new messagess')
return {messages: [], startTime: null}
} else {
await fetcher.load(initialMessage)
var messageFile = finalMessage.doc()
while (1) {
console.log('Loading ' + messageFile)
await kb.fetcher.load(messageFile)
var finalDate = dateFolder.dateFromLeafDocument(finalMessage.doc())
var previousDate = await dateFolder.loadPrevious(finalDate)
if (!previousDate) break // no more chat
var previousFile = dateFolder.leafDocumentFromDate(previousDate)
if (previousFile.sameTerm(initialMessage.doc())) {
break // Loaded enough
}
messageFile = previousFile
}
var startTime = kb.the(initialMessage, ns.dct('created')).value
var messages = kb.each(chatChannel, ns.wf('message'), null)
console.log('messages altogether: ' + messages.length)
return { messages, startTime}
}
}
async function writeToFile (text, filename) {
console.log('writing file...' + filename)
fs.writeFile(filename, text, function (err) {
if (err) {
console.error('Error writing file ' + err)
} else {
console.log('written file')
}
})
}
async function go () {
if (command === 'notify') {
const { me, subscriptions, prefs} = await logInGetSubscriptions()
for (let sub of subscriptions) {
var chatChannel = kb.the(sub, ns.schema('object'))
var lastNotified = kb.the(sub, ns.solid('lastNotified'))
if (!lastNotified) {
const dateFolder = new DateFolder(chatChannel, 'chat.ttl', ns.wf('message'), kb)
const finalMessage = await dateFolder.firstLeaf(true)
console.log('No previous notifications -- so start from here: ' + finalMessage)
updater.update([], [$rdf.st(sub, ns.solid('lastNotified'), finalMessage, prefs)])
} else {
const {messages, startTime} = await loadMessages(chatChannel, lastNotified)
const htmlText = await htmlFromMessages(chatChannel, messages, startTime)
writeToFile(htmlText, ',temp.html')
}
}
} // notify
if (command === 'show') {
if (!solidChat) throw new Error("No chat channel specified")
// const { me, subscriptions} = await logInGetSubscriptions()
const {messages, startTime, prefs} = await loadMessages(solidChat)
const html = await htmlFromMessages(solidChat, messages, startTime)
console.log(html)
writeToFile(html, outputFileName || ',exported-chat.html')
}
if (command === 'list') {
const { me, subscriptions, prefs} = await logInGetSubscriptions()
console.log('Subscriptions:')
for (let sub of subscriptions) {
console.log(' Subscription to ' + kb.any(sub, ns.schema('object')))
console.log(' by ' + kb.any(sub, ns.schema('agent')))
}
}
if (command === 'subscribe') {
const { me, subscriptions, prefs} = await logInGetSubscriptions()
if (kb.any(null, ns.schema('object'), solidChat, prefs)) {
console.log('Sorry already have something about ' + solidChat)
} else {
var subscription = hashSymbol(solidChat, prefs)
await updater.update([], [$rdf.st(subscription, a, ns.schema('SubscribeAction'), prefs),
$rdf.st(subscription, ns.schema('agent'), me, prefs),
$rdf.st(subscription, ns.schema('object'), solidChat, prefs) ])
}
}
console.log('ENDS')
}
var userContext = {}
var toBePut = []
go()
// ends