err:=db.QueryRow("SELECT username, one_time FROM accesstokens LEFT JOIN users ON user_id = id WHERE token LIKE ? AND (expires IS NULL OR expires > "+db.now()+")",t).Scan(&username,&oneTime)
err:=db.QueryRow("SELECT user_id, username, one_time FROM accesstokens LEFT JOIN users ON user_id = id WHERE token LIKE ? AND (expires IS NULL OR expires > "+db.now()+")",t).Scan(&userID,&username,&oneTime)
err:=db.QueryRow("SELECT user_id, sudo, one_time FROM accesstokens WHERE token LIKE ? AND (expires IS NULL OR expires > "+db.now()+")",t).Scan(&userID,&sudo,&oneTime)
err:=db.QueryRow("SELECT token FROM accesstokens WHERE user_id = ? AND (expires IS NULL OR expires > "+db.now()+") ORDER BY created DESC LIMIT 1",userID).Scan(&t)
switch{
caseerr==sql.ErrNoRows:
return""
caseerr!=nil:
log.Error("Failed selecting from accesstoken: %v",err)
return""
}
u,err:=uuid.Parse(t)
iferr!=nil{
return""
}
returnu.String()
}
// GetAccessToken creates a new non-expiring, valid access token for the given
// FIXME: change Collection to reflect database values. Add helper functions to get actual values
varstyleSheet,script,formatzero.String
row:=db.QueryRow("SELECT id, alias, title, description, style_sheet, script, format, owner_id, privacy, view_count FROM collections WHERE "+condition,value)
q.Where("alias = ? AND owner_id = ?",alias,c.OwnerID)
ifq.Updates==""{
returnErrPostNoUpdatableVals
}
// Find any current domain
varcollIDint64
varrowsAffectedint64
varchangedbool
varressql.Result
err:=db.QueryRow("SELECT id FROM collections WHERE alias = ?",alias).Scan(&collID)
iferr!=nil{
log.Error("Failed selecting from collections: %v. Some things won't work.",err)
}
// Update MathJax value
ifc.MathJax{
ifdb.driverName==driverSQLite{
_,err=db.Exec("INSERT OR REPLACE INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?)",collID,"render_mathjax","1")
}else{
_,err=db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?) "+db.upsert("collection_id","attribute")+" value = ?",collID,"render_mathjax","1","1")
}
iferr!=nil{
log.Error("Unable to insert render_mathjax value: %v",err)
returnerr
}
}else{
_,err=db.Exec("DELETE FROM collectionattributes WHERE collection_id = ? AND attribute = ?",collID,"render_mathjax")
iferr!=nil{
log.Error("Unable to delete render_mathjax value: %v",err)
returnerr
}
}
// Update rest of the collection data
res,err=db.Exec("UPDATE collections SET "+q.Updates+" WHERE "+q.Conditions,q.Params...)
iferr!=nil{
log.Error("Unable to update collection: %v",err)
returnerr
}
rowsAffected,_=res.RowsAffected()
if!changed||rowsAffected==0{
// Show the correct error message if nothing was updated
vardummyint
err:=db.QueryRow("SELECT 1 FROM collections WHERE alias = ? AND owner_id = ?",alias,c.OwnerID).Scan(&dummy)
switch{
caseerr==sql.ErrNoRows:
returnErrUnauthorizedEditPost
caseerr!=nil:
log.Error("Failed selecting from collections: %v",err)
}
if!updatePass{
returnnil
}
}
ifupdatePass{
hashedPass,err:=auth.HashPass([]byte(c.Pass))
iferr!=nil{
log.Error("Unable to create hash: %s",err)
returnimpart.HTTPError{http.StatusInternalServerError,"Could not create password hash."}
}
ifdb.driverName==driverSQLite{
_,err=db.Exec("INSERT OR REPLACE INTO collectionpasswords (collection_id, password) VALUES ((SELECT id FROM collections WHERE alias = ?), ?)",alias,hashedPass)
}else{
_,err=db.Exec("INSERT INTO collectionpasswords (collection_id, password) VALUES ((SELECT id FROM collections WHERE alias = ?), ?) "+db.upsert("collection_id")+" password = ?",alias,hashedPass,hashedPass)
row=db.QueryRow("SELECT "+postCols+", (SELECT username FROM users WHERE users.id = posts.owner_id) AS username FROM posts WHERE "+where+" LIMIT 1",params...)
rows,err:=db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? "+pinnedCondition+" "+timeCondition+" ORDER BY created "+order+limitStr,collID)
rows,err=db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? AND LOWER(content) regexp ? "+timeCondition+" ORDER BY created "+order+limitStr,collID,`.*#`+strings.ToLower(tag)+`\b.*`)
}else{
rows,err=db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? AND LOWER(content) RLIKE ? "+timeCondition+" ORDER BY created "+order+limitStr,collID,"#"+strings.ToLower(tag)+"[[:>:]]")
rows,err:=db.Query("SELECT actor_id, inbox, shared_inbox FROM remotefollows f INNER JOIN remoteusers u ON f.remote_user_id = u.id WHERE collection_id = ?",c.ID)
iferr!=nil{
log.Error("Failed selecting from followers: %v",err)
// FIXME: sqlite-backed instances don't include ellipsis on truncated titles
rows,err:=db.Query("SELECT id, slug, title, "+db.clip("content",80)+", pinned_position FROM posts WHERE collection_id = ? AND pinned_position IS NOT NULL ORDER BY pinned_position ASC",coll.ID)
returnnil,impart.HTTPError{http.StatusInternalServerError,"You don't seem to have any blogs; they might've moved to another account. Try logging out and logging into your other account."}
}
returnc,nil
}
func(db*datastore)GetMeStats(u*User)userMeStats{
s:=userMeStats{}
// User counts
colls,_:=db.GetUserCollectionCount(u.ID)
s.TotalCollections=colls
vararticles,collPostsuint64
err:=db.QueryRow("SELECT COUNT(*) FROM posts WHERE owner_id = ? AND collection_id IS NULL",u.ID).Scan(&articles)
iferr!=nil&&err!=sql.ErrNoRows{
log.Error("Couldn't get articles count for user %d: %v",u.ID,err)
}
s.TotalArticles=articles
err=db.QueryRow("SELECT COUNT(*) FROM posts WHERE owner_id = ? AND collection_id IS NOT NULL",u.ID).Scan(&collPosts)
iferr!=nil&&err!=sql.ErrNoRows{
log.Error("Couldn't get coll posts count for user %d: %v",u.ID,err)
rows,err:=db.Query("SELECT p.id, p.slug, p.view_count, p.title, c.alias, c.title, c.description, c.view_count FROM posts p LEFT JOIN collections c ON p.collection_id = c.id WHERE p.owner_id = ?"+where+" ORDER BY p.view_count DESC, created DESC LIMIT 25",params...)
iferr!=nil{
log.Error("Failed selecting from posts: %v",err)
returnnil,impart.HTTPError{http.StatusInternalServerError,"Couldn't retrieve user top posts."}
rows,err:=db.Query("SELECT id, view_count, title, created, updated, content FROM posts WHERE owner_id = ? AND collection_id IS NULL ORDER BY created DESC",u.ID)
iferr!=nil{
log.Error("Failed selecting from posts: %v",err)
returnnil,impart.HTTPError{http.StatusInternalServerError,"Couldn't retrieve user anonymous posts."}
rows,err:=db.Query("SELECT p.id, p.slug, p.view_count, p.title, p.created, p.updated, p.content, p.text_appearance, p.language, p.rtl, c.alias, c.title, c.description, c.view_count FROM posts p LEFT JOIN collections c ON collection_id = c.id WHERE p.owner_id = ? ORDER BY created ASC",u.ID)
iferr!=nil{
log.Error("Failed selecting from posts: %v",err)
returnnil,impart.HTTPError{http.StatusInternalServerError,"Couldn't retrieve user posts."}
err:=db.QueryRow("SELECT id, max_uses, created, expires, inactive FROM userinvites WHERE id = ?",id).Scan(&i.ID,&i.MaxUses,&i.Created,&i.Expires,&i.Inactive)