Download this file
| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "gorm.io/gorm" |
| 7 | ) |
| 8 | |
| 9 | // The picker adds visible context to the person's message. It does not grant |
| 10 | // project access or override the coordinator's project checks. |
| 11 | func (s *Application) SendProjectMessage(ctx context.Context, actor Actor, body string, projectID int64, m Mutation) (Message, error) { |
| 12 | if body == "" { |
| 13 | return Message{}, s.Invalid |
| 14 | } |
| 15 | actor.Address = s.Normalize(actor.Address) |
| 16 | var msg Message |
| 17 | err := s.Write(ctx, func(tx *gorm.DB) error { |
| 18 | cs := s.Conversation(tx) |
| 19 | if _, err := cs.deps.Authorize(tx, actor); err != nil { |
| 20 | return err |
| 21 | } |
| 22 | var project struct{ Name string } |
| 23 | err := tx.Table("projects").Select("name").Where("id=?", projectID).Take(&project).Error |
| 24 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 25 | return s.NotFound |
| 26 | } |
| 27 | if err != nil { |
| 28 | return err |
| 29 | } |
| 30 | msg, err = cs.SendMessage(actor, "", project.Name+"\n\n"+body, m) |
| 31 | if err == nil && msg.ConversationID != "" { |
| 32 | err = s.setupMessage(tx, actor, msg) |
| 33 | } |
| 34 | return err |
| 35 | }) |
| 36 | return msg, err |
| 37 | } |
| 38 | |