github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/model/note/event.go (about) 1 package note 2 3 import ( 4 "strings" 5 6 "github.com/cozy/cozy-stack/model/instance" 7 "github.com/cozy/cozy-stack/pkg/consts" 8 "github.com/cozy/cozy-stack/pkg/couchdb" 9 "github.com/cozy/cozy-stack/pkg/jsonapi" 10 "github.com/cozy/cozy-stack/pkg/realtime" 11 ) 12 13 // Event is a realtime event for a note (like the update of the position of a 14 // pointer). 15 type Event map[string]interface{} 16 17 // ID returns the event qualified identifier 18 func (e Event) ID() string { 19 id, _ := e["_id"].(string) 20 return id 21 } 22 23 // Rev returns the event revision 24 func (e Event) Rev() string { 25 rev, _ := e["_rev"].(string) 26 return rev 27 } 28 29 // DocType returns the document type 30 func (e Event) DocType() string { return consts.NotesEvents } 31 32 // Clone implements couchdb.Doc 33 func (e Event) Clone() couchdb.Doc { 34 cloned := make(Event) 35 for k, v := range e { 36 cloned[k] = v 37 } 38 return cloned 39 } 40 41 // SetID changes the event qualified identifier 42 func (e Event) SetID(id string) { 43 if id == "" { 44 delete(e, "_id") 45 } else { 46 e["_id"] = id 47 } 48 } 49 50 // SetRev changes the event revision 51 func (e Event) SetRev(rev string) { 52 if rev == "" { 53 delete(e, "_rev") 54 } else { 55 e["_rev"] = rev 56 } 57 } 58 59 // Included is part of the jsonapi.Object interface 60 func (e Event) Included() []jsonapi.Object { return nil } 61 62 // Links is part of the jsonapi.Object interface 63 func (e Event) Links() *jsonapi.LinksList { return nil } 64 65 // Relationships is part of the jsonapi.Object interface 66 func (e Event) Relationships() jsonapi.RelationshipMap { return nil } 67 68 func (e Event) publish(inst *instance.Instance) { 69 go realtime.GetHub().Publish(inst, realtime.EventUpdate, e, nil) 70 } 71 72 // PutTelepointer sends the position of a pointer in the realtime hub. 73 func PutTelepointer(inst *instance.Instance, t Event) error { 74 if t["sessionID"] == nil || t["sessionID"] == "" { 75 return ErrMissingSessionID 76 } 77 t["doctype"] = consts.NotesTelepointers 78 t.publish(inst) 79 return nil 80 } 81 82 func publishUpdatedTitle(inst *instance.Instance, fileID, title, sessionID string) { 83 event := Event{ 84 "title": title, 85 "doctype": consts.NotesDocuments, 86 "sessionID": sessionID, 87 } 88 event.SetID(fileID) 89 event.publish(inst) 90 } 91 92 func publishSteps(inst *instance.Instance, fileID string, steps []Step) { 93 for _, s := range steps { 94 e := Event(s) 95 e["doctype"] = s.DocType() 96 e.SetID(fileID) 97 e.publish(inst) 98 } 99 } 100 101 // PublishThumbnail sends information about a resized image. 102 func PublishThumbnail(inst *instance.Instance, event Event) { 103 parts := strings.SplitN(event.ID(), "/", 2) 104 event.SetID(parts[0]) // The note ID is the first part of the image ID 105 event["image_id"] = parts[1] // The image ID is the second part 106 event.publish(inst) 107 } 108 109 var _ jsonapi.Object = &Event{}