MCPcopy
hub / github.com/writefreely/writefreely / CreatePost

Method CreatePost

database.go:675–772  ·  view source on GitHub ↗
(userID, collID int64, post *SubmittedPost)

Source from the content-addressed store, hash-verified

673}
674
675func (db *datastore) CreatePost(userID, collID int64, post *SubmittedPost) (*Post, error) {
676 idLen := postIDLen
677 friendlyID := id.GenerateFriendlyRandomString(idLen)
678
679 // Handle appearance / font face
680 appearance := post.Font
681 if !post.isFontValid() {
682 appearance = "norm"
683 }
684
685 var err error
686 ownerID := sql.NullInt64{
687 Valid: false,
688 }
689 ownerCollID := sql.NullInt64{
690 Valid: false,
691 }
692 slug := sql.NullString{"", false}
693
694 // If an alias was supplied, we'll add this to the collection as well.
695 if userID > 0 {
696 ownerID.Int64 = userID
697 ownerID.Valid = true
698 if collID > 0 {
699 ownerCollID.Int64 = collID
700 ownerCollID.Valid = true
701 var slugVal string
702 if post.Slug != nil && *post.Slug != "" {
703 slugVal = *post.Slug
704 } else {
705 if post.Title != nil && *post.Title != "" {
706 slugVal = getSlug(*post.Title, post.Language.String)
707 if slugVal == "" {
708 slugVal = getSlug(*post.Content, post.Language.String)
709 }
710 } else {
711 slugVal = getSlug(*post.Content, post.Language.String)
712 }
713 }
714 if slugVal == "" {
715 slugVal = friendlyID
716 }
717 slug = sql.NullString{slugVal, true}
718 }
719 }
720
721 created := time.Now()
722 if db.driverName == driverSQLite {
723 // SQLite stores datetimes in UTC, so convert time.Now() to it here
724 created = created.UTC()
725 }
726 if post.Created != nil && *post.Created != "" {
727 created, err = time.Parse("2006-01-02T15:04:05Z", *post.Created)
728 if err != nil {
729 log.Error("Unable to parse Created time '%s': %v", *post.Created, err)
730 created = time.Now()
731 if db.driverName == driverSQLite {
732 // SQLite stores datetimes in UTC, so convert time.Now() to it here

Callers 1

CreateOwnedPostMethod · 0.95

Calls 6

nowMethod · 0.95
isDuplicateKeyErrMethod · 0.95
getSlugFunction · 0.85
handleFailedPostInsertFunction · 0.85
isFontValidMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected