forked from SolidOS/solid-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatLogic.js
More file actions
160 lines (144 loc) · 4.59 KB
/
Copy pathchatLogic.js
File metadata and controls
160 lines (144 loc) · 4.59 KB
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
/**
* Contains the [[ChatChannel]] class and logic for Solid Chat
* @packageDocumentation
*/
import * as debug from '../debug'
import { DateFolder } from './dateFolder'
import { store, authn } from 'solid-logic'
import * as ns from '../ns'
import * as $rdf from 'rdflib' // pull in first avoid cross-refs
import * as utils from '../utils'
/* The Solid logic for a 'LongChat'
*/
/**
* Common code for a chat (discussion area of messages about something)
* This version runs over a series of files for different time periods
*
* Parameters for the whole chat like its title are stored on
* index.ttl#this and the chats messages are stored in YYYY/MM/DD/chat.ttl
*
*/
export class ChatChannel {
constructor (channel, options) {
this.channel = channel
this.channelRoot = channel.doc()
this.options = options
this.dateFolder = new DateFolder(this.channelRoot, 'chat.ttl')
this.div = null // : HTMLElement
}
/* Store a new message in the web,
*/
async createMessage (text) {
return this.updateMessage(text)
}
/* Store a new message in the web,
as a replacement for an existing one.
The old one iis left, and the two are linked
*/
async updateMessage (text, oldMsg = null, deleteIt) {
const sts = []
const now = new Date()
const timestamp = '' + now.getTime()
const dateStamp = $rdf.term(now)
const chatDocument = oldMsg ? oldMsg.doc() : this.dateFolder.leafDocumentFromDate(now)
const message = store.sym(chatDocument.uri + '#' + 'Msg' + timestamp)
// const content = store.literal(text)
const me = authn.currentUser() // If already logged on
if (oldMsg) { // edit message replaces old one
sts.push($rdf.st(mostRecentVersion(oldMsg), ns.dct('isReplacedBy'), message, chatDocument))
if (deleteIt) {
sts.push($rdf.st(message, ns.schema('dateDeleted'), dateStamp, chatDocument))
}
} else { // link new message to channel
sts.push($rdf.st(this.channel, ns.wf('message'), message, chatDocument))
}
sts.push(
$rdf.st(message, ns.sioc('content'), store.literal(text), chatDocument)
)
sts.push(
$rdf.st(message, ns.dct('created'), dateStamp, chatDocument)
)
if (me) {
sts.push($rdf.st(message, ns.foaf('maker'), me, chatDocument))
}
try {
await store.updater.update([], sts)
} catch (err) {
const msg = 'Error saving chat message: ' + err
debug.warn(msg)
alert(msg)
throw new Error(msg)
}
return message
}
/* Mark a message as deleted
* Wee add a new version of the message,m witha deletion flag (deletion date)
* so that the deletion can be revoked by adding another non-deleted update
*/
async deleteMessage (message) {
return this.updateMessage('(message deleted)', message, true)
}
} // class ChatChannel
export function originalVersion (message) {
let msg = message
while (msg) {
message = msg
msg = store.any(null, ns.dct('isReplacedBy'), message, message.doc())
}
return message
}
export function mostRecentVersion (message) {
let msg = message
while (msg) {
message = msg
msg = store.any(message, ns.dct('isReplacedBy'), null, message.doc())
}
return message
}
export function isDeleted (message) {
return store.holds(message, ns.schema('dateDeleted'), null, message.doc())
}
export function isReplaced (message) {
return store.holds(message, ns.dct('isReplacedBy'), null, message.doc())
}
export function isHidden (message) {
return this.isDeleted(message) || this.isReplaced(message)
}
// A Nickname for a person
export function nick (person) {
const s = store.any(person, ns.foaf('nick'))
if (s) return '' + s.value
return '' + utils.label(person)
}
export async function _createIfNotExists (doc, contentType = 'text/turtle', data = '') {
let response
try {
response = await store.fetcher.load(doc)
} catch (err) {
if (err.response.status === 404) {
debug.log(
'createIfNotExists: doc does NOT exist, will create... ' + doc
)
try {
response = await store.fetcher.webOperation('PUT', doc.uri, {
data,
contentType
})
} catch (err) {
debug.log('createIfNotExists doc FAILED: ' + doc + ': ' + err)
throw err
}
delete store.fetcher.requested[doc.uri] // delete cached 404 error
// debug.log('createIfNotExists doc created ok ' + doc)
return response
} else {
debug.log(
'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err
)
throw err
}
}
// debug.log('createIfNotExists: doc exists, all good: ' + doc)
return response
}
// ends