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" ;
77import { ensureEditablePage , pushCommit , pushWithRetry } from "./_fetch.ts" ;
88
99/** 指定したページを削除する
@@ -59,23 +59,16 @@ export async function patch(
5959 update : ( lines : Line [ ] ) => string [ ] | Promise < string [ ] > ,
6060) : Promise < void > {
6161 const [
62- page ,
62+ head_ ,
6363 projectId ,
6464 userId ,
6565 ] = await Promise . all ( [
66- ensureEditablePage ( project , title ) ,
66+ pull ( project , title ) ,
6767 getProjectId ( project ) ,
6868 getUserId ( ) ,
6969 ] ) ;
7070
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 ;
71+ let head = head_ ;
7972
8073 const io = await socketIO ( ) ;
8174 try {
@@ -91,7 +84,7 @@ export async function patch(
9184 await pushCommit ( request , changes , {
9285 parentId : head . commitId ,
9386 projectId,
94- pageId,
87+ pageId : head . pageId ,
9588 userId,
9689 } ) ;
9790 break ;
@@ -103,14 +96,7 @@ export async function patch(
10396 "Faild to push a commit. Retry after pulling new commits" ,
10497 ) ;
10598 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- } ;
99+ head = await pull ( project , title ) ;
114100 } catch ( e : unknown ) {
115101 throw e ;
116102 }
@@ -142,36 +128,37 @@ export async function pin(
142128 option ?: PinOption ,
143129) : Promise < void > {
144130 const [
145- { id : pageId , commitId : initialCommitId , persistent , pin } ,
131+ head ,
146132 projectId ,
147133 userId ,
148134 ] = await Promise . all ( [
149- ensureEditablePage ( project , title ) ,
135+ pull ( project , title ) ,
150136 getProjectId ( project ) ,
151137 getUserId ( ) ,
152138 ] ) ;
153- let parentId = initialCommitId ;
154139
155140 // 既にピン留めされている場合は何もしない
156- if ( pin > 0 || ( ! persistent && ! ( option ?. create ?? false ) ) ) return ;
141+ if ( head . pin > 0 || ( ! head . persistent && ! ( option ?. create ?? false ) ) ) return ;
157142
158- const init = { projectId, pageId, userId, project, title } ;
143+ const init = {
144+ parentId : head . commitId ,
145+ projectId,
146+ pageId : head . pageId ,
147+ userId,
148+ project,
149+ title,
150+ } ;
159151 const io = await socketIO ( ) ;
160152 const { request } = wrap ( io ) ;
161153
162154 // タイトルのみのページを作る
163- if ( ! persistent ) {
164- parentId = await pushWithRetry ( request , [ { title } ] , {
165- parentId,
166- ...init ,
167- } ) ;
155+ if ( ! head . persistent ) {
156+ const commitId = await pushWithRetry ( request , [ { title } ] , init ) ;
157+ init . parentId = commitId ;
168158 }
169159
170160 try {
171- parentId = await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , {
172- parentId,
173- ...init ,
174- } ) ;
161+ await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , init ) ;
175162 } finally {
176163 io . disconnect ( ) ;
177164 }
@@ -186,28 +173,31 @@ export async function unpin(
186173 title : string ,
187174) : Promise < void > {
188175 const [
189- { id : pageId , commitId : initialCommitId , persistent , pin } ,
176+ head ,
190177 projectId ,
191178 userId ,
192179 ] = await Promise . all ( [
193- ensureEditablePage ( project , title ) ,
180+ pull ( project , title ) ,
194181 getProjectId ( project ) ,
195182 getUserId ( ) ,
196183 ] ) ;
197- let parentId = initialCommitId ;
198184
199185 // 既にピンが外れているか、そもそも存在しないページの場合は何もしない
200- if ( pin == 0 || ! persistent ) return ;
186+ if ( head . pin == 0 || ! head . persistent ) return ;
201187
202- const init = { projectId, pageId, userId, project, title } ;
188+ const init = {
189+ parentId : head . commitId ,
190+ projectId,
191+ pageId : head . pageId ,
192+ userId,
193+ project,
194+ title,
195+ } ;
203196 const io = await socketIO ( ) ;
204197 const { request } = wrap ( io ) ;
205198
206199 try {
207- parentId = await pushWithRetry ( request , [ { pin : 0 } ] , {
208- parentId,
209- ...init ,
210- } ) ;
200+ await pushWithRetry ( request , [ { pin : 0 } ] , init ) ;
211201 } finally {
212202 io . disconnect ( ) ;
213203 }
0 commit comments