-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.c
More file actions
134 lines (127 loc) · 4.41 KB
/
message.c
File metadata and controls
134 lines (127 loc) · 4.41 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
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
#include "linked_list.h"
#include "message.h"
#include "lamp_stamp.h"
#include <stdio.h>
#include <string.h>
message *init_message(char *poster, char *line, int number, lamp_stamp timestamp) {
message * temp = malloc(sizeof(message));
memcpy(temp->poster_name, poster, MAX_USERNAME_LEN);
memcpy(temp->text, line, MAX_MESS_LEN);
temp->likes = malloc(sizeof(llist));
temp->unlikes = malloc(sizeof(llist));
temp->num_likes = 0;
temp->update_num = number;
temp->timestamp = timestamp;
return temp;
}
void like_message(message* line, packet command) { //TODO: Add code to consider unlike timestamps
if (command.packet_type == LIKE_COMMAND) {
//like_payload command;
//memcpy(&command, &command.payload, sizeof(command));
if (memcmp(command.username, line->poster_name, MAX_USERNAME_LEN) == 0) {
return;
}
like* gotten_like = get_like(line, command.username);
like* gotten_unlike = get_unlike(line, command.username);
if(gotten_unlike == NULL) {
if (gotten_like == NULL) { //It has not been liked or unliked, so we like it
like* new_like = malloc(sizeof(like));
memcpy(new_like->username, command.username, MAX_USERNAME_LEN);
new_like->timestamp = command.timestamp;
add_to_end(line -> likes, new_like, sizeof(like));
line->num_likes++;
return;
} else {//Set the timestamp to be the correct one.
gotten_like->timestamp = max_stamp(gotten_like->timestamp, command.timestamp);
return;
}
} else {
if (compare_stamp(&gotten_unlike->timestamp, &command.timestamp) < 0) {
list_remove(line->unlikes, gotten_unlike, sizeof(like));
like* new_like = malloc(sizeof(like));
memcpy(new_like->username, command.username, MAX_USERNAME_LEN);
new_like->timestamp = command.timestamp;
add_to_end(line -> likes, new_like, sizeof(like));
line->num_likes++;
}
}
}
}
/*
int diff = difftime(command.timestamp, get_like(line, command.user_name).timestamp);
if (diff >= 0) {
*/
void unlike_message(message* line, packet command) { //TODO: Add code to consider like timestamps
if (command.packet_type == UNLIKE_COMMAND) {
//like_payload command;
//memcpy(&command, &command.payload, sizeof(command));
if (memcmp(command.username, line->poster_name, MAX_USERNAME_LEN) == 0) {
return;
}
like* gotten_like = get_like(line, command.username);
like* gotten_unlike = get_unlike(line, command.username);
printf("%p\n",gotten_like);
if(gotten_like == NULL) {
if (gotten_unlike == NULL) { //It has not been liked or unliked, so we unlike it
like* new_unlike = malloc(sizeof(like));
memcpy(new_unlike->username, command.username, MAX_USERNAME_LEN);
new_unlike->timestamp = command.timestamp;
add_to_end(line -> unlikes, new_unlike, sizeof(like));
return;
} else {//Set the timestamp to be the correct one.
gotten_unlike->timestamp = max_stamp(gotten_unlike->timestamp, command.timestamp);
return;
}
} else {
if (compare_stamp(&gotten_unlike->timestamp, &command.timestamp) < 0) {
printf("here");
list_remove(line->likes, gotten_like, sizeof(like));
like* new_unlike = malloc(sizeof(like));
memcpy(new_unlike->username, command.username, MAX_USERNAME_LEN);
new_unlike->timestamp = command.timestamp;
add_to_end(line -> unlikes, new_unlike, sizeof(like));
line->num_likes--;
}
}
}
}
like *get_like(message* line, char* name)
{
//printf("testing get\n");
llist *list = line ->likes;
node *temp = list->head;
while (temp != NULL)
{
like* temp_like = temp->ptr;
//printf("Name1: %s\nName2: %s\n", temp_like->username, name);
int compare = memcmp(temp_like->username, name, MAX_USERNAME_LEN);
if (compare == 0)
{
//printf("got something\n");
return temp -> ptr;
}
temp = temp->next;
}
//printf("got nothing\n");
return NULL;
}
like *get_unlike(message* line, char* name)
{
//printf("testing get\n");
llist *list = line ->unlikes;
node *temp = list->head;
while (temp != NULL)
{
like* temp_like = temp->ptr;
//printf("Name1: %s\nName2: %s\n", temp_like->username, name);
int compare = memcmp(temp_like->username, name, MAX_USERNAME_LEN);
if (compare == 0)
{
//printf("got something\n");
return temp -> ptr;
}
temp = temp->next;
}
//printf("got nothing\n");
return NULL;
}