Skip to content

Commit 49af151

Browse files
committed
Edit seems to work, working on new twist on delete
1 parent 546b9a2 commit 49af151

5 files changed

Lines changed: 159 additions & 94 deletions

File tree

src/chat/chatLogic.js

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,86 +23,103 @@ import * as utils from '../utils'
2323
*/
2424

2525
export 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+
9499
export 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

108125
export 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)

src/chat/infinite.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { store } from '../logic'
1010
import * as ns from '../ns'
1111
// import * as pad from '../pad'
1212
// import { DateFolder } from './dateFolder'
13-
import { mostRecentVersion, ChatChannel } from './chatLogic'
13+
import { mostRecentVersion, ChatChannel, isDeleted } from './chatLogic'
1414
import { renderMessageEditor, renderMessageRow } from './message'
1515
// import { findBookmarkDocument } from './bookmarks'
1616

@@ -24,9 +24,9 @@ import * as widgets from '../widgets'
2424
export function desktopNotification (str) {
2525
// Let's check if the browser supports notifications
2626
if (!('Notification' in window)) {
27-
debug.warn('This browser does not support desktop notification')
27+
debug.warn('This browser does no t support desktop notification')
2828
} else if (Notification.permission === 'granted') {
29-
// Let's check whether notification permissions have already been granted
29+
// Let's check whether notificatio n permissions have already been granted
3030
// eslint-disable-next-line no-new
3131
new Notification(str)
3232
} else if (Notification.permission !== 'denied') {
@@ -137,12 +137,12 @@ export async function infiniteMessageArea (dom, wasStore, chatChannel, options)
137137
}
138138
} // syncMessages
139139

140+
// Called once per original message displayed
140141
function addMessage (message, messageTable) {
141-
let content
142-
if (store.any(mostRecentVersion(message))) {
143-
content = store.any(mostRecentVersion(message), ns.sioc('content'))
144-
} else {
145-
content = store.literal('message deleted')
142+
const latest = mostRecentVersion(message)
143+
const content = store.any(latest, ns.sioc('content'))
144+
if (isDeleted(latest) && !options.showDeletedMessages) {
145+
return // ignore deleted messaged -- @@ could also leave a placeholder
146146
}
147147
const bindings = {
148148
'?msg': message,

0 commit comments

Comments
 (0)