mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {makeAutoObservable} from 'mobx'
2import {RootStoreModel} from '../root-store'
3import {FeedViewPostsSlice} from 'lib/api/feed-manip'
4import {PostsFeedItemModel} from './post'
5
6export class PostsFeedSliceModel {
7 // ui state
8 _reactKey: string = ''
9
10 // data
11 items: PostsFeedItemModel[] = []
12
13 constructor(public rootStore: RootStoreModel, slice: FeedViewPostsSlice) {
14 this._reactKey = slice._reactKey
15 for (let i = 0; i < slice.items.length; i++) {
16 this.items.push(
17 new PostsFeedItemModel(
18 rootStore,
19 `${this._reactKey} - ${i}`,
20 slice.items[i],
21 ),
22 )
23 }
24 makeAutoObservable(this, {rootStore: false})
25 }
26
27 get uri() {
28 if (this.isReply) {
29 return this.items[1].post.uri
30 }
31 return this.items[0].post.uri
32 }
33
34 get isThread() {
35 return (
36 this.items.length > 1 &&
37 this.items.every(
38 item => item.post.author.did === this.items[0].post.author.did,
39 )
40 )
41 }
42
43 get isReply() {
44 return this.items.length > 1 && !this.isThread
45 }
46
47 get rootItem() {
48 if (this.isReply) {
49 return this.items[1]
50 }
51 return this.items[0]
52 }
53
54 get moderation() {
55 // prefer the most stringent item
56 const topItem = this.items.find(item => item.moderation.content.filter)
57 if (topItem) {
58 return topItem.moderation
59 }
60 // otherwise just use the first one
61 return this.items[0].moderation
62 }
63
64 shouldFilter(ignoreFilterForDid: string | undefined): boolean {
65 const mods = this.items
66 .filter(item => item.post.author.did !== ignoreFilterForDid)
67 .map(item => item.moderation)
68 return !!mods.find(mod => mod.content.filter)
69 }
70
71 containsUri(uri: string) {
72 return !!this.items.find(item => item.post.uri === uri)
73 }
74
75 isThreadParentAt(i: number) {
76 if (this.items.length === 1) {
77 return false
78 }
79 return i < this.items.length - 1
80 }
81
82 isThreadChildAt(i: number) {
83 if (this.items.length === 1) {
84 return false
85 }
86 return i > 0
87 }
88}