From 7fc3e7dd8b605c26578b39633af89d83c756b099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E7=8E=8B=E5=B7=9E=E3=82=A2=E3=83=86=E3=83=8D?= Date: Sat, 30 Jan 2021 09:57:11 +0800 Subject: [PATCH 01/29] Update recaptcha's host (#7132) Changed to recaptcha.net because google.com are blocked in some region --- src/client/components/captcha.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/components/captcha.vue b/src/client/components/captcha.vue index a6445ecea4..710fcd6169 100644 --- a/src/client/components/captcha.vue +++ b/src/client/components/captcha.vue @@ -57,7 +57,7 @@ export default defineComponent({ src() { const endpoint = ({ hcaptcha: 'https://hcaptcha.com/1', - grecaptcha: 'https://www.google.com/recaptcha', + grecaptcha: 'https://www.recaptcha.net/recaptcha', } as Record)[this.provider]; return `${typeof endpoint == 'string' ? endpoint : 'about:invalid'}/api.js?render=explicit`; From 100a13191340399abc50f4c597f81af4cbf971b4 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 30 Jan 2021 10:59:05 +0900 Subject: [PATCH 02/29] pages refactoring, fix bug (#7066) * pages refactoring * pages: fix if block * fix code format * remove passing of the page parameter * remove comment * fix indent * replace with unref * fix conditions of isVarBlock() * Update src/client/scripts/hpml/block.ts use includes() instead of find() Co-authored-by: syuilo Co-authored-by: syuilo --- src/client/components/page/page.block.vue | 14 ++- src/client/components/page/page.button.vue | 30 ++--- src/client/components/page/page.canvas.vue | 22 +++- src/client/components/page/page.counter.vue | 34 +++--- src/client/components/page/page.if.vue | 16 +-- src/client/components/page/page.image.vue | 21 ++-- src/client/components/page/page.note.vue | 29 +++-- .../components/page/page.number-input.vue | 32 +++-- src/client/components/page/page.post.vue | 16 ++- .../components/page/page.radio-button.vue | 34 +++--- src/client/components/page/page.section.vue | 15 +-- src/client/components/page/page.switch.vue | 32 +++-- .../components/page/page.text-input.vue | 32 +++-- src/client/components/page/page.text.vue | 12 +- .../components/page/page.textarea-input.vue | 33 ++++-- src/client/components/page/page.textarea.vue | 12 +- src/client/components/page/page.vue | 87 +++++++------- .../page-editor/page-editor.script-block.vue | 6 +- src/client/scripts/hpml/block.ts | 109 ++++++++++++++++++ src/client/scripts/hpml/evaluator.ts | 97 ++++++++-------- src/client/scripts/hpml/expr.ts | 79 +++++++++++++ src/client/scripts/hpml/index.ts | 88 +------------- src/client/scripts/hpml/lib.ts | 80 +++++++++++-- src/client/scripts/hpml/type-checker.ts | 18 +-- 24 files changed, 600 insertions(+), 348 deletions(-) create mode 100644 src/client/scripts/hpml/block.ts create mode 100644 src/client/scripts/hpml/expr.ts diff --git a/src/client/components/page/page.block.vue b/src/client/components/page/page.block.vue index be933c8623..54b8b30276 100644 --- a/src/client/components/page/page.block.vue +++ b/src/client/components/page/page.block.vue @@ -1,9 +1,9 @@ diff --git a/src/client/components/page/page.counter.vue b/src/client/components/page/page.counter.vue index 9eee47c4ec..b1af8954b0 100644 --- a/src/client/components/page/page.counter.vue +++ b/src/client/components/page/page.counter.vue @@ -1,41 +1,43 @@ diff --git a/src/client/components/page/page.if.vue b/src/client/components/page/page.if.vue index 64081d5b5d..ec25332db0 100644 --- a/src/client/components/page/page.if.vue +++ b/src/client/components/page/page.if.vue @@ -1,27 +1,29 @@ diff --git a/src/client/components/page/page.note.vue b/src/client/components/page/page.note.vue index 27c1f1ed66..925844c1bd 100644 --- a/src/client/components/page/page.note.vue +++ b/src/client/components/page/page.note.vue @@ -1,15 +1,16 @@ diff --git a/src/client/components/page/page.number-input.vue b/src/client/components/page/page.number-input.vue index cf4000010f..7b7d799330 100644 --- a/src/client/components/page/page.number-input.vue +++ b/src/client/components/page/page.number-input.vue @@ -1,36 +1,44 @@ diff --git a/src/client/components/page/page.post.vue b/src/client/components/page/page.post.vue index 46870ca0d7..33c6e6b14c 100644 --- a/src/client/components/page/page.post.vue +++ b/src/client/components/page/page.post.vue @@ -6,12 +6,14 @@ diff --git a/src/client/components/page/page.section.vue b/src/client/components/page/page.section.vue index 9f05f3a0ce..d32f5dc732 100644 --- a/src/client/components/page/page.section.vue +++ b/src/client/components/page/page.section.vue @@ -1,29 +1,30 @@ diff --git a/src/client/components/page/page.text-input.vue b/src/client/components/page/page.text-input.vue index f0fe70e335..e67814af16 100644 --- a/src/client/components/page/page.text-input.vue +++ b/src/client/components/page/page.text-input.vue @@ -1,36 +1,44 @@ diff --git a/src/client/components/page/page.text.vue b/src/client/components/page/page.text.vue index f109c9f041..1896d00f42 100644 --- a/src/client/components/page/page.text.vue +++ b/src/client/components/page/page.text.vue @@ -6,7 +6,9 @@ diff --git a/src/client/components/page/page.textarea.vue b/src/client/components/page/page.textarea.vue index 205448977c..97d688368a 100644 --- a/src/client/components/page/page.textarea.vue +++ b/src/client/components/page/page.textarea.vue @@ -3,7 +3,9 @@ diff --git a/src/client/pages/page-editor/page-editor.script-block.vue b/src/client/pages/page-editor/page-editor.script-block.vue index 3fbcd1b19b..68bc1b50c1 100644 --- a/src/client/pages/page-editor/page-editor.script-block.vue +++ b/src/client/pages/page-editor/page-editor.script-block.vue @@ -61,8 +61,10 @@ import { faPencilAlt, faPlug } from '@fortawesome/free-solid-svg-icons'; import { v4 as uuid } from 'uuid'; import XContainer from './page-editor.container.vue'; import MkTextarea from '@/components/ui/textarea.vue'; -import { isLiteralBlock, funcDefs, blockDefs } from '@/scripts/hpml/index'; +import { blockDefs } from '@/scripts/hpml/index'; import * as os from '@/os'; +import { isLiteralValue } from '@/scripts/hpml/expr'; +import { funcDefs } from '@/scripts/hpml/lib'; export default defineComponent({ components: { @@ -166,7 +168,7 @@ export default defineComponent({ return; } - if (isLiteralBlock(this.value)) return; + if (isLiteralValue(this.value)) return; const empties = []; for (let i = 0; i < funcDefs[this.value.type].in.length; i++) { diff --git a/src/client/scripts/hpml/block.ts b/src/client/scripts/hpml/block.ts new file mode 100644 index 0000000000..804c5c1124 --- /dev/null +++ b/src/client/scripts/hpml/block.ts @@ -0,0 +1,109 @@ +// blocks + +export type BlockBase = { + id: string; + type: string; +}; + +export type TextBlock = BlockBase & { + type: 'text'; + text: string; +}; + +export type SectionBlock = BlockBase & { + type: 'section'; + title: string; + children: (Block | VarBlock)[]; +}; + +export type ImageBlock = BlockBase & { + type: 'image'; + fileId: string | null; +}; + +export type ButtonBlock = BlockBase & { + type: 'button'; + text: any; + primary: boolean; + action: string; + content: string; + event: string; + message: string; + var: string; + fn: string; +}; + +export type IfBlock = BlockBase & { + type: 'if'; + var: string; + children: Block[]; +}; + +export type TextareaBlock = BlockBase & { + type: 'textarea'; + text: string; +}; + +export type PostBlock = BlockBase & { + type: 'post'; + text: string; + attachCanvasImage: boolean; + canvasId: string; +}; + +export type CanvasBlock = BlockBase & { + type: 'canvas'; + name: string; // canvas id + width: number; + height: number; +}; + +export type NoteBlock = BlockBase & { + type: 'note'; + detailed: boolean; + note: string | null; +}; + +export type Block = + TextBlock | SectionBlock | ImageBlock | ButtonBlock | IfBlock | TextareaBlock | PostBlock | CanvasBlock | NoteBlock | VarBlock; + +// variable blocks + +export type VarBlockBase = BlockBase & { + name: string; +}; + +export type NumberInputVarBlock = VarBlockBase & { + type: 'numberInput'; + text: string; +}; + +export type TextInputVarBlock = VarBlockBase & { + type: 'textInput'; + text: string; +}; + +export type SwitchVarBlock = VarBlockBase & { + type: 'switch'; + text: string; +}; + +export type RadioButtonVarBlock = VarBlockBase & { + type: 'radioButton'; + title: string; + values: string[]; +}; + +export type CounterVarBlock = VarBlockBase & { + type: 'counter'; + text: string; + inc: number; +}; + +export type VarBlock = + NumberInputVarBlock | TextInputVarBlock | SwitchVarBlock | RadioButtonVarBlock | CounterVarBlock; + +const varBlock = ['numberInput', 'textInput', 'switch', 'radioButton', 'counter']; +export function isVarBlock(block: Block): block is VarBlock { + return varBlock.includes(block.type); +} diff --git a/src/client/scripts/hpml/evaluator.ts b/src/client/scripts/hpml/evaluator.ts index 4fa95e89c9..20261d333d 100644 --- a/src/client/scripts/hpml/evaluator.ts +++ b/src/client/scripts/hpml/evaluator.ts @@ -1,12 +1,13 @@ import autobind from 'autobind-decorator'; -import { Variable, PageVar, envVarsDef, Block, isFnBlock, Fn, HpmlScope, HpmlError } from '.'; +import { PageVar, envVarsDef, Fn, HpmlScope, HpmlError } from '.'; import { version } from '@/config'; import { AiScript, utils, values } from '@syuilo/aiscript'; import { createAiScriptEnv } from '../aiscript/api'; import { collectPageVars } from '../collect-page-vars'; import { initHpmlLib, initAiLib } from './lib'; import * as os from '@/os'; -import { markRaw, ref, Ref } from 'vue'; +import { markRaw, ref, Ref, unref } from 'vue'; +import { Expr, isLiteralValue, Variable } from './expr'; /** * Hpml evaluator @@ -94,7 +95,7 @@ export class Hpml { public interpolate(str: string) { if (str == null) return null; return str.replace(/{(.+?)}/g, match => { - const v = this.vars[match.slice(1, -1).trim()]; + const v = unref(this.vars)[match.slice(1, -1).trim()]; return v == null ? 'NULL' : v.toString(); }); } @@ -158,72 +159,76 @@ export class Hpml { } @autobind - private evaluate(block: Block, scope: HpmlScope): any { - if (block.type === null) { - return null; - } + private evaluate(expr: Expr, scope: HpmlScope): any { - if (block.type === 'number') { - return parseInt(block.value, 10); - } - - if (block.type === 'text' || block.type === 'multiLineText') { - return this._interpolateScope(block.value || '', scope); - } - - if (block.type === 'textList') { - return this._interpolateScope(block.value || '', scope).trim().split('\n'); - } - - if (block.type === 'ref') { - return scope.getState(block.value); - } - - if (block.type === 'aiScriptVar') { - if (this.aiscript) { - try { - return utils.valToJs(this.aiscript.scope.get(block.value)); - } catch (e) { - return null; - } - } else { + if (isLiteralValue(expr)) { + if (expr.type === null) { return null; } - } - // Define user function - if (isFnBlock(block)) { - return { - slots: block.value.slots.map(x => x.name), - exec: (slotArg: Record) => { - return this.evaluate(block.value.expression, scope.createChildScope(slotArg, block.id)); + if (expr.type === 'number') { + return parseInt((expr.value as any), 10); + } + + if (expr.type === 'text' || expr.type === 'multiLineText') { + return this._interpolateScope(expr.value || '', scope); + } + + if (expr.type === 'textList') { + return this._interpolateScope(expr.value || '', scope).trim().split('\n'); + } + + if (expr.type === 'ref') { + return scope.getState(expr.value); + } + + if (expr.type === 'aiScriptVar') { + if (this.aiscript) { + try { + return utils.valToJs(this.aiscript.scope.get(expr.value)); + } catch (e) { + return null; + } + } else { + return null; } - } as Fn; + } + + // Define user function + if (expr.type == 'fn') { + return { + slots: expr.value.slots.map(x => x.name), + exec: (slotArg: Record) => { + return this.evaluate(expr.value.expression, scope.createChildScope(slotArg, expr.id)); + } + } as Fn; + } + return; } // Call user function - if (block.type.startsWith('fn:')) { - const fnName = block.type.split(':')[1]; + if (expr.type.startsWith('fn:')) { + const fnName = expr.type.split(':')[1]; const fn = scope.getState(fnName); const args = {} as Record; for (let i = 0; i < fn.slots.length; i++) { const name = fn.slots[i]; - args[name] = this.evaluate(block.args[i], scope); + args[name] = this.evaluate(expr.args[i], scope); } return fn.exec(args); } - if (block.args === undefined) return null; + if (expr.args === undefined) return null; - const funcs = initHpmlLib(block, scope, this.opts.randomSeed, this.opts.visitor); + const funcs = initHpmlLib(expr, scope, this.opts.randomSeed, this.opts.visitor); // Call function - const fnName = block.type; + const fnName = expr.type; const fn = (funcs as any)[fnName]; if (fn == null) { throw new HpmlError(`No such function '${fnName}'`); } else { - return fn(...block.args.map(x => this.evaluate(x, scope))); + return fn(...expr.args.map(x => this.evaluate(x, scope))); } } } diff --git a/src/client/scripts/hpml/expr.ts b/src/client/scripts/hpml/expr.ts new file mode 100644 index 0000000000..00e3ed118b --- /dev/null +++ b/src/client/scripts/hpml/expr.ts @@ -0,0 +1,79 @@ +import { literalDefs, Type } from '.'; + +export type ExprBase = { + id: string; +}; + +// value + +export type EmptyValue = ExprBase & { + type: null; + value: null; +}; + +export type TextValue = ExprBase & { + type: 'text'; + value: string; +}; + +export type MultiLineTextValue = ExprBase & { + type: 'multiLineText'; + value: string; +}; + +export type TextListValue = ExprBase & { + type: 'textList'; + value: string; +}; + +export type NumberValue = ExprBase & { + type: 'number'; + value: number; +}; + +export type RefValue = ExprBase & { + type: 'ref'; + value: string; // value is variable name +}; + +export type AiScriptRefValue = ExprBase & { + type: 'aiScriptVar'; + value: string; // value is variable name +}; + +export type UserFnValue = ExprBase & { + type: 'fn'; + value: UserFnInnerValue; +}; +type UserFnInnerValue = { + slots: { + name: string; + type: Type; + }[]; + expression: Expr; +}; + +export type Value = + EmptyValue | TextValue | MultiLineTextValue | TextListValue | NumberValue | RefValue | AiScriptRefValue | UserFnValue; + +export function isLiteralValue(expr: Expr): expr is Value { + if (expr.type == null) return true; + if (literalDefs[expr.type]) return true; + return false; +} + +// call function + +export type CallFn = ExprBase & { // "fn:hoge" or string + type: string; + args: Expr[]; + value: null; +}; + +// variable +export type Variable = (Value | CallFn) & { + name: string; +}; + +// expression +export type Expr = Variable | Value | CallFn; diff --git a/src/client/scripts/hpml/index.ts b/src/client/scripts/hpml/index.ts index fa34b25d8d..924cd32eb5 100644 --- a/src/client/scripts/hpml/index.ts +++ b/src/client/scripts/hpml/index.ts @@ -3,52 +3,16 @@ */ import autobind from 'autobind-decorator'; - import { faMagic, faSquareRootAlt, faAlignLeft, - faShareAlt, - faPlus, - faMinus, - faTimes, - faDivide, faList, faQuoteRight, - faEquals, - faGreaterThan, - faLessThan, - faGreaterThanEqual, - faLessThanEqual, - faNotEqual, - faDice, faSortNumericUp, - faExchangeAlt, - faRecycle, - faIndent, - faCalculator, } from '@fortawesome/free-solid-svg-icons'; -import { faFlag } from '@fortawesome/free-regular-svg-icons'; import { Hpml } from './evaluator'; - -export type Block = { - id: string; - type: string; - args: Block[]; - value: V; -}; - -export type FnBlock = Block<{ - slots: { - name: string; - type: Type; - }[]; - expression: Block; -}>; - -export type Variable = Block & { - name: string; -}; +import { funcDefs } from './lib'; export type Fn = { slots: string[]; @@ -57,46 +21,6 @@ export type Fn = { export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null; -export const funcDefs: Record = { - if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, }, - for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, }, - not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, - add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, }, - subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, }, - multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, }, - divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, - mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, - round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, }, - eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, }, - notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, }, - gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, }, - lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, }, - gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, }, - ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, }, - strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, }, - strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, }, - strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, - strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, }, - join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, - stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, }, - numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, }, - splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, }, - pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, }, - listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, }, - rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, - dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, - seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, }, - random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, - dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, - seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, }, - randomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, - dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, - seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, }, - DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping -}; - export const literalDefs: Record = { text: { out: 'string', category: 'value', icon: faQuoteRight, }, multiLineText: { out: 'string', category: 'value', icon: faAlignLeft, }, @@ -116,10 +40,6 @@ export const blockDefs = [ })) ]; -export function isFnBlock(block: Block): block is FnBlock { - return block.type === 'fn'; -} - export type PageVar = { name: string; value: any; type: Type; }; export const envVarsDef: Record = { @@ -140,12 +60,6 @@ export const envVarsDef: Record = { NULL: null, }; -export function isLiteralBlock(v: Block) { - if (v.type === null) return true; - if (literalDefs[v.type]) return true; - return false; -} - export class HpmlScope { private layerdStates: Record[]; public name: string; diff --git a/src/client/scripts/hpml/lib.ts b/src/client/scripts/hpml/lib.ts index 11e4f2fc39..7454562184 100644 --- a/src/client/scripts/hpml/lib.ts +++ b/src/client/scripts/hpml/lib.ts @@ -2,9 +2,31 @@ import * as tinycolor from 'tinycolor2'; import Chart from 'chart.js'; import { Hpml } from './evaluator'; import { values, utils } from '@syuilo/aiscript'; -import { Block, Fn, HpmlScope } from '.'; +import { Fn, HpmlScope } from '.'; +import { Expr } from './expr'; import * as seedrandom from 'seedrandom'; +import { + faShareAlt, + faPlus, + faMinus, + faTimes, + faDivide, + faQuoteRight, + faEquals, + faGreaterThan, + faLessThan, + faGreaterThanEqual, + faLessThanEqual, + faNotEqual, + faDice, + faExchangeAlt, + faRecycle, + faIndent, + faCalculator, +} from '@fortawesome/free-solid-svg-icons'; +import { faFlag } from '@fortawesome/free-regular-svg-icons'; + // https://stackoverflow.com/questions/38493564/chart-area-background-color-chartjs Chart.pluginService.register({ beforeDraw: (chart, easing) => { @@ -125,7 +147,47 @@ export function initAiLib(hpml: Hpml) { }; } -export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, visitor?: any) { +export const funcDefs: Record = { + if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, }, + for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, }, + not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + or: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + and: { in: ['boolean', 'boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, + add: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faPlus, }, + subtract: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faMinus, }, + multiply: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faTimes, }, + divide: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, + mod: { in: ['number', 'number'], out: 'number', category: 'operation', icon: faDivide, }, + round: { in: ['number'], out: 'number', category: 'operation', icon: faCalculator, }, + eq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faEquals, }, + notEq: { in: [0, 0], out: 'boolean', category: 'comparison', icon: faNotEqual, }, + gt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThan, }, + lt: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThan, }, + gtEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faGreaterThanEqual, }, + ltEq: { in: ['number', 'number'], out: 'boolean', category: 'comparison', icon: faLessThanEqual, }, + strLen: { in: ['string'], out: 'number', category: 'text', icon: faQuoteRight, }, + strPick: { in: ['string', 'number'], out: 'string', category: 'text', icon: faQuoteRight, }, + strReplace: { in: ['string', 'string', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, + strReverse: { in: ['string'], out: 'string', category: 'text', icon: faQuoteRight, }, + join: { in: ['stringArray', 'string'], out: 'string', category: 'text', icon: faQuoteRight, }, + stringToNumber: { in: ['string'], out: 'number', category: 'convert', icon: faExchangeAlt, }, + numberToString: { in: ['number'], out: 'string', category: 'convert', icon: faExchangeAlt, }, + splitStrByLine: { in: ['string'], out: 'stringArray', category: 'convert', icon: faExchangeAlt, }, + pick: { in: [null, 'number'], out: null, category: 'list', icon: faIndent, }, + listLen: { in: [null], out: 'number', category: 'list', icon: faIndent, }, + rannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, + dailyRannum: { in: ['number', 'number'], out: 'number', category: 'random', icon: faDice, }, + seedRannum: { in: [null, 'number', 'number'], out: 'number', category: 'random', icon: faDice, }, + random: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, + dailyRandom: { in: ['number'], out: 'boolean', category: 'random', icon: faDice, }, + seedRandom: { in: [null, 'number'], out: 'boolean', category: 'random', icon: faDice, }, + randomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, + dailyRandomPick: { in: [0], out: 0, category: 'random', icon: faDice, }, + seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, }, + DRPWPM: { in: ['stringArray'], out: 'string', category: 'random', icon: faDice, }, // dailyRandomPickWithProbabilityMapping +}; + +export function initHpmlLib(expr: Expr, scope: HpmlScope, randomSeed: string, visitor?: any) { const date = new Date(); const day = `${visitor ? visitor.id : ''} ${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; @@ -166,12 +228,12 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, splitStrByLine: (a: string) => a.split('\n'), pick: (list: any[], i: number) => list[i - 1], listLen: (list: any[]) => list.length, - random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * 100) < probability, - rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * (max - min + 1)), - randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${block.id}`)() * list.length)], - dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${block.id}`)() * 100) < probability, - dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${block.id}`)() * (max - min + 1)), - dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${block.id}`)() * list.length)], + random: (probability: number) => Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * 100) < probability, + rannum: (min: number, max: number) => min + Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * (max - min + 1)), + randomPick: (list: any[]) => list[Math.floor(seedrandom(`${randomSeed}:${expr.id}`)() * list.length)], + dailyRandom: (probability: number) => Math.floor(seedrandom(`${day}:${expr.id}`)() * 100) < probability, + dailyRannum: (min: number, max: number) => min + Math.floor(seedrandom(`${day}:${expr.id}`)() * (max - min + 1)), + dailyRandomPick: (list: any[]) => list[Math.floor(seedrandom(`${day}:${expr.id}`)() * list.length)], seedRandom: (seed: any, probability: number) => Math.floor(seedrandom(seed)() * 100) < probability, seedRannum: (seed: any, min: number, max: number) => min + Math.floor(seedrandom(seed)() * (max - min + 1)), seedRandomPick: (seed: any, list: any[]) => list[Math.floor(seedrandom(seed)() * list.length)], @@ -185,7 +247,7 @@ export function initHpmlLib(block: Block, scope: HpmlScope, randomSeed: string, totalFactor += factor; xs.push({ factor, text }); } - const r = seedrandom(`${day}:${block.id}`)() * totalFactor; + const r = seedrandom(`${day}:${expr.id}`)() * totalFactor; let stackedFactor = 0; for (const x of xs) { if (r >= stackedFactor && r <= stackedFactor + x.factor) { diff --git a/src/client/scripts/hpml/type-checker.ts b/src/client/scripts/hpml/type-checker.ts index 14950e0195..9633b3cd01 100644 --- a/src/client/scripts/hpml/type-checker.ts +++ b/src/client/scripts/hpml/type-checker.ts @@ -1,5 +1,7 @@ import autobind from 'autobind-decorator'; -import { Type, Block, funcDefs, envVarsDef, Variable, PageVar, isLiteralBlock } from '.'; +import { Type, envVarsDef, PageVar } from '.'; +import { Expr, isLiteralValue, Variable } from './expr'; +import { funcDefs } from './lib'; type TypeError = { arg: number; @@ -20,10 +22,10 @@ export class HpmlTypeChecker { } @autobind - public typeCheck(v: Block): TypeError | null { - if (isLiteralBlock(v)) return null; + public typeCheck(v: Expr): TypeError | null { + if (isLiteralValue(v)) return null; - const def = funcDefs[v.type]; + const def = funcDefs[v.type || '']; if (def == null) { throw new Error('Unknown type: ' + v.type); } @@ -58,8 +60,8 @@ export class HpmlTypeChecker { } @autobind - public getExpectedType(v: Block, slot: number): Type { - const def = funcDefs[v.type]; + public getExpectedType(v: Expr, slot: number): Type { + const def = funcDefs[v.type || '']; if (def == null) { throw new Error('Unknown type: ' + v.type); } @@ -86,7 +88,7 @@ export class HpmlTypeChecker { } @autobind - public infer(v: Block): Type { + public infer(v: Expr): Type { if (v.type === null) return null; if (v.type === 'text') return 'string'; if (v.type === 'multiLineText') return 'string'; @@ -103,7 +105,7 @@ export class HpmlTypeChecker { return pageVar.type; } - const envVar = envVarsDef[v.value]; + const envVar = envVarsDef[v.value || '']; if (envVar !== undefined) { return envVar; } From ff67fb337ea74099ba45e705473d46cd1eb65128 Mon Sep 17 00:00:00 2001 From: Ehsan Javadynia <31900907+ehsanjavadynia@users.noreply.github.com> Date: Sat, 30 Jan 2021 05:39:46 +0330 Subject: [PATCH 03/29] using set instead of array for search (#7126) * Resolve #6905 * Resolve #6905 * Resolve #6905 --- src/misc/is-muted-user-related.ts | 8 ++++---- src/server/api/stream/channels/home-timeline.ts | 4 ++-- src/server/api/stream/channels/hybrid-timeline.ts | 4 ++-- src/server/api/stream/channels/local-timeline.ts | 2 +- src/server/api/stream/channels/main.ts | 4 ++-- src/server/api/stream/index.ts | 12 ++++++------ src/services/add-note-to-antenna.ts | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/misc/is-muted-user-related.ts b/src/misc/is-muted-user-related.ts index 6f074bcb90..2caa743f95 100644 --- a/src/misc/is-muted-user-related.ts +++ b/src/misc/is-muted-user-related.ts @@ -1,13 +1,13 @@ -export function isMutedUserRelated(note: any, mutedUserIds: string[]): boolean { - if (mutedUserIds.includes(note.userId)) { +export function isMutedUserRelated(note: any, mutedUserIds: Set): boolean { + if (mutedUserIds.has(note.userId)) { return true; } - if (note.reply != null && mutedUserIds.includes(note.reply.userId)) { + if (note.reply != null && mutedUserIds.has(note.reply.userId)) { return true; } - if (note.renote != null && mutedUserIds.includes(note.renote.userId)) { + if (note.renote != null && mutedUserIds.has(note.renote.userId)) { return true; } diff --git a/src/server/api/stream/channels/home-timeline.ts b/src/server/api/stream/channels/home-timeline.ts index 15fe7fa6f0..6cfa6eae7b 100644 --- a/src/server/api/stream/channels/home-timeline.ts +++ b/src/server/api/stream/channels/home-timeline.ts @@ -19,10 +19,10 @@ export default class extends Channel { @autobind private async onNote(note: PackedNote) { if (note.channelId) { - if (!this.followingChannels.includes(note.channelId)) return; + if (!this.followingChannels.has(note.channelId)) return; } else { // その投稿のユーザーをフォローしていなかったら弾く - if ((this.user!.id !== note.userId) && !this.following.includes(note.userId)) return; + if ((this.user!.id !== note.userId) && !this.following.has(note.userId)) return; } if (['followers', 'specified'].includes(note.visibility)) { diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts index 4dc5f01a32..a9e577cacb 100644 --- a/src/server/api/stream/channels/hybrid-timeline.ts +++ b/src/server/api/stream/channels/hybrid-timeline.ts @@ -29,9 +29,9 @@ export default class extends Channel { // フォローしているチャンネルの投稿 の場合だけ if (!( (note.channelId == null && this.user!.id === note.userId) || - (note.channelId == null && this.following.includes(note.userId)) || + (note.channelId == null && this.following.has(note.userId)) || (note.channelId == null && ((note.user as PackedUser).host == null && note.visibility === 'public')) || - (note.channelId != null && this.followingChannels.includes(note.channelId)) + (note.channelId != null && this.followingChannels.has(note.channelId)) )) return; if (['followers', 'specified'].includes(note.visibility)) { diff --git a/src/server/api/stream/channels/local-timeline.ts b/src/server/api/stream/channels/local-timeline.ts index baeae86603..a3a5e491fc 100644 --- a/src/server/api/stream/channels/local-timeline.ts +++ b/src/server/api/stream/channels/local-timeline.ts @@ -27,7 +27,7 @@ export default class extends Channel { private async onNote(note: PackedNote) { if ((note.user as PackedUser).host !== null) return; if (note.visibility !== 'public') return; - if (note.channelId != null && !this.followingChannels.includes(note.channelId)) return; + if (note.channelId != null && !this.followingChannels.has(note.channelId)) return; // リプライなら再pack if (note.replyId != null) { diff --git a/src/server/api/stream/channels/main.ts b/src/server/api/stream/channels/main.ts index 22e664baca..b69c2ec355 100644 --- a/src/server/api/stream/channels/main.ts +++ b/src/server/api/stream/channels/main.ts @@ -16,7 +16,7 @@ export default class extends Channel { switch (type) { case 'notification': { - if (this.muting.includes(body.userId)) return; + if (this.muting.has(body.userId)) return; if (body.note && body.note.isHidden) { body.note = await Notes.pack(body.note.id, this.user, { detail: true @@ -25,7 +25,7 @@ export default class extends Channel { break; } case 'mention': { - if (this.muting.includes(body.userId)) return; + if (this.muting.has(body.userId)) return; if (body.isHidden) { body = await Notes.pack(body.id, this.user, { detail: true diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index 36e08ec05f..5b975d07db 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -19,9 +19,9 @@ import { UserProfile } from '../../../models/entities/user-profile'; export default class Connection { public user?: User; public userProfile?: UserProfile; - public following: User['id'][] = []; - public muting: User['id'][] = []; - public followingChannels: ChannelModel['id'][] = []; + public following: Set = new Set(); + public muting: Set = new Set(); + public followingChannels: Set = new Set(); public token?: AccessToken; private wsConnection: websocket.connection; public subscriber: EventEmitter; @@ -267,7 +267,7 @@ export default class Connection { select: ['followeeId'] }); - this.following = followings.map(x => x.followeeId); + this.following = new Set(followings.map(x => x.followeeId)); } @autobind @@ -279,7 +279,7 @@ export default class Connection { select: ['muteeId'] }); - this.muting = mutings.map(x => x.muteeId); + this.muting = new Set(mutings.map(x => x.muteeId)); } @autobind @@ -291,7 +291,7 @@ export default class Connection { select: ['followeeId'] }); - this.followingChannels = followings.map(x => x.followeeId); + this.followingChannels = new Set(followings.map(x => x.followeeId)); } @autobind diff --git a/src/services/add-note-to-antenna.ts b/src/services/add-note-to-antenna.ts index e486494c9b..f11607fd43 100644 --- a/src/services/add-note-to-antenna.ts +++ b/src/services/add-note-to-antenna.ts @@ -40,7 +40,7 @@ export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: U _note.renote = await Notes.findOne(note.renoteId).then(ensure); } - if (isMutedUserRelated(_note, mutings.map(x => x.muteeId))) { + if (isMutedUserRelated(_note, new Set(mutings.map(x => x.muteeId)))) { return; } From ecae4c03f9856a9aa306c002d4fa6e6b3d4812da Mon Sep 17 00:00:00 2001 From: okpierre <1679025+okpierre@users.noreply.github.com> Date: Fri, 29 Jan 2021 21:10:33 -0500 Subject: [PATCH 04/29] Update note-detailed.vue (#7120) --- src/client/components/note-detailed.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/components/note-detailed.vue b/src/client/components/note-detailed.vue index 5d83602924..1108bd2c27 100644 --- a/src/client/components/note-detailed.vue +++ b/src/client/components/note-detailed.vue @@ -52,7 +52,7 @@
- +
From 9dd1a8fb2e89d4894da7e7af46baf0b237aa9cd8 Mon Sep 17 00:00:00 2001 From: kabo2468 <28654659+kabo2468@users.noreply.github.com> Date: Sat, 30 Jan 2021 14:03:11 +0900 Subject: [PATCH 05/29] =?UTF-8?q?=E3=82=A6=E3=82=A3=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=82=A6=E3=81=8B=E3=82=89=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AE?= =?UTF-8?q?=E3=82=BD=E3=83=BC=E3=82=B9=E3=81=8C=E8=A6=8B=E3=82=8C=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3=20(#7136)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/pages/page.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/pages/page.vue b/src/client/pages/page.vue index bd13b28e81..0b0b421ea2 100644 --- a/src/client/pages/page.vue +++ b/src/client/pages/page.vue @@ -20,7 +20,7 @@