@@ -23,86 +23,103 @@ import * as utils from '../utils'
2323 */
2424
2525export class ChatChannel {
26- constructor ( channelRoot , options ) {
27- this . channelRoot = channelRoot
26+ constructor ( channel , options ) {
27+ this . channel = channel
28+ this . channelRoot = channel . doc ( )
2829 this . options = options
29- this . dateFolder = new DateFolder ( channelRoot , 'chat.ttl' )
30+ this . dateFolder = new DateFolder ( this . channelRoot , 'chat.ttl' )
31+ this . div = null // : HTMLElement
3032 }
3133
3234 /* Store a new message in the web,
33- optionally as a replacement for an existing one.
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.
3442 The old one iis left, and the two are linked
3543 */
36- async createMessage ( newContent , oldMsg = null ) {
44+ async updateMessage ( text , oldMsg = null , deleteIt ) {
3745 const sts = [ ]
3846 const now = new Date ( )
3947 const timestamp = '' + now . getTime ( )
4048 const dateStamp = $rdf . term ( now )
4149 const chatDocument = oldMsg ? oldMsg . doc ( ) : this . dateFolder . leafDocumentFromDate ( now )
4250 const message = store . sym ( chatDocument . uri + '#' + 'Msg' + timestamp )
43- const content = store . literal ( newContent )
51+ // const content = store.literal(text )
4452
4553 const me = authn . currentUser ( ) // If already logged on
4654
47- if ( oldMsg ) { // edit message
48- sts . push (
49- new $rdf . Statement ( mostRecentVersion ( oldMsg ) , ns . dct ( 'isReplacedBy' ) , message , chatDocument )
50- )
51- } else {
52- sts . push ( // new message
53- new $rdf . Statement ( this . chatChannel , ns . wf ( 'message' ) , message , chatDocument )
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 ) )
5562 }
5663 sts . push (
57- new $rdf . Statement (
58- message ,
59- ns . sioc ( 'content' ) ,
60- store . literal ( newContent ) ,
61- chatDocument
62- )
64+ $rdf . st ( message , ns . sioc ( 'content' ) , store . literal ( text ) , chatDocument )
6365 )
6466 sts . push (
65- new $rdf . Statement ( message , ns . dct ( 'created' ) , dateStamp , chatDocument )
67+ $rdf . st ( message , ns . dct ( 'created' ) , dateStamp , chatDocument )
6668 )
6769 if ( me ) {
68- sts . push (
69- new $rdf . Statement ( message , ns . foaf ( 'maker' ) , me , chatDocument )
70- )
70+ sts . push ( $rdf . st ( message , ns . foaf ( 'maker' ) , me , chatDocument ) )
7171 }
72- return { message, dateStamp, content, chatDocument, sts }
73- }
74-
75- async deleteMessage ( message ) {
76- const sts = [ ]
77- const dateStamp = $rdf . term ( new Date ( ) )
78- const chatDocument = message . doc ( )
79- sts . push (
80- new $rdf . Statement ( mostRecentVersion ( message ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
81- )
82- sts . push (
83- new $rdf . Statement ( mostRecentVersion ( message ) , ns . schema ( 'dateDeleted' ) , dateStamp , chatDocument )
84- )
8572 try {
86- store . updater . update ( [ ] , sts )
73+ await store . updater . update ( [ ] , sts )
8774 } catch ( err ) {
88- const msg = 'Error deleting chat essage : ' + err
75+ const msg = 'Error saving chat message : ' + err
8976 alert ( msg )
9077 }
91- } // method
78+ return message
79+ }
80+
81+ /* Mark a message as deleted
82+ * Wee add a new version of the message,m witha deletion flag (deletion date)
83+ * so that the deletion can be revoked by adding another non-deleted update
84+ */
85+ async deleteMessage ( message ) {
86+ return this . updateMessage ( '(message deleted)' , message , true )
87+ }
9288} // class ChatChannel
9389
90+ export function originalVersion ( message ) {
91+ let msg = message
92+ while ( msg ) {
93+ message = msg
94+ msg = store . any ( null , ns . dct ( 'isReplacedBy' ) , message , message . doc ( ) )
95+ }
96+ return message
97+ }
98+
9499export function mostRecentVersion ( message ) {
95100 let msg = message
96101 while ( msg ) {
97102 message = msg
98- msg = store . any ( message , ns . dct ( 'isReplacedBy' ) )
103+ msg = store . any ( message , ns . dct ( 'isReplacedBy' ) , null , message . doc ( ) )
99104 }
100105 if ( store . any ( message , ns . schema ( 'dateDeleted' ) ) ) {
101106 return ns . schema ( 'dateDeleted' ) // message has been deleted
102107 }
103108 return message
104109}
105110
111+ export function isDeleted ( message ) {
112+ return store . holds ( message , ns . schema ( 'dateDeleted' ) , null , message . doc ( ) )
113+ }
114+
115+ export function isReplaced ( message ) {
116+ return store . holds ( message , ns . dct ( 'isReplacedBy' ) , null , message . doc ( ) )
117+ }
118+
119+ export function isHidden ( message ) {
120+ return this . isDeleted ( message ) || this . isReplaced ( message )
121+ }
122+
106123// A Nickname for a person
107124
108125export function nick ( person ) {
@@ -111,7 +128,7 @@ export function nick (person) {
111128 return '' + utils . label ( person )
112129}
113130
114- export async function createIfNotExists ( doc , contentType = 'text/turtle' , data = '' ) {
131+ export async function _createIfNotExists ( doc , contentType = 'text/turtle' , data = '' ) {
115132 let response
116133 try {
117134 response = await store . fetcher . load ( doc )
0 commit comments