Skip to content

Commit

Permalink
lint: lib/realtime/realtime
Browse files Browse the repository at this point in the history
- add more typing annotate
- add some references to models
  • Loading branch information
a60814billy committed Jun 13, 2021
1 parent 863337d commit 0ff29b9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 24 deletions.
5 changes: 4 additions & 1 deletion lib/models/author.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// external modules

import {Model, DataTypes} from "sequelize";
import {MySequelize, AuthorAttributes, ModelObj} from "./baseModel";
import {MySequelize, AuthorAttributes, ModelObj, UserModel} from "./baseModel";

export class Author extends Model<AuthorAttributes> implements AuthorAttributes {
color: string;
id: string;

userId?: string
user?: UserModel

static initialize(sequelize: MySequelize): void {
Author.init(
{
Expand Down
2 changes: 2 additions & 0 deletions lib/models/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class Note extends Model<NoteAttributes> implements NoteAttributes {

ownerId: string

lastchangeuserId?: string

static initialize(sequelize: MySequelize): void {
Note.init({
id: {
Expand Down
101 changes: 82 additions & 19 deletions lib/realtime/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import config from "../config";
import {logger} from "../logger";
import * as history from "../history";
import {Author, Note, User} from "../models";
import {UserProfile} from "../models/baseModel";

// ot
import ot from "ot";

import {ProcessQueue} from "./processQueue";
import {RealtimeClientConnection} from "./realtimeClientConnection";
import {CursorData, RealtimeClientConnection} from "./realtimeClientConnection";
import {UpdateDirtyNoteJob} from "./realtimeUpdateDirtyNoteJob";
import {CleanDanglingUserJob} from "./realtimeCleanDanglingUserJob";
import {SaveRevisionJob} from "./realtimeSaveRevisionJob";
Expand All @@ -40,6 +41,42 @@ export interface RealtimeUserData {
idle?: any
type?: any
}

interface RealtimeAuthorData {
userid: string
color: string
photo: string
name: string
}

export interface RealtimeNoteData {
id: string,
alias?: string,
title?: string,
// owner id
owner?: string,
ownerprofile?: UserProfile
permission?: string
// last change user id
lastchangeuser?: string
lastchangeuserprofile?: UserProfile

socks: SocketIO.Socket[]
users: Record<string, RealtimeUserData>
//???
tempUsers: any

createtime: number
updatetime: number

// type: ot.EditorSocketIOServer
server: any

authors: Record<string, RealtimeAuthorData>
authorship: string
}


const chance = new Chance()

export let io: SocketIO.Server = null
Expand Down Expand Up @@ -98,7 +135,7 @@ export function secure(socket: SocketIO.Socket, next: (err?: Error | null) => vo

// TODO: only use in `updateDirtyNote`
// TODO: test it
export function emitCheck(note) {
export function emitCheck(note: RealtimeNoteData): void {
const out = {
title: note.title,
updatetime: note.updatetime,
Expand All @@ -111,18 +148,18 @@ export function emitCheck(note) {
}

// actions
export const notes = {}
export const users: Record<string, RealtimeUserData> = {}
export const notes: Record<string, RealtimeNoteData> = {}

export function getNotePool(): any {
export function getNotePool(): Record<string, RealtimeNoteData> {
return notes
}

export function isNoteExistsInPool(noteId: string): boolean {
return !!notes[noteId]
}

export function addNote(note) {
export function addNote(note: RealtimeNoteData): boolean {
if (exports.isNoteExistsInPool(note.id)) return false
notes[note.id] = note
return true
Expand All @@ -142,7 +179,7 @@ export function deleteAllNoteFromPool(): void {
})
}

export function getNoteFromNotePool(noteId) {
export function getNoteFromNotePool(noteId: string): RealtimeNoteData | null {
return notes[noteId]
}

Expand All @@ -159,7 +196,7 @@ updateDirtyNoteJob.start()
cleanDanglingUserJob.start()
saveRevisionJob.start()

export function disconnectSocketOnNote(note) {
export function disconnectSocketOnNote(note: RealtimeNoteData): void {
note.socks.forEach((sock) => {
if (sock) {
sock.emit('delete')
Expand All @@ -170,7 +207,7 @@ export function disconnectSocketOnNote(note) {
})
}

export function updateNote(note, callback) {
export function updateNote(note: RealtimeNoteData, callback: (err: Error | null, note: Note) => void): void {
_updateNoteAsync(note).then(_note => {
callback(null, _note)
}).catch((err) => {
Expand All @@ -179,15 +216,15 @@ export function updateNote(note, callback) {
})
}

function findNoteByIdAsync(id) {
function findNoteByIdAsync(id: string): Promise<Note> {
return Note.findOne({
where: {
id: id
}
})
}

function updateHistoryForEveryUserCollaborateNote(note) {
function updateHistoryForEveryUserCollaborateNote(note: RealtimeNoteData): void {
// update history to every user in this note
const tempUsers = Object.assign({}, note.tempUsers)
note.tempUsers = {}
Expand All @@ -197,7 +234,7 @@ function updateHistoryForEveryUserCollaborateNote(note) {
})
}

async function getUserProfileByIdAsync(id) {
async function getUserProfileByIdAsync(id: string): Promise<UserProfile> {
const user = await User.findOne({
where: {
id: id
Expand Down Expand Up @@ -237,7 +274,7 @@ function buildNoteUpdateData(note) {
}
}

async function _updateNoteAsync(note) {
async function _updateNoteAsync(note: RealtimeNoteData) {
let noteModel = await findNoteByIdAsync(note.id)
if (!noteModel) return null

Expand All @@ -247,7 +284,7 @@ async function _updateNoteAsync(note) {
note.lastchangeuserprofile = await getLastChangeUserProfileAsync(
note.lastchangeuser,
noteModel.lastchangeuserId,
noteModel.lastchangeuserprofile
note.lastchangeuser
)
} catch (err) {
if (err instanceof UserNotFoundException) {
Expand All @@ -262,8 +299,22 @@ async function _updateNoteAsync(note) {
return noteModel
}

interface StatusData {
onlineNotes: number
onlineUsers: number
distinctOnlineUsers: number
notesCount: number
registeredUsers: number
onlineRegisteredUsers: number
distinctOnlineRegisteredUsers: number
isConnectionBusy: boolean
connectionSocketQueueLength: number
isDisconnectBusy: boolean
disconnectSocketQueueLength: number
}

// TODO: test it
export function getStatus() {
export function getStatus(): Promise<StatusData> {
return Note.count()
.then(function (notecount: number) {
const distinctaddresses = []
Expand Down Expand Up @@ -402,7 +453,7 @@ export const parseNoteIdFromSocketAsync = async function (socket: SocketIO.Socke
export function emitOnlineUsers(socket: SocketIO.Socket): void {
const noteId = socket.noteId
if (!noteId || !notes[noteId]) return
const users = []
const users: RealtimeClientUserData[] = []
Object.keys(notes[noteId].users).forEach(function (key) {
const user = notes[noteId].users[key]
if (user) {
Expand Down Expand Up @@ -486,10 +537,10 @@ async function fetchFullNoteAsync(noteId: string): Promise<Note> {
})
}

function buildAuthorProfilesFromNote(noteAuthors) {
const authors = {}
function buildAuthorProfilesFromNote(noteAuthors: Author[]): Record<string, RealtimeAuthorData> {
const authors: Record<string, RealtimeAuthorData> = {}
noteAuthors.forEach((author) => {
const profile = User.getProfile(author.user)
const profile = User.getProfile(author.user as User)
if (profile) {
authors[author.userId] = {
userid: author.userId,
Expand Down Expand Up @@ -584,7 +635,19 @@ export function queueForDisconnect(socket: SocketIO.Socket): void {
})
}

export function buildUserOutData(user) {
interface RealtimeClientUserData {
id?: string
login?: boolean
userid?: string
photo?: string
color?: string
cursor?: CursorData
name?: string
idle?: boolean
type?: string
}

export function buildUserOutData(user: RealtimeUserData): RealtimeClientUserData {
const out = {
id: user.id,
login: user.login,
Expand Down
4 changes: 2 additions & 2 deletions lib/realtime/realtimeClientConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Socket} from "socket.io";
import config from "../config";
import {Note} from "../models";
import {logger} from "../logger";
import {RealtimeUserData} from "./realtime";
import {RealtimeNoteData, RealtimeUserData} from "./realtime";

export type CursorData = Record<string, string>

Expand Down Expand Up @@ -91,7 +91,7 @@ export class RealtimeClientConnection {
return get(this.socket, 'request.user.id')
}

getCurrentNote() {
getCurrentNote(): RealtimeNoteData {
if (!this.socket.noteId) return
return this.realtime.getNoteFromNotePool(this.socket.noteId)
}
Expand Down
6 changes: 4 additions & 2 deletions lib/realtime/realtimeUpdateDirtyNoteJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import moment from "moment";

import config from "../config";
import {logger} from "../logger";
import {Note} from "../models";
import {JobWorker} from "./jobWorker";
import {RealtimeNoteData} from "./realtime";

export class UpdateDirtyNoteJob implements JobWorker {
private realtime: any;
Expand Down Expand Up @@ -34,7 +36,7 @@ export class UpdateDirtyNoteJob implements JobWorker {
})
}

async updateDirtyNote(note) {
async updateDirtyNote(note: RealtimeNoteData): Promise<void> {
const notes = this.realtime.getNotePool()
if (!note.server.isDirty) return

Expand Down Expand Up @@ -66,7 +68,7 @@ export class UpdateDirtyNoteJob implements JobWorker {
}
}

updateNoteAsync(note): Promise<any> {
updateNoteAsync(note: RealtimeNoteData): Promise<Note> {
return new Promise((resolve, reject) => {
this.realtime.updateNote(note, (err, _note) => {
if (err) {
Expand Down

0 comments on commit 0ff29b9

Please sign in to comment.