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,141 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Server) handleWebData(w http.ResponseWriter, r *http.Request, _ *AuthUser) {
|
||||
lists, err := s.db.queryLists(r.Context(), false, nil)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
|
||||
return
|
||||
}
|
||||
fields, err := s.db.queryFields(r.Context(), false, nil, "")
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
|
||||
return
|
||||
}
|
||||
items, err := s.db.queryItems(r.Context(), false, nil, "")
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
|
||||
return
|
||||
}
|
||||
values, err := s.db.queryValues(r.Context(), nil, "")
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to fetch data")
|
||||
return
|
||||
}
|
||||
|
||||
itemToList := make(map[string]string, len(items))
|
||||
for _, item := range items {
|
||||
itemToList[item.ID] = item.ListID
|
||||
}
|
||||
fieldMax := make(map[string]int64)
|
||||
for _, field := range fields {
|
||||
if int64(field.UpdatedAt) > fieldMax[field.ListID] {
|
||||
fieldMax[field.ListID] = int64(field.UpdatedAt)
|
||||
}
|
||||
}
|
||||
itemMax := make(map[string]int64)
|
||||
for _, item := range items {
|
||||
if int64(item.UpdatedAt) > itemMax[item.ListID] {
|
||||
itemMax[item.ListID] = int64(item.UpdatedAt)
|
||||
}
|
||||
}
|
||||
valueMax := make(map[string]int64)
|
||||
for _, value := range values {
|
||||
listID := itemToList[value.ItemID]
|
||||
if listID != "" && int64(value.UpdatedAt) > valueMax[listID] {
|
||||
valueMax[listID] = int64(value.UpdatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
type webList struct {
|
||||
List
|
||||
EffectiveUpdatedAt int64 `json:"effectiveUpdatedAt"`
|
||||
}
|
||||
webLists := make([]webList, 0, len(lists))
|
||||
for _, list := range lists {
|
||||
effective := int64(list.UpdatedAt)
|
||||
if fieldMax[list.ID] > effective {
|
||||
effective = fieldMax[list.ID]
|
||||
}
|
||||
if itemMax[list.ID] > effective {
|
||||
effective = itemMax[list.ID]
|
||||
}
|
||||
if valueMax[list.ID] > effective {
|
||||
effective = valueMax[list.ID]
|
||||
}
|
||||
webLists = append(webLists, webList{List: list, EffectiveUpdatedAt: effective})
|
||||
}
|
||||
|
||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"lists": webLists,
|
||||
"fields": fields,
|
||||
"items": items,
|
||||
"values": values,
|
||||
"stats": map[string]int{
|
||||
"lists": len(lists),
|
||||
"fields": len(fields),
|
||||
"items": len(items),
|
||||
"values": len(values),
|
||||
},
|
||||
"timestamp": time.Now().UnixMilli(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) handleNotificationPoll(w http.ResponseWriter, r *http.Request) {
|
||||
since, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
||||
if err != nil || since < 0 {
|
||||
since = 0
|
||||
}
|
||||
deviceID := requestDeviceID(r, "")
|
||||
|
||||
query := `
|
||||
SELECT id, deviceidorigin, eventtype, entitytype, entityid, listid, createdat
|
||||
FROM notifications
|
||||
WHERE createdat > $1
|
||||
`
|
||||
args := []any{since}
|
||||
if deviceID != "" {
|
||||
query += ` AND (deviceidorigin IS NULL OR deviceidorigin <> $2)`
|
||||
args = append(args, deviceID)
|
||||
}
|
||||
query += ` ORDER BY createdat ASC LIMIT 500`
|
||||
|
||||
rows, err := s.db.pool.Query(r.Context(), query, args...)
|
||||
if err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
events := make([]NotificationEvent, 0)
|
||||
for rows.Next() {
|
||||
var event NotificationEvent
|
||||
var origin, entityID, listID *string
|
||||
if err := rows.Scan(&event.ID, &origin, &event.EventType, &event.EntityType, &entityID, &listID, &event.CreatedAt); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
|
||||
return
|
||||
}
|
||||
if origin != nil {
|
||||
event.DeviceIDOrigin = *origin
|
||||
}
|
||||
if entityID != nil {
|
||||
event.EntityID = *entityID
|
||||
}
|
||||
if listID != nil {
|
||||
event.ListID = *listID
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
writeJSONError(w, http.StatusInternalServerError, "Failed to poll notifications")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, PollResponse{Notifications: events, ServerTimestamp: time.Now().UnixMilli()})
|
||||
}
|
||||
Reference in new issue
Block a user