🐛 Fix notified draft post
This commit is contained in:
		| @@ -501,6 +501,54 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) { | ||||
| 	return item, nil | ||||
| } | ||||
|  | ||||
| func NotifyReplying(item models.Post, user models.Publisher) error { | ||||
| 	content, ok := item.Body["content"].(string) | ||||
| 	if !ok { | ||||
| 		content = "Posted a post" | ||||
| 	} | ||||
| 	var title *string | ||||
| 	title, _ = item.Body["title"].(*string) | ||||
| 	item.Publisher = user | ||||
| 	if err := NotifyUserSubscription(user, item, content, title); err != nil { | ||||
| 		log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by user...") | ||||
| 	} | ||||
| 	for _, tag := range item.Tags { | ||||
| 		if err := NotifyTagSubscription(tag, user, item, content, title); err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by tag...") | ||||
| 		} | ||||
| 	} | ||||
| 	for _, category := range item.Categories { | ||||
| 		if err := NotifyCategorySubscription(category, user, item, content, title); err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by category...") | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func NotifySubscribers(item models.Post, user models.Publisher) error { | ||||
| 	content, ok := item.Body["content"].(string) | ||||
| 	if !ok { | ||||
| 		content = "Posted a post" | ||||
| 	} | ||||
| 	var title *string | ||||
| 	title, _ = item.Body["title"].(*string) | ||||
| 	item.Publisher = user | ||||
| 	if err := NotifyUserSubscription(user, item, content, title); err != nil { | ||||
| 		log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by user...") | ||||
| 	} | ||||
| 	for _, tag := range item.Tags { | ||||
| 		if err := NotifyTagSubscription(tag, user, item, content, title); err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by tag...") | ||||
| 		} | ||||
| 	} | ||||
| 	for _, category := range item.Categories { | ||||
| 		if err := NotifyCategorySubscription(category, user, item, content, title); err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by category...") | ||||
| 		} | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func NewPost(user models.Publisher, item models.Post) (models.Post, error) { | ||||
| 	if item.Alias != nil && len(*item.Alias) == 0 { | ||||
| 		item.Alias = nil | ||||
| @@ -543,60 +591,12 @@ func NewPost(user models.Publisher, item models.Post) (models.Post, error) { | ||||
| 	} | ||||
|  | ||||
| 	// Notify the original poster its post has been replied | ||||
| 	if item.ReplyID != nil { | ||||
| 		content, ok := item.Body["content"].(string) | ||||
| 		if !ok { | ||||
| 			content = "Posted a post" | ||||
| 		} else { | ||||
| 			content = TruncatePostContentShort(content) | ||||
| 		} | ||||
|  | ||||
| 		var op models.Post | ||||
| 		if err := database.C. | ||||
| 			Where("id = ?", item.ReplyID). | ||||
| 			Preload("Publisher"). | ||||
| 			First(&op).Error; err == nil { | ||||
| 			if op.Publisher.AccountID != nil && op.Publisher.ID != user.ID { | ||||
| 				log.Debug().Uint("user", *op.Publisher.AccountID).Msg("Notifying the original poster their post got replied...") | ||||
| 				err = NotifyPosterAccount( | ||||
| 					op.Publisher, | ||||
| 					op, | ||||
| 					"Post got replied", | ||||
| 					fmt.Sprintf("%s (%s) replied you: %s", user.Nick, user.Name, content), | ||||
| 					"interactive.reply", | ||||
| 					fmt.Sprintf("%s replied your post #%d", user.Nick, *item.ReplyID), | ||||
| 				) | ||||
| 				if err != nil { | ||||
| 					log.Error().Err(err).Msg("An error occurred when notifying user...") | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	if item.ReplyID != nil && !item.IsDraft { | ||||
| 		go NotifyReplying(item, user) | ||||
| 	} | ||||
|  | ||||
| 	// Notify the subscriptions | ||||
| 	if item.ReplyID == nil { | ||||
| 		content, ok := item.Body["content"].(string) | ||||
| 		if !ok { | ||||
| 			content = "Posted a post" | ||||
| 		} | ||||
| 		var title *string | ||||
| 		title, _ = item.Body["title"].(*string) | ||||
| 		go func() { | ||||
| 			item.Publisher = user | ||||
| 			if err := NotifyUserSubscription(user, item, content, title); err != nil { | ||||
| 				log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by user...") | ||||
| 			} | ||||
| 			for _, tag := range item.Tags { | ||||
| 				if err := NotifyTagSubscription(tag, user, item, content, title); err != nil { | ||||
| 					log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by tag...") | ||||
| 				} | ||||
| 			} | ||||
| 			for _, category := range item.Categories { | ||||
| 				if err := NotifyCategorySubscription(category, user, item, content, title); err != nil { | ||||
| 					log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by category...") | ||||
| 				} | ||||
| 			} | ||||
| 		}() | ||||
| 	if item.ReplyID == nil && !item.IsDraft { | ||||
| 		go NotifySubscribers(item, user) | ||||
| 	} | ||||
|  | ||||
| 	log.Debug().Dur("elapsed", time.Since(start)).Msg("The post is posted.") | ||||
| @@ -645,6 +645,17 @@ func EditPost(item models.Post, og models.Post) (models.Post, error) { | ||||
| 		if err != nil { | ||||
| 			log.Error().Err(err).Msg("An error occurred when updating post attachment meta...") | ||||
| 		} | ||||
|  | ||||
| 		if og.IsDraft && !item.IsDraft { | ||||
| 			// Notify the original poster its post has been replied | ||||
| 			if item.ReplyID != nil { | ||||
| 				go NotifyReplying(item, item.Publisher) | ||||
| 			} | ||||
| 			// Notify the subscriptions | ||||
| 			if item.ReplyID == nil { | ||||
| 				go NotifySubscribers(item, item.Publisher) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return item, err | ||||
|   | ||||
		Reference in New Issue
	
	Block a user