aboutsummaryrefslogtreecommitdiff
path: root/db/report.go
blob: a64c94fbf0f3cfdc2fc53b8470499ae626f9f4a1 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package db

type Report struct {
	ID     string
	Count  int
	Reason string
}

type Removed struct {
	ID    string
	Type  string
	Board string
}

func DeleteReportActivity(id string) error {
	query := `delete from reported where id=$1`

	_, err := db.Exec(query, id)
	return err
}

func ReportActivity(id string, reason string) (bool, error) {
	if res, err := IsIDLocal(id); err == nil && !res {
		// TODO: not local error
		return false, nil
	} else if err != nil {
		return false, err
	}

	actor, err := GetActivityFromDB(id)
	if err != nil {
		return false, err
	}

	query := `select count from reported where id=$1`

	rows, err := db.Query(query, id)
	if err != nil {
		return false, err
	}
	defer rows.Close()

	var count int
	for rows.Next() {
		if err := rows.Scan(&count); err != nil {
			return false, err
		}
	}

	if count < 1 {
		query = `insert into reported (id, count, board, reason) values ($1, $2, $3, $4)`

		_, err := db.Exec(query, id, 1, actor.Actor.Id, reason)
		if err != nil {
			return false, err
		}
	} else {
		count = count + 1
		query = `update reported set count=$1 where id=$2`

		_, err := db.Exec(query, count, id)
		if err != nil {
			return false, err
		}
	}

	return true, nil
}

func CreateLocalDeleteDB(id string, _type string) error {
	query := `select id from removed where id=$1`

	rows, err := db.Query(query, id)
	if err != nil {
		return err
	}
	defer rows.Close()

	if rows.Next() {
		var i string

		if err := rows.Scan(&i); err != nil {
			return err
		}

		if i != "" {
			query := `update removed set type=$1 where id=$2`

			if _, err := db.Exec(query, _type, id); err != nil {
				return err
			}
		}
	} else {
		query := `insert into removed (id, type) values ($1, $2)`

		if _, err := db.Exec(query, id, _type); err != nil {
			return err
		}
	}

	return nil
}

func GetLocalDeleteDB() ([]Removed, error) {
	var deleted []Removed

	query := `select id, type from removed`

	rows, err := db.Query(query)
	if err != nil {
		return deleted, err
	}

	defer rows.Close()

	for rows.Next() {
		var r Removed

		if err := rows.Scan(&r.ID, &r.Type); err != nil {
			return deleted, err
		}

		deleted = append(deleted, r)
	}

	return deleted, nil
}

func CreateLocalReportDB(id string, board string, reason string) error {
	query := `select id, count from reported where id=$1 and board=$2`

	rows, err := db.Query(query, id, board)
	if err != nil {
		return err
	}
	defer rows.Close()

	if rows.Next() {
		var i string
		var count int

		if err := rows.Scan(&i, &count); err != nil {
			return err
		}

		if i != "" {
			count = count + 1
			query := `update reported set count=$1 where id=$2`

			if _, err := db.Exec(query, count, id); err != nil {
				return err
			}
		}
	} else {
		query := `insert into reported (id, count, board, reason) values ($1, $2, $3, $4)`

		if _, err := db.Exec(query, id, 1, board, reason); err != nil {
			return err
		}
	}

	return nil
}

func GetLocalReportDB(board string) ([]Report, error) {
	var reported []Report

	query := `select id, count, reason from reported where board=$1`

	rows, err := db.Query(query, board)
	if err != nil {
		return reported, err
	}
	defer rows.Close()

	for rows.Next() {
		var r Report

		if err := rows.Scan(&r.ID, &r.Count, &r.Reason); err != nil {
			return reported, err
		}

		reported = append(reported, r)
	}

	return reported, nil
}

func CloseLocalReportDB(id string, board string) error {
	query := `delete from reported where id=$1 and board=$2`

	_, err := db.Exec(query, id, board)
	return err
}