@@ -40,7 +40,7 @@ export class ChatChannel {
4040 as a replacement for an existing one.
4141 The old one iis left, and the two are linked
4242 */
43- async updateMessage ( text , oldMsg = null , deleteIt ) {
43+ async updateMessage ( text , oldMsg = null , deleteIt , thread = null ) {
4444 const sts = [ ]
4545 const now = new Date ( )
4646 const timestamp = '' + now . getTime ( )
@@ -68,8 +68,14 @@ export class ChatChannel {
6868 if ( me ) {
6969 sts . push ( $rdf . st ( message , ns . foaf ( 'maker' ) , me , chatDocument ) )
7070 }
71+ if ( thread ) {
72+ sts . push ( $rdf . st ( thread , ns . sioc ( 'has_member' ) , message , chatDocument ) )
73+ if ( ! thread . doc ( ) . sameTerm ( message . doc ( ) ) ) {
74+ sts . push ( $rdf . st ( thread , ns . sioc ( 'has_member' ) , message , thread . doc ( ) ) )
75+ }
76+ }
7177 try {
72- await store . updater . update ( [ ] , sts )
78+ await store . updater . updateMany ( [ ] , sts )
7379 } catch ( err ) {
7480 const msg = 'Error saving chat message: ' + err
7581 debug . warn ( msg )
@@ -86,8 +92,27 @@ export class ChatChannel {
8692 async deleteMessage ( message ) {
8793 return this . updateMessage ( '(message deleted)' , message , true )
8894 }
95+
96+ // Create a new thread of replies to the thread root message
97+ // or returns one which already exists
98+
99+ async createThread ( threadRoot ) {
100+ const already = store . each ( threadRoot , ns . sioc ( 'has_reply' ) , null , threadRoot . doc ( ) )
101+ . filter ( thread => store . holds ( thread , ns . rdf ( 'type' ) , ns . sioc ( 'Thread' ) , thread . doc ( ) ) )
102+ if ( already . length > 0 ) return already [ 0 ]
103+
104+ const thread = $rdf . sym ( threadRoot . uri + '-thread' )
105+ const insert = [
106+ $rdf . st ( thread , ns . rdf ( 'type' ) , ns . sioc ( 'Thread' ) , thread . doc ( ) ) ,
107+ $rdf . st ( threadRoot , ns . sioc ( 'has_reply' ) , thread , thread . doc ( ) ) ,
108+ ] ;
109+ await store . updater . update ( [ ] , insert )
110+ return thread
111+ }
89112} // class ChatChannel
90113
114+ //////////// Utility functons
115+
91116export async function allVersions ( message ) {
92117 let versions = [ message ]
93118 let m = message
0 commit comments