Skip to content

Commit 27ed356

Browse files
changes
1 parent 2f7bf12 commit 27ed356

File tree

227 files changed

+22202
-3822
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

227 files changed

+22202
-3822
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import 'package:flutter/rendering.dart';
2+
3+
class CommentModel {
4+
String? sId;
5+
Author? author;
6+
String? post;
7+
List<String>? mentions;
8+
List<CommentModel>? replies;
9+
String? parent;
10+
String? parentType;
11+
String? text;
12+
String? createdAt;
13+
bool? isDeleted;
14+
int? votes;
15+
int? repliesCount;
16+
bool? locked;
17+
bool? nsfw;
18+
bool? spoiler;
19+
String? modState;
20+
double? sortOnHot;
21+
int? iV;
22+
String? type;
23+
int? count;
24+
List<String>? children;
25+
26+
CommentModel(
27+
{this.sId,
28+
this.author,
29+
this.post,
30+
this.mentions,
31+
this.replies,
32+
this.parent,
33+
this.parentType,
34+
this.text,
35+
this.createdAt,
36+
this.isDeleted,
37+
this.votes,
38+
this.repliesCount,
39+
this.locked,
40+
this.nsfw,
41+
this.spoiler,
42+
this.modState,
43+
this.sortOnHot,
44+
this.iV,
45+
this.type,
46+
this.count,
47+
this.children});
48+
49+
CommentModel.fromJson(Map<String, dynamic> json) {
50+
print(json);
51+
print(json['_id'].runtimeType);
52+
print(json['author'].runtimeType);
53+
print(json['post'].runtimeType);
54+
print(json['mentions'].runtimeType);
55+
print(json['replies'].runtimeType);
56+
print(json['parent'].runtimeType);
57+
print(json['parentType'].runtimeType);
58+
print(json['createdAt'].runtimeType);
59+
print(json['isDeleted'].runtimeType);
60+
print(json['repliesCount'].runtimeType);
61+
print(json['locked'].runtimeType);
62+
print(json['nsfw'].runtimeType);
63+
print(json['spoiler'].runtimeType);
64+
print(json['modState'].runtimeType);
65+
print(json['sortOnHot'].runtimeType);
66+
print(json['__v'].runtimeType);
67+
68+
sId = json['_id'];
69+
author =
70+
json['author'] != null ? new Author.fromJson(json['author']) : null;
71+
post = json['post'];
72+
if (json['mentions'] != null) {
73+
mentions = json['mentions'].cast<String>();
74+
} else {
75+
mentions = [''];
76+
}
77+
78+
if (json['replies'] != null) {
79+
replies = <CommentModel>[];
80+
json['replies'].forEach((v) {
81+
replies!.add(CommentModel.fromJson(v));
82+
});
83+
}
84+
parent = json['parent'];
85+
parentType = json['parentType'];
86+
text = json['text'];
87+
createdAt = json['createdAt'];
88+
isDeleted = json['isDeleted'];
89+
votes = json['votes'];
90+
repliesCount = json['repliesCount'];
91+
locked = json['locked'];
92+
nsfw = json['nsfw'];
93+
spoiler = json['spoiler'];
94+
modState = json['modState'];
95+
iV = json['__v'];
96+
type = json['Type'];
97+
count = json['count'];
98+
if (json['children'] != null) {
99+
children = json['children'].cast<String>();
100+
} else {
101+
children = [''];
102+
}
103+
}
104+
}
105+
106+
class Author {
107+
String? sId;
108+
String? userName;
109+
String? profilePicture;
110+
String? profileBackground;
111+
112+
Author(
113+
{this.sId, this.userName, this.profilePicture, this.profileBackground});
114+
115+
Author.fromJson(Map<String, dynamic> json) {
116+
sId = json['_id'];
117+
userName = json['userName'];
118+
profilePicture = json['profilePicture'];
119+
profileBackground = json['profileBackground'];
120+
}
121+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:post/comments/models/comment_model.dart';
3+
import 'package:shared_preferences/shared_preferences.dart';
4+
5+
import '../../networks/dio_client.dart';
6+
7+
class PostCommentsProvider with ChangeNotifier {
8+
late List<CommentModel> commentsData;
9+
10+
List<CommentModel>? get gettingPostComments {
11+
return commentsData;
12+
}
13+
14+
Future<void> fetchPostComments(String postId, int limit, int depth) async {
15+
try {
16+
final prefs = await SharedPreferences.getInstance();
17+
DioClient.init(prefs);
18+
19+
await DioClient.get(path: '/comments/comment_tree/$postId', query: {
20+
'limit': limit,
21+
'depth': depth,
22+
}).then((response) {
23+
print(response.data);
24+
List<CommentModel> tempData = [];
25+
response.data['comments'].forEach((comment) {
26+
tempData.add(CommentModel.fromJson(comment));
27+
});
28+
commentsData = tempData;
29+
notifyListeners();
30+
});
31+
} catch (error) {
32+
print(error);
33+
print('heelo');
34+
throw (error);
35+
}
36+
}
37+
38+
Future<bool> postComment(
39+
String parentId, String parentType, String text) async {
40+
try {
41+
final prefs = await SharedPreferences.getInstance();
42+
await DioClient.init(prefs);
43+
var res = await DioClient.post(
44+
path: '/comments',
45+
data: {'parent': parentId, 'parentType': parentType, 'text': text},
46+
//query: {'dir': state}
47+
);
48+
if (res.statusCode == 201) {
49+
return true;
50+
} else {
51+
return false;
52+
}
53+
} catch (err) {
54+
print(err);
55+
rethrow;
56+
}
57+
}
58+
59+
Future<bool> updateVotes(String id, int state) async {
60+
try {
61+
final prefs = await SharedPreferences.getInstance();
62+
await DioClient.init(prefs);
63+
var res = await DioClient.post(
64+
path: '/posts/${id}/vote',
65+
data: {'dir': state},
66+
//query: {'dir': state}
67+
);
68+
if (res.statusCode == 200) {
69+
return true;
70+
} else {
71+
return false;
72+
}
73+
} catch (err) {
74+
print(err);
75+
rethrow;
76+
}
77+
}
78+
79+
Future<bool> saveComments(String id) async {
80+
try {
81+
final prefs = await SharedPreferences.getInstance();
82+
await DioClient.init(prefs);
83+
var res = await DioClient.post(
84+
path: '/comments/${id}/save',
85+
data: {},
86+
);
87+
if (res.statusCode == 200) {
88+
return true;
89+
} else {
90+
return false;
91+
}
92+
} catch (err) {
93+
print(err);
94+
rethrow;
95+
}
96+
}
97+
98+
Future<bool> unSaveComment(String id) async {
99+
try {
100+
final prefs = await SharedPreferences.getInstance();
101+
await DioClient.init(prefs);
102+
var res = await DioClient.post(
103+
path: '/comments/${id}/unsave',
104+
data: {},
105+
);
106+
if (res.statusCode == 200) {
107+
return true;
108+
} else {
109+
return false;
110+
}
111+
} catch (err) {
112+
print(err);
113+
rethrow;
114+
}
115+
}
116+
117+
Future<bool> deleteComment(String id) async {
118+
try {
119+
final prefs = await SharedPreferences.getInstance();
120+
await DioClient.init(prefs);
121+
var res = await DioClient.delete(
122+
path: '/comments/$id',
123+
);
124+
if (res.statusCode == 204) {
125+
return true;
126+
} else {
127+
return false;
128+
}
129+
} catch (err) {
130+
print(err);
131+
rethrow;
132+
}
133+
}
134+
135+
Future<bool> postAction(String id, String action) async {
136+
try {
137+
final prefs = await SharedPreferences.getInstance();
138+
await DioClient.init(prefs);
139+
var res = await DioClient.patch(
140+
path: '/posts/${id}/actions/${action}',
141+
data: {},
142+
);
143+
if (res.statusCode == 204) {
144+
return true;
145+
} else {
146+
return false;
147+
}
148+
} catch (err) {
149+
print(err);
150+
rethrow;
151+
}
152+
}
153+
154+
Future<bool> postModerationAction(String id, String action) async {
155+
try {
156+
final prefs = await SharedPreferences.getInstance();
157+
await DioClient.init(prefs);
158+
var res = await DioClient.patch(
159+
path: '/posts/${id}/moderate/${action}',
160+
data: {},
161+
);
162+
if (res.statusCode == 204) {
163+
return true;
164+
} else {
165+
return false;
166+
}
167+
} catch (err) {
168+
print(err);
169+
rethrow;
170+
}
171+
}
172+
173+
Future<bool> reportSpam(String id) async {
174+
try {
175+
final prefs = await SharedPreferences.getInstance();
176+
await DioClient.init(prefs);
177+
var res = await DioClient.patch(
178+
path: '/posts/${id}/spam',
179+
data: {"dir": 1},
180+
);
181+
if (res.statusCode == 204) {
182+
return true;
183+
} else {
184+
return false;
185+
}
186+
} catch (err) {
187+
print(err);
188+
rethrow;
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)