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,9 +36,22 @@ func apiESubmitHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
panic(err)
}
GLOBAL_DB.Where("user_id = ?", )
for _, entry := range entries {
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)
}
}
}
func apiEQueryHandler(w http.ResponseWriter, r *http.Request) {
@ -75,8 +88,10 @@ func apiEBootstrapHandler(w http.ResponseWriter, r *http.Request) {
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) {
@ -144,6 +159,6 @@ func main() {
http.HandleFunc("/api/v1/esubmit", apiESubmitHandler)
http.HandleFunc("/api/v1/equery", apiEQueryHandler)
http.HandleFunc("/api/v1/ebootstrap", apiEBootstrapHandler)
http.HandleFunc("/api/v1/eregister", apiERegister)
http.HandleFunc("/api/v1/eregister", apiERegisterHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}

View File

@ -135,6 +135,19 @@ func TestESubmitThenQuery(t *testing.T) {
InitDB()
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
entry, err := shared.BuildHistoryEntry([]string{"unused", "saveHistoryEntry", "120", " 123 ls / ", "1641774958326745663"})
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)
}
// Bootstrap handler should return 3 entries, one for each device
// Bootstrap handler should return 2 entries, one for each device
w = httptest.NewRecorder()
searchReq = httptest.NewRequest(http.MethodGet, "/?user_id="+shared.UserId("key"), nil)
apiEBootstrapHandler(w, searchReq)
@ -211,8 +224,8 @@ func TestESubmitThenQuery(t *testing.T) {
data, err = ioutil.ReadAll(res.Body)
shared.Check(t, err)
shared.Check(t, json.Unmarshal(data, &retrievedEntries))
if len(retrievedEntries) != 3 {
t.Fatalf("Expected to retrieve 3 entries, found %d", len(retrievedEntries))
if len(retrievedEntries) != 2 {
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"`
UserId string `json:"user_id"`
Date time.Time `json:"time"`
Id string `json:"id"`
EncryptedId string `json:"id"`
ReadCount int `json:"read_count"`
}
@ -48,13 +48,13 @@ type Device struct {
DeviceId string `json:"device_id"`
}
const (
MESSAGE_TYPE_REQUEST_DUMP = iota
)
// const (
// MESSAGE_TYPE_REQUEST_DUMP = iota
// )
type AsyncMessage struct {
MessageType int `json:"message_type"`
}
// type AsyncMessage struct {
// MessageType int `json:"message_type"`
// }
const (
HISHTORY_PATH = ".hishtory"
@ -145,7 +145,7 @@ func EncryptHistoryEntry(userSecret string, entry HistoryEntry) (EncHistoryEntry
Nonce: nonce,
UserId: UserId(userSecret),
Date: time.Now(),
Id: uuid.Must(uuid.NewRandom()).String(),
EncryptedId: uuid.Must(uuid.NewRandom()).String(),
ReadCount: 0,
}, nil
}
@ -196,6 +196,7 @@ func OpenLocalSqliteDb() (*gorm.DB, error) {
}
db.AutoMigrate(&HistoryEntry{})
db.AutoMigrate(&EncHistoryEntry{})
db.AutoMigrate(&Device{})
return db, nil
}