11/**
2- * Contains the [[ChatChannel]] class
2+ * Contains the [[ChatChannel]] class and logic for Solid Chat
33 * @packageDocumentation
44 */
55
6+ import * as debug from '../debug'
7+ import { authn } from '../authn/index'
68import { DateFolder } from './dateFolder'
79import { store } from '../logic'
810import * as ns from '../ns'
911import * as $rdf from 'rdflib' // pull in first avoid cross-refs
10-
11- const store = store
12+ import * as utils from '../utils'
1213
1314/* The Solid logic for a 'LongChat'
1415*/
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+
1525export class ChatChannel {
1626 constructor ( channelRoot , options ) {
1727 this . channelRoot = channelRoot
1828 this . options = options
19- this . dateFolder = new DateFolder ( channelRoot )
29+ this . dateFolder = new DateFolder ( channelRoot , 'chat.ttl' )
2030 }
2131
2232 /* Store a new message in the web,
@@ -32,13 +42,15 @@ export class ChatChannel {
3242 const message = store . sym ( chatDocument . uri + '#' + 'Msg' + timestamp )
3343 const content = store . literal ( newContent )
3444
45+ const me = authn . currentUser ( ) // If already logged on
46+
3547 if ( oldMsg ) { // edit message
3648 sts . push (
3749 new $rdf . Statement ( mostRecentVersion ( oldMsg ) , ns . dct ( 'isReplacedBy' ) , message , chatDocument )
3850 )
3951 } else {
4052 sts . push ( // new message
41- new $rdf . Statement ( chatChannel , ns . wf ( 'message' ) , message , chatDocument )
53+ new $rdf . Statement ( this . chatChannel , ns . wf ( 'message' ) , message , chatDocument )
4254 )
4355 }
4456 sts . push (
@@ -60,29 +72,26 @@ export class ChatChannel {
6072 return { message, dateStamp, content, chatDocument, sts }
6173 }
6274
63-
64- deleteMessage ( newContent , oldMsg = null ) {
75+ async deleteMessage ( message ) {
6576 const sts = [ ]
66- const now = new Date ( )
67- const timestamp = '' + now . getTime ( )
68- const dateStamp = $rdf . term ( now )
69-
70- const chatDocument = oldMsg . doc ( )
71-
77+ const dateStamp = $rdf . term ( new Date ( ) )
78+ const chatDocument = message . doc ( )
7279 sts . push (
73- new $rdf . Statement ( mostRecentVersion ( oldMsg ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
80+ new $rdf . Statement ( mostRecentVersion ( message ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
7481 )
75-
7682 sts . push (
77- new $rdf . Statement ( mostRecentVersion ( oldMsg ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
83+ new $rdf . Statement ( mostRecentVersion ( message ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
7884 )
79-
80- return
81- }
82-
85+ try {
86+ store . updater . update ( [ ] , sts )
87+ } catch ( err ) {
88+ const msg = 'Error deleting chat essage: ' + err
89+ alert ( msg )
90+ }
91+ } // method
8392} // class ChatChannel
8493
85- export function mostRecentVersion ( message ) {
94+ export function mostRecentVersion ( message ) {
8695 let msg = message
8796 while ( msg ) {
8897 message = msg
@@ -94,26 +103,33 @@ export function mostRecentVersion (message) {
94103 return message
95104}
96105
106+ // A Nickname for a person
107+
108+ export function nick ( person ) {
109+ const s = store . any ( person , ns . foaf ( 'nick' ) )
110+ if ( s ) return '' + s . value
111+ return '' + utils . label ( person )
112+ }
113+
97114export async function createIfNotExists ( doc , contentType = 'text/turtle' , data = '' ) {
98- const fetcher = UI . store . fetcher
99115 let response
100116 try {
101- response = await fetcher . load ( doc )
117+ response = await store . fetcher . load ( doc )
102118 } catch ( err ) {
103119 if ( err . response . status === 404 ) {
104120 debug . log (
105121 'createIfNotExists: doc does NOT exist, will create... ' + doc
106122 )
107123 try {
108- response = await fetcher . webOperation ( 'PUT' , doc . uri , {
124+ response = await store . fetcher . webOperation ( 'PUT' , doc . uri , {
109125 data,
110126 contentType
111127 } )
112128 } catch ( err ) {
113129 debug . log ( 'createIfNotExists doc FAILED: ' + doc + ': ' + err )
114130 throw err
115131 }
116- delete fetcher . requested [ doc . uri ] // delete cached 404 error
132+ delete store . fetcher . requested [ doc . uri ] // delete cached 404 error
117133 // debug.log('createIfNotExists doc created ok ' + doc)
118134 return response
119135 } else {
@@ -126,3 +142,4 @@ export async function createIfNotExists (doc, contentType = 'text/turtle', data
126142 // debug.log('createIfNotExists: doc exists, all good: ' + doc)
127143 return response
128144}
145+ // ends
0 commit comments