MCPcopy Create free account
hub / github.com/blues/note / hubNotefileAddNote

Function hubNotefileAddNote

lib/hubreq.go:1194–1305  ·  view source on GitHub ↗

Notefile AddNote, which is used only by the (undocumented) "live" note add NotefileIDs is overloaded with the OLC 'where' of the note, if any.

(ctx context.Context, session *HubSession, req notehubMessage, rsp *notehubMessage, event EventFunc)

Source from the content-addressed store, hash-verified

1192// Notefile AddNote, which is used only by the (undocumented) "live" note add
1193// NotefileIDs is overloaded with the OLC 'where' of the note, if any.
1194func hubNotefileAddNote(ctx context.Context, session *HubSession, req notehubMessage, rsp *notehubMessage, event EventFunc) (err error) {
1195 // Open the box
1196 box, ei, _, err2 := openHubNoteboxForDevice(ctx, session, req.DeviceUID, req.DeviceSN, req.ProductUID, req.DeviceEndpointID, event)
1197 if err2 != nil {
1198 err = err2
1199 return
1200 }
1201
1202 // If the notefile doesn't exist, create it
1203 if !box.NotefileExists(req.NotefileID) {
1204 err = box.AddNotefile(ctx, req.NotefileID, nil)
1205 if err != nil {
1206 box.Close(ctx)
1207 return
1208 }
1209 }
1210
1211 // Open the specified notefile
1212 openfile, file, err4 := box.OpenNotefile(ctx, req.NotefileID)
1213 if err4 != nil {
1214 err = err4
1215 box.Close(ctx)
1216 return
1217 }
1218 sawNotefile(session, box, req.NotefileID)
1219
1220 // First, extract the body and payload
1221 var body []byte
1222 bodyObject, err5 := req.GetBody()
1223 if err5 != nil {
1224 err = err5
1225 } else {
1226 body, err = note.JSONMarshal(bodyObject)
1227 }
1228 payload := req.GetPayload()
1229
1230 // See if this is a special note that has a payload that was encoded with a template specified in the body
1231 decodePayloadWithTemplateFlag := uint32(0x00000001)
1232 if err == nil && len(payload) > 0 && (req.PowerSource&decodePayloadWithTemplateFlag) != 0 {
1233
1234 // The template is in the body
1235 binTemplateJSON := body
1236 body = []byte{}
1237 var bulkContext BulkTemplateContext
1238 bulkContext, err = BulkDecodeTemplate(ctx, binTemplateJSON, payload)
1239 if err == nil {
1240 events := []note.Event{}
1241 for {
1242 noteBody, notePayload, when, wherewhen, olc, _, success := bulkContext.BulkDecodeNextEntry(ctx)
1243 if !success {
1244 break
1245 }
1246 event := note.Event{}
1247 event.When = when
1248 if olc != "" {
1249 area, err := golc.Decode(olc)
1250 if err == nil {
1251 event.WhereLat, event.WhereLon = area.Center()

Callers 1

processRequestFunction · 0.85

Calls 15

BulkDecodeNextEntryMethod · 0.95
openHubNoteboxForDeviceFunction · 0.85
sawNotefileFunction · 0.85
BulkDecodeTemplateFunction · 0.85
newHistoryFunction · 0.85
NotefileAttributesFromIDFunction · 0.85
NotefileExistsMethod · 0.80
AddNotefileMethod · 0.80
OpenNotefileMethod · 0.80
GetBodyMethod · 0.80
GetPayloadMethod · 0.80
PurgeTombstonesMethod · 0.80

Tested by

no test coverage detected