func NewsReportAction(w http.ResponseWriter, r *http.Request) {
	header := w.Header()
	header.Set("Content-Type", "application/javascript; charset=utf-8")
	header.Set("X-Content-Type-Options", "nosniff")
	header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
	header.Set("Pragma", "no-cache")
	header.Set("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")

	// fmt.Printf("%#v\n", news)
	output := ApiResponseJson{}
	var params []interface{}
	pi := 0
	offset := r.FormValue("offset")
	length := r.FormValue("rows")
	qsearch := r.FormValue("q")
	if offset == "" {
		offset = "0"
	}
	if length == "" {
		length = "20"
	}
	sql := "RIGHT JOIN `comments` ON `comments`.`news_id` = `news`.`id` WHERE 1 AND `news`.`referral` = \"批踢踢實業坊\" AND delete_time IS NULL ORDER BY `comments`.`created_at` DESC LIMIT ?, ? "
	if qsearch != "" {
		sql = "RIGHT JOIN `comments` ON `comments`.`news_id` = `news`.`id` WHERE 1 AND `news`.`referral` = \"批踢踢實業坊\" AND delete_time IS NULL AND title LIKE ? ORDER BY `comments`.`created_at` DESC LIMIT ?, ? "
		params = make([]interface{}, 3)
		params[pi] = "%" + qsearch + "%"
		pi++
	} else {
		params = make([]interface{}, 2)
	}
	params[pi] = offset
	pi++
	params[pi] = length
	pi++
	rows, err := gatsby.QuerySelectWith(DbConnect, &News{}, sql, params...)
	if err == nil {
		news := News{}
		data, err := gatsby.CreateStructSliceFromRows(&news, rows)
		if err != nil {
			fmt.Println(err)
			output.Error(501, err.Error())
		} else {
			newsList := data.([]News)
			for k, _ := range newsList {
				newsList[k].Raw = ""
				newsList[k].Body = ""
			}
			output.Data = newsList
		}
	} else {
		output.Error(404, err.Error())
	}
	writeResponseJson(w, output, r.FormValue("callback"))

	w.(http.Flusher).Flush()
}
func AuthAction(w http.ResponseWriter, r *http.Request) {

	header := w.Header()
	// header.Set("Content-Type", "text/plain; charset=utf-8")
	header.Set("X-Content-Type-Options", "nosniff")
	header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
	header.Set("Pragma", "no-cache")
	header.Set("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")

	r.ParseForm()
	var salt string
	deviceID := r.FormValue("deviceID")
	regID := r.FormValue("regID")
	output := ApiResponseJson{}
	device := Devices{}

	rows, err := gatsby.QuerySelectWith(DbConnect, &device, "WHERE device_id = ? LIMIT 1", deviceID)
	if err != nil {
		output.Error(501, err.Error())
	} else {
		if data, err := gatsby.CreateStructSliceFromRows(&device, rows); err != nil {
			output.Error(501, err.Error())
		} else {
			device := data.([]Devices)
			if len(device) == 1 {
				if device[0].RegId == regID {
					salt = device[0].SauthSalt
				} else {
					output.Error(401, "")
				}
			} else {
				if salt = WriteDevice(deviceID, regID); salt != "" {
					GCMRegistration(regID)
				} else {
					output.Error(501, "Write device failed")
				}
			}
		}
	}

	if output.NoError() {
		sha256h := sha256.New()
		token := fmt.Sprintf("%d/%d/%d", device.Id, time.Now().Unix(), time.Now().UnixNano())
		io.WriteString(sha256h, token)
		output.Data = token + "/" + hex.EncodeToString(sha256h.Sum(nil))
	}

	writeResponseJson(w, output, r.FormValue("callback"))

	w.(http.Flusher).Flush()
}
func NewsHotestsAction(w http.ResponseWriter, r *http.Request) {

	header := w.Header()
	header.Set("Content-Type", "application/javascript; charset=utf-8")
	header.Set("X-Content-Type-Options", "nosniff")
	header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
	header.Set("Pragma", "no-cache")
	header.Set("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")

	// fmt.Printf("%#v\n", news)
	output := ApiResponseJson{}
	randSource := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
	var news_max int64
	err := DbConnect.QueryRow("SELECT MAX(id) FROM news").Scan(&news_max)
	switch {
	case err != nil:
		fmt.Println(err)
		output.Error(501, err.Error())
	default:
		params := make([]interface{}, 7)
		for i := 0; i < 6; i++ {
			params[i] = randSource.Int63n(news_max)
		}
		params[6] = time.Now().AddDate(0, 0, -2).Unix()
		fmt.Println(params)
		rows, err := gatsby.QuerySelectWith(DbConnect, &News{}, "WHERE id IN (?,?,?,?,?,?) AND create_time > ? AND thumb_path <> '' AND delete_time IS NULL ", params...)
		if err == nil {
			news := News{}
			data, err := gatsby.CreateStructSliceFromRows(&news, rows)
			if err != nil {
				fmt.Println(err)
				output.Error(501, err.Error())
			} else {
				output.Data = data.([]News)
			}
		} else {
			output.Error(404, err.Error())
		}
	}
	writeResponseJson(w, output, r.FormValue("callback"))

	w.(http.Flusher).Flush()
}
Example #4
0
func GetNewsMeta(newsId int64) (*map[string]interface{}, error) {
	comment := Comments{}
	rows, err := gatsby.QuerySelectWith(DbConnect, &comment, "WHERE news_id = ? AND deleted_at IS NULL ORDER BY type", newsId)
	if err != nil {
		return nil, err
	} else {
		if data, err := gatsby.CreateStructSliceFromRows(&comment, rows); err != nil {
			return nil, err
		} else {
			output := make(map[string]interface{})
			commentsList := data.([]Comments)
			ti, ri, ni, length := 0, 0, 0, len(commentsList)
			reports := make([]Comments, length)
			talks := make([]Comments, length)
			newsIds := make([]string, length)
			newsIdFlags := make(map[int64]bool)
			for _, comment := range commentsList {
				if comment.Id == 0 {
					continue
				}
				if comment.DeviceID == 0 {
					if _, ok := newsIdFlags[comment.NewsID]; !ok {
						newsIds[ni] = fmt.Sprintf("%d", comment.NewsID)
						ni++
					}
					reports[ri] = comment
					ri++
				} else {
					talks[ti] = comment
					ti++
				}
			}
			if ni > 0 {
				cnews := make(map[int64]*News, ni)
				nrows, err := gatsby.QuerySelectWith(DbConnect, &News{}, "WHERE id IN ("+strings.Join(newsIds, ",")+") AND delete_time IS NULL ")
				if err == nil {
					news := News{}
					data, err := gatsby.CreateStructSliceFromRows(&news, nrows)
					if err != nil {
						return nil, err
					} else {
						newsList := data.([]News)
						for _, news := range newsList {
							news.Body = ""
							news.PicPath = UrlDomain + news.PicPath[2:]
							news.ThumbPath = UrlDomain + news.ThumbPath[2:]
							cnews[news.Id] = &news
						}
						for k, v := range reports {
							reports[k].News = cnews[v.NewsID]
						}
					}
				} else {
					return nil, err
				}
			}
			output["reports"] = reports
			output["talks"] = talks
			return &output, nil
		}
	}
}
func NewsHotAction(w http.ResponseWriter, r *http.Request) {
	header := w.Header()
	header.Set("Content-Type", "application/javascript; charset=utf-8")
	header.Set("X-Content-Type-Options", "nosniff")
	header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
	header.Set("Pragma", "no-cache")
	header.Set("Expires", "Thu, 01 Dec 1994 16:00:00 GMT")

	// fmt.Printf("%#v\n", news)
	output := ApiResponseJson{}
	randomSource := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
	var params []interface{}
	pi := 0
	offset := fmt.Sprintf("%d", randomSource.Int31n(2000))
	qlength := 100
	// offset := r.FormValue("offset")
	length := r.FormValue("rows")
	qsearch := r.FormValue("q")
	// if offset == "" {
	//     offset = "0"
	// }
	if length == "" {
		length = "30"
	}
	sql := "WHERE 1 AND delete_time IS NULL ORDER BY guid LIMIT ?, ? "
	if qsearch != "" {
		sql = "WHERE 1 AND delete_time IS NULL AND title LIKE ? ORDER BY guid LIMIT ?, ? "
		params = make([]interface{}, 3)
		params[pi] = "%" + qsearch + "%"
		pi++
		offset = "0"
	} else {
		params = make([]interface{}, 2)
	}
	// params[pi] = randomSource.Int31n(29)
	// pi++
	params[pi] = offset
	pi++
	params[pi] = qlength
	pi++
	rows, err := gatsby.QuerySelectWith(DbConnect, &News{}, sql, params...)
	if err == nil {
		news := News{}
		data, err := gatsby.CreateStructSliceFromRows(&news, rows)
		if err != nil {
			fmt.Println(err)
			output.Error(501, err.Error())
		} else {
			newsList := data.([]News)
			l := len(newsList)
			for i := 1; i < l; i++ {
				j := randomSource.Int63n(int64(i))
				newsList[i], newsList[j] = newsList[j], newsList[i]
			}
			var limit int
			if len(newsList) > 30 {
				limit = 30
			} else {
				limit = len(newsList)
			}
			nco := newsList[:limit]
			for k, _ := range nco {
				nco[k].Raw = ""
				nco[k].Body = ""
			}
			output.Data = nco
		}
	} else {
		output.Error(404, err.Error())
	}
	writeResponseJson(w, output, r.FormValue("callback"))

	w.(http.Flusher).Flush()
}