Pad: Rename author vars to authorId for consistency

This commit is contained in:
Richard Hansen 2022-02-16 22:07:23 -05:00
parent c2910b98e8
commit 65bd5ffa6b

View file

@ -76,10 +76,8 @@ Pad.prototype.getPublicStatus = function () {
return this.publicStatus; return this.publicStatus;
}; };
Pad.prototype.appendRevision = async function (aChangeset, author) { Pad.prototype.appendRevision = async function (aChangeset, authorId) {
if (!author) { if (!authorId) authorId = '';
author = '';
}
const newAText = Changeset.applyToAText(aChangeset, this.atext, this.pool); const newAText = Changeset.applyToAText(aChangeset, this.atext, this.pool);
if (newAText.text === this.atext.text && newAText.attribs === this.atext.attribs) { if (newAText.text === this.atext.text && newAText.attribs === this.atext.attribs) {
@ -92,13 +90,11 @@ Pad.prototype.appendRevision = async function (aChangeset, author) {
const newRevData = {}; const newRevData = {};
newRevData.changeset = aChangeset; newRevData.changeset = aChangeset;
newRevData.meta = {}; newRevData.meta = {};
newRevData.meta.author = author; newRevData.meta.author = authorId;
newRevData.meta.timestamp = Date.now(); newRevData.meta.timestamp = Date.now();
// ex. getNumForAuthor // ex. getNumForAuthor
if (author !== '') { if (authorId !== '') this.pool.putAttrib(['author', authorId]);
this.pool.putAttrib(['author', author]);
}
if (newRev % 100 === 0) { if (newRev % 100 === 0) {
newRevData.meta.pool = this.pool; newRevData.meta.pool = this.pool;
@ -111,14 +107,12 @@ Pad.prototype.appendRevision = async function (aChangeset, author) {
]; ];
// set the author to pad // set the author to pad
if (author) { if (authorId) p.push(authorManager.addPad(authorId, this.id));
p.push(authorManager.addPad(author, this.id));
}
if (this.head === 0) { if (this.head === 0) {
hooks.callAll('padCreate', {pad: this, author}); hooks.callAll('padCreate', {pad: this, author: authorId});
} else { } else {
hooks.callAll('padUpdate', {pad: this, author, revs: newRev, changeset: aChangeset}); hooks.callAll('padUpdate', {pad: this, author: authorId, revs: newRev, changeset: aChangeset});
} }
await Promise.all(p); await Promise.all(p);
@ -162,15 +156,15 @@ Pad.prototype.getRevisionDate = async function (revNum) {
}; };
Pad.prototype.getAllAuthors = function () { Pad.prototype.getAllAuthors = function () {
const authors = []; const authorIds = [];
for (const key in this.pool.numToAttrib) { for (const key in this.pool.numToAttrib) {
if (this.pool.numToAttrib[key][0] === 'author' && this.pool.numToAttrib[key][1] !== '') { if (this.pool.numToAttrib[key][0] === 'author' && this.pool.numToAttrib[key][1] !== '') {
authors.push(this.pool.numToAttrib[key][1]); authorIds.push(this.pool.numToAttrib[key][1]);
} }
} }
return authors; return authorIds;
}; };
Pad.prototype.getInternalRevisionAText = async function (targetRev) { Pad.prototype.getInternalRevisionAText = async function (targetRev) {
@ -213,14 +207,14 @@ Pad.prototype.getRevision = async function (revNum) {
}; };
Pad.prototype.getAllAuthorColors = async function () { Pad.prototype.getAllAuthorColors = async function () {
const authors = this.getAllAuthors(); const authorIds = this.getAllAuthors();
const returnTable = {}; const returnTable = {};
const colorPalette = authorManager.getColorPalette(); const colorPalette = authorManager.getColorPalette();
await Promise.all( await Promise.all(
authors.map((author) => authorManager.getAuthorColorId(author).then((colorId) => { authorIds.map((authorId) => authorManager.getAuthorColorId(authorId).then((colorId) => {
// colorId might be a hex color or an number out of the palette // colorId might be a hex color or an number out of the palette
returnTable[author] = colorPalette[colorId] || colorId; returnTable[authorId] = colorPalette[colorId] || colorId;
}))); })));
return returnTable; return returnTable;
@ -562,8 +556,8 @@ Pad.prototype.remove = async function () {
})); }));
// remove pad from all authors who contributed // remove pad from all authors who contributed
this.getAllAuthors().forEach((authorID) => { this.getAllAuthors().forEach((authorId) => {
p.push(authorManager.removePad(authorID, padID)); p.push(authorManager.removePad(authorId, padID));
}); });
// delete the pad entry and delete pad from padManager // delete the pad entry and delete pad from padManager
@ -643,22 +637,22 @@ Pad.prototype.check = async function () {
assert(pool instanceof AttributePool); assert(pool instanceof AttributePool);
await pool.check(); await pool.check();
const authors = new Set(); const authorIds = new Set();
pool.eachAttrib((k, v) => { pool.eachAttrib((k, v) => {
if (k === 'author' && v) authors.add(v); if (k === 'author' && v) authorIds.add(v);
}); });
let atext = Changeset.makeAText('\n'); let atext = Changeset.makeAText('\n');
let r; let r;
try { try {
for (r = 0; r <= head; ++r) { for (r = 0; r <= head; ++r) {
const [changeset, author, timestamp] = await Promise.all([ const [changeset, authorId, timestamp] = await Promise.all([
this.getRevisionChangeset(r), this.getRevisionChangeset(r),
this.getRevisionAuthor(r), this.getRevisionAuthor(r),
this.getRevisionDate(r), this.getRevisionDate(r),
]); ]);
assert(author != null); assert(authorId != null);
assert.equal(typeof author, 'string'); assert.equal(typeof authorId, 'string');
if (author) authors.add(author); if (authorId) authorIds.add(authorId);
assert(timestamp != null); assert(timestamp != null);
assert.equal(typeof timestamp, 'number'); assert.equal(typeof timestamp, 'number');
assert(timestamp > 0); assert(timestamp > 0);
@ -689,7 +683,7 @@ Pad.prototype.check = async function () {
} }
assert.equal(this.text(), atext.text); assert.equal(this.text(), atext.text);
assert.deepEqual(this.atext, atext); assert.deepEqual(this.atext, atext);
assert.deepEqual(this.getAllAuthors().sort(), [...authors].sort()); assert.deepEqual(this.getAllAuthors().sort(), [...authorIds].sort());
assert(Number.isInteger(this.chatHead)); assert(Number.isInteger(this.chatHead));
assert(this.chatHead >= -1); assert(this.chatHead >= -1);