Skip to content

Commit 50f4b6b

Browse files
committed
lint: lib/models/note.ts
- add Authorship type
1 parent bcc8921 commit 50f4b6b

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lib/models/baseModel.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export interface AuthorAttributes {
3333
color: string
3434
}
3535

36+
export interface Authorship {
37+
[0]: string // userId
38+
[1]: number // startPos
39+
[2]: number // endPos
40+
[3]: number // createdAt
41+
[4]: number // updatedAt
42+
}
43+
3644
export interface NoteAttributes {
3745
id?: string
3846
shortid?: string
@@ -41,7 +49,7 @@ export interface NoteAttributes {
4149
viewcount?: number
4250
title?: string
4351
content?: string
44-
authorship?: string
52+
authorship?: Authorship[]
4553
lastchangeAt?: Date | Moment
4654
savedAt?: Date
4755

lib/models/note.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import config from "../config";
2020
import {logger} from "../logger";
2121
import {createNoteWithRevision, syncNote} from "../services/note";
2222
import {stripTags} from "../string";
23-
import {ModelObj, MySequelize, NoteAttributes, NoteMeta} from "./baseModel";
23+
import {Authorship, ModelObj, MySequelize, NoteAttributes, NoteMeta} from "./baseModel";
2424

2525
const md = markdownIt()
2626
export const dmp = new DiffMatchPatch()
@@ -34,7 +34,7 @@ interface ParsedMeta {
3434

3535
export class Note extends Model<NoteAttributes> implements NoteAttributes {
3636
alias: string;
37-
authorship: string;
37+
authorship: Authorship[];
3838
content: string;
3939
id: string;
4040
lastchangeAt: Date | Moment;
@@ -96,9 +96,13 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
9696
authorship: {
9797
type: DataTypes.TEXT({length: 'long'}),
9898
get: function () {
99+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
100+
// @ts-ignore
99101
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
100102
},
101103
set: function (value) {
104+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
105+
// @ts-ignore
102106
this.setDataValue('authorship', JSON.stringify(value))
103107
}
104108
},
@@ -459,16 +463,16 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
459463
return _meta
460464
}
461465

462-
static updateAuthorshipByOperation(operation: any, userId: string, authorships: any): any {
466+
static updateAuthorshipByOperation(operation: (string|number)[], userId: string, authorships: Authorship[]): Authorship[] {
463467
let index = 0
464468
const timestamp = Date.now()
465469
for (let i = 0; i < operation.length; i++) {
466470
const op = operation[i]
467471
if (ot.TextOperation.isRetain(op)) {
468-
index += op
472+
index += op as number
469473
} else if (ot.TextOperation.isInsert(op)) {
470474
const opStart = index
471-
const opEnd = index + op.length
475+
const opEnd = index + (op as string).length
472476
let inserted = false
473477
// authorship format: [userId, startPos, endPos, createdAt, updatedAt]
474478
if (authorships.length <= 0) authorships.push([userId, opStart, opEnd, timestamp, timestamp])
@@ -499,15 +503,15 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
499503
}
500504
}
501505
if (authorship[1] >= opStart) {
502-
authorship[1] += op.length
503-
authorship[2] += op.length
506+
authorship[1] += (op as string).length
507+
authorship[2] += (op as string).length
504508
}
505509
}
506510
}
507-
index += op.length
511+
index += (op as string).length
508512
} else if (ot.TextOperation.isDelete(op)) {
509513
const opStart = index
510-
const opEnd = index - op
514+
const opEnd = index - (op as number)
511515
if (operation.length === 1) {
512516
authorships = []
513517
} else if (authorships.length > 0) {
@@ -517,7 +521,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
517521
authorships.splice(j, 1)
518522
j -= 1
519523
} else if (authorship[1] < opStart && authorship[1] < opEnd && authorship[2] > opStart && authorship[2] > opEnd) {
520-
authorship[2] += op
524+
authorship[2] += op as number
521525
authorship[4] = timestamp
522526
} else if (authorship[2] >= opStart && authorship[2] <= opEnd) {
523527
authorship[2] = opStart
@@ -527,12 +531,12 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
527531
authorship[4] = timestamp
528532
}
529533
if (authorship[1] >= opEnd) {
530-
authorship[1] += op
531-
authorship[2] += op
534+
authorship[1] += op as number
535+
authorship[2] += op as number
532536
}
533537
}
534538
}
535-
index += op
539+
index += (op as number)
536540
}
537541
}
538542
// merge
@@ -561,7 +565,7 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {
561565
return authorships
562566
}
563567

564-
static transformPatchToOperations(patch: any, contentLength: number) {
568+
static transformPatchToOperations(patch: Patch[], contentLength: number): (number | string)[][] {
565569
const operations = []
566570
if (patch.length > 0) {
567571
// calculate original content length

lib/realtime/realtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import config from "../config";
1414
import {logger} from "../logger";
1515
import * as history from "../history";
1616
import {Author, Note, User} from "../models";
17-
import {UserProfile} from "../models/baseModel";
17+
import {Authorship, UserProfile} from "../models/baseModel";
1818

1919
// ot
2020
import ot from "ot";
@@ -73,7 +73,7 @@ export interface RealtimeNoteData {
7373
server: any
7474

7575
authors: Record<string, RealtimeAuthorData>
76-
authorship: string
76+
authorship: Authorship[]
7777
}
7878

7979

0 commit comments

Comments
 (0)