|
| 1 | +/** |
| 2 | + * Contains the [[ChatChannel]] class and logic for Solid Chat |
| 3 | + * @packageDocumentation |
| 4 | + */ |
| 5 | + |
| 6 | +import * as debug from '../debug' |
| 7 | +import { authn } from '../authn/index' |
| 8 | +import { DateFolder } from './dateFolder' |
| 9 | +import { store } from '../logic' |
| 10 | +import * as ns from '../ns' |
| 11 | +import * as $rdf from 'rdflib' // pull in first avoid cross-refs |
| 12 | +import * as utils from '../utils' |
| 13 | + |
| 14 | +/* The Solid logic for a 'LongChat' |
| 15 | +*/ |
| 16 | +/** |
| 17 | + * Common code for a chat (discussion area of messages about something) |
| 18 | + * This version runs over a series of files for different time periods |
| 19 | + * |
| 20 | + * Parameters for the whole chat like its title are stored on |
| 21 | + * index.ttl#this and the chats messages are stored in YYYY/MM/DD/chat.ttl |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +export class ChatChannel { |
| 26 | + constructor (channel, options) { |
| 27 | + this.channel = channel |
| 28 | + this.channelRoot = channel.doc() |
| 29 | + this.options = options |
| 30 | + this.dateFolder = new DateFolder(this.channelRoot, 'chat.ttl') |
| 31 | + this.div = null // : HTMLElement |
| 32 | + } |
| 33 | + |
| 34 | + /* Store a new message in the web, |
| 35 | + */ |
| 36 | + async createMessage (text) { |
| 37 | + return this.updateMessage(text) |
| 38 | + } |
| 39 | + |
| 40 | + /* Store a new message in the web, |
| 41 | + as a replacement for an existing one. |
| 42 | + The old one iis left, and the two are linked |
| 43 | + */ |
| 44 | + async updateMessage (text, oldMsg = null, deleteIt) { |
| 45 | + const sts = [] |
| 46 | + const now = new Date() |
| 47 | + const timestamp = '' + now.getTime() |
| 48 | + const dateStamp = $rdf.term(now) |
| 49 | + const chatDocument = oldMsg ? oldMsg.doc() : this.dateFolder.leafDocumentFromDate(now) |
| 50 | + const message = store.sym(chatDocument.uri + '#' + 'Msg' + timestamp) |
| 51 | + // const content = store.literal(text) |
| 52 | + |
| 53 | + const me = authn.currentUser() // If already logged on |
| 54 | + |
| 55 | + if (oldMsg) { // edit message replaces old one |
| 56 | + sts.push($rdf.st(mostRecentVersion(oldMsg), ns.dct('isReplacedBy'), message, chatDocument)) |
| 57 | + if (deleteIt) { |
| 58 | + sts.push($rdf.st(message, ns.schema('dateDeleted'), dateStamp, chatDocument)) |
| 59 | + } |
| 60 | + } else { // link new message to channel |
| 61 | + sts.push($rdf.st(this.channel, ns.wf('message'), message, chatDocument)) |
| 62 | + } |
| 63 | + sts.push( |
| 64 | + $rdf.st(message, ns.sioc('content'), store.literal(text), chatDocument) |
| 65 | + ) |
| 66 | + sts.push( |
| 67 | + $rdf.st(message, ns.dct('created'), dateStamp, chatDocument) |
| 68 | + ) |
| 69 | + if (me) { |
| 70 | + sts.push($rdf.st(message, ns.foaf('maker'), me, chatDocument)) |
| 71 | + } |
| 72 | + try { |
| 73 | + await store.updater.update([], sts) |
| 74 | + } catch (err) { |
| 75 | + const msg = 'Error saving chat message: ' + err |
| 76 | + debug.warn(msg) |
| 77 | + alert(msg) |
| 78 | + throw new Error(msg) |
| 79 | + } |
| 80 | + return message |
| 81 | + } |
| 82 | + |
| 83 | + /* Mark a message as deleted |
| 84 | + * Wee add a new version of the message,m witha deletion flag (deletion date) |
| 85 | + * so that the deletion can be revoked by adding another non-deleted update |
| 86 | + */ |
| 87 | + async deleteMessage (message) { |
| 88 | + return this.updateMessage('(message deleted)', message, true) |
| 89 | + } |
| 90 | +} // class ChatChannel |
| 91 | + |
| 92 | +export function originalVersion (message) { |
| 93 | + let msg = message |
| 94 | + while (msg) { |
| 95 | + message = msg |
| 96 | + msg = store.any(null, ns.dct('isReplacedBy'), message, message.doc()) |
| 97 | + } |
| 98 | + return message |
| 99 | +} |
| 100 | + |
| 101 | +export function mostRecentVersion (message) { |
| 102 | + let msg = message |
| 103 | + while (msg) { |
| 104 | + message = msg |
| 105 | + msg = store.any(message, ns.dct('isReplacedBy'), null, message.doc()) |
| 106 | + } |
| 107 | + return message |
| 108 | +} |
| 109 | + |
| 110 | +export function isDeleted (message) { |
| 111 | + return store.holds(message, ns.schema('dateDeleted'), null, message.doc()) |
| 112 | +} |
| 113 | + |
| 114 | +export function isReplaced (message) { |
| 115 | + return store.holds(message, ns.dct('isReplacedBy'), null, message.doc()) |
| 116 | +} |
| 117 | + |
| 118 | +export function isHidden (message) { |
| 119 | + return this.isDeleted(message) || this.isReplaced(message) |
| 120 | +} |
| 121 | + |
| 122 | +// A Nickname for a person |
| 123 | + |
| 124 | +export function nick (person) { |
| 125 | + const s = store.any(person, ns.foaf('nick')) |
| 126 | + if (s) return '' + s.value |
| 127 | + return '' + utils.label(person) |
| 128 | +} |
| 129 | + |
| 130 | +export async function _createIfNotExists (doc, contentType = 'text/turtle', data = '') { |
| 131 | + let response |
| 132 | + try { |
| 133 | + response = await store.fetcher.load(doc) |
| 134 | + } catch (err) { |
| 135 | + if (err.response.status === 404) { |
| 136 | + debug.log( |
| 137 | + 'createIfNotExists: doc does NOT exist, will create... ' + doc |
| 138 | + ) |
| 139 | + try { |
| 140 | + response = await store.fetcher.webOperation('PUT', doc.uri, { |
| 141 | + data, |
| 142 | + contentType |
| 143 | + }) |
| 144 | + } catch (err) { |
| 145 | + debug.log('createIfNotExists doc FAILED: ' + doc + ': ' + err) |
| 146 | + throw err |
| 147 | + } |
| 148 | + delete store.fetcher.requested[doc.uri] // delete cached 404 error |
| 149 | + // debug.log('createIfNotExists doc created ok ' + doc) |
| 150 | + return response |
| 151 | + } else { |
| 152 | + debug.log( |
| 153 | + 'createIfNotExists doc load error NOT 404: ' + doc + ': ' + err |
| 154 | + ) |
| 155 | + throw err |
| 156 | + } |
| 157 | + } |
| 158 | + // debug.log('createIfNotExists: doc exists, all good: ' + doc) |
| 159 | + return response |
| 160 | +} |
| 161 | +// ends |
0 commit comments