Phase 10, part 7: posting accounts, and who may post to which
Social accounts become posting accounts: several per network, the company's and each person's own, with their tokens kept per account. The old sign-ins are adopted as the company's. Admins say who may post to which (Settings → Posting accounts); a draft can go out from one account or a copy to several, each recording who posted it and as what. Signing an account out, or removing its owner, holds the posts scheduled to it and tells whoever scheduled them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+136
-25
@@ -71,16 +71,23 @@ func groupBatches(drafts []content.Draft) []*draftBatch {
|
||||
return batches
|
||||
}
|
||||
|
||||
func (s *Server) connected() map[string]bool {
|
||||
// connected says, by channel, whether the viewer has an account to post to.
|
||||
func (s *Server) connected(v *viewer) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
for id, p := range s.publishers() {
|
||||
if _, expired := p.(expiredPublisher); expired {
|
||||
continue
|
||||
for ch, list := range s.usableAccounts(v) {
|
||||
out[ch] = len(list) > 0
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// chosenAccounts is the account each draft goes out from, as things stand:
|
||||
// its own, or the company's for its channel.
|
||||
func (s *Server) chosenAccounts(drafts []content.Draft) map[int64]int64 {
|
||||
out := map[int64]int64{}
|
||||
for i := range drafts {
|
||||
if a, err := s.draftAccount(&drafts[i]); err == nil && a != nil {
|
||||
out[drafts[i].ID] = a.ID
|
||||
}
|
||||
if li, ok := p.(linkedInPublisher); ok && li.me == nil && li.page == nil {
|
||||
continue // only expired sign-ins
|
||||
}
|
||||
out[id] = true
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -103,8 +110,9 @@ func (s *Server) renderInbox(w http.ResponseWriter, r *http.Request, errMsg stri
|
||||
"Reminders": s.reminders(),
|
||||
"Sections": sections,
|
||||
"Reddit": s.redditViews(r.Context(), pending),
|
||||
"Connected": s.connected(),
|
||||
"Page": s.liPageIfSignedIn(),
|
||||
"Connected": s.connected(viewerOf(r)),
|
||||
"Accounts": s.usableAccounts(viewerOf(r)),
|
||||
"Chosen": s.chosenAccounts(pending),
|
||||
"Tagged": s.tagger() != nil,
|
||||
"Now": time.Now().Add(time.Hour).Truncate(time.Hour).Format(dateTimeLocal),
|
||||
"Zone": time.Now().Format("MST"),
|
||||
@@ -174,7 +182,7 @@ func (s *Server) renderScheduled(w http.ResponseWriter, r *http.Request, errMsg
|
||||
return
|
||||
}
|
||||
s.render(w, r, "content-scheduled", page{Title: "Scheduled", Nav: "content/scheduled", Error: errMsg, Data: map[string]any{
|
||||
"Drafts": drafts, "Connected": s.connected(), "Zone": time.Now().Format("MST"),
|
||||
"Drafts": drafts, "Connected": s.connected(viewerOf(r)), "Zone": time.Now().Format("MST"),
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -258,24 +266,70 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// The accounts chosen to post to: this draft goes to the first; the
|
||||
// others get copies, published or scheduled with it.
|
||||
var copies []int64
|
||||
switch action {
|
||||
case "save", "publish", "schedule":
|
||||
chosen, err := s.chosenFromForm(r, id)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
if len(chosen) > 0 {
|
||||
if err := content.SetAccount(s.DB, id, chosen[0]); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if action != "save" {
|
||||
if err := s.checkPostAs(viewerOf(r), id); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
for _, a := range chosen[min(1, len(chosen)):] {
|
||||
cid, err := content.Copy(s.DB, id, a)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
copies = append(copies, cid)
|
||||
}
|
||||
}
|
||||
}
|
||||
by := int64(0)
|
||||
if v := viewerOf(r); v.User != nil {
|
||||
by = v.User.ID
|
||||
}
|
||||
switch action {
|
||||
case "save":
|
||||
redirect(w, r, back, "saved")
|
||||
case "publish":
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
d, err := content.Publish(ctx, s.DB, s.publishers(), s.tagger(), id)
|
||||
if err != nil {
|
||||
name := "the channel"
|
||||
if d != nil {
|
||||
name = d.ChannelName()
|
||||
var failed []string
|
||||
reddit := false
|
||||
for _, did := range append([]int64{id}, copies...) {
|
||||
_ = content.SetPostedBy(s.DB, did, by)
|
||||
d, err := content.Publish(ctx, s.DB, s.publishers(), s.tagger(), did)
|
||||
if err != nil {
|
||||
name := "the channel"
|
||||
if d != nil {
|
||||
name = d.ChannelName()
|
||||
}
|
||||
failed = append(failed, fmt.Sprintf("Couldn't publish to %s: %v", name, err))
|
||||
continue
|
||||
}
|
||||
fail(fmt.Errorf("Couldn't publish to %s: %w", name, err))
|
||||
return
|
||||
reddit = reddit || d.Channel == content.ChannelReddit
|
||||
s.Log.Info("content: published", "draft", did, "as", d.PostedAs, "by", by)
|
||||
}
|
||||
if d.Channel == content.ChannelReddit {
|
||||
if reddit {
|
||||
s.forgetRedditHistory()
|
||||
}
|
||||
if len(failed) > 0 {
|
||||
fail(errors.New(strings.Join(failed, " ")))
|
||||
return
|
||||
}
|
||||
redirect(w, r, back, "published")
|
||||
case "schedule":
|
||||
at, err := parseWhen(r.PostForm.Get("when"), time.Now())
|
||||
@@ -292,9 +346,12 @@ func (s *Server) draftAction(w http.ResponseWriter, r *http.Request) {
|
||||
fail(fmt.Errorf("%s: fix it before scheduling", problem))
|
||||
return
|
||||
}
|
||||
if err := content.Schedule(s.DB, id, at); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
for _, did := range append([]int64{id}, copies...) {
|
||||
if err := content.Schedule(s.DB, did, at); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = content.SetPostedBy(s.DB, did, by)
|
||||
}
|
||||
redirect(w, r, back, "scheduled")
|
||||
case "open":
|
||||
@@ -396,9 +453,15 @@ func (s *Server) publishDue(ctx context.Context, now time.Time) {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
pctx, cancel := context.WithTimeout(ctx, 60*time.Second)
|
||||
_, err := content.Publish(pctx, s.DB, pubs, tags, d.ID)
|
||||
cancel()
|
||||
var err error
|
||||
if d.PostedBy != 0 {
|
||||
err = s.checkPostAs(s.viewerFor(d.PostedBy), d.ID)
|
||||
}
|
||||
if err == nil {
|
||||
pctx, cancel := context.WithTimeout(ctx, 60*time.Second)
|
||||
_, err = content.Publish(pctx, s.DB, pubs, tags, d.ID)
|
||||
cancel()
|
||||
}
|
||||
if err != nil {
|
||||
// A failed post is recorded on the draft by Publish. One refused
|
||||
// before posting (not connected, too long) is still scheduled:
|
||||
@@ -443,3 +506,51 @@ func redditSubmitURL(d *content.Draft, tags *utm.Tagger, info *reddit.Subreddit)
|
||||
q.Del("text")
|
||||
return base + "?" + q.Encode()
|
||||
}
|
||||
|
||||
// chosenFromForm reads the accounts chosen on a draft's form (accounts=<id>,
|
||||
// several): each must be one the viewer may post to, for the draft's
|
||||
// channel.
|
||||
func (s *Server) chosenFromForm(r *http.Request, id int64) ([]int64, error) {
|
||||
raw, sent := r.PostForm["accounts"]
|
||||
if !sent {
|
||||
return nil, nil
|
||||
}
|
||||
d, err := content.GetDraft(s.DB, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
usable := map[int64]bool{}
|
||||
for _, a := range s.usableAccounts(viewerOf(r))[d.Channel] {
|
||||
usable[a.ID] = true
|
||||
}
|
||||
var out []int64
|
||||
seen := map[int64]bool{}
|
||||
for _, v := range raw {
|
||||
a, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil || !usable[a] {
|
||||
return nil, errors.New("you can't post to one of the accounts chosen: choose among those listed")
|
||||
}
|
||||
if !seen[a] {
|
||||
seen[a] = true
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// checkPostAs says why a viewer may not post a draft from its account, or
|
||||
// nil if they may.
|
||||
func (s *Server) checkPostAs(v *viewer, id int64) error {
|
||||
d, err := content.GetDraft(s.DB, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a, err := s.draftAccount(d)
|
||||
if err != nil || a == nil {
|
||||
return err // Publish says it isn't connected
|
||||
}
|
||||
if !s.mayPostAs(v, a) {
|
||||
return fmt.Errorf("you may not post as %s: choose another account, or ask an admin", a.Label())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user