aboutsummaryrefslogtreecommitdiff
path: root/db/report.go
blob: 4fcb8067bda7575b4f2754fda018b25e993e8372 (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
package db

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
}