Ejemplo n.º 1
0
func handleSubmit(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	userid, sessionid, err := getSession(c, r)
	if err != nil {
		sendError(w, r, "No session cookie")
		return
	}

	blob, err := ioutil.ReadAll(r.Body)
	if err != nil {
		sendError(w, r, "Error reading request body")
		return
	}
	r.Body.Close()

	s := newStore(c)
	g := grapher.NewGrapher(userid, schema, s, s, nil)
	s.SetGrapher(g)
	tf.NewTransformer(g)
	tf.NewMapTransformer(g)
	tf.NewLatestTransformer(g)
	newChannelAPI(c, s, userid, sessionid, false, g)

	//  log.Printf("Received: %v", string(blob))
	node, e := g.HandleClientBlob(blob)
	if e != nil {
		fmt.Fprintf(w, `{"ok":false, "error":"%v"}`, e.String())
		return
	}

	if perm, ok := node.(grapher.PermissionNode); ok {
		// Is the user who was granted permissions registered locally?
		usr, err := isLocalUser(c, perm.UserName())
		if err != nil {
			log.Printf("Err in HasUserName: %v", err)
		}
		knownuser := "******"
		if usr == "" {
			knownuser = "******"
		}
		fmt.Fprintf(w, `{"ok":true, "blobref":"%v", "seq":%v, "knownuser":%v}`, perm.BlobRef(), perm.SequenceNumber(), knownuser)
	} else if otnode, ok := node.(grapher.OTNode); ok {
		fmt.Fprintf(w, `{"ok":true, "blobref":"%v", "seq":%v, "time":%v}`, otnode.BlobRef(), otnode.SequenceNumber(), otnode.Time())
	} else if perma, ok := node.(grapher.PermaNode); ok {
		// Add to the inbox of the user who created the permanode
		b := inboxStruct{LastSeq: 0, Archived: true}
		parent := datastore.NewKey("user", userid, 0, nil)
		_, err = datastore.Put(c, datastore.NewKey("inbox", perma.BlobRef(), 0, parent), &b)
		fmt.Fprintf(w, `{"ok":true, "blobref":"%v"}`, node.BlobRef())
	} else {
		fmt.Fprintf(w, `{"ok":true, "blobref":"%v"}`, node.BlobRef())
	}
}
Ejemplo n.º 2
0
func createBook(c appengine.Context, userid string) (perma_blobref string, err os.Error) {
	s := newStore(c)
	g := grapher.NewGrapher(userid, schema, s, s, nil)
	s.SetGrapher(g)

	blob := []byte(`{"type":"permanode", "mimetype":"application/x-lightwave-book"}`)
	var node grapher.AbstractNode
	node, err = g.HandleClientBlob(blob)
	if err == nil {
		perma_blobref = node.BlobRef()
	}
	return
}
Ejemplo n.º 3
0
func handleOpen(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	userid, sessionid, err := getSession(c, r)
	if err != nil {
		sendError(w, r, "No session cookie")
		return
	}
	// Read the request body
	jreq, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "Error reading request body", http.StatusInternalServerError)
		return
	}
	r.Body.Close()
	// Parse request
	var req openCloseRequest
	err = json.Unmarshal(jreq, &req)
	if err != nil {
		sendError(w, r, "Malformed JSON")
		return
	}
	// Load the channel infos
	var ch channelStruct
	if err = datastore.Get(c, datastore.NewKey("channel", userid+"/"+sessionid, 0, nil), &ch); err != nil {
		sendError(w, r, "Unknown channel: "+userid+"/"+sessionid)
		return
	}
	// Check
	if len(ch.OpenPermas) >= 10 {
		sendError(w, r, "Too many open channels")
		return
	}
	is_open := false
	for _, p := range ch.OpenPermas {
		if p == req.Perma {
			is_open = true
			break
		}
	}
	var perma grapher.PermaNode
	if !is_open {
		// Update channel infos
		ch.OpenPermas = append(ch.OpenPermas, req.Perma)
		_, err = datastore.Put(c, datastore.NewKey("channel", userid+"/"+sessionid, 0, nil), &ch)
		if err != nil {
			sendError(w, r, "Internal server error")
			return
		}
		// Repeat all blobs from this document.
		s := newStore(c)
		g := grapher.NewGrapher(userid, schema, s, s, nil)
		s.SetGrapher(g)
		ch := newChannelAPI(c, s, userid, sessionid, true, g)
		perma, err = g.Repeat(req.Perma, req.From)
		if err != nil {
			sendError(w, r, "Failed opening")
			return
		}
		fmt.Fprintf(w, `{"ok":true, "blobs":[%v]}`, strings.Join(ch.messageBuffer, ","))
	} else {
		fmt.Fprint(w, `{"ok":true, "blobs":[]}`)
	}

	if req.MarkAsRead {
		if perma == nil {
			s := newStore(c)
			data, err := s.GetPermaNode(req.Perma)
			if err != nil {
				log.Printf("Err: Failed reading permanode")
				return
			}
			perma = grapher.NewPermaNode(nil)
			perma.FromMap(req.Perma, data)
		}
		markAsRead(c, userid, perma.BlobRef(), perma.SequenceNumber()-1)
	}
}
Ejemplo n.º 4
0
func TestFederation(t *testing.T) {
	ns := &dummyNameService{}
	store1 := NewSimpleBlobStore()
	store2 := NewSimpleBlobStore()
	store3 := NewSimpleBlobStore()
	store4 := NewSimpleBlobStore()
	sg1 := grapher.NewSimpleGraphStore()
	sg2 := grapher.NewSimpleGraphStore()
	sg3 := grapher.NewSimpleGraphStore()
	sg4 := grapher.NewSimpleGraphStore()
	fed1 := NewFederation("a@alice", "alice", 8181, http.DefaultServeMux, ns, store1)
	fed2 := NewFederation("b@bob", "bob", 8181, http.DefaultServeMux, ns, store2)
	fed3 := NewFederation("c@charly", "charly", 8181, http.DefaultServeMux, ns, store3)
	fed4 := NewFederation("d@daisy", "daisy", 8181, http.DefaultServeMux, ns, store4)
	grapher1 := grapher.NewGrapher("a@alice", store1, sg1, fed1)
	grapher2 := grapher.NewGrapher("b@bob", store2, sg2, fed2)
	grapher3 := grapher.NewGrapher("c@charly", store3, sg3, fed3)
	grapher4 := grapher.NewGrapher("d@daisy", store4, sg4, fed4)
	store1.AddListener(grapher1)
	store2.AddListener(grapher2)
	store3.AddListener(grapher3)
	store4.AddListener(grapher4)
	app1 := &dummyAPI{"a@alice", grapher1, t}
	grapher1.SetAPI(app1)
	app2 := &dummyAPI{"b@bob", grapher2, t}
	grapher2.SetAPI(app2)
	app3 := &dummyAPI{"c@charly", grapher3, t}
	grapher3.SetAPI(app3)
	app4 := &dummyAPI{"d@daisy", grapher4, t}
	grapher4.SetAPI(app4)
	go listen(t)

	time.Sleep(1000000000 * 2)

	blob1 := []byte(`{"type":"permanode", "signer":"a@alice", "random":"perma1abc"}`)
	blobref1 := NewBlobRef(blob1)
	blob1b := []byte(`{"type":"keep", "signer":"a@alice", "perma":"` + blobref1 + `"}`)
	blobref1b := NewBlobRef(blob1b)
	blob1c := []byte(`{"type":"entity", "signer":"a@b", "perma":"` + blobref1 + `", "content":"", "dep":["` + blobref1b + `"]}`)
	blobref1c := NewBlobRef(blob1c)
	blob2 := []byte(`{"type":"mutation", "signer":"a@alice", "perma":"` + blobref1 + `", "dep":["` + blobref1c + `"], "op":{"$t":["Hello World"]}, "entity":"` + blobref1c + `"}`)
	blobref2 := NewBlobRef(blob2)
	blob3 := []byte(`{"type":"mutation", "signer":"a@alice", "perma":"` + blobref1 + `", "dep":["` + blobref1c + `"], "op":{"$t":["Olla!!"]}, "entity":"` + blobref1c + `"}`)
	blobref3 := NewBlobRef(blob3)
	blob4 := []byte(`{"type":"mutation", "signer":"a@alice", "perma":"` + blobref1 + `", "dep":["` + blobref2 + `"], "op":{"$t":[{"$s":11}, "??"]}, "entity":"` + blobref1c + `"}`)
	blobref4 := NewBlobRef(blob4)
	// Grant user foo@bar read access
	blob5 := []byte(`{"type":"permission", "signer":"a@alice", "action":"invite", "perma":"` + blobref1 + `", "dep":["` + blobref4 + `"], "user":"******", "allow":` + fmt.Sprintf("%v", grapher.Perm_Read) + `, "deny":0}`)
	blobref5 := NewBlobRef(blob5)
	blob7 := []byte(`{"type":"mutation", "signer":"a@alice", "perma":"` + blobref1 + `", "dep":["` + blobref2 + `"], "op":{"$t":[{"$s":11}, "!!"]}, "entity":"` + blobref1c + `"}`)
	blobref7 := NewBlobRef(blob7)
	blob8 := []byte(`{"type":"permission", "signer":"a@alice", "action":"invite", "perma":"` + blobref1 + `", "dep":["` + blobref4 + `"], "user":"******", "allow":` + fmt.Sprintf("%v", grapher.Perm_Read) + `, "deny":0}`)
	blobref8 := NewBlobRef(blob8)
	blob9 := []byte(`{"type":"permission", "signer":"a@alice", "action":"invite", "perma":"` + blobref1 + `", "dep":["` + blobref4 + `"], "user":"******", "allow":` + fmt.Sprintf("%v", grapher.Perm_Read) + `, "deny":0}`)
	blobref9 := NewBlobRef(blob9)

	log.Printf("Hashes:\n1: %v\n1b: %v\n2: %v\n3: %v\n4: %v\n5: %v\n7: %v\n8: %v\n", blobref1, blobref1b, blobref2, blobref3, blobref4, blobref5, blobref7, blobref8)

	// Insert them in the wrong order
	store1.StoreBlob(blob1, blobref1)
	store1.StoreBlob(blob1b, blobref1b)
	store1.StoreBlob(blob1c, blobref1c)
	store1.StoreBlob(blob2, blobref2)
	store1.StoreBlob(blob3, blobref3)
	store1.StoreBlob(blob4, blobref4)
	store1.StoreBlob(blob5, blobref5)
	store1.StoreBlob(blob8, blobref8)

	time.Sleep(1000000000 * 2)

	store1.StoreBlob(blob7, blobref7)
	store1.StoreBlob(blob9, blobref9)

	time.Sleep(1000000000 * 2)

	followers, err := grapher1.Followers(blobref1)
	if err != nil {
		t.Fatal(err.String())
	}
	if len(followers) != 4 {
		t.Fatal("Indexer1 has wrong number of followers")
	}
	followers, err = grapher2.Followers(blobref1)
	if err != nil {
		t.Fatal(err.String())
	}
	if len(followers) != 4 {
		t.Fatal("Indexer2 has wrong number of followers")
	}
	followers, err = grapher3.Followers(blobref1)
	if err != nil {
		t.Fatal(err.String())
	}
	if len(followers) != 4 {
		t.Fatal("Indexer3 has wrong number of followers")
	}
	followers, err = grapher4.Followers(blobref1)
	if err != nil {
		t.Fatal(err.String())
	}
	if len(followers) != 4 {
		t.Fatal("Indexer4 has wrong number of followers")
	}
}