Files
CollabTable/CollabTableServer/models.go
T
Gabriel20xx cac2faae2e
Docker / Docker Build & Push (push) Successful in 10m21s
Lint / Lint (push) Failing after 14s
Tests / Go backend (push) Successful in 21s
Tests / Dashboard checks (push) Successful in 21s
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
2026-08-03 18:07:40 +02:00

126 lines
3.4 KiB
Go

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"`
}