changesettracker: Remove unnecessary `.numToAttrib` check

This commit is contained in:
Richard Hansen 2021-11-16 18:08:06 -05:00
parent 3ec5e84737
commit d0e74ada2f
1 changed files with 34 additions and 35 deletions

View File

@ -142,43 +142,42 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
// Sanitize authorship
// We need to replace all author attribs with thisSession.author,
// in case they copy/pasted or otherwise inserted other peoples changes
if (apool.numToAttrib) {
let authorAttr;
for (const attr in apool.numToAttrib) {
if (apool.numToAttrib[attr][0] === 'author' &&
apool.numToAttrib[attr][1] === authorId) {
authorAttr = Number(attr).toString(36);
}
let authorAttr;
for (const attr in apool.numToAttrib) {
if (apool.numToAttrib[attr][0] === 'author' &&
apool.numToAttrib[attr][1] === authorId) {
authorAttr = Number(attr).toString(36);
}
// Replace all added 'author' attribs with the value of the current user
const cs = Changeset.unpack(userChangeset);
const iterator = Changeset.opIterator(cs.ops);
let op;
const assem = Changeset.mergingOpAssembler();
while (iterator.hasNext()) {
op = iterator.next();
if (op.opcode === '+') {
let newAttrs = '';
op.attribs.split('*').forEach((attrNum) => {
if (!attrNum) return;
const attr = apool.getAttrib(parseInt(attrNum, 36));
if (!attr) return;
if ('author' === attr[0]) {
// replace that author with the current one
newAttrs += `*${authorAttr}`;
} else { newAttrs += `*${attrNum}`; } // overtake all other attribs as is
});
op.attribs = newAttrs;
}
assem.append(op);
}
assem.endDocument();
userChangeset = Changeset.pack(cs.oldLen, cs.newLen, assem.toString(), cs.charBank);
Changeset.checkRep(userChangeset);
}
// Replace all added 'author' attribs with the value of the current user
const cs = Changeset.unpack(userChangeset);
const iterator = Changeset.opIterator(cs.ops);
let op;
const assem = Changeset.mergingOpAssembler();
while (iterator.hasNext()) {
op = iterator.next();
if (op.opcode === '+') {
let newAttrs = '';
op.attribs.split('*').forEach((attrNum) => {
if (!attrNum) return;
const attr = apool.getAttrib(parseInt(attrNum, 36));
if (!attr) return;
if ('author' === attr[0]) {
// replace that author with the current one
newAttrs += `*${authorAttr}`;
} else { newAttrs += `*${attrNum}`; } // overtake all other attribs as is
});
op.attribs = newAttrs;
}
assem.append(op);
}
assem.endDocument();
userChangeset = Changeset.pack(cs.oldLen, cs.newLen, assem.toString(), cs.charBank);
Changeset.checkRep(userChangeset);
if (Changeset.isIdentity(userChangeset)) toSubmit = null;
else toSubmit = userChangeset;
}