1// Copyright 2019 Keybase, Inc. All rights reserved. Use of
2// this source code is governed by the included BSD license.
3
4package engine
5
6import (
7	"sync"
8	"time"
9
10	"github.com/keybase/client/go/invitefriends"
11	"github.com/keybase/client/go/libkb"
12)
13
14var InviteFriendsPollBackgroundSettings = BackgroundTaskSettings{
15	Start:                         5 * time.Second,
16	MobileForegroundStartAddition: 30 * time.Second,
17	StartStagger:                  5 * time.Second,
18	WakeUp:                        15 * time.Second,
19	Interval:                      1 * time.Hour,
20	Limit:                         5 * time.Minute,
21}
22
23type InviteFriendsPollBackground struct {
24	libkb.Contextified
25	sync.Mutex
26
27	task *BackgroundTask
28}
29
30func NewInviteFriendsPollBackground(g *libkb.GlobalContext) *InviteFriendsPollBackground {
31	task := NewBackgroundTask(g, &BackgroundTaskArgs{
32		Name:     "InviteFriendsPollBackground",
33		F:        InviteFriendsPollBackgroundRound,
34		Settings: InviteFriendsPollBackgroundSettings,
35	})
36	return &InviteFriendsPollBackground{
37		Contextified: libkb.NewContextified(g),
38		// Install the task early so that Shutdown can be called before RunEngine.
39		task: task,
40	}
41}
42
43func (e *InviteFriendsPollBackground) Name() string {
44	return "InviteFriendsPollBackground"
45}
46
47func (e *InviteFriendsPollBackground) Prereqs() Prereqs {
48	return Prereqs{}
49}
50
51func (e *InviteFriendsPollBackground) RequiredUIs() []libkb.UIKind {
52	return []libkb.UIKind{}
53}
54
55func (e *InviteFriendsPollBackground) SubConsumers() []libkb.UIConsumer {
56	return []libkb.UIConsumer{}
57}
58
59// Run starts the engine.
60// Returns immediately, kicks off a background goroutine.
61func (e *InviteFriendsPollBackground) Run(m libkb.MetaContext) (err error) {
62	return RunEngine2(m, e.task)
63}
64
65func (e *InviteFriendsPollBackground) Shutdown() {
66	e.task.Shutdown()
67}
68
69func InviteFriendsPollBackgroundRound(mctx libkb.MetaContext) error {
70	counts, err := invitefriends.GetCounts(mctx)
71	if err != nil {
72		return err
73	}
74	mctx.G().NotifyRouter.HandleUpdateInviteCounts(mctx.Ctx(), counts)
75	return nil
76}
77