-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLike.ts
More file actions
65 lines (57 loc) · 1.48 KB
/
Copy pathLike.ts
File metadata and controls
65 lines (57 loc) · 1.48 KB
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
import * as DB from "../Database.js";
import { VC } from "../ViewerContext.js";
import { GraphQLNode, getLocalTypeAssert } from "../graphql/Node.js";
import { User } from "./User.js";
import { Model } from "./Model.js";
import { ID } from "../../../dist/src/index.js";
import { GqlDate } from "../graphql/CustomScalars.js";
import { Post } from "./Post.js";
/**
* A reaction from a user indicating that they like a post.
* @gqlType */
export class Like extends Model<DB.LikeRow> implements GraphQLNode {
__typename = "Like" as const;
/**
* The date and time at which the post was liked.
* @gqlField */
createdAt(): GqlDate {
return this.row.createdAt;
}
/**
* The user who liked the post.
* @gqlField */
async liker(): Promise<User> {
return this.vc.getUserById(this.row.userId);
}
/**
* The post that was liked.
* @gqlField */
async post(): Promise<Post> {
return this.vc.getPostById(this.row.postId);
}
}
// --- Mutations ---
/** @gqlInput */
type CreateLikeInput = {
postId: ID;
};
/** @gqlType */
type CreateLikePayload = {
/** @gqlField */
post: Post;
};
/**
* Like a post. This action is taken as the currently logged in user.
* @gqlMutationField */
export async function createLike(
input: CreateLikeInput,
vc: VC,
): Promise<CreateLikePayload> {
const id = getLocalTypeAssert(input.postId, "Post");
await DB.createLike(vc, {
...input,
userId: vc.userId(),
postId: id,
});
return { post: await vc.getPostById(id) };
}