1
1
import { socketIO , wrap } from "../../deps/socket.ts" ;
2
2
import { getProjectId , getUserId } from "./id.ts" ;
3
3
import { makeChanges } from "./makeChanges.ts" ;
4
+ import { pull } from "./pull.ts" ;
4
5
import { pinNumber } from "./pin.ts" ;
5
6
import 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" ;
8
8
9
9
/** 指定したページを削除する
10
10
*
@@ -16,23 +16,22 @@ export async function deletePage(
16
16
title : string ,
17
17
) : Promise < void > {
18
18
const [
19
- { id : pageId , commitId : initialCommitId , persistent } ,
19
+ { pageId, commitId : parentId , persistent } ,
20
20
projectId ,
21
21
userId ,
22
22
] = await Promise . all ( [
23
- ensureEditablePage ( project , title ) ,
23
+ pull ( project , title ) ,
24
24
getProjectId ( project ) ,
25
25
getUserId ( ) ,
26
26
] ) ;
27
- let parentId = initialCommitId ;
28
27
29
28
if ( ! persistent ) return ;
30
29
31
30
const io = await socketIO ( ) ;
32
31
const { request } = wrap ( io ) ;
33
32
34
33
try {
35
- parentId = await pushWithRetry ( request , [ { deleted : true } ] , {
34
+ await pushWithRetry ( request , [ { deleted : true } ] , {
36
35
projectId,
37
36
pageId,
38
37
parentId,
@@ -59,23 +58,16 @@ export async function patch(
59
58
update : ( lines : Line [ ] ) => string [ ] | Promise < string [ ] > ,
60
59
) : Promise < void > {
61
60
const [
62
- page ,
61
+ head_ ,
63
62
projectId ,
64
63
userId ,
65
64
] = await Promise . all ( [
66
- ensureEditablePage ( project , title ) ,
65
+ pull ( project , title ) ,
67
66
getProjectId ( project ) ,
68
67
getUserId ( ) ,
69
68
] ) ;
70
69
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_ ;
79
71
80
72
const io = await socketIO ( ) ;
81
73
try {
@@ -91,7 +83,7 @@ export async function patch(
91
83
await pushCommit ( request , changes , {
92
84
parentId : head . commitId ,
93
85
projectId,
94
- pageId,
86
+ pageId : head . pageId ,
95
87
userId,
96
88
} ) ;
97
89
break ;
@@ -103,14 +95,7 @@ export async function patch(
103
95
"Faild to push a commit. Retry after pulling new commits" ,
104
96
) ;
105
97
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 ) ;
114
99
} catch ( e : unknown ) {
115
100
throw e ;
116
101
}
@@ -142,36 +127,37 @@ export async function pin(
142
127
option ?: PinOption ,
143
128
) : Promise < void > {
144
129
const [
145
- { id : pageId , commitId : initialCommitId , persistent , pin } ,
130
+ head ,
146
131
projectId ,
147
132
userId ,
148
133
] = await Promise . all ( [
149
- ensureEditablePage ( project , title ) ,
134
+ pull ( project , title ) ,
150
135
getProjectId ( project ) ,
151
136
getUserId ( ) ,
152
137
] ) ;
153
- let parentId = initialCommitId ;
154
138
155
139
// 既にピン留めされている場合は何もしない
156
- if ( pin > 0 || ( ! persistent && ! ( option ?. create ?? false ) ) ) return ;
140
+ if ( head . pin > 0 || ( ! head . persistent && ! ( option ?. create ?? false ) ) ) return ;
157
141
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
+ } ;
159
150
const io = await socketIO ( ) ;
160
151
const { request } = wrap ( io ) ;
161
152
162
153
// タイトルのみのページを作る
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 ;
168
157
}
169
158
170
159
try {
171
- parentId = await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , {
172
- parentId,
173
- ...init ,
174
- } ) ;
160
+ await pushWithRetry ( request , [ { pin : pinNumber ( ) } ] , init ) ;
175
161
} finally {
176
162
io . disconnect ( ) ;
177
163
}
@@ -186,28 +172,31 @@ export async function unpin(
186
172
title : string ,
187
173
) : Promise < void > {
188
174
const [
189
- { id : pageId , commitId : initialCommitId , persistent , pin } ,
175
+ head ,
190
176
projectId ,
191
177
userId ,
192
178
] = await Promise . all ( [
193
- ensureEditablePage ( project , title ) ,
179
+ pull ( project , title ) ,
194
180
getProjectId ( project ) ,
195
181
getUserId ( ) ,
196
182
] ) ;
197
- let parentId = initialCommitId ;
198
183
199
184
// 既にピンが外れているか、そもそも存在しないページの場合は何もしない
200
- if ( pin == 0 || ! persistent ) return ;
185
+ if ( head . pin == 0 || ! head . persistent ) return ;
201
186
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
+ } ;
203
195
const io = await socketIO ( ) ;
204
196
const { request } = wrap ( io ) ;
205
197
206
198
try {
207
- parentId = await pushWithRetry ( request , [ { pin : 0 } ] , {
208
- parentId,
209
- ...init ,
210
- } ) ;
199
+ await pushWithRetry ( request , [ { pin : 0 } ] , init ) ;
211
200
} finally {
212
201
io . disconnect ( ) ;
213
202
}
0 commit comments