about summary refs log tree commit diff
path: root/src/state/models/feeds/posts-slice.ts
blob: 2501cef6fca1e421c3afd18b3d01443b8e5e1bb2 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {makeAutoObservable} from 'mobx'
import {RootStoreModel} from '../root-store'
import {FeedViewPostsSlice} from 'lib/api/feed-manip'
import {PostsFeedItemModel} from './post'
import {FeedSourceInfo} from 'lib/api/feed/types'

export class PostsFeedSliceModel {
  // ui state
  _reactKey: string = ''

  // data
  items: PostsFeedItemModel[] = []
  source: FeedSourceInfo | undefined

  constructor(public rootStore: RootStoreModel, slice: FeedViewPostsSlice) {
    this._reactKey = slice._reactKey
    this.source = slice.source
    for (let i = 0; i < slice.items.length; i++) {
      this.items.push(
        new PostsFeedItemModel(
          rootStore,
          `${this._reactKey} - ${i}`,
          slice.items[i],
        ),
      )
    }
    makeAutoObservable(this, {rootStore: false})
  }

  get uri() {
    if (this.isReply) {
      return this.items[1].post.uri
    }
    return this.items[0].post.uri
  }

  get isThread() {
    return (
      this.items.length > 1 &&
      this.items.every(
        item => item.post.author.did === this.items[0].post.author.did,
      )
    )
  }

  get isReply() {
    return this.items.length > 1 && !this.isThread
  }

  get rootItem() {
    if (this.isReply) {
      return this.items[1]
    }
    return this.items[0]
  }

  get moderation() {
    // prefer the most stringent item
    const topItem = this.items.find(item => item.moderation.content.filter)
    if (topItem) {
      return topItem.moderation
    }
    // otherwise just use the first one
    return this.items[0].moderation
  }

  shouldFilter(ignoreFilterForDid: string | undefined): boolean {
    const mods = this.items
      .filter(item => item.post.author.did !== ignoreFilterForDid)
      .map(item => item.moderation)
    return !!mods.find(mod => mod.content.filter)
  }

  containsUri(uri: string) {
    return !!this.items.find(item => item.post.uri === uri)
  }

  isThreadParentAt(i: number) {
    if (this.items.length === 1) {
      return false
    }
    return i < this.items.length - 1
  }

  isThreadChildAt(i: number) {
    if (this.items.length === 1) {
      return false
    }
    return i > 0
  }
}