prepare has two roles: 1. render the title & body templates 2. build a dispatcher from the given message, payload, and these templates - to be used for delivering the notification
(ctx context.Context, msg database.AcquireNotificationMessagesRow)
| 225 | // 1. render the title & body templates |
| 226 | // 2. build a dispatcher from the given message, payload, and these templates - to be used for delivering the notification |
| 227 | func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotificationMessagesRow) (dispatch.DeliveryFunc, error) { |
| 228 | select { |
| 229 | case <-ctx.Done(): |
| 230 | return nil, ctx.Err() |
| 231 | default: |
| 232 | } |
| 233 | |
| 234 | // NOTE: when we change the format of the MessagePayload, we have to bump its version and handle unmarshalling |
| 235 | // differently here based on that version. |
| 236 | var payload types.MessagePayload |
| 237 | err := json.Unmarshal(msg.Payload, &payload) |
| 238 | if err != nil { |
| 239 | return nil, xerrors.Errorf("unmarshal payload: %w", err) |
| 240 | } |
| 241 | |
| 242 | handler, ok := n.handlers[msg.Method] |
| 243 | if !ok { |
| 244 | return nil, xerrors.Errorf("failed to resolve handler %q", msg.Method) |
| 245 | } |
| 246 | |
| 247 | helpers, err := n.fetchHelpers(ctx) |
| 248 | if err != nil { |
| 249 | return nil, decorateHelpersError{err} |
| 250 | } |
| 251 | |
| 252 | var title, body string |
| 253 | if title, err = render.GoTemplate(msg.TitleTemplate, payload, helpers); err != nil { |
| 254 | return nil, xerrors.Errorf("render title: %w", err) |
| 255 | } |
| 256 | if body, err = render.GoTemplate(msg.BodyTemplate, payload, helpers); err != nil { |
| 257 | return nil, xerrors.Errorf("render body: %w", err) |
| 258 | } |
| 259 | |
| 260 | return handler.Dispatcher(payload, title, body, helpers) |
| 261 | } |
| 262 | |
| 263 | // deliver sends a given notification message via its defined method. |
| 264 | // This method *only* returns an error when a context error occurs; any other error is interpreted as a failure to |
no test coverage detected