A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2815 lines
86 KiB

  1. /*
  2. * Copyright © 2018-2021 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package writefreely
  11. import (
  12. "context"
  13. "database/sql"
  14. "fmt"
  15. "github.com/writeas/web-core/silobridge"
  16. wf_db "github.com/writefreely/writefreely/db"
  17. "net/http"
  18. "strings"
  19. "time"
  20. "github.com/guregu/null"
  21. "github.com/guregu/null/zero"
  22. uuid "github.com/nu7hatch/gouuid"
  23. "github.com/writeas/activityserve"
  24. "github.com/writeas/impart"
  25. "github.com/writeas/web-core/activitypub"
  26. "github.com/writeas/web-core/auth"
  27. "github.com/writeas/web-core/data"
  28. "github.com/writeas/web-core/id"
  29. "github.com/writeas/web-core/log"
  30. "github.com/writeas/web-core/query"
  31. "github.com/writefreely/writefreely/author"
  32. "github.com/writefreely/writefreely/config"
  33. "github.com/writefreely/writefreely/key"
  34. )
  35. const (
  36. mySQLErrDuplicateKey = 1062
  37. mySQLErrCollationMix = 1267
  38. mySQLErrTooManyConns = 1040
  39. mySQLErrMaxUserConns = 1203
  40. driverMySQL = "mysql"
  41. driverSQLite = "sqlite3"
  42. )
  43. var (
  44. SQLiteEnabled bool
  45. )
  46. type writestore interface {
  47. CreateUser(*config.Config, *User, string, string) error
  48. UpdateUserEmail(keys *key.Keychain, userID int64, email string) error
  49. UpdateEncryptedUserEmail(int64, []byte) error
  50. GetUserByID(int64) (*User, error)
  51. GetUserForAuth(string) (*User, error)
  52. GetUserForAuthByID(int64) (*User, error)
  53. GetUserNameFromToken(string) (string, error)
  54. GetUserDataFromToken(string) (int64, string, error)
  55. GetAPIUser(header string) (*User, error)
  56. GetUserID(accessToken string) int64
  57. GetUserIDPrivilege(accessToken string) (userID int64, sudo bool)
  58. DeleteToken(accessToken []byte) error
  59. FetchLastAccessToken(userID int64) string
  60. GetAccessToken(userID int64) (string, error)
  61. GetTemporaryAccessToken(userID int64, validSecs int) (string, error)
  62. GetTemporaryOneTimeAccessToken(userID int64, validSecs int, oneTime bool) (string, error)
  63. DeleteAccount(userID int64) error
  64. ChangeSettings(app *App, u *User, s *userSettings) error
  65. ChangePassphrase(userID int64, sudo bool, curPass string, hashedPass []byte) error
  66. GetCollections(u *User, hostName string) (*[]Collection, error)
  67. GetPublishableCollections(u *User, hostName string) (*[]Collection, error)
  68. GetMeStats(u *User) userMeStats
  69. GetTotalCollections() (int64, error)
  70. GetTotalPosts() (int64, error)
  71. GetTopPosts(u *User, alias string) (*[]PublicPost, error)
  72. GetAnonymousPosts(u *User, page int) (*[]PublicPost, error)
  73. GetUserPosts(u *User) (*[]PublicPost, error)
  74. CreateOwnedPost(post *SubmittedPost, accessToken, collAlias, hostName string) (*PublicPost, error)
  75. CreatePost(userID, collID int64, post *SubmittedPost) (*Post, error)
  76. UpdateOwnedPost(post *AuthenticatedPost, userID int64) error
  77. GetEditablePost(id, editToken string) (*PublicPost, error)
  78. PostIDExists(id string) bool
  79. GetPost(id string, collectionID int64) (*PublicPost, error)
  80. GetOwnedPost(id string, ownerID int64) (*PublicPost, error)
  81. GetPostProperty(id string, collectionID int64, property string) (interface{}, error)
  82. CreateCollectionFromToken(*config.Config, string, string, string) (*Collection, error)
  83. CreateCollection(*config.Config, string, string, int64) (*Collection, error)
  84. GetCollectionBy(condition string, value interface{}) (*Collection, error)
  85. GetCollection(alias string) (*Collection, error)
  86. GetCollectionForPad(alias string) (*Collection, error)
  87. GetCollectionByID(id int64) (*Collection, error)
  88. UpdateCollection(c *SubmittedCollection, alias string) error
  89. DeleteCollection(alias string, userID int64) error
  90. UpdatePostPinState(pinned bool, postID string, collID, ownerID, pos int64) error
  91. GetLastPinnedPostPos(collID int64) int64
  92. GetPinnedPosts(coll *CollectionObj, includeFuture bool) (*[]PublicPost, error)
  93. RemoveCollectionRedirect(t *sql.Tx, alias string) error
  94. GetCollectionRedirect(alias string) (new string)
  95. IsCollectionAttributeOn(id int64, attr string) bool
  96. CollectionHasAttribute(id int64, attr string) bool
  97. CanCollect(cpr *ClaimPostRequest, userID int64) bool
  98. AttemptClaim(p *ClaimPostRequest, query string, params []interface{}, slugIdx int) (sql.Result, error)
  99. DispersePosts(userID int64, postIDs []string) (*[]ClaimPostResult, error)
  100. ClaimPosts(cfg *config.Config, userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error)
  101. GetPostsCount(c *CollectionObj, includeFuture bool)
  102. GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error)
  103. GetPostsTagged(cfg *config.Config, c *Collection, tag string, page int, includeFuture bool) (*[]PublicPost, error)
  104. GetAPFollowers(c *Collection) (*[]RemoteUser, error)
  105. GetAPActorKeys(collectionID int64) ([]byte, []byte)
  106. CreateUserInvite(id string, userID int64, maxUses int, expires *time.Time) error
  107. GetUserInvites(userID int64) (*[]Invite, error)
  108. GetUserInvite(id string) (*Invite, error)
  109. GetUsersInvitedCount(id string) int64
  110. CreateInvitedUser(inviteID string, userID int64) error
  111. GetDynamicContent(id string) (*instanceContent, error)
  112. UpdateDynamicContent(id, title, content, contentType string) error
  113. GetAllUsers(page uint) (*[]User, error)
  114. GetAllUsersCount() int64
  115. GetUserLastPostTime(id int64) (*time.Time, error)
  116. GetCollectionLastPostTime(id int64) (*time.Time, error)
  117. GetIDForRemoteUser(context.Context, string, string, string) (int64, error)
  118. RecordRemoteUserID(context.Context, int64, string, string, string, string) error
  119. ValidateOAuthState(context.Context, string) (string, string, int64, string, error)
  120. GenerateOAuthState(context.Context, string, string, int64, string) (string, error)
  121. GetOauthAccounts(ctx context.Context, userID int64) ([]oauthAccountInfo, error)
  122. RemoveOauth(ctx context.Context, userID int64, provider string, clientID string, remoteUserID string) error
  123. DatabaseInitialized() bool
  124. }
  125. type datastore struct {
  126. *sql.DB
  127. driverName string
  128. }
  129. var _ writestore = &datastore{}
  130. func (db *datastore) now() string {
  131. if db.driverName == driverSQLite {
  132. return "strftime('%Y-%m-%d %H:%M:%S','now')"
  133. }
  134. return "NOW()"
  135. }
  136. func (db *datastore) clip(field string, l int) string {
  137. if db.driverName == driverSQLite {
  138. return fmt.Sprintf("SUBSTR(%s, 0, %d)", field, l)
  139. }
  140. return fmt.Sprintf("LEFT(%s, %d)", field, l)
  141. }
  142. func (db *datastore) upsert(indexedCols ...string) string {
  143. if db.driverName == driverSQLite {
  144. // NOTE: SQLite UPSERT syntax only works in v3.24.0 (2018-06-04) or later
  145. // Leaving this for whenever we can upgrade and include it in our binary
  146. cc := strings.Join(indexedCols, ", ")
  147. return "ON CONFLICT(" + cc + ") DO UPDATE SET"
  148. }
  149. return "ON DUPLICATE KEY UPDATE"
  150. }
  151. func (db *datastore) dateSub(l int, unit string) string {
  152. if db.driverName == driverSQLite {
  153. return fmt.Sprintf("DATETIME('now', '-%d %s')", l, unit)
  154. }
  155. return fmt.Sprintf("DATE_SUB(NOW(), INTERVAL %d %s)", l, unit)
  156. }
  157. // CreateUser creates a new user in the database from the given User, UPDATING it in the process with the user's ID.
  158. func (db *datastore) CreateUser(cfg *config.Config, u *User, collectionTitle string, collectionDesc string) error {
  159. if db.PostIDExists(u.Username) {
  160. return impart.HTTPError{http.StatusConflict, "Invalid collection name."}
  161. }
  162. // New users get a `users` and `collections` row.
  163. t, err := db.Begin()
  164. if err != nil {
  165. return err
  166. }
  167. // 1. Add to `users` table
  168. // NOTE: Assumes User's Password is already hashed!
  169. res, err := t.Exec("INSERT INTO users (username, password, email) VALUES (?, ?, ?)", u.Username, u.HashedPass, u.Email)
  170. if err != nil {
  171. t.Rollback()
  172. if db.isDuplicateKeyErr(err) {
  173. return impart.HTTPError{http.StatusConflict, "Username is already taken."}
  174. }
  175. log.Error("Rolling back users INSERT: %v\n", err)
  176. return err
  177. }
  178. u.ID, err = res.LastInsertId()
  179. if err != nil {
  180. t.Rollback()
  181. log.Error("Rolling back after LastInsertId: %v\n", err)
  182. return err
  183. }
  184. // 2. Create user's Collection
  185. if collectionTitle == "" {
  186. collectionTitle = u.Username
  187. }
  188. res, err = t.Exec("INSERT INTO collections (alias, title, description, privacy, owner_id, view_count) VALUES (?, ?, ?, ?, ?, ?)", u.Username, collectionTitle, collectionDesc, defaultVisibility(cfg), u.ID, 0)
  189. if err != nil {
  190. t.Rollback()
  191. if db.isDuplicateKeyErr(err) {
  192. return impart.HTTPError{http.StatusConflict, "Username is already taken."}
  193. }
  194. log.Error("Rolling back collections INSERT: %v\n", err)
  195. return err
  196. }
  197. db.RemoveCollectionRedirect(t, u.Username)
  198. err = t.Commit()
  199. if err != nil {
  200. t.Rollback()
  201. log.Error("Rolling back after Commit(): %v\n", err)
  202. return err
  203. }
  204. return nil
  205. }
  206. // FIXME: We're returning errors inconsistently in this file. Do we use Errorf
  207. // for returned value, or impart?
  208. func (db *datastore) UpdateUserEmail(keys *key.Keychain, userID int64, email string) error {
  209. encEmail, err := data.Encrypt(keys.EmailKey, email)
  210. if err != nil {
  211. return fmt.Errorf("Couldn't encrypt email %s: %s\n", email, err)
  212. }
  213. return db.UpdateEncryptedUserEmail(userID, encEmail)
  214. }
  215. func (db *datastore) UpdateEncryptedUserEmail(userID int64, encEmail []byte) error {
  216. _, err := db.Exec("UPDATE users SET email = ? WHERE id = ?", encEmail, userID)
  217. if err != nil {
  218. return fmt.Errorf("Unable to update user email: %s", err)
  219. }
  220. return nil
  221. }
  222. func (db *datastore) CreateCollectionFromToken(cfg *config.Config, alias, title, accessToken string) (*Collection, error) {
  223. userID := db.GetUserID(accessToken)
  224. if userID == -1 {
  225. return nil, ErrBadAccessToken
  226. }
  227. return db.CreateCollection(cfg, alias, title, userID)
  228. }
  229. func (db *datastore) GetUserCollectionCount(userID int64) (uint64, error) {
  230. var collCount uint64
  231. err := db.QueryRow("SELECT COUNT(*) FROM collections WHERE owner_id = ?", userID).Scan(&collCount)
  232. switch {
  233. case err == sql.ErrNoRows:
  234. return 0, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user from database."}
  235. case err != nil:
  236. log.Error("Couldn't get collections count for user %d: %v", userID, err)
  237. return 0, err
  238. }
  239. return collCount, nil
  240. }
  241. func (db *datastore) CreateCollection(cfg *config.Config, alias, title string, userID int64) (*Collection, error) {
  242. if db.PostIDExists(alias) {
  243. return nil, impart.HTTPError{http.StatusConflict, "Invalid collection name."}
  244. }
  245. // All good, so create new collection
  246. res, err := db.Exec("INSERT INTO collections (alias, title, description, privacy, owner_id, view_count) VALUES (?, ?, ?, ?, ?, ?)", alias, title, "", defaultVisibility(cfg), userID, 0)
  247. if err != nil {
  248. if db.isDuplicateKeyErr(err) {
  249. return nil, impart.HTTPError{http.StatusConflict, "Collection already exists."}
  250. }
  251. log.Error("Couldn't add to collections: %v\n", err)
  252. return nil, err
  253. }
  254. c := &Collection{
  255. Alias: alias,
  256. Title: title,
  257. OwnerID: userID,
  258. PublicOwner: false,
  259. Public: defaultVisibility(cfg) == CollPublic,
  260. }
  261. c.ID, err = res.LastInsertId()
  262. if err != nil {
  263. log.Error("Couldn't get collection LastInsertId: %v\n", err)
  264. }
  265. return c, nil
  266. }
  267. func (db *datastore) GetUserByID(id int64) (*User, error) {
  268. u := &User{ID: id}
  269. err := db.QueryRow("SELECT username, password, email, created, status FROM users WHERE id = ?", id).Scan(&u.Username, &u.HashedPass, &u.Email, &u.Created, &u.Status)
  270. switch {
  271. case err == sql.ErrNoRows:
  272. return nil, ErrUserNotFound
  273. case err != nil:
  274. log.Error("Couldn't SELECT user password: %v", err)
  275. return nil, err
  276. }
  277. return u, nil
  278. }
  279. // IsUserSilenced returns true if the user account associated with id is
  280. // currently silenced.
  281. func (db *datastore) IsUserSilenced(id int64) (bool, error) {
  282. u := &User{ID: id}
  283. err := db.QueryRow("SELECT status FROM users WHERE id = ?", id).Scan(&u.Status)
  284. switch {
  285. case err == sql.ErrNoRows:
  286. return false, fmt.Errorf("is user silenced: %v", ErrUserNotFound)
  287. case err != nil:
  288. log.Error("Couldn't SELECT user status: %v", err)
  289. return false, fmt.Errorf("is user silenced: %v", err)
  290. }
  291. return u.IsSilenced(), nil
  292. }
  293. // DoesUserNeedAuth returns true if the user hasn't provided any methods for
  294. // authenticating with the account, such a passphrase or email address.
  295. // Any errors are reported to admin and silently quashed, returning false as the
  296. // result.
  297. func (db *datastore) DoesUserNeedAuth(id int64) bool {
  298. var pass, email []byte
  299. // Find out if user has an email set first
  300. err := db.QueryRow("SELECT password, email FROM users WHERE id = ?", id).Scan(&pass, &email)
  301. switch {
  302. case err == sql.ErrNoRows:
  303. // ERROR. Don't give false positives on needing auth methods
  304. return false
  305. case err != nil:
  306. // ERROR. Don't give false positives on needing auth methods
  307. log.Error("Couldn't SELECT user %d from users: %v", id, err)
  308. return false
  309. }
  310. // User doesn't need auth if there's an email
  311. return len(email) == 0 && len(pass) == 0
  312. }
  313. func (db *datastore) IsUserPassSet(id int64) (bool, error) {
  314. var pass []byte
  315. err := db.QueryRow("SELECT password FROM users WHERE id = ?", id).Scan(&pass)
  316. switch {
  317. case err == sql.ErrNoRows:
  318. return false, nil
  319. case err != nil:
  320. log.Error("Couldn't SELECT user %d from users: %v", id, err)
  321. return false, err
  322. }
  323. return len(pass) > 0, nil
  324. }
  325. func (db *datastore) GetUserForAuth(username string) (*User, error) {
  326. u := &User{Username: username}
  327. err := db.QueryRow("SELECT id, password, email, created, status FROM users WHERE username = ?", username).Scan(&u.ID, &u.HashedPass, &u.Email, &u.Created, &u.Status)
  328. switch {
  329. case err == sql.ErrNoRows:
  330. // Check if they've entered the wrong, unnormalized username
  331. username = getSlug(username, "")
  332. if username != u.Username {
  333. err = db.QueryRow("SELECT id FROM users WHERE username = ? LIMIT 1", username).Scan(&u.ID)
  334. if err == nil {
  335. return db.GetUserForAuth(username)
  336. }
  337. }
  338. return nil, ErrUserNotFound
  339. case err != nil:
  340. log.Error("Couldn't SELECT user password: %v", err)
  341. return nil, err
  342. }
  343. return u, nil
  344. }
  345. func (db *datastore) GetUserForAuthByID(userID int64) (*User, error) {
  346. u := &User{ID: userID}
  347. err := db.QueryRow("SELECT id, password, email, created, status FROM users WHERE id = ?", u.ID).Scan(&u.ID, &u.HashedPass, &u.Email, &u.Created, &u.Status)
  348. switch {
  349. case err == sql.ErrNoRows:
  350. return nil, ErrUserNotFound
  351. case err != nil:
  352. log.Error("Couldn't SELECT userForAuthByID: %v", err)
  353. return nil, err
  354. }
  355. return u, nil
  356. }
  357. func (db *datastore) GetUserNameFromToken(accessToken string) (string, error) {
  358. t := auth.GetToken(accessToken)
  359. if len(t) == 0 {
  360. return "", ErrNoAccessToken
  361. }
  362. var oneTime bool
  363. var username string
  364. 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)
  365. switch {
  366. case err == sql.ErrNoRows:
  367. return "", ErrBadAccessToken
  368. case err != nil:
  369. return "", ErrInternalGeneral
  370. }
  371. // Delete token if it was one-time
  372. if oneTime {
  373. db.DeleteToken(t[:])
  374. }
  375. return username, nil
  376. }
  377. func (db *datastore) GetUserDataFromToken(accessToken string) (int64, string, error) {
  378. t := auth.GetToken(accessToken)
  379. if len(t) == 0 {
  380. return 0, "", ErrNoAccessToken
  381. }
  382. var userID int64
  383. var oneTime bool
  384. var username string
  385. 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)
  386. switch {
  387. case err == sql.ErrNoRows:
  388. return 0, "", ErrBadAccessToken
  389. case err != nil:
  390. return 0, "", ErrInternalGeneral
  391. }
  392. // Delete token if it was one-time
  393. if oneTime {
  394. db.DeleteToken(t[:])
  395. }
  396. return userID, username, nil
  397. }
  398. func (db *datastore) GetAPIUser(header string) (*User, error) {
  399. uID := db.GetUserID(header)
  400. if uID == -1 {
  401. return nil, fmt.Errorf(ErrUserNotFound.Error())
  402. }
  403. return db.GetUserByID(uID)
  404. }
  405. // GetUserID takes a hexadecimal accessToken, parses it into its binary
  406. // representation, and gets any user ID associated with the token. If no user
  407. // is associated, -1 is returned.
  408. func (db *datastore) GetUserID(accessToken string) int64 {
  409. i, _ := db.GetUserIDPrivilege(accessToken)
  410. return i
  411. }
  412. func (db *datastore) GetUserIDPrivilege(accessToken string) (userID int64, sudo bool) {
  413. t := auth.GetToken(accessToken)
  414. if len(t) == 0 {
  415. return -1, false
  416. }
  417. var oneTime bool
  418. 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)
  419. switch {
  420. case err == sql.ErrNoRows:
  421. return -1, false
  422. case err != nil:
  423. return -1, false
  424. }
  425. // Delete token if it was one-time
  426. if oneTime {
  427. db.DeleteToken(t[:])
  428. }
  429. return
  430. }
  431. func (db *datastore) DeleteToken(accessToken []byte) error {
  432. res, err := db.Exec("DELETE FROM accesstokens WHERE token LIKE ?", accessToken)
  433. if err != nil {
  434. return err
  435. }
  436. rowsAffected, _ := res.RowsAffected()
  437. if rowsAffected == 0 {
  438. return impart.HTTPError{http.StatusNotFound, "Token is invalid or doesn't exist"}
  439. }
  440. return nil
  441. }
  442. // FetchLastAccessToken creates a new non-expiring, valid access token for the given
  443. // userID.
  444. func (db *datastore) FetchLastAccessToken(userID int64) string {
  445. var t []byte
  446. 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)
  447. switch {
  448. case err == sql.ErrNoRows:
  449. return ""
  450. case err != nil:
  451. log.Error("Failed selecting from accesstoken: %v", err)
  452. return ""
  453. }
  454. u, err := uuid.Parse(t)
  455. if err != nil {
  456. return ""
  457. }
  458. return u.String()
  459. }
  460. // GetAccessToken creates a new non-expiring, valid access token for the given
  461. // userID.
  462. func (db *datastore) GetAccessToken(userID int64) (string, error) {
  463. return db.GetTemporaryOneTimeAccessToken(userID, 0, false)
  464. }
  465. // GetTemporaryAccessToken creates a new valid access token for the given
  466. // userID that remains valid for the given time in seconds. If validSecs is 0,
  467. // the access token doesn't automatically expire.
  468. func (db *datastore) GetTemporaryAccessToken(userID int64, validSecs int) (string, error) {
  469. return db.GetTemporaryOneTimeAccessToken(userID, validSecs, false)
  470. }
  471. // GetTemporaryOneTimeAccessToken creates a new valid access token for the given
  472. // userID that remains valid for the given time in seconds and can only be used
  473. // once if oneTime is true. If validSecs is 0, the access token doesn't
  474. // automatically expire.
  475. func (db *datastore) GetTemporaryOneTimeAccessToken(userID int64, validSecs int, oneTime bool) (string, error) {
  476. u, err := uuid.NewV4()
  477. if err != nil {
  478. log.Error("Unable to generate token: %v", err)
  479. return "", err
  480. }
  481. // Insert UUID to `accesstokens`
  482. binTok := u[:]
  483. expirationVal := "NULL"
  484. if validSecs > 0 {
  485. expirationVal = fmt.Sprintf("DATE_ADD("+db.now()+", INTERVAL %d SECOND)", validSecs)
  486. }
  487. _, err = db.Exec("INSERT INTO accesstokens (token, user_id, one_time, expires) VALUES (?, ?, ?, "+expirationVal+")", string(binTok), userID, oneTime)
  488. if err != nil {
  489. log.Error("Couldn't INSERT accesstoken: %v", err)
  490. return "", err
  491. }
  492. return u.String(), nil
  493. }
  494. func (db *datastore) CreateOwnedPost(post *SubmittedPost, accessToken, collAlias, hostName string) (*PublicPost, error) {
  495. var userID, collID int64 = -1, -1
  496. var coll *Collection
  497. var err error
  498. if accessToken != "" {
  499. userID = db.GetUserID(accessToken)
  500. if userID == -1 {
  501. return nil, ErrBadAccessToken
  502. }
  503. if collAlias != "" {
  504. coll, err = db.GetCollection(collAlias)
  505. if err != nil {
  506. return nil, err
  507. }
  508. coll.hostName = hostName
  509. if coll.OwnerID != userID {
  510. return nil, ErrForbiddenCollection
  511. }
  512. collID = coll.ID
  513. }
  514. }
  515. rp := &PublicPost{}
  516. rp.Post, err = db.CreatePost(userID, collID, post)
  517. if err != nil {
  518. return rp, err
  519. }
  520. if coll != nil {
  521. coll.ForPublic()
  522. rp.Collection = &CollectionObj{Collection: *coll}
  523. }
  524. return rp, nil
  525. }
  526. func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Post, error) {
  527. idLen := postIDLen
  528. friendlyID := id.GenerateFriendlyRandomString(idLen)
  529. // Handle appearance / font face
  530. appearance := post.Font
  531. if !post.isFontValid() {
  532. appearance = "norm"
  533. }
  534. var err error
  535. ownerID := sql.NullInt64{
  536. Valid: false,
  537. }
  538. ownerCollID := sql.NullInt64{
  539. Valid: false,
  540. }
  541. slug := sql.NullString{"", false}
  542. // If an alias was supplied, we'll add this to the collection as well.
  543. if userID > 0 {
  544. ownerID.Int64 = userID
  545. ownerID.Valid = true
  546. if collID > 0 {
  547. ownerCollID.Int64 = collID
  548. ownerCollID.Valid = true
  549. var slugVal string
  550. if post.Slug != nil && *post.Slug != "" {
  551. slugVal = *post.Slug
  552. } else {
  553. if post.Title != nil && *post.Title != "" {
  554. slugVal = getSlug(*post.Title, post.Language.String)
  555. if slugVal == "" {
  556. slugVal = getSlug(*post.Content, post.Language.String)
  557. }
  558. } else {
  559. slugVal = getSlug(*post.Content, post.Language.String)
  560. }
  561. }
  562. if slugVal == "" {
  563. slugVal = friendlyID
  564. }
  565. slug = sql.NullString{slugVal, true}
  566. }
  567. }
  568. created := time.Now()
  569. if db.driverName == driverSQLite {
  570. // SQLite stores datetimes in UTC, so convert time.Now() to it here
  571. created = created.UTC()
  572. }
  573. if post.Created != nil {
  574. created, err = time.Parse("2006-01-02T15:04:05Z", *post.Created)
  575. if err != nil {
  576. log.Error("Unable to parse Created time '%s': %v", *post.Created, err)
  577. created = time.Now()
  578. if db.driverName == driverSQLite {
  579. // SQLite stores datetimes in UTC, so convert time.Now() to it here
  580. created = created.UTC()
  581. }
  582. }
  583. }
  584. stmt, err := db.Prepare("INSERT INTO posts (id, slug, title, content, text_appearance, language, rtl, privacy, owner_id, collection_id, created, updated, view_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + db.now() + ", ?)")
  585. if err != nil {
  586. return nil, err
  587. }
  588. defer stmt.Close()
  589. _, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, created, 0)
  590. if err != nil {
  591. if db.isDuplicateKeyErr(err) {
  592. // Duplicate entry error; try a new slug
  593. // TODO: make this a little more robust
  594. slug = sql.NullString{id.GenSafeUniqueSlug(slug.String), true}
  595. _, err = stmt.Exec(friendlyID, slug, post.Title, post.Content, appearance, post.Language, post.IsRTL, 0, ownerID, ownerCollID, created, 0)
  596. if err != nil {
  597. return nil, handleFailedPostInsert(fmt.Errorf("Retried slug generation, still failed: %v", err))
  598. }
  599. } else {
  600. return nil, handleFailedPostInsert(err)
  601. }
  602. }
  603. // TODO: return Created field in proper format
  604. return &Post{
  605. ID: friendlyID,
  606. Slug: null.NewString(slug.String, slug.Valid),
  607. Font: appearance,
  608. Language: zero.NewString(post.Language.String, post.Language.Valid),
  609. RTL: zero.NewBool(post.IsRTL.Bool, post.IsRTL.Valid),
  610. OwnerID: null.NewInt(userID, true),
  611. CollectionID: null.NewInt(userID, true),
  612. Created: created.Truncate(time.Second).UTC(),
  613. Updated: time.Now().Truncate(time.Second).UTC(),
  614. Title: zero.NewString(*(post.Title), true),
  615. Content: *(post.Content),
  616. }, nil
  617. }
  618. // UpdateOwnedPost updates an existing post with only the given fields in the
  619. // supplied AuthenticatedPost.
  620. func (db *datastore) UpdateOwnedPost(post *AuthenticatedPost, userID int64) error {
  621. params := []interface{}{}
  622. var queryUpdates, sep, authCondition string
  623. if post.Slug != nil && *post.Slug != "" {
  624. queryUpdates += sep + "slug = ?"
  625. sep = ", "
  626. params = append(params, getSlug(*post.Slug, ""))
  627. }
  628. if post.Content != nil {
  629. queryUpdates += sep + "content = ?"
  630. sep = ", "
  631. params = append(params, post.Content)
  632. }
  633. if post.Title != nil {
  634. queryUpdates += sep + "title = ?"
  635. sep = ", "
  636. params = append(params, post.Title)
  637. }
  638. if post.Language.Valid {
  639. queryUpdates += sep + "language = ?"
  640. sep = ", "
  641. params = append(params, post.Language.String)
  642. }
  643. if post.IsRTL.Valid {
  644. queryUpdates += sep + "rtl = ?"
  645. sep = ", "
  646. params = append(params, post.IsRTL.Bool)
  647. }
  648. if post.Font != "" {
  649. queryUpdates += sep + "text_appearance = ?"
  650. sep = ", "
  651. params = append(params, post.Font)
  652. }
  653. if post.Created != nil {
  654. createTime, err := time.Parse(postMetaDateFormat, *post.Created)
  655. if err != nil {
  656. log.Error("Unable to parse Created date: %v", err)
  657. return fmt.Errorf("That's the incorrect format for Created date.")
  658. }
  659. queryUpdates += sep + "created = ?"
  660. sep = ", "
  661. params = append(params, createTime)
  662. }
  663. // WHERE parameters...
  664. // id = ?
  665. params = append(params, post.ID)
  666. // AND owner_id = ?
  667. authCondition = "(owner_id = ?)"
  668. params = append(params, userID)
  669. if queryUpdates == "" {
  670. return ErrPostNoUpdatableVals
  671. }
  672. queryUpdates += sep + "updated = " + db.now()
  673. res, err := db.Exec("UPDATE posts SET "+queryUpdates+" WHERE id = ? AND "+authCondition, params...)
  674. if err != nil {
  675. log.Error("Unable to update owned post: %v", err)
  676. return err
  677. }
  678. rowsAffected, _ := res.RowsAffected()
  679. if rowsAffected == 0 {
  680. // Show the correct error message if nothing was updated
  681. var dummy int
  682. err := db.QueryRow("SELECT 1 FROM posts WHERE id = ? AND "+authCondition, post.ID, params[len(params)-1]).Scan(&dummy)
  683. switch {
  684. case err == sql.ErrNoRows:
  685. return ErrUnauthorizedEditPost
  686. case err != nil:
  687. log.Error("Failed selecting from posts: %v", err)
  688. }
  689. return nil
  690. }
  691. return nil
  692. }
  693. func (db *datastore) GetCollectionBy(condition string, value interface{}) (*Collection, error) {
  694. c := &Collection{}
  695. // FIXME: change Collection to reflect database values. Add helper functions to get actual values
  696. var styleSheet, script, signature, format zero.String
  697. row := db.QueryRow("SELECT id, alias, title, description, style_sheet, script, post_signature, format, owner_id, privacy, view_count FROM collections WHERE "+condition, value)
  698. err := row.Scan(&c.ID, &c.Alias, &c.Title, &c.Description, &styleSheet, &script, &signature, &format, &c.OwnerID, &c.Visibility, &c.Views)
  699. switch {
  700. case err == sql.ErrNoRows:
  701. return nil, impart.HTTPError{http.StatusNotFound, "Collection doesn't exist."}
  702. case db.isHighLoadError(err):
  703. return nil, ErrUnavailable
  704. case err != nil:
  705. log.Error("Failed selecting from collections: %v", err)
  706. return nil, err
  707. }
  708. c.StyleSheet = styleSheet.String
  709. c.Script = script.String
  710. c.Signature = signature.String
  711. c.Format = format.String
  712. c.Public = c.IsPublic()
  713. c.Monetization = db.GetCollectionAttribute(c.ID, "monetization_pointer")
  714. c.db = db
  715. return c, nil
  716. }
  717. func (db *datastore) GetCollection(alias string) (*Collection, error) {
  718. return db.GetCollectionBy("alias = ?", alias)
  719. }
  720. func (db *datastore) GetCollectionForPad(alias string) (*Collection, error) {
  721. c := &Collection{Alias: alias}
  722. row := db.QueryRow("SELECT id, alias, title, description, privacy FROM collections WHERE alias = ?", alias)
  723. err := row.Scan(&c.ID, &c.Alias, &c.Title, &c.Description, &c.Visibility)
  724. switch {
  725. case err == sql.ErrNoRows:
  726. return c, impart.HTTPError{http.StatusNotFound, "Collection doesn't exist."}
  727. case err != nil:
  728. log.Error("Failed selecting from collections: %v", err)
  729. return c, ErrInternalGeneral
  730. }
  731. c.Public = c.IsPublic()
  732. return c, nil
  733. }
  734. func (db *datastore) GetCollectionByID(id int64) (*Collection, error) {
  735. return db.GetCollectionBy("id = ?", id)
  736. }
  737. func (db *datastore) GetCollectionFromDomain(host string) (*Collection, error) {
  738. return db.GetCollectionBy("host = ?", host)
  739. }
  740. func (db *datastore) UpdateCollection(c *SubmittedCollection, alias string) error {
  741. q := query.NewUpdate().
  742. SetStringPtr(c.Title, "title").
  743. SetStringPtr(c.Description, "description").
  744. SetNullString(c.StyleSheet, "style_sheet").
  745. SetNullString(c.Script, "script").
  746. SetNullString(c.Signature, "post_signature")
  747. if c.Format != nil {
  748. cf := &CollectionFormat{Format: c.Format.String}
  749. if cf.Valid() {
  750. q.SetNullString(c.Format, "format")
  751. }
  752. }
  753. var updatePass bool
  754. if c.Visibility != nil && (collVisibility(*c.Visibility)&CollProtected == 0 || c.Pass != "") {
  755. q.SetIntPtr(c.Visibility, "privacy")
  756. if c.Pass != "" {
  757. updatePass = true
  758. }
  759. }
  760. // WHERE values
  761. q.Where("alias = ? AND owner_id = ?", alias, c.OwnerID)
  762. if q.Updates == "" && c.Monetization == nil {
  763. return ErrPostNoUpdatableVals
  764. }
  765. // Find any current domain
  766. var collID int64
  767. var rowsAffected int64
  768. var changed bool
  769. var res sql.Result
  770. err := db.QueryRow("SELECT id FROM collections WHERE alias = ?", alias).Scan(&collID)
  771. if err != nil {
  772. log.Error("Failed selecting from collections: %v. Some things won't work.", err)
  773. }
  774. // Update MathJax value
  775. if c.MathJax {
  776. if db.driverName == driverSQLite {
  777. _, err = db.Exec("INSERT OR REPLACE INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?)", collID, "render_mathjax", "1")
  778. } else {
  779. _, err = db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?) "+db.upsert("collection_id", "attribute")+" value = ?", collID, "render_mathjax", "1", "1")
  780. }
  781. if err != nil {
  782. log.Error("Unable to insert render_mathjax value: %v", err)
  783. return err
  784. }
  785. } else {
  786. _, err = db.Exec("DELETE FROM collectionattributes WHERE collection_id = ? AND attribute = ?", collID, "render_mathjax")
  787. if err != nil {
  788. log.Error("Unable to delete render_mathjax value: %v", err)
  789. return err
  790. }
  791. }
  792. // Update Monetization value
  793. if c.Monetization != nil {
  794. skipUpdate := false
  795. if *c.Monetization != "" {
  796. // Strip away any excess spaces
  797. trimmed := strings.TrimSpace(*c.Monetization)
  798. // Only update value when it starts with "$", per spec: https://paymentpointers.org
  799. if strings.HasPrefix(trimmed, "$") {
  800. c.Monetization = &trimmed
  801. } else {
  802. // Value appears invalid, so don't update
  803. skipUpdate = true
  804. }
  805. }
  806. if !skipUpdate {
  807. _, err = db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = ?", collID, "monetization_pointer", *c.Monetization, *c.Monetization)
  808. if err != nil {
  809. log.Error("Unable to insert monetization_pointer value: %v", err)
  810. return err
  811. }
  812. }
  813. }
  814. // Update rest of the collection data
  815. if q.Updates != "" {
  816. res, err = db.Exec("UPDATE collections SET "+q.Updates+" WHERE "+q.Conditions, q.Params...)
  817. if err != nil {
  818. log.Error("Unable to update collection: %v", err)
  819. return err
  820. }
  821. }
  822. rowsAffected, _ = res.RowsAffected()
  823. if !changed || rowsAffected == 0 {
  824. // Show the correct error message if nothing was updated
  825. var dummy int
  826. err := db.QueryRow("SELECT 1 FROM collections WHERE alias = ? AND owner_id = ?", alias, c.OwnerID).Scan(&dummy)
  827. switch {
  828. case err == sql.ErrNoRows:
  829. return ErrUnauthorizedEditPost
  830. case err != nil:
  831. log.Error("Failed selecting from collections: %v", err)
  832. }
  833. if !updatePass {
  834. return nil
  835. }
  836. }
  837. if updatePass {
  838. hashedPass, err := auth.HashPass([]byte(c.Pass))
  839. if err != nil {
  840. log.Error("Unable to create hash: %s", err)
  841. return impart.HTTPError{http.StatusInternalServerError, "Could not create password hash."}
  842. }
  843. if db.driverName == driverSQLite {
  844. _, err = db.Exec("INSERT OR REPLACE INTO collectionpasswords (collection_id, password) VALUES ((SELECT id FROM collections WHERE alias = ?), ?)", alias, hashedPass)
  845. } else {
  846. _, err = db.Exec("INSERT INTO collectionpasswords (collection_id, password) VALUES ((SELECT id FROM collections WHERE alias = ?), ?) "+db.upsert("collection_id")+" password = ?", alias, hashedPass, hashedPass)
  847. }
  848. if err != nil {
  849. return err
  850. }
  851. }
  852. return nil
  853. }
  854. const postCols = "id, slug, text_appearance, language, rtl, privacy, owner_id, collection_id, pinned_position, created, updated, view_count, title, content"
  855. // getEditablePost returns a PublicPost with the given ID only if the given
  856. // edit token is valid for the post.
  857. func (db *datastore) GetEditablePost(id, editToken string) (*PublicPost, error) {
  858. // FIXME: code duplicated from getPost()
  859. // TODO: add slight logic difference to getPost / one func
  860. var ownerName sql.NullString
  861. p := &Post{}
  862. row := db.QueryRow("SELECT "+postCols+", (SELECT username FROM users WHERE users.id = posts.owner_id) AS username FROM posts WHERE id = ? LIMIT 1", id)
  863. err := row.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content, &ownerName)
  864. switch {
  865. case err == sql.ErrNoRows:
  866. return nil, ErrPostNotFound
  867. case err != nil:
  868. log.Error("Failed selecting from collections: %v", err)
  869. return nil, err
  870. }
  871. if p.Content == "" && p.Title.String == "" {
  872. return nil, ErrPostUnpublished
  873. }
  874. res := p.processPost()
  875. if ownerName.Valid {
  876. res.Owner = &PublicUser{Username: ownerName.String}
  877. }
  878. return &res, nil
  879. }
  880. func (db *datastore) PostIDExists(id string) bool {
  881. var dummy bool
  882. err := db.QueryRow("SELECT 1 FROM posts WHERE id = ?", id).Scan(&dummy)
  883. return err == nil && dummy
  884. }
  885. // GetPost gets a public-facing post object from the database. If collectionID
  886. // is > 0, the post will be retrieved by slug and collection ID, rather than
  887. // post ID.
  888. // TODO: break this into two functions:
  889. // - GetPost(id string)
  890. // - GetCollectionPost(slug string, collectionID int64)
  891. func (db *datastore) GetPost(id string, collectionID int64) (*PublicPost, error) {
  892. var ownerName sql.NullString
  893. p := &Post{}
  894. var row *sql.Row
  895. var where string
  896. params := []interface{}{id}
  897. if collectionID > 0 {
  898. where = "slug = ? AND collection_id = ?"
  899. params = append(params, collectionID)
  900. } else {
  901. where = "id = ?"
  902. }
  903. row = db.QueryRow("SELECT "+postCols+", (SELECT username FROM users WHERE users.id = posts.owner_id) AS username FROM posts WHERE "+where+" LIMIT 1", params...)
  904. err := row.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content, &ownerName)
  905. switch {
  906. case err == sql.ErrNoRows:
  907. if collectionID > 0 {
  908. return nil, ErrCollectionPageNotFound
  909. }
  910. return nil, ErrPostNotFound
  911. case err != nil:
  912. log.Error("Failed selecting from collections: %v", err)
  913. return nil, err
  914. }
  915. if p.Content == "" && p.Title.String == "" {
  916. return nil, ErrPostUnpublished
  917. }
  918. res := p.processPost()
  919. if ownerName.Valid {
  920. res.Owner = &PublicUser{Username: ownerName.String}
  921. }
  922. return &res, nil
  923. }
  924. // TODO: don't duplicate getPost() functionality
  925. func (db *datastore) GetOwnedPost(id string, ownerID int64) (*PublicPost, error) {
  926. p := &Post{}
  927. var row *sql.Row
  928. where := "id = ? AND owner_id = ?"
  929. params := []interface{}{id, ownerID}
  930. row = db.QueryRow("SELECT "+postCols+" FROM posts WHERE "+where+" LIMIT 1", params...)
  931. err := row.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content)
  932. switch {
  933. case err == sql.ErrNoRows:
  934. return nil, ErrPostNotFound
  935. case err != nil:
  936. log.Error("Failed selecting from collections: %v", err)
  937. return nil, err
  938. }
  939. if p.Content == "" && p.Title.String == "" {
  940. return nil, ErrPostUnpublished
  941. }
  942. res := p.processPost()
  943. return &res, nil
  944. }
  945. func (db *datastore) GetPostProperty(id string, collectionID int64, property string) (interface{}, error) {
  946. propSelects := map[string]string{
  947. "views": "view_count AS views",
  948. }
  949. selectQuery, ok := propSelects[property]
  950. if !ok {
  951. return nil, impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Invalid property: %s.", property)}
  952. }
  953. var res interface{}
  954. var row *sql.Row
  955. if collectionID != 0 {
  956. row = db.QueryRow("SELECT "+selectQuery+" FROM posts WHERE slug = ? AND collection_id = ? LIMIT 1", id, collectionID)
  957. } else {
  958. row = db.QueryRow("SELECT "+selectQuery+" FROM posts WHERE id = ? LIMIT 1", id)
  959. }
  960. err := row.Scan(&res)
  961. switch {
  962. case err == sql.ErrNoRows:
  963. return nil, impart.HTTPError{http.StatusNotFound, "Post not found."}
  964. case err != nil:
  965. log.Error("Failed selecting post: %v", err)
  966. return nil, err
  967. }
  968. return res, nil
  969. }
  970. // GetPostsCount modifies the CollectionObj to include the correct number of
  971. // standard (non-pinned) posts. It will return future posts if `includeFuture`
  972. // is true.
  973. func (db *datastore) GetPostsCount(c *CollectionObj, includeFuture bool) {
  974. var count int64
  975. timeCondition := ""
  976. if !includeFuture {
  977. timeCondition = "AND created <= " + db.now()
  978. }
  979. err := db.QueryRow("SELECT COUNT(*) FROM posts WHERE collection_id = ? AND pinned_position IS NULL "+timeCondition, c.ID).Scan(&count)
  980. switch {
  981. case err == sql.ErrNoRows:
  982. c.TotalPosts = 0
  983. case err != nil:
  984. log.Error("Failed selecting from collections: %v", err)
  985. c.TotalPosts = 0
  986. }
  987. c.TotalPosts = int(count)
  988. }
  989. // GetPosts retrieves all posts for the given Collection.
  990. // It will return future posts if `includeFuture` is true.
  991. // It will include only standard (non-pinned) posts unless `includePinned` is true.
  992. // TODO: change includeFuture to isOwner, since that's how it's used
  993. func (db *datastore) GetPosts(cfg *config.Config, c *Collection, page int, includeFuture, forceRecentFirst, includePinned bool) (*[]PublicPost, error) {
  994. collID := c.ID
  995. cf := c.NewFormat()
  996. order := "DESC"
  997. if cf.Ascending() && !forceRecentFirst {
  998. order = "ASC"
  999. }
  1000. pagePosts := cf.PostsPerPage()
  1001. start := page*pagePosts - pagePosts
  1002. if page == 0 {
  1003. start = 0
  1004. pagePosts = 1000
  1005. }
  1006. limitStr := ""
  1007. if page > 0 {
  1008. limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts)
  1009. }
  1010. timeCondition := ""
  1011. if !includeFuture {
  1012. timeCondition = "AND created <= " + db.now()
  1013. }
  1014. pinnedCondition := ""
  1015. if !includePinned {
  1016. pinnedCondition = "AND pinned_position IS NULL"
  1017. }
  1018. rows, err := db.Query("SELECT "+postCols+" FROM posts WHERE collection_id = ? "+pinnedCondition+" "+timeCondition+" ORDER BY created "+order+limitStr, collID)
  1019. if err != nil {
  1020. log.Error("Failed selecting from posts: %v", err)
  1021. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."}
  1022. }
  1023. defer rows.Close()
  1024. // TODO: extract this common row scanning logic for queries using `postCols`
  1025. posts := []PublicPost{}
  1026. for rows.Next() {
  1027. p := &Post{}
  1028. err = rows.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content)
  1029. if err != nil {
  1030. log.Error("Failed scanning row: %v", err)
  1031. break
  1032. }
  1033. p.extractData()
  1034. p.augmentContent(c)
  1035. p.formatContent(cfg, c, includeFuture, false)
  1036. posts = append(posts, p.processPost())
  1037. }
  1038. err = rows.Err()
  1039. if err != nil {
  1040. log.Error("Error after Next() on rows: %v", err)
  1041. }
  1042. return &posts, nil
  1043. }
  1044. // GetPostsTagged retrieves all posts on the given Collection that contain the
  1045. // given tag.
  1046. // It will return future posts if `includeFuture` is true.
  1047. // TODO: change includeFuture to isOwner, since that's how it's used
  1048. func (db *datastore) GetPostsTagged(cfg *config.Config, c *Collection, tag string, page int, includeFuture bool) (*[]PublicPost, error) {
  1049. collID := c.ID
  1050. cf := c.NewFormat()
  1051. order := "DESC"
  1052. if cf.Ascending() {
  1053. order = "ASC"
  1054. }
  1055. pagePosts := cf.PostsPerPage()
  1056. start := page*pagePosts - pagePosts
  1057. if page == 0 {
  1058. start = 0
  1059. pagePosts = 1000
  1060. }
  1061. limitStr := ""
  1062. if page > 0 {
  1063. limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts)
  1064. }
  1065. timeCondition := ""
  1066. if !includeFuture {
  1067. timeCondition = "AND created <= " + db.now()
  1068. }
  1069. var rows *sql.Rows
  1070. var err error
  1071. if db.driverName == driverSQLite {
  1072. 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.*`)
  1073. } else {
  1074. 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)+"[[:>:]]")
  1075. }
  1076. if err != nil {
  1077. log.Error("Failed selecting from posts: %v", err)
  1078. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."}
  1079. }
  1080. defer rows.Close()
  1081. // TODO: extract this common row scanning logic for queries using `postCols`
  1082. posts := []PublicPost{}
  1083. for rows.Next() {
  1084. p := &Post{}
  1085. err = rows.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content)
  1086. if err != nil {
  1087. log.Error("Failed scanning row: %v", err)
  1088. break
  1089. }
  1090. p.extractData()
  1091. p.augmentContent(c)
  1092. p.formatContent(cfg, c, includeFuture, false)
  1093. posts = append(posts, p.processPost())
  1094. }
  1095. err = rows.Err()
  1096. if err != nil {
  1097. log.Error("Error after Next() on rows: %v", err)
  1098. }
  1099. return &posts, nil
  1100. }
  1101. func (db *datastore) GetAPFollowers(c *Collection) (*[]RemoteUser, error) {
  1102. 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)
  1103. if err != nil {
  1104. log.Error("Failed selecting from followers: %v", err)
  1105. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve followers."}
  1106. }
  1107. defer rows.Close()
  1108. followers := []RemoteUser{}
  1109. for rows.Next() {
  1110. f := RemoteUser{}
  1111. err = rows.Scan(&f.ActorID, &f.Inbox, &f.SharedInbox)
  1112. followers = append(followers, f)
  1113. }
  1114. return &followers, nil
  1115. }
  1116. // CanCollect returns whether or not the given user can add the given post to a
  1117. // collection. This is true when a post is already owned by the user.
  1118. // NOTE: this is currently only used to potentially add owned posts to a
  1119. // collection. This has the SIDE EFFECT of also generating a slug for the post.
  1120. // FIXME: make this side effect more explicit (or extract it)
  1121. func (db *datastore) CanCollect(cpr *ClaimPostRequest, userID int64) bool {
  1122. var title, content string
  1123. var lang sql.NullString
  1124. err := db.QueryRow("SELECT title, content, language FROM posts WHERE id = ? AND owner_id = ?", cpr.ID, userID).Scan(&title, &content, &lang)
  1125. switch {
  1126. case err == sql.ErrNoRows:
  1127. return false
  1128. case err != nil:
  1129. log.Error("Failed on post CanCollect(%s, %d): %v", cpr.ID, userID, err)
  1130. return false
  1131. }
  1132. // Since we have the post content and the post is collectable, generate the
  1133. // post's slug now.
  1134. cpr.Slug = getSlugFromPost(title, content, lang.String)
  1135. return true
  1136. }
  1137. func (db *datastore) AttemptClaim(p *ClaimPostRequest, query string, params []interface{}, slugIdx int) (sql.Result, error) {
  1138. qRes, err := db.Exec(query, params...)
  1139. if err != nil {
  1140. if db.isDuplicateKeyErr(err) && slugIdx > -1 {
  1141. s := id.GenSafeUniqueSlug(p.Slug)
  1142. if s == p.Slug {
  1143. // Sanity check to prevent infinite recursion
  1144. return qRes, fmt.Errorf("GenSafeUniqueSlug generated nothing unique: %s", s)
  1145. }
  1146. p.Slug = s
  1147. params[slugIdx] = p.Slug
  1148. return db.AttemptClaim(p, query, params, slugIdx)
  1149. }
  1150. return qRes, fmt.Errorf("attemptClaim: %s", err)
  1151. }
  1152. return qRes, nil
  1153. }
  1154. func (db *datastore) DispersePosts(userID int64, postIDs []string) (*[]ClaimPostResult, error) {
  1155. postClaimReqs := map[string]bool{}
  1156. res := []ClaimPostResult{}
  1157. for i := range postIDs {
  1158. postID := postIDs[i]
  1159. r := ClaimPostResult{Code: 0, ErrorMessage: ""}
  1160. // Perform post validation
  1161. if postID == "" {
  1162. r.ErrorMessage = "Missing post ID. "
  1163. }
  1164. if _, ok := postClaimReqs[postID]; ok {
  1165. r.Code = 429
  1166. r.ErrorMessage = "You've already tried anonymizing this post."
  1167. r.ID = postID
  1168. res = append(res, r)
  1169. continue
  1170. }
  1171. postClaimReqs[postID] = true
  1172. var err error
  1173. // Get full post information to return
  1174. var fullPost *PublicPost
  1175. fullPost, err = db.GetPost(postID, 0)
  1176. if err != nil {
  1177. if err, ok := err.(impart.HTTPError); ok {
  1178. r.Code = err.Status
  1179. r.ErrorMessage = err.Message
  1180. r.ID = postID
  1181. res = append(res, r)
  1182. continue
  1183. } else {
  1184. log.Error("Error getting post in dispersePosts: %v", err)
  1185. }
  1186. }
  1187. if fullPost.OwnerID.Int64 != userID {
  1188. r.Code = http.StatusConflict
  1189. r.ErrorMessage = "Post is already owned by someone else."
  1190. r.ID = postID
  1191. res = append(res, r)
  1192. continue
  1193. }
  1194. var qRes sql.Result
  1195. var query string
  1196. var params []interface{}
  1197. // Do AND owner_id = ? for sanity.
  1198. // This should've been caught and returned with a good error message
  1199. // just above.
  1200. query = "UPDATE posts SET collection_id = NULL WHERE id = ? AND owner_id = ?"
  1201. params = []interface{}{postID, userID}
  1202. qRes, err = db.Exec(query, params...)
  1203. if err != nil {
  1204. r.Code = http.StatusInternalServerError
  1205. r.ErrorMessage = "A glitch happened on our end."
  1206. r.ID = postID
  1207. res = append(res, r)
  1208. log.Error("dispersePosts (post %s): %v", postID, err)
  1209. continue
  1210. }
  1211. // Post was successfully dispersed
  1212. r.Code = http.StatusOK
  1213. r.Post = fullPost
  1214. rowsAffected, _ := qRes.RowsAffected()
  1215. if rowsAffected == 0 {
  1216. // This was already claimed, but return 200
  1217. r.Code = http.StatusOK
  1218. }
  1219. res = append(res, r)
  1220. }
  1221. return &res, nil
  1222. }
  1223. func (db *datastore) ClaimPosts(cfg *config.Config, userID int64, collAlias string, posts *[]ClaimPostRequest) (*[]ClaimPostResult, error) {
  1224. postClaimReqs := map[string]bool{}
  1225. res := []ClaimPostResult{}
  1226. postCollAlias := collAlias
  1227. for i := range *posts {
  1228. p := (*posts)[i]
  1229. if &p == nil {
  1230. continue
  1231. }
  1232. r := ClaimPostResult{Code: 0, ErrorMessage: ""}
  1233. // Perform post validation
  1234. if p.ID == "" {
  1235. r.ErrorMessage = "Missing post ID `id`. "
  1236. }
  1237. if _, ok := postClaimReqs[p.ID]; ok {
  1238. r.Code = 429
  1239. r.ErrorMessage = "You've already tried claiming this post."
  1240. r.ID = p.ID
  1241. res = append(res, r)
  1242. continue
  1243. }
  1244. postClaimReqs[p.ID] = true
  1245. canCollect := db.CanCollect(&p, userID)
  1246. if !canCollect && p.Token == "" {
  1247. // TODO: ensure post isn't owned by anyone else when a valid modify
  1248. // token is given.
  1249. r.ErrorMessage += "Missing post Edit Token `token`."
  1250. }
  1251. if r.ErrorMessage != "" {
  1252. // Post validate failed
  1253. r.Code = http.StatusBadRequest
  1254. r.ID = p.ID
  1255. res = append(res, r)
  1256. continue
  1257. }
  1258. var err error
  1259. var qRes sql.Result
  1260. var query string
  1261. var params []interface{}
  1262. var slugIdx int = -1
  1263. var coll *Collection
  1264. if collAlias == "" {
  1265. // Posts are being claimed at /posts/claim, not
  1266. // /collections/{alias}/collect, so use given individual collection
  1267. // to associate post with.
  1268. postCollAlias = p.CollectionAlias
  1269. }
  1270. if postCollAlias != "" {
  1271. // Associate this post with a collection
  1272. if p.CreateCollection {
  1273. // This is a new collection
  1274. // TODO: consider removing this. This seriously complicates this
  1275. // method and adds another (unnecessary?) logic path.
  1276. coll, err = db.CreateCollection(cfg, postCollAlias, "", userID)
  1277. if err != nil {
  1278. if err, ok := err.(impart.HTTPError); ok {
  1279. r.Code = err.Status
  1280. r.ErrorMessage = err.Message
  1281. } else {
  1282. r.Code = http.StatusInternalServerError
  1283. r.ErrorMessage = "Unknown error occurred creating collection"
  1284. }
  1285. r.ID = p.ID
  1286. res = append(res, r)
  1287. continue
  1288. }
  1289. } else {
  1290. // Attempt to add to existing collection
  1291. coll, err = db.GetCollection(postCollAlias)
  1292. if err != nil {
  1293. if err, ok := err.(impart.HTTPError); ok {
  1294. if err.Status == http.StatusNotFound {
  1295. // Show obfuscated "forbidden" response, as if attempting to add to an
  1296. // unowned blog.
  1297. r.Code = ErrForbiddenCollection.Status
  1298. r.ErrorMessage = ErrForbiddenCollection.Message
  1299. } else {
  1300. r.Code = err.Status
  1301. r.ErrorMessage = err.Message
  1302. }
  1303. } else {
  1304. r.Code = http.StatusInternalServerError
  1305. r.ErrorMessage = "Unknown error occurred claiming post with collection"
  1306. }
  1307. r.ID = p.ID
  1308. res = append(res, r)
  1309. continue
  1310. }
  1311. if coll.OwnerID != userID {
  1312. r.Code = ErrForbiddenCollection.Status
  1313. r.ErrorMessage = ErrForbiddenCollection.Message
  1314. r.ID = p.ID
  1315. res = append(res, r)
  1316. continue
  1317. }
  1318. }
  1319. if p.Slug == "" {
  1320. p.Slug = p.ID
  1321. }
  1322. if canCollect {
  1323. // User already owns this post, so just add it to the given
  1324. // collection.
  1325. query = "UPDATE posts SET collection_id = ?, slug = ? WHERE id = ? AND owner_id = ?"
  1326. params = []interface{}{coll.ID, p.Slug, p.ID, userID}
  1327. slugIdx = 1
  1328. } else {
  1329. query = "UPDATE posts SET owner_id = ?, collection_id = ?, slug = ? WHERE id = ? AND modify_token = ? AND owner_id IS NULL"
  1330. params = []interface{}{userID, coll.ID, p.Slug, p.ID, p.Token}
  1331. slugIdx = 2
  1332. }
  1333. } else {
  1334. query = "UPDATE posts SET owner_id = ? WHERE id = ? AND modify_token = ? AND owner_id IS NULL"
  1335. params = []interface{}{userID, p.ID, p.Token}
  1336. }
  1337. qRes, err = db.AttemptClaim(&p, query, params, slugIdx)
  1338. if err != nil {
  1339. r.Code = http.StatusInternalServerError
  1340. r.ErrorMessage = "An unknown error occurred."
  1341. r.ID = p.ID
  1342. res = append(res, r)
  1343. log.Error("claimPosts (post %s): %v", p.ID, err)
  1344. continue
  1345. }
  1346. // Get full post information to return
  1347. var fullPost *PublicPost
  1348. if p.Token != "" {
  1349. fullPost, err = db.GetEditablePost(p.ID, p.Token)
  1350. } else {
  1351. fullPost, err = db.GetPost(p.ID, 0)
  1352. }
  1353. if err != nil {
  1354. if err, ok := err.(impart.HTTPError); ok {
  1355. r.Code = err.Status
  1356. r.ErrorMessage = err.Message
  1357. r.ID = p.ID
  1358. res = append(res, r)
  1359. continue
  1360. }
  1361. }
  1362. if fullPost.OwnerID.Int64 != userID {
  1363. r.Code = http.StatusConflict
  1364. r.ErrorMessage = "Post is already owned by someone else."
  1365. r.ID = p.ID
  1366. res = append(res, r)
  1367. continue
  1368. }
  1369. // Post was successfully claimed
  1370. r.Code = http.StatusOK
  1371. r.Post = fullPost
  1372. if coll != nil {
  1373. r.Post.Collection = &CollectionObj{Collection: *coll}
  1374. }
  1375. rowsAffected, _ := qRes.RowsAffected()
  1376. if rowsAffected == 0 {
  1377. // This was already claimed, but return 200
  1378. r.Code = http.StatusOK
  1379. }
  1380. res = append(res, r)
  1381. }
  1382. return &res, nil
  1383. }
  1384. func (db *datastore) UpdatePostPinState(pinned bool, postID string, collID, ownerID, pos int64) error {
  1385. if pos <= 0 || pos > 20 {
  1386. pos = db.GetLastPinnedPostPos(collID) + 1
  1387. if pos == -1 {
  1388. pos = 1
  1389. }
  1390. }
  1391. var err error
  1392. if pinned {
  1393. _, err = db.Exec("UPDATE posts SET pinned_position = ? WHERE id = ?", pos, postID)
  1394. } else {
  1395. _, err = db.Exec("UPDATE posts SET pinned_position = NULL WHERE id = ?", postID)
  1396. }
  1397. if err != nil {
  1398. log.Error("Unable to update pinned post: %v", err)
  1399. return err
  1400. }
  1401. return nil
  1402. }
  1403. func (db *datastore) GetLastPinnedPostPos(collID int64) int64 {
  1404. var lastPos sql.NullInt64
  1405. err := db.QueryRow("SELECT MAX(pinned_position) FROM posts WHERE collection_id = ? AND pinned_position IS NOT NULL", collID).Scan(&lastPos)
  1406. switch {
  1407. case err == sql.ErrNoRows:
  1408. return -1
  1409. case err != nil:
  1410. log.Error("Failed selecting from posts: %v", err)
  1411. return -1
  1412. }
  1413. if !lastPos.Valid {
  1414. return -1
  1415. }
  1416. return lastPos.Int64
  1417. }
  1418. func (db *datastore) GetPinnedPosts(coll *CollectionObj, includeFuture bool) (*[]PublicPost, error) {
  1419. // FIXME: sqlite-backed instances don't include ellipsis on truncated titles
  1420. timeCondition := ""
  1421. if !includeFuture {
  1422. timeCondition = "AND created <= " + db.now()
  1423. }
  1424. 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 "+timeCondition+" ORDER BY pinned_position ASC", coll.ID)
  1425. if err != nil {
  1426. log.Error("Failed selecting pinned posts: %v", err)
  1427. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve pinned posts."}
  1428. }
  1429. defer rows.Close()
  1430. posts := []PublicPost{}
  1431. for rows.Next() {
  1432. p := &Post{}
  1433. err = rows.Scan(&p.ID, &p.Slug, &p.Title, &p.Content, &p.PinnedPosition)
  1434. if err != nil {
  1435. log.Error("Failed scanning row: %v", err)
  1436. break
  1437. }
  1438. p.extractData()
  1439. p.augmentContent(&coll.Collection)
  1440. pp := p.processPost()
  1441. pp.Collection = coll
  1442. posts = append(posts, pp)
  1443. }
  1444. return &posts, nil
  1445. }
  1446. func (db *datastore) GetCollections(u *User, hostName string) (*[]Collection, error) {
  1447. rows, err := db.Query("SELECT id, alias, title, description, privacy, view_count FROM collections WHERE owner_id = ? ORDER BY id ASC", u.ID)
  1448. if err != nil {
  1449. log.Error("Failed selecting from collections: %v", err)
  1450. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user collections."}
  1451. }
  1452. defer rows.Close()
  1453. colls := []Collection{}
  1454. for rows.Next() {
  1455. c := Collection{}
  1456. err = rows.Scan(&c.ID, &c.Alias, &c.Title, &c.Description, &c.Visibility, &c.Views)
  1457. if err != nil {
  1458. log.Error("Failed scanning row: %v", err)
  1459. break
  1460. }
  1461. c.hostName = hostName
  1462. c.URL = c.CanonicalURL()
  1463. c.Public = c.IsPublic()
  1464. /*
  1465. // NOTE: future functionality
  1466. if visibility != nil { // TODO: && visibility == CollPublic {
  1467. // Add Monetization info when retrieving all public collections
  1468. c.Monetization = db.GetCollectionAttribute(c.ID, "monetization_pointer")
  1469. }
  1470. */
  1471. colls = append(colls, c)
  1472. }
  1473. err = rows.Err()
  1474. if err != nil {
  1475. log.Error("Error after Next() on rows: %v", err)
  1476. }
  1477. return &colls, nil
  1478. }
  1479. func (db *datastore) GetPublishableCollections(u *User, hostName string) (*[]Collection, error) {
  1480. c, err := db.GetCollections(u, hostName)
  1481. if err != nil {
  1482. return nil, err
  1483. }
  1484. if len(*c) == 0 {
  1485. return nil, 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."}
  1486. }
  1487. return c, nil
  1488. }
  1489. func (db *datastore) GetPublicCollections(hostName string) (*[]Collection, error) {
  1490. rows, err := db.Query(`SELECT c.id, alias, title, description, privacy, view_count
  1491. FROM collections c
  1492. LEFT JOIN users u ON u.id = c.owner_id
  1493. WHERE c.privacy = 1 AND u.status = 0
  1494. ORDER BY id ASC`)
  1495. if err != nil {
  1496. log.Error("Failed selecting public collections: %v", err)
  1497. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve public collections."}
  1498. }
  1499. defer rows.Close()
  1500. colls := []Collection{}
  1501. for rows.Next() {
  1502. c := Collection{}
  1503. err = rows.Scan(&c.ID, &c.Alias, &c.Title, &c.Description, &c.Visibility, &c.Views)
  1504. if err != nil {
  1505. log.Error("Failed scanning row: %v", err)
  1506. break
  1507. }
  1508. c.hostName = hostName
  1509. c.URL = c.CanonicalURL()
  1510. c.Public = c.IsPublic()
  1511. // Add Monetization information
  1512. c.Monetization = db.GetCollectionAttribute(c.ID, "monetization_pointer")
  1513. colls = append(colls, c)
  1514. }
  1515. err = rows.Err()
  1516. if err != nil {
  1517. log.Error("Error after Next() on rows: %v", err)
  1518. }
  1519. return &colls, nil
  1520. }
  1521. func (db *datastore) GetMeStats(u *User) userMeStats {
  1522. s := userMeStats{}
  1523. // User counts
  1524. colls, _ := db.GetUserCollectionCount(u.ID)
  1525. s.TotalCollections = colls
  1526. var articles, collPosts uint64
  1527. err := db.QueryRow("SELECT COUNT(*) FROM posts WHERE owner_id = ? AND collection_id IS NULL", u.ID).Scan(&articles)
  1528. if err != nil && err != sql.ErrNoRows {
  1529. log.Error("Couldn't get articles count for user %d: %v", u.ID, err)
  1530. }
  1531. s.TotalArticles = articles
  1532. err = db.QueryRow("SELECT COUNT(*) FROM posts WHERE owner_id = ? AND collection_id IS NOT NULL", u.ID).Scan(&collPosts)
  1533. if err != nil && err != sql.ErrNoRows {
  1534. log.Error("Couldn't get coll posts count for user %d: %v", u.ID, err)
  1535. }
  1536. s.CollectionPosts = collPosts
  1537. return s
  1538. }
  1539. func (db *datastore) GetTotalCollections() (collCount int64, err error) {
  1540. err = db.QueryRow(`
  1541. SELECT COUNT(*)
  1542. FROM collections c
  1543. LEFT JOIN users u ON u.id = c.owner_id
  1544. WHERE u.status = 0`).Scan(&collCount)
  1545. if err != nil {
  1546. log.Error("Unable to fetch collections count: %v", err)
  1547. }
  1548. return
  1549. }
  1550. func (db *datastore) GetTotalPosts() (postCount int64, err error) {
  1551. err = db.QueryRow(`
  1552. SELECT COUNT(*)
  1553. FROM posts p
  1554. LEFT JOIN users u ON u.id = p.owner_id
  1555. WHERE u.status = 0`).Scan(&postCount)
  1556. if err != nil {
  1557. log.Error("Unable to fetch posts count: %v", err)
  1558. }
  1559. return
  1560. }
  1561. func (db *datastore) GetTopPosts(u *User, alias string) (*[]PublicPost, error) {
  1562. params := []interface{}{u.ID}
  1563. where := ""
  1564. if alias != "" {
  1565. where = " AND alias = ?"
  1566. params = append(params, alias)
  1567. }
  1568. 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...)
  1569. if err != nil {
  1570. log.Error("Failed selecting from posts: %v", err)
  1571. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user top posts."}
  1572. }
  1573. defer rows.Close()
  1574. posts := []PublicPost{}
  1575. var gotErr bool
  1576. for rows.Next() {
  1577. p := Post{}
  1578. c := Collection{}
  1579. var alias, title, description sql.NullString
  1580. var views sql.NullInt64
  1581. err = rows.Scan(&p.ID, &p.Slug, &p.ViewCount, &p.Title, &alias, &title, &description, &views)
  1582. if err != nil {
  1583. log.Error("Failed scanning User.getPosts() row: %v", err)
  1584. gotErr = true
  1585. break
  1586. }
  1587. p.extractData()
  1588. pubPost := p.processPost()
  1589. if alias.Valid && alias.String != "" {
  1590. c.Alias = alias.String
  1591. c.Title = title.String
  1592. c.Description = description.String
  1593. c.Views = views.Int64
  1594. pubPost.Collection = &CollectionObj{Collection: c}
  1595. }
  1596. posts = append(posts, pubPost)
  1597. }
  1598. err = rows.Err()
  1599. if err != nil {
  1600. log.Error("Error after Next() on rows: %v", err)
  1601. }
  1602. if gotErr && len(posts) == 0 {
  1603. // There were a lot of errors
  1604. return nil, impart.HTTPError{http.StatusInternalServerError, "Unable to get data."}
  1605. }
  1606. return &posts, nil
  1607. }
  1608. func (db *datastore) GetAnonymousPosts(u *User, page int) (*[]PublicPost, error) {
  1609. pagePosts := 10
  1610. start := page*pagePosts - pagePosts
  1611. if page == 0 {
  1612. start = 0
  1613. pagePosts = 1000
  1614. }
  1615. limitStr := ""
  1616. if page > 0 {
  1617. limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts)
  1618. }
  1619. 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"+limitStr, u.ID)
  1620. if err != nil {
  1621. log.Error("Failed selecting from posts: %v", err)
  1622. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user anonymous posts."}
  1623. }
  1624. defer rows.Close()
  1625. posts := []PublicPost{}
  1626. for rows.Next() {
  1627. p := Post{}
  1628. err = rows.Scan(&p.ID, &p.ViewCount, &p.Title, &p.Created, &p.Updated, &p.Content)
  1629. if err != nil {
  1630. log.Error("Failed scanning row: %v", err)
  1631. break
  1632. }
  1633. p.extractData()
  1634. posts = append(posts, p.processPost())
  1635. }
  1636. err = rows.Err()
  1637. if err != nil {
  1638. log.Error("Error after Next() on rows: %v", err)
  1639. }
  1640. return &posts, nil
  1641. }
  1642. func (db *datastore) GetUserPosts(u *User) (*[]PublicPost, error) {
  1643. 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)
  1644. if err != nil {
  1645. log.Error("Failed selecting from posts: %v", err)
  1646. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user posts."}
  1647. }
  1648. defer rows.Close()
  1649. posts := []PublicPost{}
  1650. var gotErr bool
  1651. for rows.Next() {
  1652. p := Post{}
  1653. c := Collection{}
  1654. var alias, title, description sql.NullString
  1655. var views sql.NullInt64
  1656. err = rows.Scan(&p.ID, &p.Slug, &p.ViewCount, &p.Title, &p.Created, &p.Updated, &p.Content, &p.Font, &p.Language, &p.RTL, &alias, &title, &description, &views)
  1657. if err != nil {
  1658. log.Error("Failed scanning User.getPosts() row: %v", err)
  1659. gotErr = true
  1660. break
  1661. }
  1662. p.extractData()
  1663. pubPost := p.processPost()
  1664. if alias.Valid && alias.String != "" {
  1665. c.Alias = alias.String
  1666. c.Title = title.String
  1667. c.Description = description.String
  1668. c.Views = views.Int64
  1669. pubPost.Collection = &CollectionObj{Collection: c}
  1670. }
  1671. posts = append(posts, pubPost)
  1672. }
  1673. err = rows.Err()
  1674. if err != nil {
  1675. log.Error("Error after Next() on rows: %v", err)
  1676. }
  1677. if gotErr && len(posts) == 0 {
  1678. // There were a lot of errors
  1679. return nil, impart.HTTPError{http.StatusInternalServerError, "Unable to get data."}
  1680. }
  1681. return &posts, nil
  1682. }
  1683. func (db *datastore) GetUserPostsCount(userID int64) int64 {
  1684. var count int64
  1685. err := db.QueryRow("SELECT COUNT(*) FROM posts WHERE owner_id = ?", userID).Scan(&count)
  1686. switch {
  1687. case err == sql.ErrNoRows:
  1688. return 0
  1689. case err != nil:
  1690. log.Error("Failed selecting posts count for user %d: %v", userID, err)
  1691. return 0
  1692. }
  1693. return count
  1694. }
  1695. // ChangeSettings takes a User and applies the changes in the given
  1696. // userSettings, MODIFYING THE USER with successful changes.
  1697. func (db *datastore) ChangeSettings(app *App, u *User, s *userSettings) error {
  1698. var errPass error
  1699. q := query.NewUpdate()
  1700. // Update email if given
  1701. if s.Email != "" {
  1702. encEmail, err := data.Encrypt(app.keys.EmailKey, s.Email)
  1703. if err != nil {
  1704. log.Error("Couldn't encrypt email %s: %s\n", s.Email, err)
  1705. return impart.HTTPError{http.StatusInternalServerError, "Unable to encrypt email address."}
  1706. }
  1707. q.SetBytes(encEmail, "email")
  1708. // Update the email if something goes awry updating the password
  1709. defer func() {
  1710. if errPass != nil {
  1711. db.UpdateEncryptedUserEmail(u.ID, encEmail)
  1712. }
  1713. }()
  1714. u.Email = zero.StringFrom(s.Email)
  1715. }
  1716. // Update username if given
  1717. var newUsername string
  1718. if s.Username != "" {
  1719. var ie *impart.HTTPError
  1720. newUsername, ie = getValidUsername(app, s.Username, u.Username)
  1721. if ie != nil {
  1722. // Username is invalid
  1723. return *ie
  1724. }
  1725. if !author.IsValidUsername(app.cfg, newUsername) {
  1726. // Ensure the username is syntactically correct.
  1727. return impart.HTTPError{http.StatusPreconditionFailed, "Username isn't valid."}
  1728. }
  1729. t, err := db.Begin()
  1730. if err != nil {
  1731. log.Error("Couldn't start username change transaction: %v", err)
  1732. return err
  1733. }
  1734. _, err = t.Exec("UPDATE users SET username = ? WHERE id = ?", newUsername, u.ID)
  1735. if err != nil {
  1736. t.Rollback()
  1737. if db.isDuplicateKeyErr(err) {
  1738. return impart.HTTPError{http.StatusConflict, "Username is already taken."}
  1739. }
  1740. log.Error("Unable to update users table: %v", err)
  1741. return ErrInternalGeneral
  1742. }
  1743. _, err = t.Exec("UPDATE collections SET alias = ? WHERE alias = ? AND owner_id = ?", newUsername, u.Username, u.ID)
  1744. if err != nil {
  1745. t.Rollback()
  1746. if db.isDuplicateKeyErr(err) {
  1747. return impart.HTTPError{http.StatusConflict, "Username is already taken."}
  1748. }
  1749. log.Error("Unable to update collection: %v", err)
  1750. return ErrInternalGeneral
  1751. }
  1752. // Keep track of name changes for redirection
  1753. db.RemoveCollectionRedirect(t, newUsername)
  1754. _, err = t.Exec("UPDATE collectionredirects SET new_alias = ? WHERE new_alias = ?", newUsername, u.Username)
  1755. if err != nil {
  1756. log.Error("Unable to update collectionredirects: %v", err)
  1757. }
  1758. _, err = t.Exec("INSERT INTO collectionredirects (prev_alias, new_alias) VALUES (?, ?)", u.Username, newUsername)
  1759. if err != nil {
  1760. log.Error("Unable to add new collectionredirect: %v", err)
  1761. }
  1762. err = t.Commit()
  1763. if err != nil {
  1764. t.Rollback()
  1765. log.Error("Rolling back after Commit(): %v\n", err)
  1766. return err
  1767. }
  1768. u.Username = newUsername
  1769. }
  1770. // Update passphrase if given
  1771. if s.NewPass != "" {
  1772. // Check if user has already set a password
  1773. var err error
  1774. u.HasPass, err = db.IsUserPassSet(u.ID)
  1775. if err != nil {
  1776. errPass = impart.HTTPError{http.StatusInternalServerError, "Unable to retrieve user data."}
  1777. return errPass
  1778. }
  1779. if u.HasPass {
  1780. // Check if currently-set password is correct
  1781. hashedPass := u.HashedPass
  1782. if len(hashedPass) == 0 {
  1783. authUser, err := db.GetUserForAuthByID(u.ID)
  1784. if err != nil {
  1785. errPass = err
  1786. return errPass
  1787. }
  1788. hashedPass = authUser.HashedPass
  1789. }
  1790. if !auth.Authenticated(hashedPass, []byte(s.OldPass)) {
  1791. errPass = impart.HTTPError{http.StatusUnauthorized, "Incorrect password."}
  1792. return errPass
  1793. }
  1794. }
  1795. hashedPass, err := auth.HashPass([]byte(s.NewPass))
  1796. if err != nil {
  1797. errPass = impart.HTTPError{http.StatusInternalServerError, "Could not create password hash."}
  1798. return errPass
  1799. }
  1800. q.SetBytes(hashedPass, "password")
  1801. }
  1802. // WHERE values
  1803. q.Append(u.ID)
  1804. if q.Updates == "" {
  1805. if s.Username == "" {
  1806. return ErrPostNoUpdatableVals
  1807. }
  1808. // Nothing to update except username. That was successful, so return now.
  1809. return nil
  1810. }
  1811. res, err := db.Exec("UPDATE users SET "+q.Updates+" WHERE id = ?", q.Params...)
  1812. if err != nil {
  1813. log.Error("Unable to update collection: %v", err)
  1814. return err
  1815. }
  1816. rowsAffected, _ := res.RowsAffected()
  1817. if rowsAffected == 0 {
  1818. // Show the correct error message if nothing was updated
  1819. var dummy int
  1820. err := db.QueryRow("SELECT 1 FROM users WHERE id = ?", u.ID).Scan(&dummy)
  1821. switch {
  1822. case err == sql.ErrNoRows:
  1823. return ErrUnauthorizedGeneral
  1824. case err != nil:
  1825. log.Error("Failed selecting from users: %v", err)
  1826. }
  1827. return nil
  1828. }
  1829. if s.NewPass != "" && !u.HasPass {
  1830. u.HasPass = true
  1831. }
  1832. return nil
  1833. }
  1834. func (db *datastore) ChangePassphrase(userID int64, sudo bool, curPass string, hashedPass []byte) error {
  1835. var dbPass []byte
  1836. err := db.QueryRow("SELECT password FROM users WHERE id = ?", userID).Scan(&dbPass)
  1837. switch {
  1838. case err == sql.ErrNoRows:
  1839. return ErrUserNotFound
  1840. case err != nil:
  1841. log.Error("Couldn't SELECT user password for change: %v", err)
  1842. return err
  1843. }
  1844. if !sudo && !auth.Authenticated(dbPass, []byte(curPass)) {
  1845. return impart.HTTPError{http.StatusUnauthorized, "Incorrect password."}
  1846. }
  1847. _, err = db.Exec("UPDATE users SET password = ? WHERE id = ?", hashedPass, userID)
  1848. if err != nil {
  1849. log.Error("Could not update passphrase: %v", err)
  1850. return err
  1851. }
  1852. return nil
  1853. }
  1854. func (db *datastore) RemoveCollectionRedirect(t *sql.Tx, alias string) error {
  1855. _, err := t.Exec("DELETE FROM collectionredirects WHERE prev_alias = ?", alias)
  1856. if err != nil {
  1857. log.Error("Unable to delete from collectionredirects: %v", err)
  1858. return err
  1859. }
  1860. return nil
  1861. }
  1862. func (db *datastore) GetCollectionRedirect(alias string) (new string) {
  1863. row := db.QueryRow("SELECT new_alias FROM collectionredirects WHERE prev_alias = ?", alias)
  1864. err := row.Scan(&new)
  1865. if err != nil && err != sql.ErrNoRows && !db.isIgnorableError(err) {
  1866. log.Error("Failed selecting from collectionredirects: %v", err)
  1867. }
  1868. return
  1869. }
  1870. func (db *datastore) DeleteCollection(alias string, userID int64) error {
  1871. c := &Collection{Alias: alias}
  1872. var username string
  1873. row := db.QueryRow("SELECT username FROM users WHERE id = ?", userID)
  1874. err := row.Scan(&username)
  1875. if err != nil {
  1876. return err
  1877. }
  1878. // Ensure user isn't deleting their main blog
  1879. if alias == username {
  1880. return impart.HTTPError{http.StatusForbidden, "You cannot currently delete your primary blog."}
  1881. }
  1882. row = db.QueryRow("SELECT id FROM collections WHERE alias = ? AND owner_id = ?", alias, userID)
  1883. err = row.Scan(&c.ID)
  1884. switch {
  1885. case err == sql.ErrNoRows:
  1886. return impart.HTTPError{http.StatusNotFound, "Collection doesn't exist or you're not allowed to delete it."}
  1887. case err != nil:
  1888. log.Error("Failed selecting from collections: %v", err)
  1889. return ErrInternalGeneral
  1890. }
  1891. t, err := db.Begin()
  1892. if err != nil {
  1893. return err
  1894. }
  1895. // Float all collection's posts
  1896. _, err = t.Exec("UPDATE posts SET collection_id = NULL WHERE collection_id = ? AND owner_id = ?", c.ID, userID)
  1897. if err != nil {
  1898. t.Rollback()
  1899. return err
  1900. }
  1901. // Remove redirects to or from this collection
  1902. _, err = t.Exec("DELETE FROM collectionredirects WHERE prev_alias = ? OR new_alias = ?", alias, alias)
  1903. if err != nil {
  1904. t.Rollback()
  1905. return err
  1906. }
  1907. // Remove any optional collection password
  1908. _, err = t.Exec("DELETE FROM collectionpasswords WHERE collection_id = ?", c.ID)
  1909. if err != nil {
  1910. t.Rollback()
  1911. return err
  1912. }
  1913. // Finally, delete collection itself
  1914. _, err = t.Exec("DELETE FROM collections WHERE id = ?", c.ID)
  1915. if err != nil {
  1916. t.Rollback()
  1917. return err
  1918. }
  1919. err = t.Commit()
  1920. if err != nil {
  1921. t.Rollback()
  1922. return err
  1923. }
  1924. return nil
  1925. }
  1926. func (db *datastore) IsCollectionAttributeOn(id int64, attr string) bool {
  1927. var v string
  1928. err := db.QueryRow("SELECT value FROM collectionattributes WHERE collection_id = ? AND attribute = ?", id, attr).Scan(&v)
  1929. switch {
  1930. case err == sql.ErrNoRows:
  1931. return false
  1932. case err != nil:
  1933. log.Error("Couldn't SELECT value in isCollectionAttributeOn for attribute '%s': %v", attr, err)
  1934. return false
  1935. }
  1936. return v == "1"
  1937. }
  1938. func (db *datastore) CollectionHasAttribute(id int64, attr string) bool {
  1939. var dummy string
  1940. err := db.QueryRow("SELECT value FROM collectionattributes WHERE collection_id = ? AND attribute = ?", id, attr).Scan(&dummy)
  1941. switch {
  1942. case err == sql.ErrNoRows:
  1943. return false
  1944. case err != nil:
  1945. log.Error("Couldn't SELECT value in collectionHasAttribute for attribute '%s': %v", attr, err)
  1946. return false
  1947. }
  1948. return true
  1949. }
  1950. func (db *datastore) GetCollectionAttribute(id int64, attr string) string {
  1951. var v string
  1952. err := db.QueryRow("SELECT value FROM collectionattributes WHERE collection_id = ? AND attribute = ?", id, attr).Scan(&v)
  1953. switch {
  1954. case err == sql.ErrNoRows:
  1955. return ""
  1956. case err != nil:
  1957. log.Error("Couldn't SELECT value in getCollectionAttribute for attribute '%s': %v", attr, err)
  1958. return ""
  1959. }
  1960. return v
  1961. }
  1962. func (db *datastore) SetCollectionAttribute(id int64, attr, v string) error {
  1963. _, err := db.Exec("INSERT INTO collectionattributes (collection_id, attribute, value) VALUES (?, ?, ?)", id, attr, v)
  1964. if err != nil {
  1965. log.Error("Unable to INSERT into collectionattributes: %v", err)
  1966. return err
  1967. }
  1968. return nil
  1969. }
  1970. // DeleteAccount will delete the entire account for userID
  1971. func (db *datastore) DeleteAccount(userID int64) error {
  1972. // Get all collections
  1973. rows, err := db.Query("SELECT id, alias FROM collections WHERE owner_id = ?", userID)
  1974. if err != nil {
  1975. log.Error("Unable to get collections: %v", err)
  1976. return err
  1977. }
  1978. defer rows.Close()
  1979. colls := []Collection{}
  1980. var c Collection
  1981. for rows.Next() {
  1982. err = rows.Scan(&c.ID, &c.Alias)
  1983. if err != nil {
  1984. log.Error("Unable to scan collection cols: %v", err)
  1985. return err
  1986. }
  1987. colls = append(colls, c)
  1988. }
  1989. // Start transaction
  1990. t, err := db.Begin()
  1991. if err != nil {
  1992. log.Error("Unable to begin: %v", err)
  1993. return err
  1994. }
  1995. // Clean up all collection related information
  1996. var res sql.Result
  1997. for _, c := range colls {
  1998. // Delete tokens
  1999. res, err = t.Exec("DELETE FROM collectionattributes WHERE collection_id = ?", c.ID)
  2000. if err != nil {
  2001. t.Rollback()
  2002. log.Error("Unable to delete attributes on %s: %v", c.Alias, err)
  2003. return err
  2004. }
  2005. rs, _ := res.RowsAffected()
  2006. log.Info("Deleted %d for %s from collectionattributes", rs, c.Alias)
  2007. // Remove any optional collection password
  2008. res, err = t.Exec("DELETE FROM collectionpasswords WHERE collection_id = ?", c.ID)
  2009. if err != nil {
  2010. t.Rollback()
  2011. log.Error("Unable to delete passwords on %s: %v", c.Alias, err)
  2012. return err
  2013. }
  2014. rs, _ = res.RowsAffected()
  2015. log.Info("Deleted %d for %s from collectionpasswords", rs, c.Alias)
  2016. // Remove redirects to this collection
  2017. res, err = t.Exec("DELETE FROM collectionredirects WHERE new_alias = ?", c.Alias)
  2018. if err != nil {
  2019. t.Rollback()
  2020. log.Error("Unable to delete redirects on %s: %v", c.Alias, err)
  2021. return err
  2022. }
  2023. rs, _ = res.RowsAffected()
  2024. log.Info("Deleted %d for %s from collectionredirects", rs, c.Alias)
  2025. // Remove any collection keys
  2026. res, err = t.Exec("DELETE FROM collectionkeys WHERE collection_id = ?", c.ID)
  2027. if err != nil {
  2028. t.Rollback()
  2029. log.Error("Unable to delete keys on %s: %v", c.Alias, err)
  2030. return err
  2031. }
  2032. rs, _ = res.RowsAffected()
  2033. log.Info("Deleted %d for %s from collectionkeys", rs, c.Alias)
  2034. // TODO: federate delete collection
  2035. // Remove remote follows
  2036. res, err = t.Exec("DELETE FROM remotefollows WHERE collection_id = ?", c.ID)
  2037. if err != nil {
  2038. t.Rollback()
  2039. log.Error("Unable to delete remote follows on %s: %v", c.Alias, err)
  2040. return err
  2041. }
  2042. rs, _ = res.RowsAffected()
  2043. log.Info("Deleted %d for %s from remotefollows", rs, c.Alias)
  2044. }
  2045. // Delete collections
  2046. res, err = t.Exec("DELETE FROM collections WHERE owner_id = ?", userID)
  2047. if err != nil {
  2048. t.Rollback()
  2049. log.Error("Unable to delete collections: %v", err)
  2050. return err
  2051. }
  2052. rs, _ := res.RowsAffected()
  2053. log.Info("Deleted %d from collections", rs)
  2054. // Delete tokens
  2055. res, err = t.Exec("DELETE FROM accesstokens WHERE user_id = ?", userID)
  2056. if err != nil {
  2057. t.Rollback()
  2058. log.Error("Unable to delete access tokens: %v", err)
  2059. return err
  2060. }
  2061. rs, _ = res.RowsAffected()
  2062. log.Info("Deleted %d from accesstokens", rs)
  2063. // Delete user attributes
  2064. res, err = t.Exec("DELETE FROM oauth_users WHERE user_id = ?", userID)
  2065. if err != nil {
  2066. t.Rollback()
  2067. log.Error("Unable to delete oauth_users: %v", err)
  2068. return err
  2069. }
  2070. rs, _ = res.RowsAffected()
  2071. log.Info("Deleted %d from oauth_users", rs)
  2072. // Delete posts
  2073. // TODO: should maybe get each row so we can federate a delete
  2074. // if so needs to be outside of transaction like collections
  2075. res, err = t.Exec("DELETE FROM posts WHERE owner_id = ?", userID)
  2076. if err != nil {
  2077. t.Rollback()
  2078. log.Error("Unable to delete posts: %v", err)
  2079. return err
  2080. }
  2081. rs, _ = res.RowsAffected()
  2082. log.Info("Deleted %d from posts", rs)
  2083. // Delete user attributes
  2084. res, err = t.Exec("DELETE FROM userattributes WHERE user_id = ?", userID)
  2085. if err != nil {
  2086. t.Rollback()
  2087. log.Error("Unable to delete attributes: %v", err)
  2088. return err
  2089. }
  2090. rs, _ = res.RowsAffected()
  2091. log.Info("Deleted %d from userattributes", rs)
  2092. // Delete user invites
  2093. res, err = t.Exec("DELETE FROM userinvites WHERE owner_id = ?", userID)
  2094. if err != nil {
  2095. t.Rollback()
  2096. log.Error("Unable to delete invites: %v", err)
  2097. return err
  2098. }
  2099. rs, _ = res.RowsAffected()
  2100. log.Info("Deleted %d from userinvites", rs)
  2101. // Delete the user
  2102. res, err = t.Exec("DELETE FROM users WHERE id = ?", userID)
  2103. if err != nil {
  2104. t.Rollback()
  2105. log.Error("Unable to delete user: %v", err)
  2106. return err
  2107. }
  2108. rs, _ = res.RowsAffected()
  2109. log.Info("Deleted %d from users", rs)
  2110. // Commit all changes to the database
  2111. err = t.Commit()
  2112. if err != nil {
  2113. t.Rollback()
  2114. log.Error("Unable to commit: %v", err)
  2115. return err
  2116. }
  2117. // TODO: federate delete actor
  2118. return nil
  2119. }
  2120. func (db *datastore) GetAPActorKeys(collectionID int64) ([]byte, []byte) {
  2121. var pub, priv []byte
  2122. err := db.QueryRow("SELECT public_key, private_key FROM collectionkeys WHERE collection_id = ?", collectionID).Scan(&pub, &priv)
  2123. switch {
  2124. case err == sql.ErrNoRows:
  2125. // Generate keys
  2126. pub, priv = activitypub.GenerateKeys()
  2127. _, err = db.Exec("INSERT INTO collectionkeys (collection_id, public_key, private_key) VALUES (?, ?, ?)", collectionID, pub, priv)
  2128. if err != nil {
  2129. log.Error("Unable to INSERT new activitypub keypair: %v", err)
  2130. return nil, nil
  2131. }
  2132. case err != nil:
  2133. log.Error("Couldn't SELECT collectionkeys: %v", err)
  2134. return nil, nil
  2135. }
  2136. return pub, priv
  2137. }
  2138. func (db *datastore) CreateUserInvite(id string, userID int64, maxUses int, expires *time.Time) error {
  2139. _, err := db.Exec("INSERT INTO userinvites (id, owner_id, max_uses, created, expires, inactive) VALUES (?, ?, ?, "+db.now()+", ?, 0)", id, userID, maxUses, expires)
  2140. return err
  2141. }
  2142. func (db *datastore) GetUserInvites(userID int64) (*[]Invite, error) {
  2143. rows, err := db.Query("SELECT id, max_uses, created, expires, inactive FROM userinvites WHERE owner_id = ? ORDER BY created DESC", userID)
  2144. if err != nil {
  2145. log.Error("Failed selecting from userinvites: %v", err)
  2146. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user invites."}
  2147. }
  2148. defer rows.Close()
  2149. is := []Invite{}
  2150. for rows.Next() {
  2151. i := Invite{}
  2152. err = rows.Scan(&i.ID, &i.MaxUses, &i.Created, &i.Expires, &i.Inactive)
  2153. is = append(is, i)
  2154. }
  2155. return &is, nil
  2156. }
  2157. func (db *datastore) GetUserInvite(id string) (*Invite, error) {
  2158. var i Invite
  2159. 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)
  2160. switch {
  2161. case err == sql.ErrNoRows, db.isIgnorableError(err):
  2162. return nil, impart.HTTPError{http.StatusNotFound, "Invite doesn't exist."}
  2163. case err != nil:
  2164. log.Error("Failed selecting invite: %v", err)
  2165. return nil, err
  2166. }
  2167. return &i, nil
  2168. }
  2169. // IsUsersInvite returns true if the user with ID created the invite with code
  2170. // and an error other than sql no rows, if any. Will return false in the event
  2171. // of an error.
  2172. func (db *datastore) IsUsersInvite(code string, userID int64) (bool, error) {
  2173. var id string
  2174. err := db.QueryRow("SELECT id FROM userinvites WHERE id = ? AND owner_id = ?", code, userID).Scan(&id)
  2175. if err != nil && err != sql.ErrNoRows {
  2176. log.Error("Failed selecting invite: %v", err)
  2177. return false, err
  2178. }
  2179. return id != "", nil
  2180. }
  2181. func (db *datastore) GetUsersInvitedCount(id string) int64 {
  2182. var count int64
  2183. err := db.QueryRow("SELECT COUNT(*) FROM usersinvited WHERE invite_id = ?", id).Scan(&count)
  2184. switch {
  2185. case err == sql.ErrNoRows:
  2186. return 0
  2187. case err != nil:
  2188. log.Error("Failed selecting users invited count: %v", err)
  2189. return 0
  2190. }
  2191. return count
  2192. }
  2193. func (db *datastore) CreateInvitedUser(inviteID string, userID int64) error {
  2194. _, err := db.Exec("INSERT INTO usersinvited (invite_id, user_id) VALUES (?, ?)", inviteID, userID)
  2195. return err
  2196. }
  2197. func (db *datastore) GetInstancePages() ([]*instanceContent, error) {
  2198. return db.GetAllDynamicContent("page")
  2199. }
  2200. func (db *datastore) GetAllDynamicContent(t string) ([]*instanceContent, error) {
  2201. where := ""
  2202. params := []interface{}{}
  2203. if t != "" {
  2204. where = " WHERE content_type = ?"
  2205. params = append(params, t)
  2206. }
  2207. rows, err := db.Query("SELECT id, title, content, updated, content_type FROM appcontent"+where, params...)
  2208. if err != nil {
  2209. log.Error("Failed selecting from appcontent: %v", err)
  2210. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve instance pages."}
  2211. }
  2212. defer rows.Close()
  2213. pages := []*instanceContent{}
  2214. for rows.Next() {
  2215. c := &instanceContent{}
  2216. err = rows.Scan(&c.ID, &c.Title, &c.Content, &c.Updated, &c.Type)
  2217. if err != nil {
  2218. log.Error("Failed scanning row: %v", err)
  2219. break
  2220. }
  2221. pages = append(pages, c)
  2222. }
  2223. err = rows.Err()
  2224. if err != nil {
  2225. log.Error("Error after Next() on rows: %v", err)
  2226. }
  2227. return pages, nil
  2228. }
  2229. func (db *datastore) GetDynamicContent(id string) (*instanceContent, error) {
  2230. c := &instanceContent{
  2231. ID: id,
  2232. }
  2233. err := db.QueryRow("SELECT title, content, updated, content_type FROM appcontent WHERE id = ?", id).Scan(&c.Title, &c.Content, &c.Updated, &c.Type)
  2234. switch {
  2235. case err == sql.ErrNoRows:
  2236. return nil, nil
  2237. case err != nil:
  2238. log.Error("Couldn't SELECT FROM appcontent for id '%s': %v", id, err)
  2239. return nil, err
  2240. }
  2241. return c, nil
  2242. }
  2243. func (db *datastore) UpdateDynamicContent(id, title, content, contentType string) error {
  2244. var err error
  2245. if db.driverName == driverSQLite {
  2246. _, err = db.Exec("INSERT OR REPLACE INTO appcontent (id, title, content, updated, content_type) VALUES (?, ?, ?, "+db.now()+", ?)", id, title, content, contentType)
  2247. } else {
  2248. _, err = db.Exec("INSERT INTO appcontent (id, title, content, updated, content_type) VALUES (?, ?, ?, "+db.now()+", ?) "+db.upsert("id")+" title = ?, content = ?, updated = "+db.now(), id, title, content, contentType, title, content)
  2249. }
  2250. if err != nil {
  2251. log.Error("Unable to INSERT appcontent for '%s': %v", id, err)
  2252. }
  2253. return err
  2254. }
  2255. func (db *datastore) GetAllUsers(page uint) (*[]User, error) {
  2256. limitStr := fmt.Sprintf("0, %d", adminUsersPerPage)
  2257. if page > 1 {
  2258. limitStr = fmt.Sprintf("%d, %d", (page-1)*adminUsersPerPage, adminUsersPerPage)
  2259. }
  2260. rows, err := db.Query("SELECT id, username, created, status FROM users ORDER BY created DESC LIMIT " + limitStr)
  2261. if err != nil {
  2262. log.Error("Failed selecting from users: %v", err)
  2263. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve all users."}
  2264. }
  2265. defer rows.Close()
  2266. users := []User{}
  2267. for rows.Next() {
  2268. u := User{}
  2269. err = rows.Scan(&u.ID, &u.Username, &u.Created, &u.Status)
  2270. if err != nil {
  2271. log.Error("Failed scanning GetAllUsers() row: %v", err)
  2272. break
  2273. }
  2274. users = append(users, u)
  2275. }
  2276. return &users, nil
  2277. }
  2278. func (db *datastore) GetAllUsersCount() int64 {
  2279. var count int64
  2280. err := db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
  2281. switch {
  2282. case err == sql.ErrNoRows:
  2283. return 0
  2284. case err != nil:
  2285. log.Error("Failed selecting all users count: %v", err)
  2286. return 0
  2287. }
  2288. return count
  2289. }
  2290. func (db *datastore) GetUserLastPostTime(id int64) (*time.Time, error) {
  2291. var t time.Time
  2292. err := db.QueryRow("SELECT created FROM posts WHERE owner_id = ? ORDER BY created DESC LIMIT 1", id).Scan(&t)
  2293. switch {
  2294. case err == sql.ErrNoRows:
  2295. return nil, nil
  2296. case err != nil:
  2297. log.Error("Failed selecting last post time from posts: %v", err)
  2298. return nil, err
  2299. }
  2300. return &t, nil
  2301. }
  2302. // SetUserStatus changes a user's status in the database. see Users.UserStatus
  2303. func (db *datastore) SetUserStatus(id int64, status UserStatus) error {
  2304. _, err := db.Exec("UPDATE users SET status = ? WHERE id = ?", status, id)
  2305. if err != nil {
  2306. return fmt.Errorf("failed to update user status: %v", err)
  2307. }
  2308. return nil
  2309. }
  2310. func (db *datastore) GetCollectionLastPostTime(id int64) (*time.Time, error) {
  2311. var t time.Time
  2312. err := db.QueryRow("SELECT created FROM posts WHERE collection_id = ? ORDER BY created DESC LIMIT 1", id).Scan(&t)
  2313. switch {
  2314. case err == sql.ErrNoRows:
  2315. return nil, nil
  2316. case err != nil:
  2317. log.Error("Failed selecting last post time from posts: %v", err)
  2318. return nil, err
  2319. }
  2320. return &t, nil
  2321. }
  2322. func (db *datastore) GenerateOAuthState(ctx context.Context, provider string, clientID string, attachUser int64, inviteCode string) (string, error) {
  2323. state := id.Generate62RandomString(24)
  2324. attachUserVal := sql.NullInt64{Valid: attachUser > 0, Int64: attachUser}
  2325. inviteCodeVal := sql.NullString{Valid: inviteCode != "", String: inviteCode}
  2326. _, err := db.ExecContext(ctx, "INSERT INTO oauth_client_states (state, provider, client_id, used, created_at, attach_user_id, invite_code) VALUES (?, ?, ?, FALSE, "+db.now()+", ?, ?)", state, provider, clientID, attachUserVal, inviteCodeVal)
  2327. if err != nil {
  2328. return "", fmt.Errorf("unable to record oauth client state: %w", err)
  2329. }
  2330. return state, nil
  2331. }
  2332. func (db *datastore) ValidateOAuthState(ctx context.Context, state string) (string, string, int64, string, error) {
  2333. var provider string
  2334. var clientID string
  2335. var attachUserID sql.NullInt64
  2336. var inviteCode sql.NullString
  2337. err := wf_db.RunTransactionWithOptions(ctx, db.DB, &sql.TxOptions{}, func(ctx context.Context, tx *sql.Tx) error {
  2338. err := tx.
  2339. QueryRowContext(ctx, "SELECT provider, client_id, attach_user_id, invite_code FROM oauth_client_states WHERE state = ? AND used = FALSE", state).
  2340. Scan(&provider, &clientID, &attachUserID, &inviteCode)
  2341. if err != nil {
  2342. return err
  2343. }
  2344. res, err := tx.ExecContext(ctx, "UPDATE oauth_client_states SET used = TRUE WHERE state = ?", state)
  2345. if err != nil {
  2346. return err
  2347. }
  2348. rowsAffected, err := res.RowsAffected()
  2349. if err != nil {
  2350. return err
  2351. }
  2352. if rowsAffected != 1 {
  2353. return fmt.Errorf("state not found")
  2354. }
  2355. return nil
  2356. })
  2357. if err != nil {
  2358. return "", "", 0, "", nil
  2359. }
  2360. return provider, clientID, attachUserID.Int64, inviteCode.String, nil
  2361. }
  2362. func (db *datastore) RecordRemoteUserID(ctx context.Context, localUserID int64, remoteUserID, provider, clientID, accessToken string) error {
  2363. var err error
  2364. if db.driverName == driverSQLite {
  2365. _, err = db.ExecContext(ctx, "INSERT OR REPLACE INTO oauth_users (user_id, remote_user_id, provider, client_id, access_token) VALUES (?, ?, ?, ?, ?)", localUserID, remoteUserID, provider, clientID, accessToken)
  2366. } else {
  2367. _, err = db.ExecContext(ctx, "INSERT INTO oauth_users (user_id, remote_user_id, provider, client_id, access_token) VALUES (?, ?, ?, ?, ?) "+db.upsert("user")+" access_token = ?", localUserID, remoteUserID, provider, clientID, accessToken, accessToken)
  2368. }
  2369. if err != nil {
  2370. log.Error("Unable to INSERT oauth_users for '%d': %v", localUserID, err)
  2371. }
  2372. return err
  2373. }
  2374. // GetIDForRemoteUser returns a user ID associated with a remote user ID.
  2375. func (db *datastore) GetIDForRemoteUser(ctx context.Context, remoteUserID, provider, clientID string) (int64, error) {
  2376. var userID int64 = -1
  2377. err := db.
  2378. QueryRowContext(ctx, "SELECT user_id FROM oauth_users WHERE remote_user_id = ? AND provider = ? AND client_id = ?", remoteUserID, provider, clientID).
  2379. Scan(&userID)
  2380. // Not finding a record is OK.
  2381. if err != nil && err != sql.ErrNoRows {
  2382. return -1, err
  2383. }
  2384. return userID, nil
  2385. }
  2386. type oauthAccountInfo struct {
  2387. Provider string
  2388. ClientID string
  2389. RemoteUserID string
  2390. DisplayName string
  2391. AllowDisconnect bool
  2392. }
  2393. func (db *datastore) GetOauthAccounts(ctx context.Context, userID int64) ([]oauthAccountInfo, error) {
  2394. rows, err := db.QueryContext(ctx, "SELECT provider, client_id, remote_user_id FROM oauth_users WHERE user_id = ? ", userID)
  2395. if err != nil {
  2396. log.Error("Failed selecting from oauth_users: %v", err)
  2397. return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user oauth accounts."}
  2398. }
  2399. defer rows.Close()
  2400. var records []oauthAccountInfo
  2401. for rows.Next() {
  2402. info := oauthAccountInfo{}
  2403. err = rows.Scan(&info.Provider, &info.ClientID, &info.RemoteUserID)
  2404. if err != nil {
  2405. log.Error("Failed scanning GetAllUsers() row: %v", err)
  2406. break
  2407. }
  2408. records = append(records, info)
  2409. }
  2410. return records, nil
  2411. }
  2412. // DatabaseInitialized returns whether or not the current datastore has been
  2413. // initialized with the correct schema.
  2414. // Currently, it checks to see if the `users` table exists.
  2415. func (db *datastore) DatabaseInitialized() bool {
  2416. var dummy string
  2417. var err error
  2418. if db.driverName == driverSQLite {
  2419. err = db.QueryRow("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'users'").Scan(&dummy)
  2420. } else {
  2421. err = db.QueryRow("SHOW TABLES LIKE 'users'").Scan(&dummy)
  2422. }
  2423. switch {
  2424. case err == sql.ErrNoRows:
  2425. return false
  2426. case err != nil:
  2427. log.Error("Couldn't SHOW TABLES: %v", err)
  2428. return false
  2429. }
  2430. return true
  2431. }
  2432. func (db *datastore) RemoveOauth(ctx context.Context, userID int64, provider string, clientID string, remoteUserID string) error {
  2433. _, err := db.ExecContext(ctx, `DELETE FROM oauth_users WHERE user_id = ? AND provider = ? AND client_id = ? AND remote_user_id = ?`, userID, provider, clientID, remoteUserID)
  2434. return err
  2435. }
  2436. func stringLogln(log *string, s string, v ...interface{}) {
  2437. *log += fmt.Sprintf(s+"\n", v...)
  2438. }
  2439. func handleFailedPostInsert(err error) error {
  2440. log.Error("Couldn't insert into posts: %v", err)
  2441. return err
  2442. }
  2443. func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, error) {
  2444. handle = strings.TrimLeft(handle, "@")
  2445. actorIRI := ""
  2446. parts := strings.Split(handle, "@")
  2447. if len(parts) != 2 {
  2448. return "", fmt.Errorf("invalid handle format")
  2449. }
  2450. domain := parts[1]
  2451. // Check non-AP instances
  2452. if siloProfileURL := silobridge.Profile(parts[0], domain); siloProfileURL != "" {
  2453. return siloProfileURL, nil
  2454. }
  2455. remoteUser, err := getRemoteUserFromHandle(app, handle)
  2456. if err != nil {
  2457. // can't find using handle in the table but the table may already have this user without
  2458. // handle from a previous version
  2459. // TODO: Make this determination. We should know whether a user exists without a handle, or doesn't exist at all
  2460. actorIRI = RemoteLookup(handle)
  2461. _, errRemoteUser := getRemoteUser(app, actorIRI)
  2462. // if it exists then we need to update the handle
  2463. if errRemoteUser == nil {
  2464. _, err := app.db.Exec("UPDATE remoteusers SET handle = ? WHERE actor_id = ?", handle, actorIRI)
  2465. if err != nil {
  2466. log.Error("Couldn't update handle '%s' for user %s", handle, actorIRI)
  2467. }
  2468. } else {
  2469. // this probably means we don't have the user in the table so let's try to insert it
  2470. // here we need to ask the server for the inboxes
  2471. remoteActor, err := activityserve.NewRemoteActor(actorIRI)
  2472. if err != nil {
  2473. log.Error("Couldn't fetch remote actor: %v", err)
  2474. }
  2475. if debugging {
  2476. log.Info("%s %s %s %s", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
  2477. }
  2478. _, err = app.db.Exec("INSERT INTO remoteusers (actor_id, inbox, shared_inbox, handle) VALUES(?, ?, ?, ?)", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
  2479. if err != nil {
  2480. log.Error("Couldn't insert remote user: %v", err)
  2481. return "", err
  2482. }
  2483. }
  2484. } else {
  2485. actorIRI = remoteUser.ActorID
  2486. }
  2487. return actorIRI, nil
  2488. }