feat: initialize CollabTableServer with models, sync, and notification handling
- Add go.mod and go.sum for dependency management - Implement models for List, Field, Item, ItemValue, SyncRequest, SyncResponse, and NotificationEvent - Create notification handling functions including enqueueNotification and ignoreNotificationError - Implement sync functionality to handle data synchronization between client and server - Add web data handling and notification polling endpoints - Include tests for timestamp normalization and sync logic - Embed web assets for serving the frontend application
This commit is contained in:
1 parent
e12940fef0
commit
cac2faae2e
41 files changed
+2535
-5502
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Timestamp accepts the millisecond timestamps emitted by the Android client as
|
||||
// well as the older string/second-based representations accepted by the Node
|
||||
// server. It marshals back as a JSON number for the mobile client.
|
||||
type Timestamp int64
|
||||
|
||||
func (t *Timestamp) UnmarshalJSON(data []byte) error {
|
||||
value := bytes.TrimSpace(data)
|
||||
if len(value) == 0 || bytes.Equal(value, []byte("null")) {
|
||||
*t = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
if value[0] == '"' {
|
||||
var stringValue string
|
||||
if err := json.Unmarshal(value, &stringValue); err != nil {
|
||||
return err
|
||||
}
|
||||
parsed, err := parseTimestamp(stringValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = Timestamp(parsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseFloat(string(value), 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid timestamp %q: %w", string(value), err)
|
||||
}
|
||||
*t = Timestamp(parsed)
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseTimestamp(value string) (int64, error) {
|
||||
if numeric, err := strconv.ParseFloat(value, 64); err == nil {
|
||||
return int64(numeric), nil
|
||||
}
|
||||
return 0, fmt.Errorf("invalid timestamp %q", value)
|
||||
}
|
||||
|
||||
type List struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt Timestamp `json:"createdAt"`
|
||||
UpdatedAt Timestamp `json:"updatedAt"`
|
||||
IsDeleted bool `json:"isDeleted"`
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
FieldType string `json:"fieldType"`
|
||||
FieldOptions string `json:"fieldOptions"`
|
||||
Alignment string `json:"alignment"`
|
||||
ListID string `json:"listId"`
|
||||
Order int `json:"order"`
|
||||
CreatedAt Timestamp `json:"createdAt"`
|
||||
UpdatedAt Timestamp `json:"updatedAt"`
|
||||
IsDeleted bool `json:"isDeleted"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
ID string `json:"id"`
|
||||
ListID string `json:"listId"`
|
||||
CreatedAt Timestamp `json:"createdAt"`
|
||||
UpdatedAt Timestamp `json:"updatedAt"`
|
||||
IsDeleted bool `json:"isDeleted"`
|
||||
}
|
||||
|
||||
type ItemValue struct {
|
||||
ID string `json:"id"`
|
||||
ItemID string `json:"itemId"`
|
||||
FieldID string `json:"fieldId"`
|
||||
Value string `json:"value"`
|
||||
UpdatedAt Timestamp `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type SyncRequest struct {
|
||||
LastSyncTimestamp Timestamp `json:"lastSyncTimestamp"`
|
||||
Lists []List `json:"lists"`
|
||||
Fields []Field `json:"fields"`
|
||||
Items []Item `json:"items"`
|
||||
ItemValues []ItemValue `json:"itemValues"`
|
||||
DeviceID string `json:"deviceId,omitempty"`
|
||||
}
|
||||
|
||||
type SyncResponse struct {
|
||||
Lists []List `json:"lists"`
|
||||
Fields []Field `json:"fields"`
|
||||
Items []Item `json:"items"`
|
||||
ItemValues []ItemValue `json:"itemValues"`
|
||||
OrphanItemValues []OrphanValue `json:"orphanItemValues"`
|
||||
ServerTimestamp int64 `json:"serverTimestamp"`
|
||||
}
|
||||
|
||||
type OrphanValue struct {
|
||||
ID string `json:"id"`
|
||||
ItemID string `json:"itemId"`
|
||||
FieldID string `json:"fieldId"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type NotificationEvent struct {
|
||||
ID string `json:"id"`
|
||||
DeviceIDOrigin string `json:"deviceIdOrigin,omitempty"`
|
||||
EventType string `json:"eventType"`
|
||||
EntityType string `json:"entityType"`
|
||||
EntityID string `json:"entityId,omitempty"`
|
||||
ListID string `json:"listId,omitempty"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
}
|
||||
|
||||
type PollResponse struct {
|
||||
Notifications []NotificationEvent `json:"notifications"`
|
||||
ServerTimestamp int64 `json:"serverTimestamp"`
|
||||
}
|
||||
Reference in new issue
Block a user