11import { socketIO , wrap } from "../../deps/socket.ts" ;
22import { getProjectId , getUserId } from "./id.ts" ;
33import { makeChanges } from "./makeChanges.ts" ;
4+ import { pull } from "./pull.ts" ;
45import { pinNumber } from "./pin.ts" ;
56import type { Line } from "../../deps/scrapbox.ts" ;
6- import { toTitleLc } from "../../title.ts" ;
7- import { ensureEditablePage , pushCommit , pushWithRetry } from "./_fetch.ts" ;
7+ import { pushCommit , pushWithRetry } from "./_fetch.ts" ;
88
99/** 指定したページを削除する
1010 *
@@ -16,23 +16,22 @@ export async function deletePage(
1616 title : string ,
1717) : Promise < void > {
1818 const [
19- { id : pageId , commitId : initialCommitId , persistent } ,
19+ { pageId, commitId : parentId , persistent } ,
2020 projectId ,
2121 userId ,
2222 ] = await Promise . all ( [
23- ensureEditablePage ( project , title ) ,
23+ pull ( project , title ) ,
2424 getProjectId ( project ) ,
2525 getUserId ( ) ,
2626 ] ) ;
27- let parentId = initialCommitId ;
2827
2928 if ( ! persistent ) return ;
3029
3130 const io = await socketIO ( ) ;
3231 const { request } = wrap ( io ) ;
3332
3433 try {
35- parentId = await pushWithRetry ( request , [ { deleted : true } ] , {
34+ await pushWithRetry ( request , [ { deleted : true } ] , {
3635 projectId,
3736 pageId,
3837 parentId,
@@ -59,23 +58,16 @@ export async function patch(
5958 update : ( lines : Line [ ] ) => string [ ] | Promise < string [ ] > ,
6059) : Promise < void > {
6160 const [
62- page ,
61+ head_ ,
6362 projectId ,
6463 userId ,
6564 ] = await Promise . all ( [
66- ensureEditablePage ( project , title ) ,
65+ pull ( project , title ) ,
6766 getProjectId ( project ) ,
6867 getUserId ( ) ,
6968 ] ) ;
7069
71- let head = {
72- persistent : page . persistent ,
73- lines : page . lines ,
74- image : page . image ,
75- commitId : page . commitId ,
76- linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
77- } ;
78- const pageId = page . id ;
70+ let head = head_ ;
7971
8072 const io = await socketIO ( ) ;
8173 try {
@@ -91,7 +83,7 @@ export async function patch(
9183 await pushCommit ( request , changes , {
9284 parentId : head . commitId ,
9385 projectId,
94- pageId,
86+ pageId : head . pageId ,
9587 userId,
9688 } ) ;
9789 break ;
@@ -103,14 +95,7 @@ export async function patch(
10395 "Faild to push a commit. Retry after pulling new commits" ,
10496 ) ;
10597 try {
106- const page = await ensureEditablePage ( project , title ) ;
107- head = {
108- persistent : page . persistent ,
109- lines : page . lines ,
110- image : page . image ,
111- commitId : page . commitId ,
112- linksLc : page . links . map ( ( link ) => toTitleLc ( link ) ) ,
113- } ;
98+ head = await pull ( project , title ) ;
11499 } catch ( e : unknown ) {
115100 throw e ;
116101 }
@@ -142,36 +127,37 @@ export async function pin(
142127 option ?: PinOption ,
143128) : Promise < void > {
144129 const [
145- { id : pageId , commitId : initialCommitId , persistent , pin } ,
130+ head ,
146131 projectId ,
147132 userId ,
148133 ] = await Promise . all ( [
149- ensureEditablePage ( project , title ) ,
134+ pull ( project , title ) ,
150135 getProjectId ( project ) ,
151136 getUserId ( ) ,
152137 ] ) ;
153- let parentId = initialCommitId ;
154138
155139 // 既にピン留めされている場合は何もしない
156- if ( pin > 0 || ( ! persistent && ! ( option ?. create ?? false ) ) ) return ;
140+ if ( head . pin > 0 || ( ! head . persistent && ! ( option ?. create ?? false ) ) ) return ;
157141
158- const init = { projectId, pageId, userId, project, title } ;
142+ const init = {
143+ parentId : head . commitId ,
144+ projectId,
145+ pageId : head . pageId ,
146+ userId,
147+ project,
148+ title,
149+ } ;
159150 const io = await socketIO ( ) ;
160151 const { request } = wrap ( io ) ;
161152
162153 // タイトルのみのページを作る
163- if ( ! persistent ) {
164- parentId = await pushWithRetry ( request , [ { title } ] , {
165- parentId,
166- ...init ,
167- } ) ;
154+ if ( ! head . persistent ) {
155+ const commitId = await pushWithRetry ( request , [ { title } ] , init ) ;
156+ init . parentId = commitId ;
168157 }
169158
170159 try {
171- parentId = await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , {
172- parentId,
173- ...init ,
174- } ) ;
160+ await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , init ) ;
175161 } finally {
176162 io . disconnect ( ) ;
177163 }
@@ -186,28 +172,31 @@ export async function unpin(
186172 title : string ,
187173) : Promise < void > {
188174 const [
189- { id : pageId , commitId : initialCommitId , persistent , pin } ,
175+ head ,
190176 projectId ,
191177 userId ,
192178 ] = await Promise . all ( [
193- ensureEditablePage ( project , title ) ,
179+ pull ( project , title ) ,
194180 getProjectId ( project ) ,
195181 getUserId ( ) ,
196182 ] ) ;
197- let parentId = initialCommitId ;
198183
199184 // 既にピンが外れているか、そもそも存在しないページの場合は何もしない
200- if ( pin == 0 || ! persistent ) return ;
185+ if ( head . pin == 0 || ! head . persistent ) return ;
201186
202- const init = { projectId, pageId, userId, project, title } ;
187+ const init = {
188+ parentId : head . commitId ,
189+ projectId,
190+ pageId : head . pageId ,
191+ userId,
192+ project,
193+ title,
194+ } ;
203195 const io = await socketIO ( ) ;
204196 const { request } = wrap ( io ) ;
205197
206198 try {
207- parentId = await pushWithRetry ( request , [ { pin : 0 } ] , {
208- parentId,
209- ...init ,
210- } ) ;
199+ await pushWithRetry ( request , [ { pin : 0 } ] , init ) ;
211200 } finally {
212201 io . disconnect ( ) ;
213202 }
0 commit comments