-
Notifications
You must be signed in to change notification settings - Fork 519
/
ActivityTable.h
209 lines (164 loc) · 8.11 KB
/
ActivityTable.h
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2012 Viewfinder. All rights reserved.
// Author: Spencer Kimball.
#ifndef VIEWFINDER_ACTIVITY_TABLE_H
#define VIEWFINDER_ACTIVITY_TABLE_H
#import "ActivityMetadata.pb.h"
#import "ContentTable.h"
#import "DayMetadata.pb.h"
#import "PhotoSelection.h"
#import "WallTime.h"
typedef google::protobuf::RepeatedPtrField<ActivityMetadata::Episode> ShareEpisodes;
// The ActivityTable class maintains the mappings:
// <device-activity-id> -> <ActivityMetadata>
// <server-activity-id> -> <device-activity-id>
// <device-viewpoint-id>,<timestamp>,<device-activity-id> -> <>
// <timestamp>,<device-activity-id> -> <>
// <server-episode-id>,<device-activity-id> -> <>
//
// For quarantined activities, maintain an index of device activity ids.
// <device-activity-id> -> <>
class ActivityTable_Activity : public ActivityMetadata {
public:
virtual void MergeFrom(const ActivityMetadata& m);
// Unimplemented; exists to get the compiler not to complain about hiding the base class's overloaded MergeFrom.
virtual void MergeFrom(const ::google::protobuf::Message&);
// Filters the share new or share existing activity of any photos that are
// not present in selection. If the activity is modified, any deletions
// needed to clean up database indexes are added to updates. Returns true if
// a share activity was filtered and it still contains photos.
bool FilterShare(const PhotoSelectionSet& selection, const DBHandle& updates);
// Returns formatted name of user who created activity. If
// "shorten" is true, returns just first name; otherwise full.
string FormatName(bool shorten);
// Returns a formatted timestamp, relative to the current date.
string FormatTimestamp(bool shorten);
// Returns formatted version of activity content. If not NULL, uses the
// supplied activity row to inform the formatting of the activity contents.
// This provides conversation-dependent context, such as eliminating
// photos from a share activity which are duplicates in the conversation.
string FormatContent(
const ViewpointSummaryMetadata::ActivityRow* activity_row, bool shorten);
// Returns the timestamp at which this activity was viewed. If none
// has been set, returns the current wall time.
WallTime GetViewedTimestamp() const;
// Returns whether this activity is an update to the conversation,
// as opposed to content.
bool IsUpdate() const;
// Returns whether the activity is visible in the viewpoint.
// This excludes non-displayed activity types as well as any
// quarantined activities.
bool IsVisible() const;
// Returns the list of shared episodes, if this is a share_new,
// share_existing, save_photos or unshare activity; NULL otherwise.
const ShareEpisodes* GetShareEpisodes();
protected:
bool Load();
void SaveHook(const DBHandle& updates);
void DeleteHook(const DBHandle& updates);
// Specialty function for default share activity "caption" in the event
// one is not specified for the activity.
string FormatShareContent(
const ViewpointSummaryMetadata::ActivityRow* activity_row, bool shorten);
// Invalidates all days which are affected by this activity. This includes
// the day of the activity itself, any days on which episodes shared, updated,
// or unshared in this activity took place, and the first and last days
// of the viewpoint which this activity is part of.
void InvalidateDays(const DBHandle& updates);
int64_t local_id() const { return activity_id().local_id(); }
const string& server_id() const { return activity_id().server_id(); }
ActivityTable_Activity(AppState* state, const DBHandle& db, int64_t id);
protected:
AppState* const state_;
DBHandle db_;
private:
// The timestamp as stored on disk.
WallTime disk_timestamp_;
};
class ActivityTable : public ContentTable<ActivityTable_Activity> {
typedef ActivityTable_Activity Activity;
typedef ::google::protobuf::RepeatedPtrField<ContactMetadata> ContactArray;
public:
// Iterates over activities. The current activity
// may be fetched via a call to GetActivity().
class ActivityIterator : public ContentIterator {
public:
virtual ~ActivityIterator();
// Only valid to call if !done().
ContentHandle GetActivity();
int64_t activity_id() const { return cur_activity_id_; }
WallTime timestamp() const { return cur_timestamp_; }
virtual void Seek(WallTime seek_time) = 0;
protected:
ActivityIterator(ActivityTable* table, bool reverse, const DBHandle& db);
protected:
ActivityTable* table_;
DBHandle db_;
int64_t cur_activity_id_;
WallTime cur_timestamp_;
};
ActivityTable(AppState* state);
virtual ~ActivityTable();
ContentHandle NewActivity(const DBHandle& updates) {
return NewContent(updates);
}
ContentHandle LoadActivity(int64_t id, const DBHandle& db) {
return LoadContent(id, db);
}
ContentHandle LoadActivity(const string& server_id, const DBHandle& db) {
return LoadContent(server_id, db);
}
// Returns the most recent activity for the viewpoint or an
// empty handle if none was found.
ContentHandle GetLatestActivity(int64_t viewpoint_id, const DBHandle& db);
// Returns the first activity for the viewpoint or an empty
// handle if none was found.
ContentHandle GetFirstActivity(int64_t viewpoint_id, const DBHandle& db);
// Returns the activity that posted the comment.
ContentHandle GetCommentActivity(const string& comment_server_id, const DBHandle& db);
// Returns the activities which added photos from the specified episode.
void ListEpisodeActivities(
const string& episode_server_id, vector<int64_t>* activity_ids, const DBHandle& db);
// Returns a new ActivityIterator object for iterating over
// activities in timestamp order. Specify reverse to iterate from
// most to least recent. The caller is responsible for deleting the
// iterator.
ActivityIterator* NewTimestampActivityIterator(
WallTime start, bool reverse, const DBHandle& db);
// Returns a new ActivityIterator object for iterating over
// activities in the specified viewpoint. The activities within the
// viewpoint are returned in sorted timestamp order from oldest to
// newest. Specify reverse to iterate instead from newest to
// oldest. The caller is responsible for deleting the iterator.
ActivityIterator* NewViewpointActivityIterator(
int64_t viewpoint_id, WallTime start, bool reverse, const DBHandle& db);
// Override of base class FSCK to unquarantine activities on startup.
virtual bool FSCK(
bool force, ProgressUpdateBlock progress_update, const DBHandle& updates);
// Repairs secondary indexes and sanity checks all references from
// activities to other assets.
bool FSCKImpl(int prev_fsck_version, const DBHandle& updates);
// Consistency check on activity metdata.
bool FSCKActivity(const DBHandle& updates);
// Consistency check on activity-by-timestamp index.
bool FSCKActivityTimestampIndex(const DBHandle& updates);
// Consistency check on viewpoint-activity-timestamp index.
bool FSCKViewpointActivityIndex(const DBHandle& updates);
static const ContactArray* GetActivityContacts(const ActivityMetadata& m);
};
typedef ActivityTable::ContentHandle ActivityHandle;
string EncodeActivityTimestampKey(WallTime timestamp, int64_t activity_id);
string EncodeCommentActivityKey(const string& episode_server_id);
string EncodeEpisodeActivityKey(const string& episode_server_id, int64_t activity_id);
string EncodeEpisodeActivityKeyPrefix(const string& episode_server_id);
string EncodeQuarantinedActivityKey(int64_t activity_id);
string EncodeViewpointActivityKey(int64_t viewpoint_id, WallTime timestamp, int64_t activity_id);
bool DecodeActivityTimestampKey(Slice key, WallTime* timestamp, int64_t* activity_id);
bool DecodeCommentActivityKey(Slice key, string* comment_server_id);
bool DecodeEpisodeActivityKey(Slice key, string* episode_server_id, int64_t* activity_id);
bool DecodeViewpointActivityKey(Slice key, int64_t* viewpoint_id,
WallTime* timestamp, int64_t* activity_id);
bool DecodeQuarantinedActivityKey(Slice key, int64_t* activity_id);
#endif // VIEWFINDER_ACTIVITY_TABLE_H
// local variables:
// mode: c++
// end: