err:=app.db.QueryRow(fmt.Sprintf("SELECT owner_id, title, content, text_appearance, view_count, language, rtl FROM posts WHERE id = ?"),friendlyID).Scan(&ownerID,&title,&content,&font,&views,&language,&rtl)
switch{
caseerr==sql.ErrNoRows:
found=false
// Output the error in the correct format
ifisJSON{
content="{\"error\": \"Post not found.\"}"
}elseifisRaw{
content="Post not found."
}else{
returnErrPostNotFound
}
caseerr!=nil:
found=false
log.Error("Post loading err: %s\n",err)
returnErrInternalGeneral
default:
found=true
vardstring
iflen(rtl)==0{
d="auto"
}elseifrtl[0]==49{
// TODO: find a cleaner way to get this (possibly NULL) value
// TODO: merge this into getSlugFromPost or phase it out
funcgetSlug(title,langstring)string{
returngetSlugFromPost("",title,lang)
}
funcgetSlugFromPost(title,body,langstring)string{
iftitle==""{
title=postTitle(body,body)
}
title=parse.PostLede(title,false)
// Truncate lede if needed
title,_=parse.TruncToWord(title,80)
varsstring
iflang!=""&&len(lang)==2{
s=slug.MakeLang(title,lang)
}else{
s=slug.Make(title)
}
// Transliteration may cause the slug to expand past the limit, so truncate again
s,_=parse.TruncToWord(s,80)
returnstrings.TrimFunc(s,func(rrune)bool{
// TruncToWord doesn't respect words in a slug, since spaces are replaced
// with hyphens. So remove any trailing hyphens.
returnr=='-'
})
}
// isFontValid returns whether or not the submitted post's appearance is valid.
func(p*SubmittedPost)isFontValid()bool{
validFonts:=map[string]bool{
"norm":true,
"sans":true,
"mono":true,
"wrap":true,
"code":true,
}
_,valid:=validFonts[p.Font]
returnvalid
}
funcgetRawPost(app*App,friendlyIDstring)*RawPost{
varcontent,font,titlestring
varisRTLsql.NullBool
varlangsql.NullString
varownerIDsql.NullInt64
varcreatedtime.Time
err:=app.db.QueryRow("SELECT title, content, text_appearance, language, rtl, created, owner_id FROM posts WHERE id = ?",friendlyID).Scan(&title,&content,&font,&lang,&isRTL,&created,&ownerID)
err=app.db.QueryRow("SELECT id, title, content, text_appearance, language, rtl, view_count, created, owner_id FROM posts WHERE slug = ? AND collection_id = 1",slug).Scan(&id,&title,&content,&font,&lang,&isRTL,&views,&created,&ownerID)
}else{
err=app.db.QueryRow("SELECT id, title, content, text_appearance, language, rtl, view_count, created, owner_id FROM posts WHERE slug = ? AND collection_id = (SELECT id FROM collections WHERE alias = ?)",slug,collAlias).Scan(&id,&title,&content,&font,&lang,&isRTL,&views,&created,&ownerID)
}
switch{
caseerr==sql.ErrNoRows:
return&RawPost{Content:"",Found:false,Gone:false}
caseerr!=nil:
return&RawPost{Content:"",Found:true,Gone:false}
}
return&RawPost{
Id:id,
Slug:slug,
Title:title,
Content:content,
Font:font,
Created:created,
IsRTL:isRTL,
Language:lang,
OwnerID:ownerID.Int64,
Found:true,
Gone:content=="",
Views:views,
}
}
funcisRaw(r*http.Request)bool{
vars:=mux.Vars(r)
slug:=vars["slug"]
// NOTE: until this is done better, be sure to keep this in parity with
// isRaw in viewCollectionPost() and handleViewPost()