@@ -133,7 +133,11 @@ func NewJournal(pk crypto.PrivKey, username string, bus event.Bus, fs qfs.Filesy
133133
134134 if err := book .load (ctx ); err != nil {
135135 if err == ErrNotFound {
136- err = book .initialize (ctx )
136+ keyID , err := profile .KeyIDFromPriv (book .pk )
137+ if err != nil {
138+ return nil , err
139+ }
140+ err = book .initialize (ctx , keyID )
137141 return book , err
138142 }
139143 return nil , err
@@ -145,23 +149,51 @@ func NewJournal(pk crypto.PrivKey, username string, bus event.Bus, fs qfs.Filesy
145149 return book , nil
146150}
147151
148- func (book * Book ) initialize (ctx context.Context ) error {
149- keyID , err := profile .KeyIDFromPriv (book .pk )
150- if err != nil {
151- return err
152+ // NewJournalOverwriteWithProfileID initializes a new logbook using the
153+ // given profileID. Any existing logbook will be overwritten.
154+ func NewJournalOverwriteWithProfileID (pk crypto.PrivKey , username string , bus event.Bus , fs qfs.Filesystem , location , profileID string ) (* Book , error ) {
155+ ctx := context .Background ()
156+ if pk == nil {
157+ return nil , fmt .Errorf ("logbook: private key is required" )
158+ }
159+ if fs == nil {
160+ return nil , fmt .Errorf ("logbook: filesystem is required" )
161+ }
162+ if location == "" {
163+ return nil , fmt .Errorf ("logbook: location is required" )
164+ }
165+ if profileID == "" {
166+ return nil , fmt .Errorf ("logbook: profileID is required" )
167+ }
168+ if bus == nil {
169+ return nil , fmt .Errorf ("logbook: event.Bus is required" )
152170 }
153171
172+ book := & Book {
173+ store : & oplog.Journal {},
174+ fs : fs ,
175+ pk : pk ,
176+ authorName : username ,
177+ fsLocation : location ,
178+ publisher : bus ,
179+ }
180+
181+ err := book .initialize (ctx , profileID )
182+ return book , err
183+ }
184+
185+ func (book * Book ) initialize (ctx context.Context , authorID string ) error {
154186 // initialize author's log of user actions
155187 userActions := oplog .InitLog (oplog.Op {
156188 Type : oplog .OpTypeInit ,
157189 Model : AuthorModel ,
158190 Name : book .Username (),
159- AuthorID : keyID ,
191+ AuthorID : authorID ,
160192 Timestamp : NewTimestamp (),
161193 })
162194 book .authorID = userActions .ID ()
163195
164- if err = book .store .MergeLog (ctx , userActions ); err != nil {
196+ if err : = book .store .MergeLog (ctx , userActions ); err != nil {
165197 return err
166198 }
167199 if al , ok := book .store .(oplog.AuthorLogstore ); ok {
@@ -215,6 +247,16 @@ func (book *Book) DeleteAuthor() error {
215247 return fmt .Errorf ("not finished" )
216248}
217249
250+ // ReplaceAll replaces the contents of the logbook with
251+ // the provided log data
252+ func (book * Book ) ReplaceAll (ctx context.Context , lg * oplog.Log ) error {
253+ err := book .store .ReplaceAll (ctx , lg )
254+ if err != nil {
255+ return err
256+ }
257+ return book .save (ctx )
258+ }
259+
218260// save writes the book to book.fsLocation
219261func (book * Book ) save (ctx context.Context ) (err error ) {
220262 if al , ok := book .store .(oplog.AuthorLogstore ); ok {
0 commit comments