コード例 #1
0
ファイル: contacts.go プロジェクト: tstranex/carpcomm
func stationContactsHandler(
	cdb *db.ContactDB, sdb *db.StationDB,
	w http.ResponseWriter, r *http.Request, user userView) {

	id := r.URL.Query().Get("id")
	if id == "" {
		http.Error(w, "'id' param missing", http.StatusBadRequest)
		return
	}

	s, err := sdb.Lookup(id)
	if err != nil {
		log.Printf("Sation DB lookup error: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
	if s == nil {
		http.NotFound(w, r)
		return
	}

	if s.Userid == nil || user.Id != *s.Userid {
		http.Error(w, "Unauthorized", http.StatusUnauthorized)
		return
	}

	contacts, err := cdb.SearchByStationId(id, 100)
	if err != nil {
		log.Printf("SearchByStationId error: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	var cv contactsView
	cv.Title = *s.Name
	cv.C = contacts

	c := NewRenderContext(user, cv)
	err = contactsTemplate.Get().ExecuteTemplate(w, "contacts.html", c)
	if err != nil {
		log.Printf("Error rendering contacts view: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return
	}
}
コード例 #2
0
ファイル: stations.go プロジェクト: tstranex/carpcomm
func GetStationContext(s *pb.Station, userid string,
	m *rpc.Client, userdb *db.UserDB, cdb *db.ContactDB) (
	sc stationContext) {
	sc.S = s
	sc.IsOwner = (userid == *s.Userid)

	var args mux.StationStatusArgs
	args.StationId = *s.Id
	var status mux.StationStatusResult
	err := m.Call("Coordinator.StationStatus", args, &status)
	if err != nil {
		sc.IsOnline = false
	} else {
		sc.IsOnline = status.IsConnected
	}

	sc.CurrentTime = time.Now().UTC().Format(TimeFormat)
	passes, _ := scheduler.PassPredictions(s)
	passViews := make([]predictionView, 0)
	for _, pass := range passes {
		var pv predictionView
		FillPredictionView(pass, &pv)
		// Don't display different modes of the same satellite.
		if len(passViews) == 0 || passViews[len(passViews)-1] != pv {
			passViews = append(passViews, pv)
		}
	}
	sc.NextPasses = passViews

	sc.Operator = LookupUserView(userdb, *s.Userid)

	if sc.IsOwner {
		c, err := cdb.SearchByStationId(*s.Id, 5)
		if err != nil {
			log.Printf("SearchByStationId error: %s", err.Error())
			// This is not a fatal error.
		}
		sc.Contacts = c
	}

	return sc
}