about summary refs log tree commit diff
path: root/src/state/models/me.ts
blob: cefd893ca1bff108e6433b66076e1083f158ec58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'
import {MembershipsViewModel} from './memberships-view'

export class MeModel {
  did?: string
  handle?: string
  displayName?: string
  description?: string
  notificationCount: number = 0
  memberships?: MembershipsViewModel

  constructor(public rootStore: RootStoreModel) {
    makeAutoObservable(this, {rootStore: false}, {autoBind: true})
  }

  clear() {
    this.did = undefined
    this.handle = undefined
    this.displayName = undefined
    this.description = undefined
    this.notificationCount = 0
    this.memberships = undefined
  }

  async load() {
    const sess = this.rootStore.session
    if (sess.isAuthed && sess.data) {
      this.did = sess.data.did || ''
      this.handle = sess.data.handle
      const profile = await this.rootStore.api.app.bsky.actor.getProfile({
        actor: this.did,
      })
      runInAction(() => {
        if (profile?.data) {
          this.displayName = profile.data.displayName
          this.description = profile.data.description
        } else {
          this.displayName = ''
          this.description = ''
        }
      })
      this.memberships = new MembershipsViewModel(this.rootStore, {
        actor: this.did,
      })
      await this.memberships?.setup()
    } else {
      this.clear()
    }
  }

  async fetchStateUpdate() {
    const res = await this.rootStore.api.app.bsky.notification.getCount()
    runInAction(() => {
      this.notificationCount = res.data.count
    })
  }

  async refreshMemberships() {
    return this.memberships?.refresh()
  }
}