adapted to have it duplicate entries into per-device entries server-side

This commit is contained in:
David Dworken 2022-04-03 10:08:18 -07:00
parent 0a3d60769c
commit 2a3887b9ed
3 changed files with 44 additions and 15 deletions

View File

@ -36,8 +36,21 @@ func apiESubmitHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
GLOBAL_DB.Where("user_id = ?", )
for _, entry := range entries { for _, entry := range entries {
GLOBAL_DB.Create(&entry) tx := GLOBAL_DB.Where("user_id = ?", entry.UserId)
var devices []*shared.Device;
result := tx.Find(&devices)
if result.Error != nil {
panic(fmt.Errorf("DB query error: %v", result.Error))
}
if len(devices) == 0{
panic(fmt.Errorf("Found no devices associated with user_id=%s, can't save history entry!", entry.UserId))
}
for _, device := range devices {
entry.DeviceId = device.DeviceId;
GLOBAL_DB.Create(&entry)
}
} }
} }
@ -75,8 +88,10 @@ func apiEBootstrapHandler(w http.ResponseWriter, r *http.Request) {
w.Write(resp) w.Write(resp)
} }
func apiERegister(w http.ResponseWriter, r *http.Request) { func apiERegisterHandler(w http.ResponseWriter, r *http.Request) {
userId := r.URL.Query().Get("user_id")
deviceId := r.URL.Query().Get("device_id")
GLOBAL_DB.Create(&shared.Device{UserId: userId, DeviceId: deviceId})
} }
func OpenDB() (*gorm.DB, error) { func OpenDB() (*gorm.DB, error) {
@ -144,6 +159,6 @@ func main() {
http.HandleFunc("/api/v1/esubmit", apiESubmitHandler) http.HandleFunc("/api/v1/esubmit", apiESubmitHandler)
http.HandleFunc("/api/v1/equery", apiEQueryHandler) http.HandleFunc("/api/v1/equery", apiEQueryHandler)
http.HandleFunc("/api/v1/ebootstrap", apiEBootstrapHandler) http.HandleFunc("/api/v1/ebootstrap", apiEBootstrapHandler)
http.HandleFunc("/api/v1/eregister", apiERegister) http.HandleFunc("/api/v1/eregister", apiERegisterHandler)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
} }

View File

@ -135,6 +135,19 @@ func TestESubmitThenQuery(t *testing.T) {
InitDB() InitDB()
shared.Check(t, shared.Setup(0, []string{})) shared.Check(t, shared.Setup(0, []string{}))
// Register a few devices
userId := shared.UserId("key")
devId1 := shared.DeviceId("key", 1)
devId2 := shared.DeviceId("key", 2)
otherUser := shared.UserId("otherkey")
otherDev := shared.DeviceId("otherkey", 1)
deviceReq := httptest.NewRequest(http.MethodGet, "/?device_id="+devId1+"&user_id="+userId, nil)
apiERegisterHandler(nil, deviceReq)
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+devId2+"&user_id="+userId, nil)
apiERegisterHandler(nil, deviceReq)
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+otherDev+"&user_id="+otherUser, nil)
apiERegisterHandler(nil, deviceReq)
// Submit a few entries for different devices // Submit a few entries for different devices
entry, err := shared.BuildHistoryEntry([]string{"unused", "saveHistoryEntry", "120", " 123 ls / ", "1641774958326745663"}) entry, err := shared.BuildHistoryEntry([]string{"unused", "saveHistoryEntry", "120", " 123 ls / ", "1641774958326745663"})
shared.Check(t, err) shared.Check(t, err)
@ -202,7 +215,7 @@ func TestESubmitThenQuery(t *testing.T) {
t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, *entry) t.Fatalf("DB data is different than input! \ndb =%#v\ninput=%#v", *dbEntry, *entry)
} }
// Bootstrap handler should return 3 entries, one for each device // Bootstrap handler should return 2 entries, one for each device
w = httptest.NewRecorder() w = httptest.NewRecorder()
searchReq = httptest.NewRequest(http.MethodGet, "/?user_id="+shared.UserId("key"), nil) searchReq = httptest.NewRequest(http.MethodGet, "/?user_id="+shared.UserId("key"), nil)
apiEBootstrapHandler(w, searchReq) apiEBootstrapHandler(w, searchReq)
@ -211,8 +224,8 @@ func TestESubmitThenQuery(t *testing.T) {
data, err = ioutil.ReadAll(res.Body) data, err = ioutil.ReadAll(res.Body)
shared.Check(t, err) shared.Check(t, err)
shared.Check(t, json.Unmarshal(data, &retrievedEntries)) shared.Check(t, json.Unmarshal(data, &retrievedEntries))
if len(retrievedEntries) != 3 { if len(retrievedEntries) != 2 {
t.Fatalf("Expected to retrieve 3 entries, found %d", len(retrievedEntries)) t.Fatalf("Expected to retrieve 2 entries, found %d", len(retrievedEntries))
} }
} }

View File

@ -39,7 +39,7 @@ type EncHistoryEntry struct {
DeviceId string `json:"device_id"` DeviceId string `json:"device_id"`
UserId string `json:"user_id"` UserId string `json:"user_id"`
Date time.Time `json:"time"` Date time.Time `json:"time"`
Id string `json:"id"` EncryptedId string `json:"id"`
ReadCount int `json:"read_count"` ReadCount int `json:"read_count"`
} }
@ -48,13 +48,13 @@ type Device struct {
DeviceId string `json:"device_id"` DeviceId string `json:"device_id"`
} }
const ( // const (
MESSAGE_TYPE_REQUEST_DUMP = iota // MESSAGE_TYPE_REQUEST_DUMP = iota
) // )
type AsyncMessage struct { // type AsyncMessage struct {
MessageType int `json:"message_type"` // MessageType int `json:"message_type"`
} // }
const ( const (
HISHTORY_PATH = ".hishtory" HISHTORY_PATH = ".hishtory"
@ -145,7 +145,7 @@ func EncryptHistoryEntry(userSecret string, entry HistoryEntry) (EncHistoryEntry
Nonce: nonce, Nonce: nonce,
UserId: UserId(userSecret), UserId: UserId(userSecret),
Date: time.Now(), Date: time.Now(),
Id: uuid.Must(uuid.NewRandom()).String(), EncryptedId: uuid.Must(uuid.NewRandom()).String(),
ReadCount: 0, ReadCount: 0,
}, nil }, nil
} }
@ -196,6 +196,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
} }
db.AutoMigrate(&HistoryEntry{}) db.AutoMigrate(&HistoryEntry{})
db.AutoMigrate(&EncHistoryEntry{}) db.AutoMigrate(&EncHistoryEntry{})
db.AutoMigrate(&Device{})
return db, nil return db, nil
} }